@Test
public void testGroovyScriptEnsuresThreadSafe() throws InterruptedException {

    GroovyScriptEngine groovyScriptEngine = new GroovyScriptEngine();
    String groovyExpression = "a % 2 == 0";
    final GroovyScript<Boolean> groovyScript = new GroovyScript<>(groovyExpression, groovyScriptEngine);
    final AtomicInteger index = new AtomicInteger(0);
    List<Thread> threads = new ArrayList<>();
    final AtomicReference<Throwable> anyException = new AtomicReference<>();
    for (int i = 0 ; i < 500 ; i++) {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int j = 0 ; j < 10 ; j++) {
                    try {
                        Thread.sleep(new Random().nextInt(10));
                    } catch (InterruptedException e) {
                        // no-op
                    }
                    int aVal = index.getAndIncrement();
                    HashMap<String, Object> fieldsAndValue = new HashMap<>();
                    fieldsAndValue.put("a", aVal);
                    try {
                        assertEquals(aVal % 2 == 0, groovyScript.evaluate(StreamlineEventImpl.builder().fieldsAndValues(fieldsAndValue).dataSourceId("1").build()));
                    } catch (Throwable e) {
                        e.printStackTrace();
                        anyException.set(e);
                    }
                }
            }
        });
        threads.add(t);
        t.start();
    }
    for (Thread t : threads) {
        t.join();
    }
    if (anyException.get() != null) {
        Assert.fail("Exception occurred within thread, first one is " + anyException.get().getMessage());
    }
}