|
| 1 | +/** |
| 2 | + * Copyright (c) 2002-2016 "Neo Technology," |
| 3 | + * Network Engine for Objects in Lund AB [http://neotechnology.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | +package org.neo4j.driver.v1.stress; |
| 20 | + |
| 21 | +import org.junit.Rule; |
| 22 | +import org.junit.Test; |
| 23 | + |
| 24 | +import java.util.List; |
| 25 | +import java.util.Random; |
| 26 | +import java.util.concurrent.ExecutorService; |
| 27 | +import java.util.concurrent.Executors; |
| 28 | +import java.util.concurrent.ThreadLocalRandom; |
| 29 | +import java.util.concurrent.TimeUnit; |
| 30 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 31 | + |
| 32 | +import org.neo4j.driver.v1.Config; |
| 33 | +import org.neo4j.driver.v1.Driver; |
| 34 | +import org.neo4j.driver.v1.Session; |
| 35 | +import org.neo4j.driver.v1.StatementResult; |
| 36 | +import org.neo4j.driver.v1.util.TestNeo4j; |
| 37 | + |
| 38 | +import static java.util.Arrays.asList; |
| 39 | +import static org.neo4j.driver.v1.GraphDatabase.driver; |
| 40 | + |
| 41 | +public class SessionPoolingStressIT |
| 42 | +{ |
| 43 | + @Rule |
| 44 | + public TestNeo4j neo4j = new TestNeo4j(); |
| 45 | + |
| 46 | + private static final int N_THREADS = 10; |
| 47 | + private final ExecutorService executor = Executors.newFixedThreadPool( N_THREADS ); |
| 48 | + private static final List<String> QUERIES = asList( "RETURN 1295 + 42", "UNWIND range(1,10000) AS x CREATE (n {prop:x}) DELETE n " ); |
| 49 | + private static final int MAX_TIME = 10000; |
| 50 | + private final AtomicBoolean hasFailed = new AtomicBoolean( false ); |
| 51 | + |
| 52 | + |
| 53 | + @Test |
| 54 | + public void shouldWorkFine() throws InterruptedException |
| 55 | + { |
| 56 | + Driver driver = driver( neo4j.address(), |
| 57 | + Config.build() |
| 58 | + .withEncryptionLevel( Config.EncryptionLevel.NONE ) |
| 59 | + .withMaxSessions( N_THREADS + 1 ).toConfig() ); |
| 60 | + |
| 61 | + doWork( driver ); |
| 62 | + executor.awaitTermination( MAX_TIME + (int)(MAX_TIME * 0.2), TimeUnit.MILLISECONDS ); |
| 63 | + } |
| 64 | + |
| 65 | + private void doWork( final Driver driver ) |
| 66 | + { |
| 67 | + for ( int i = 0; i < N_THREADS; i++ ) |
| 68 | + { |
| 69 | + executor.execute( new Worker( driver ) ); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private class Worker implements Runnable |
| 74 | + { |
| 75 | + private final Random random = ThreadLocalRandom.current(); |
| 76 | + private final Driver driver; |
| 77 | + |
| 78 | + public Worker( Driver driver ) |
| 79 | + { |
| 80 | + this.driver = driver; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void run() |
| 85 | + { |
| 86 | + try |
| 87 | + { |
| 88 | + long deadline = System.currentTimeMillis() + MAX_TIME; |
| 89 | + for (;;) |
| 90 | + { |
| 91 | + for ( String query : QUERIES ) |
| 92 | + { |
| 93 | + runQuery( query ); |
| 94 | + } |
| 95 | + long left = deadline - System.currentTimeMillis(); |
| 96 | + if ( left <= 0 ) |
| 97 | + { |
| 98 | + break; |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + catch ( Throwable e ) |
| 103 | + { |
| 104 | + e.printStackTrace(); |
| 105 | + hasFailed.set( true ); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private void runQuery( String query ) throws InterruptedException |
| 110 | + { |
| 111 | + try ( Session session = driver.session() ) |
| 112 | + { |
| 113 | + StatementResult run = session.run( query ); |
| 114 | + Thread.sleep( random.nextInt( 100 ) ); |
| 115 | + run.consume(); |
| 116 | + Thread.sleep( random.nextInt( 100 ) ); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments