private DefaultMustacheFactory initParallel() { DefaultMustacheFactory cf = createMustacheFactory(); cf.setExecutorService(Executors.newCachedThreadPool()); return cf; }
@Test public void testConcurrentExecutionWithConcurrentTemplate() throws InterruptedException { if (skip()) return; String template = "{{calla}}:{{callb}}:{{callc}}"; ExecutorService es = Executors.newCachedThreadPool(); DefaultMustacheFactory dmf = new DefaultMustacheFactory(); dmf.setExecutorService(es); final Mustache test = dmf.compile(new StringReader(template), "test"); final AtomicInteger total = render(test, es); assertEquals(0, total.intValue()); } }
public void testConcurrency() throws IOException { DefaultMustacheFactory c = createMustacheFactory(); c.setExecutorService(Executors.newCachedThreadPool()); Mustache m = c.compile(new StringReader("{{a}} {{#caps}}{{b}}{{/caps}} {{c}}"), "concurrency"); StringWriter sw = new StringWriter(); long start = System.currentTimeMillis(); Writer execute = m.execute(sw, new Object() { Callable<Object> a = () -> { Thread.sleep(300); return "How"; }; Callable<Object> b = () -> { Thread.sleep(200); return "are"; }; Callable<Object> c = () -> { Thread.sleep(100); return "you?"; }; Callable<Function> caps = () -> (Function) o -> o.toString().toUpperCase(); }); execute.close(); assertTrue("Time < 600ms", System.currentTimeMillis() - start < 600); assertEquals("How ARE you?", sw.toString()); }
public void testNestedLatches() throws IOException { DefaultMustacheFactory c = createMustacheFactory(); c.setExecutorService(Executors.newCachedThreadPool()); Mustache m = c.compile("latchedtest.html"); StringWriter sw = new StringWriter(); Writer execute = m.execute(sw, new Object() { Callable<Object> nest = () -> { Thread.sleep(300); return "How"; }; Callable<Object> nested = () -> { Thread.sleep(200); return "are"; }; Callable<Object> nestest = () -> { Thread.sleep(100); return "you?"; }; }); execute.close(); assertEquals("<outer>\n<inner>How</inner>\n<inner>are</inner>\n<inner>you?</inner>\n</outer>\n", sw.toString()); }
public void testNestedLatchesIterable() throws IOException { DefaultMustacheFactory c = createMustacheFactory(); c.setExecutorService(Executors.newCachedThreadPool()); Mustache m = c.compile("latchedtestiterable.html"); StringWriter sw = new StringWriter();
public void testDeferred() throws IOException { DefaultMustacheFactory mf = new DeferringMustacheFactory(root); mf.setExecutorService(Executors.newCachedThreadPool()); Object context = new Object() { String title = "Deferred"; Object deferred = new DeferringMustacheFactory.DeferredCallable(); Object deferredpartial = DeferringMustacheFactory.DEFERRED; }; Mustache m = mf.compile("deferred.html"); StringWriter sw = new StringWriter(); m.execute(sw, context).close(); assertEquals(getContents(root, "deferred.txt"), sw.toString()); }
public void testParallelComplex() throws MustacheException, IOException { if (skip()) return; System.out.println("complex.html evaluations per millisecond:"); for (int i = 0; i < 3; i++) { { DefaultMustacheFactory cf = createMustacheFactory(); cf.setExecutorService(Executors.newCachedThreadPool()); Mustache m = cf.compile("complex.html"); complextest(m, new ParallelComplexObject()).toString(); long start = System.currentTimeMillis(); int total = 0; while (true) { complextest(m, new ParallelComplexObject()); total++; if (System.currentTimeMillis() - start > TIME) break; } System.out.println("Parallel: " + total / TIME); } } }