Search in sources :

Example 1 with Invocation

use of mockit.Invocation in project drill by apache.

the class FunctionInitializerTest method testConcurrentFunctionBodyLoad.

@Test
public void testConcurrentFunctionBodyLoad() throws Exception {
    final FunctionInitializer functionInitializer = new FunctionInitializer(CLASS_NAME, classLoader);
    final AtomicInteger counter = new AtomicInteger();
    new MockUp<FunctionInitializer>() {

        @Mock
        Java.CompilationUnit convertToCompilationUnit(Invocation inv, Class<?> clazz) {
            counter.incrementAndGet();
            return inv.proceed();
        }
    };
    int threadsNumber = 5;
    ExecutorService executor = Executors.newFixedThreadPool(threadsNumber);
    try {
        List<Future<String>> results = executor.invokeAll(Collections.nCopies(threadsNumber, new Callable<String>() {

            @Override
            public String call() throws Exception {
                return functionInitializer.getMethod("eval");
            }
        }));
        final Set<String> uniqueResults = new HashSet<>();
        for (Future<String> result : results) {
            uniqueResults.add(result.get());
        }
        assertEquals("All threads should have received the same result", 1, uniqueResults.size());
        assertEquals("Number of function body loads should match", 1, counter.intValue());
    } finally {
        executor.shutdownNow();
    }
}
Also used : Java(org.codehaus.janino.Java) Invocation(mockit.Invocation) MockUp(mockit.MockUp) Callable(java.util.concurrent.Callable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) BeforeClass(org.junit.BeforeClass) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

HashSet (java.util.HashSet)1 Callable (java.util.concurrent.Callable)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Invocation (mockit.Invocation)1 MockUp (mockit.MockUp)1 Java (org.codehaus.janino.Java)1 BeforeClass (org.junit.BeforeClass)1 Test (org.junit.Test)1