Search in sources :

Example 56 with MethodSource

use of org.junit.jupiter.params.provider.MethodSource in project neo4j by neo4j.

the class DijkstraTest method canFindNeighbourMultipleCorrectPaths.

@ParameterizedTest
@MethodSource("params")
void canFindNeighbourMultipleCorrectPaths(DijkstraFactory factory) {
    /*
         *     - 1.0 -
         *   /        \
         * (A) - 1 - (B)
         */
    try (Transaction transaction = graphDb.beginTx()) {
        Node nodeA = graph.makeNode(transaction, "A");
        Node nodeB = graph.makeNode(transaction, "B");
        graph.makeEdge(transaction, "A", "B", "length", 1.0);
        graph.makeEdge(transaction, "A", "B", "length", 1);
        var context = new BasicEvaluationContext(transaction, graphDb);
        PathFinder<WeightedPath> finder = factory.dijkstra(context, PathExpanders.allTypesAndDirections());
        assertPaths(finder.findAllPaths(nodeA, nodeB), "A,B", "A,B");
        transaction.commit();
    }
}
Also used : BasicEvaluationContext(org.neo4j.graphalgo.BasicEvaluationContext) WeightedPath(org.neo4j.graphalgo.WeightedPath) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 57 with MethodSource

use of org.junit.jupiter.params.provider.MethodSource in project neo4j by neo4j.

the class DijkstraTest method canGetMultiplePathsInTriangleGraph.

@ParameterizedTest
@MethodSource("params")
void canGetMultiplePathsInTriangleGraph(DijkstraFactory factory) {
    /* NODE (NAME/INDEX)
         * ==> (two relationships)
         *
         * (A/0) ====== 1 =====> (B/1)
         *   \                    /
         *    - 5 -> (C/2) <- 2 -
         */
    try (Transaction transaction = graphDb.beginTx()) {
        Node nodeA = graph.makeNode(transaction, "A");
        Node nodeB = graph.makeNode(transaction, "B");
        Node nodeC = graph.makeNode(transaction, "C");
        Set<Relationship> expectedFirsts = new HashSet<>();
        expectedFirsts.add(graph.makeEdge(transaction, "A", "B", "length", 1d));
        expectedFirsts.add(graph.makeEdge(transaction, "A", "B", "length", 1));
        Relationship expectedSecond = graph.makeEdge(transaction, "B", "C", "length", 2L);
        graph.makeEdge(transaction, "A", "C", "length", 5d);
        var context = new BasicEvaluationContext(transaction, graphDb);
        PathFinder<WeightedPath> finder = factory.dijkstra(context, PathExpanders.allTypesAndDirections());
        Iterator<WeightedPath> paths = finder.findAllPaths(nodeA, nodeC).iterator();
        for (int i = 0; i < 2; i++) {
            assertTrue(paths.hasNext(), "expected more paths");
            Path path = paths.next();
            assertPath(path, nodeA, nodeB, nodeC);
            Iterator<Relationship> relationships = path.relationships().iterator();
            assertTrue(relationships.hasNext(), "found shorter path than expected");
            assertTrue(expectedFirsts.remove(relationships.next()), "path contained unexpected relationship");
            assertTrue(relationships.hasNext(), "found shorter path than expected");
            assertEquals(expectedSecond, relationships.next());
            assertFalse(relationships.hasNext(), "found longer path than expected");
        }
        assertFalse(paths.hasNext(), "expected at most two paths");
        transaction.commit();
    }
}
Also used : WeightedPath(org.neo4j.graphalgo.WeightedPath) Path(org.neo4j.graphdb.Path) Node(org.neo4j.graphdb.Node) BasicEvaluationContext(org.neo4j.graphalgo.BasicEvaluationContext) WeightedPath(org.neo4j.graphalgo.WeightedPath) Transaction(org.neo4j.graphdb.Transaction) Relationship(org.neo4j.graphdb.Relationship) HashSet(java.util.HashSet) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 58 with MethodSource

