Search in sources :

Example 1 with ResourceTracker

use of org.neo4j.kernel.api.ResourceTracker in project neo4j by neo4j.

the class ProcedureCompilationTest method shouldHandleThrowingProcedure.

@Test
void shouldHandleThrowingProcedure() throws ProcedureException {
    // Given
    ResourceTracker tracker = mock(ResourceTracker.class);
    ProcedureSignature signature = ProcedureSignature.procedureSignature("test", "foo").in("in", NTString).out(singletonList(inputField("name", NTString))).build();
    // When
    CallableProcedure longMethod = compileProcedure(signature, emptyList(), method("throwingLongStreamMethod"));
    // Then
    assertThrows(ProcedureException.class, () -> longMethod.apply(ctx, EMPTY, tracker).next());
    verify(tracker).registerCloseableResource(any(Stream.class));
    verify(tracker).unregisterCloseableResource(any(Stream.class));
}
Also used : ProcedureSignature(org.neo4j.internal.kernel.api.procs.ProcedureSignature) ResourceTracker(org.neo4j.kernel.api.ResourceTracker) CallableProcedure(org.neo4j.kernel.api.procedure.CallableProcedure) Stream(java.util.stream.Stream) Test(org.junit.jupiter.api.Test)

Example 2 with ResourceTracker

use of org.neo4j.kernel.api.ResourceTracker in project neo4j by neo4j.

the class BoltKeepAliveSchedulingIT method installSleepProcedure.

private static void installSleepProcedure(GraphDatabaseService db) throws ProcedureException {
    GraphDatabaseAPI dbApi = (GraphDatabaseAPI) db;
    dbApi.getDependencyResolver().resolveDependency(GlobalProcedures.class).register(new CallableProcedure.BasicProcedure(procedureSignature("boltissue", "sleep").out(ProcedureSignature.VOID).build()) {

        @Override
        public RawIterator<AnyValue[], ProcedureException> apply(Context context, AnyValue[] objects, ResourceTracker resourceTracker) throws ProcedureException {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new ProcedureException(Status.General.UnknownError, e, "Interrupted");
            }
            return RawIterator.empty();
        }
    });
}
Also used : Context(org.neo4j.kernel.api.procedure.Context) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) ResourceTracker(org.neo4j.kernel.api.ResourceTracker) AnyValue(org.neo4j.values.AnyValue) CallableProcedure(org.neo4j.kernel.api.procedure.CallableProcedure) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException) RawIterator(org.neo4j.collection.RawIterator) GlobalProcedures(org.neo4j.kernel.api.procedure.GlobalProcedures)

Example 3 with ResourceTracker

use of org.neo4j.kernel.api.ResourceTracker in project neo4j by neo4j.

the class ProceduresTest method shouldMakeContextAvailable.

@Test
void shouldMakeContextAvailable() throws Throwable {
    // Given
    procs.register(new CallableProcedure.BasicProcedure(signature) {

        @Override
        public RawIterator<AnyValue[], ProcedureException> apply(Context ctx, AnyValue[] input, ResourceTracker resourceTracker) {
            return RawIterator.<AnyValue[], ProcedureException>of(new AnyValue[] { stringValue(ctx.thread().getName()) });
        }
    });
    Context ctx = prepareContext();
    ProcedureHandle procedureHandle = procs.procedure(signature.name());
    // When
    RawIterator<AnyValue[], ProcedureException> result = procs.callProcedure(ctx, procedureHandle.id(), new AnyValue[0], EMPTY_RESOURCE_TRACKER);
    // Then
    assertThat(asList(result)).contains(new AnyValue[] { stringValue(Thread.currentThread().getName()) });
}
Also used : BasicContext.buildContext(org.neo4j.kernel.api.procedure.BasicContext.buildContext) Context(org.neo4j.kernel.api.procedure.Context) ProcedureHandle(org.neo4j.internal.kernel.api.procs.ProcedureHandle) ResourceTracker(org.neo4j.kernel.api.ResourceTracker) AnyValue(org.neo4j.values.AnyValue) CallableProcedure(org.neo4j.kernel.api.procedure.CallableProcedure) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException) RawIterator(org.neo4j.collection.RawIterator) Test(org.junit.jupiter.api.Test)

Example 4 with ResourceTracker

use of org.neo4j.kernel.api.ResourceTracker in project neo4j by neo4j.

the class BoltChannelAutoReadLimiterIT method installSleepProcedure.

private static void installSleepProcedure(GraphDatabaseService db) throws ProcedureException {
    GraphDatabaseAPI dbApi = (GraphDatabaseAPI) db;
    dbApi.getDependencyResolver().resolveDependency(GlobalProcedures.class).register(new CallableProcedure.BasicProcedure(procedureSignature("boltissue", "sleep").in("data", Neo4jTypes.NTString).out(ProcedureSignature.VOID).build()) {

        @Override
        public RawIterator<AnyValue[], ProcedureException> apply(Context context, AnyValue[] objects, ResourceTracker resourceTracker) throws ProcedureException {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                throw new ProcedureException(Status.General.UnknownError, e, "Interrupted");
            }
            return RawIterator.empty();
        }
    });
}
Also used : Context(org.neo4j.kernel.api.procedure.Context) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) ResourceTracker(org.neo4j.kernel.api.ResourceTracker) AnyValue(org.neo4j.values.AnyValue) CallableProcedure(org.neo4j.kernel.api.procedure.CallableProcedure) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException) RawIterator(org.neo4j.collection.RawIterator) GlobalProcedures(org.neo4j.kernel.api.procedure.GlobalProcedures)

Aggregations

ResourceTracker (org.neo4j.kernel.api.ResourceTracker)4 CallableProcedure (org.neo4j.kernel.api.procedure.CallableProcedure)4 RawIterator (org.neo4j.collection.RawIterator)3 ProcedureException (org.neo4j.internal.kernel.api.exceptions.ProcedureException)3 Context (org.neo4j.kernel.api.procedure.Context)3 AnyValue (org.neo4j.values.AnyValue)3 Test (org.junit.jupiter.api.Test)2 GlobalProcedures (org.neo4j.kernel.api.procedure.GlobalProcedures)2 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)2 Stream (java.util.stream.Stream)1 ProcedureHandle (org.neo4j.internal.kernel.api.procs.ProcedureHandle)1 ProcedureSignature (org.neo4j.internal.kernel.api.procs.ProcedureSignature)1 BasicContext.buildContext (org.neo4j.kernel.api.procedure.BasicContext.buildContext)1