|
| 1 | +package com.github.mustachejava; |
| 2 | + |
| 3 | +import com.github.mustachejavabenchmarks.BenchmarkTest; |
| 4 | +import org.junit.Test; |
| 5 | + |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.StringReader; |
| 8 | +import java.io.StringWriter; |
| 9 | +import java.io.Writer; |
| 10 | +import java.security.SecureRandom; |
| 11 | +import java.util.Random; |
| 12 | +import java.util.concurrent.Callable; |
| 13 | +import java.util.concurrent.ExecutorService; |
| 14 | +import java.util.concurrent.Executors; |
| 15 | +import java.util.concurrent.Semaphore; |
| 16 | +import java.util.concurrent.atomic.AtomicInteger; |
| 17 | + |
| 18 | +import static com.github.mustachejavabenchmarks.BenchmarkTest.skip; |
| 19 | +import static junit.framework.Assert.assertEquals; |
| 20 | + |
| 21 | +/** |
| 22 | + * Inspired by an unconfirmed bug report. |
| 23 | + */ |
| 24 | +public class ConcurrencyTest { |
| 25 | + |
| 26 | + static Random r = new SecureRandom(); |
| 27 | + |
| 28 | + private static class TestObject { |
| 29 | + final int a; |
| 30 | + final int b; |
| 31 | + final int c; |
| 32 | + |
| 33 | + int aa() throws InterruptedException { |
| 34 | + Thread.sleep(r.nextInt(10)); |
| 35 | + return a; |
| 36 | + } |
| 37 | + |
| 38 | + int bb() throws InterruptedException { |
| 39 | + Thread.sleep(r.nextInt(10)); |
| 40 | + return b; |
| 41 | + } |
| 42 | + |
| 43 | + int cc() throws InterruptedException { |
| 44 | + Thread.sleep(r.nextInt(10)); |
| 45 | + return c; |
| 46 | + } |
| 47 | + |
| 48 | + Callable<Integer> calla() throws InterruptedException { |
| 49 | + return new Callable<Integer>() { |
| 50 | + @Override |
| 51 | + public Integer call() throws Exception { |
| 52 | + Thread.sleep(r.nextInt(10)); |
| 53 | + return a; |
| 54 | + } |
| 55 | + }; |
| 56 | + } |
| 57 | + |
| 58 | + Callable<Integer> callb() throws InterruptedException { |
| 59 | + return new Callable<Integer>() { |
| 60 | + @Override |
| 61 | + public Integer call() throws Exception { |
| 62 | + Thread.sleep(r.nextInt(10)); |
| 63 | + return b; |
| 64 | + } |
| 65 | + }; |
| 66 | + } |
| 67 | + |
| 68 | + Callable<Integer> callc() throws InterruptedException { |
| 69 | + return new Callable<Integer>() { |
| 70 | + @Override |
| 71 | + public Integer call() throws Exception { |
| 72 | + Thread.sleep(r.nextInt(10)); |
| 73 | + return c; |
| 74 | + } |
| 75 | + }; |
| 76 | + } |
| 77 | + |
| 78 | + private TestObject(int a, int b, int c) { |
| 79 | + this.a = a; |
| 80 | + this.b = b; |
| 81 | + this.c = c; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // Alternate render |
| 86 | + static String render(TestObject to) { |
| 87 | + return to.a + ":" + to.b + ":" + to.c; |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testConcurrentExecution() throws InterruptedException { |
| 92 | + if (skip()) return; |
| 93 | + String template = "{{aa}}:{{bb}}:{{cc}}"; |
| 94 | + final Mustache test = new DefaultMustacheFactory().compile(new StringReader(template), "test"); |
| 95 | + ExecutorService es = Executors.newCachedThreadPool(); |
| 96 | + final AtomicInteger total = new AtomicInteger(); |
| 97 | + final Semaphore semaphore = new Semaphore(100); |
| 98 | + for (int i = 0; i < 100000; i++) { |
| 99 | + semaphore.acquire(); |
| 100 | + es.submit(new Runnable() { |
| 101 | + @Override |
| 102 | + public void run() { |
| 103 | + try { |
| 104 | + TestObject testObject = new TestObject(r.nextInt(), r.nextInt(), r.nextInt()); |
| 105 | + StringWriter sw = new StringWriter(); |
| 106 | + test.execute(sw, testObject).close(); |
| 107 | + if (!render(testObject).equals(sw.toString())) { |
| 108 | + total.incrementAndGet(); |
| 109 | + } |
| 110 | + } catch (IOException e) { |
| 111 | + // Can't fail |
| 112 | + e.printStackTrace(); |
| 113 | + System.exit(1); |
| 114 | + } finally { |
| 115 | + semaphore.release(); |
| 116 | + } |
| 117 | + } |
| 118 | + }); |
| 119 | + } |
| 120 | + // Wait for them all to complete |
| 121 | + semaphore.acquire(100); |
| 122 | + assertEquals(0, total.intValue()); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void testConcurrentExecutionWithConcurrentTemplate() throws InterruptedException { |
| 127 | + if (skip()) return; |
| 128 | + String template = "{{calla}}:{{callb}}:{{callc}}"; |
| 129 | + ExecutorService es = Executors.newCachedThreadPool(); |
| 130 | + DefaultMustacheFactory dmf = new DefaultMustacheFactory(); |
| 131 | + dmf.setExecutorService(es); |
| 132 | + final Mustache test = dmf.compile(new StringReader(template), "test"); |
| 133 | + final AtomicInteger total = new AtomicInteger(); |
| 134 | + final Semaphore semaphore = new Semaphore(100); |
| 135 | + for (int i = 0; i < 100000; i++) { |
| 136 | + semaphore.acquire(); |
| 137 | + es.submit(new Runnable() { |
| 138 | + @Override |
| 139 | + public void run() { |
| 140 | + try { |
| 141 | + TestObject testObject = new TestObject(r.nextInt(), r.nextInt(), r.nextInt()); |
| 142 | + StringWriter sw = new StringWriter(); |
| 143 | + test.execute(sw, testObject).close(); |
| 144 | + if (!render(testObject).equals(sw.toString())) { |
| 145 | + total.incrementAndGet(); |
| 146 | + } |
| 147 | + } catch (IOException e) { |
| 148 | + // Can't fail |
| 149 | + e.printStackTrace(); |
| 150 | + System.exit(1); |
| 151 | + } finally { |
| 152 | + semaphore.release(); |
| 153 | + } |
| 154 | + } |
| 155 | + }); |
| 156 | + } |
| 157 | + // Wait for them all to complete |
| 158 | + semaphore.acquire(100); |
| 159 | + assertEquals(0, total.intValue()); |
| 160 | + } |
| 161 | +} |
0 commit comments