use of org.junit.jupiter.params.provider.MethodSource in project neo4j by neo4j.

the class DijkstraTest method testSmallGraph.

@ParameterizedTest
@MethodSource("params")
void testSmallGraph(DijkstraFactory factory) {
    try (Transaction transaction = graphDb.beginTx()) {
        Relationship shortCTOXRelationship = createGraph(transaction, true);
        var context = new BasicEvaluationContext(transaction, graphDb);
        PathFinder<WeightedPath> finder = factory.dijkstra(context, PathExpanders.forTypeAndDirection(MyRelTypes.R1, Direction.OUTGOING), CommonEvaluators.doubleCostEvaluator("cost"));
        // Assert that there are two matching paths
        Node startNode = graph.getNode(transaction, "start");
        Node endNode = graph.getNode(transaction, "x");
        assertPaths(finder.findAllPaths(startNode, endNode), "start,a,b,c,x", "start,a,b,c,d,e,x");
        // of the two from (c) --> (x)
        for (WeightedPath path : finder.findAllPaths(startNode, endNode)) {
            if (getPathDef(path).equals("start,a,b,c,x")) {
                assertContainsRelationship(path, shortCTOXRelationship);
            }
        }
        transaction.commit();
    }
}
Also used : BasicEvaluationContext(org.neo4j.graphalgo.BasicEvaluationContext) WeightedPath(org.neo4j.graphalgo.WeightedPath) Transaction(org.neo4j.graphdb.Transaction) Relationship(org.neo4j.graphdb.Relationship) Node(org.neo4j.graphdb.Node) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 59 with MethodSource

use of org.junit.jupiter.params.provider.MethodSource in project neo4j by neo4j.

the class DijkstraTest method canGetMultiplePathsInASmallRoadNetwork.

@ParameterizedTest
@MethodSource("params")
void canGetMultiplePathsInASmallRoadNetwork(DijkstraFactory factory) {
    /*    NODE (NAME/INDEX)
         *
         *     --------------------- 25 -----------------------
         *    /                                                 \
         *  (A/0) - 2 - (B/1) - 2.5 - (D/3) - 3 - (E/4) - 5 - (F/5)
         *    |                        /           /           /
         *   2.5  ---------- 7.3 -----            /           /
         *    |  /                               /           /
         *  (C/2) ------------------ 5 ---------            /
         *    \                                            /
         *      ------------------ 12 --------------------
         *
         */
    try (Transaction transaction = graphDb.beginTx()) {
        Node nodeA = graph.makeNode(transaction, "A");
        Node nodeB = graph.makeNode(transaction, "B");
        Node nodeC = graph.makeNode(transaction, "C");
        Node nodeD = graph.makeNode(transaction, "D");
        Node nodeE = graph.makeNode(transaction, "E");
        Node nodeF = graph.makeNode(transaction, "F");
        graph.makeEdge(transaction, "A", "B", "length", 2d);
        graph.makeEdge(transaction, "A", "C", "length", 2.5f);
        graph.makeEdge(transaction, "C", "D", "length", 7.3d);
        graph.makeEdge(transaction, "B", "D", "length", 2.5f);
        graph.makeEdge(transaction, "D", "E", "length", 3L);
        graph.makeEdge(transaction, "C", "E", "length", 5);
        graph.makeEdge(transaction, "E", "F", "length", (byte) 5);
        graph.makeEdge(transaction, "C", "F", "length", (short) 12);
        graph.makeEdge(transaction, "A", "F", "length", (long) 25);
        var context = new BasicEvaluationContext(transaction, graphDb);
        PathFinder<WeightedPath> finder = factory.dijkstra(context, PathExpanders.allTypesAndDirections());
        // Try the search in both directions.
        for (Node[] nodes : new Node[][] { { nodeA, nodeF }, { nodeF, nodeA } }) {
            int found = 0;
            Iterator<WeightedPath> paths = finder.findAllPaths(nodes[0], nodes[1]).iterator();
            for (int i = 0; i < 2; i++) {
                assertTrue(paths.hasNext(), "expected more paths");
                Path path = paths.next();
                if (path.length() != found && path.length() == 3) {
                    assertContains(path.nodes(), nodeA, nodeC, nodeE, nodeF);
                } else if (path.length() != found && path.length() == 4) {
                    assertContains(path.nodes(), nodeA, nodeB, nodeD, nodeE, nodeF);
                } else {
                    fail("unexpected path length: " + path.length());
                }
                found = path.length();
            }
            assertFalse(paths.hasNext(), "expected at most two paths");
        }
        transaction.commit();
    }
}
Also used : WeightedPath(org.neo4j.graphalgo.WeightedPath) Path(org.neo4j.graphdb.Path) BasicEvaluationContext(org.neo4j.graphalgo.BasicEvaluationContext) WeightedPath(org.neo4j.graphalgo.WeightedPath) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 60 with MethodSource

