Last active
October 19, 2019 14:11
-
-
Save sebersole/c8c3664049b2c864c61a5ead45af5c21 to your computer and use it in GitHub Desktop.
Concurrency testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void testQueryConcurrency(SessionFactoryScope scope) throws InterruptedException { | |
final int numberOfIterations = 40; | |
final int numberOfForks = 5; | |
final ExecutorService executor = Executors.newFixedThreadPool( 5 ); | |
final List<Future<String>> futures = CollectionHelper.arrayList( numberOfIterations * numberOfForks ); | |
try { | |
for ( int f = 0; f < numberOfForks; f++ ) { | |
final ArrayList<Callable<String>> tasks = CollectionHelper.arrayList( numberOfIterations ); | |
for ( int i = 0; i < numberOfIterations; i++ ) { | |
tasks.add( () -> executeQueriesForConcurrency( scope ) ); | |
} | |
futures.addAll( executor.invokeAll( tasks ) ); | |
assertThat( futures.size(), is( numberOfIterations * (f+1) ) ); | |
} | |
} | |
finally { | |
// make sure all iterations/tasks have completed | |
executor.shutdown(); | |
executor.awaitTermination( 15, TimeUnit.SECONDS ); | |
while ( ! futures.isEmpty() ) { | |
futures.removeIf( future -> future.isCancelled() || future.isDone() ); | |
} | |
} | |
// no matter what I have tried above in finally I have been unable to successfully wait until | |
// all queries have finished executing. So I get failures here, although the actual value of | |
// `statistics.getPrepareStatementCount()` varies each time - which is what I base my conclusion | |
// that not all queries have completed by the time execution gets here. | |
assertThat( | |
statistics.getPrepareStatementCount(), | |
// 3 queries per iteration | |
is( numberOfForks * numberOfIterations * 3 ) | |
); | |
} | |
public String executeQueriesForConcurrency(SessionFactoryScope scope) { | |
scope.inTransaction( | |
session -> { | |
final QueryImplementor<Component> query1 = session.createQuery( | |
"select e.component from SimpleEntity e where e.component.attribute1 = :param", | |
Component.class | |
); | |
query1.setParameter( "param", "a1" ).list(); | |
final QueryImplementor<Component> query2 = session.createQuery( | |
"select e.component from SimpleEntity e where e.component.attribute1 = :param", | |
Component.class | |
); | |
query2.setParameter( "param", "b1" ).list(); | |
} | |
); | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment