Search in sources :

Example 1 with NamedFunction

use of org.neo4j.test.NamedFunction in project neo4j by neo4j.

the class ThreadedTransaction method doExecute.

private String[] doExecute(ThreadingRule threading, S subject, KernelTransaction.Type txType, boolean startEarly, String... queries) {
    NamedFunction<S, Throwable> startTransaction = new NamedFunction<S, Throwable>("threaded-transaction-" + Arrays.hashCode(queries)) {

        @Override
        public Throwable apply(S subject) {
            try (InternalTransaction tx = neo.beginLocalTransactionAsUser(subject, txType)) {
                Result result = null;
                try {
                    if (startEarly) {
                        latch.start();
                    }
                    for (String query : queries) {
                        if (result != null) {
                            result.close();
                        }
                        result = neo.getLocalGraph().execute(query);
                    }
                    if (!startEarly) {
                        latch.startAndWaitForAllToStart();
                    }
                } finally {
                    if (!startEarly) {
                        latch.start();
                    }
                    latch.finishAndWaitForAllToFinish();
                }
                result.close();
                tx.success();
                return null;
            } catch (Throwable t) {
                return t;
            }
        }
    };
    done = threading.execute(startTransaction, subject);
    return queries;
}
Also used : NamedFunction(org.neo4j.test.NamedFunction) Matchers.containsString(org.hamcrest.Matchers.containsString) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) Result(org.neo4j.graphdb.Result)

Example 2 with NamedFunction

use of org.neo4j.test.NamedFunction in project neo4j by neo4j.

the class ThreadedTransactionPeriodicCommit method execute.

void execute(ThreadingRule threading, S subject, int nLines) {
    NamedFunction<Integer, Throwable> servCsv = new NamedFunction<Integer, Throwable>("serv-csv") {

        @Override
        public Throwable apply(Integer n) throws RuntimeException {
            try {
                ServerSocket serverSocket = new ServerSocket(csvHttpPort);
                Socket clientSocket = serverSocket.accept();
                PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
                // Start sending our reply, using the HTTP 1.1 protocol
                // Version & status code
                out.print("HTTP/1.1 200 \r\n");
                // The type of data
                out.print("Content-Type: text/plain\r\n");
                // Will close stream
                out.print("Connection: close\r\n");
                // End of headers
                out.print("\r\n");
                for (int i = 0; i < n - 1; i++) {
                    out.print("line " + i + "\r\n");
                }
                out.flush();
                barrier.reached();
                out.print("line " + (n - 1) + "\r\n");
                out.close();
                clientSocket.close();
                serverSocket.close();
                return null;
            } catch (Throwable t) {
                return t;
            }
        }
    };
    NamedFunction<S, String> loadCsv = new NamedFunction<S, String>("load-csv") {

        @Override
        public String apply(S subject) {
            try {
                return neo.executeQuery(subject, "USING PERIODIC COMMIT 1 " + "LOAD CSV FROM 'http://localhost:" + csvHttpPort + "/file.csv' AS line " + "CREATE (l:Line {name: line[0]}) RETURN line[0] as name", null, r -> {
                });
            } catch (Throwable t) {
                return t.getMessage();
            }
        }
    };
    servCsvResult = threading.execute(servCsv, nLines);
    loadCsvResult = threading.execute(loadCsv, subject);
}
Also used : ServerSocket(java.net.ServerSocket) NamedFunction(org.neo4j.test.NamedFunction) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) PrintWriter(java.io.PrintWriter)

Aggregations

NamedFunction (org.neo4j.test.NamedFunction)2 PrintWriter (java.io.PrintWriter)1 ServerSocket (java.net.ServerSocket)1 Socket (java.net.Socket)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Result (org.neo4j.graphdb.Result)1 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)1