use of org.junit.jupiter.params.provider.MethodSource in project neo4j by neo4j.

the class IndexingServiceIntegrationTest method tracePageCacheAccessOnIndexUpdatesApply.

@ParameterizedTest
@MethodSource("parameters")
void tracePageCacheAccessOnIndexUpdatesApply(GraphDatabaseSettings.SchemaIndex schemaIndex) throws KernelException {
    setUp(schemaIndex);
    var marker = Label.label("marker");
    var propertyName = "property";
    var testConstraint = "testConstraint";
    try (Transaction transaction = database.beginTx()) {
        transaction.schema().constraintFor(marker).withName(testConstraint).assertPropertyIsUnique(propertyName).create();
        transaction.commit();
    }
    var dependencyResolver = ((GraphDatabaseAPI) database).getDependencyResolver();
    var indexingService = dependencyResolver.resolveDependency(IndexingService.class);
    var pageCacheTracer = dependencyResolver.resolveDependency(PageCacheTracer.class);
    try (Transaction transaction = database.beginTx()) {
        var kernelTransaction = ((InternalTransaction) transaction).kernelTransaction();
        var indexDescriptor = kernelTransaction.schemaRead().indexGetForName(testConstraint);
        try (var cursorContext = new CursorContext(pageCacheTracer.createPageCursorTracer("tracePageCacheAccessOnIndexUpdatesApply"))) {
            Iterable<IndexEntryUpdate<IndexDescriptor>> updates = List.of(add(1, indexDescriptor, longValue(4)));
            indexingService.applyUpdates(updates, cursorContext);
            PageCursorTracer cursorTracer = cursorContext.getCursorTracer();
            assertEquals(5L, cursorTracer.pins());
            assertEquals(5L, cursorTracer.unpins());
            assertEquals(2L, cursorTracer.hits());
            assertEquals(3L, cursorTracer.faults());
        }
    }
}
Also used : IndexEntryUpdate(org.neo4j.storageengine.api.IndexEntryUpdate) PageCursorTracer(org.neo4j.io.pagecache.tracing.cursor.PageCursorTracer) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) CursorContext(org.neo4j.io.pagecache.context.CursorContext) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

MethodSource (org.junit.jupiter.params.provider.MethodSource)1199 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1184 Transaction (org.neo4j.graphdb.Transaction)103 Stream (java.util.stream.Stream)70 Test (org.junit.jupiter.api.Test)67 ArrayList (java.util.ArrayList)63 InterruptAfter (io.aeron.test.InterruptAfter)60 List (java.util.List)60 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)58 TimeUnit (java.util.concurrent.TimeUnit)54 IOException (java.io.IOException)52 CountDownLatch (java.util.concurrent.CountDownLatch)52 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)52 lombok.val (lombok.val)52 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)51 Arguments (org.junit.jupiter.params.provider.Arguments)47 AfterEach (org.junit.jupiter.api.AfterEach)46 SSLEngine (javax.net.ssl.SSLEngine)44 AtomicReference (java.util.concurrent.atomic.AtomicReference)43 Path (java.nio.file.Path)42