Skip to content

ParallellTesting

Keyhan Hadjari edited this page Mar 13, 2017 · 1 revision
@Test
public void testParallellGedis() throws Exception {
    ExecutorService executor = Executors.newFixedThreadPool(10);
    Callable<String> task = () ->{
        try {
            return idMapService.getGedi("cnr", "123", new DateTime()).getGedi();
        }  catch (Exception e) {
            fail(e.getMessage());
        }
        return null;
    };

    List<Future<String>> futures = new ArrayList<>();

    for(int i = 0; i < 10; ++i) {
        futures.add(executor.submit(task));
    }

    executor.shutdown(); //shuts down the executor service.
    if(!executor.awaitTermination(4000, TimeUnit.MILLISECONDS)) {//waits for the threads to finish or timeout
        fail();
    }

    futures.stream().map(future -> {
        try {
            return future.get();
        }  catch (Exception e) {
            fail(e.getMessage());
        }
        return null;
    }).reduce((a, b) -> {
            if(!a.equals(b)) {
                fail("Gedis generated by parallell threads not equal(Race Condition): " + a + ", " + b);
            }
        return a;
    }).ifPresent(gedi ->
            System.out.println("Generated Gedi For all Threads = " + gedi));
}

Clone this wiki locally