Move partial thread safety test into ThreadSafetyTest(s).
This commit is contained in:
@@ -1,67 +0,0 @@
|
||||
//
|
||||
// JMustache - A Java implementation of the Mustache templating language
|
||||
// http://github.com/samskivert/jmustache/blob/master/LICENSE
|
||||
|
||||
package com.samskivert.mustache;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentLinkedDeque;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.samskivert.mustache.Mustache.TemplateLoader;
|
||||
|
||||
public class PartialThreadSafeTest {
|
||||
|
||||
@Test
|
||||
public void testPartialThreadSafe() throws Exception {
|
||||
AtomicInteger loadCount = new AtomicInteger();
|
||||
TemplateLoader loader = new TemplateLoader() {
|
||||
@Override
|
||||
public Reader getTemplate(String name) throws Exception {
|
||||
if ("partial".equals(name)) {
|
||||
loadCount.incrementAndGet();
|
||||
TimeUnit.MILLISECONDS.sleep(20);
|
||||
return new StringReader("Hello");
|
||||
}
|
||||
throw new IOException(name);
|
||||
}
|
||||
};
|
||||
|
||||
Template template = Mustache.compiler().withLoader(loader).compile("{{stuff}}\n\t{{> partial }}");
|
||||
ExecutorService executor = Executors.newFixedThreadPool(64);
|
||||
ConcurrentLinkedDeque<Exception> q = new ConcurrentLinkedDeque<>();
|
||||
|
||||
Map<String, Object> m = new HashMap<>();
|
||||
m.put("stuff", "Foo");
|
||||
for (int i = 100; i > 0; i--) {
|
||||
int ii = i;
|
||||
executor.execute(() -> {
|
||||
try {
|
||||
TimeUnit.MILLISECONDS.sleep(ii % 10);
|
||||
template.execute(m);
|
||||
} catch (Exception e) {
|
||||
q.add(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
executor.shutdown();
|
||||
executor.awaitTermination(10_000, TimeUnit.MILLISECONDS);
|
||||
if (!q.isEmpty()) {
|
||||
System.out.println(q);
|
||||
}
|
||||
assertTrue(q.isEmpty());
|
||||
assertEquals(1, loadCount.get());
|
||||
}
|
||||
}
|
||||
@@ -4,12 +4,19 @@
|
||||
|
||||
package com.samskivert.mustache;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import java.util.concurrent.ConcurrentLinkedDeque;
|
||||
import java.util.concurrent.CyclicBarrier;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import com.samskivert.mustache.Mustache;
|
||||
@@ -79,4 +86,46 @@ public class ThreadSafetyTest {
|
||||
|
||||
assertEquals(1, loader.counter());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPartialThreadSafe () throws Exception {
|
||||
AtomicInteger loadCount = new AtomicInteger();
|
||||
Mustache.TemplateLoader loader = new Mustache.TemplateLoader() {
|
||||
@Override
|
||||
public Reader getTemplate(String name) throws Exception {
|
||||
if ("partial".equals(name)) {
|
||||
loadCount.incrementAndGet();
|
||||
TimeUnit.MILLISECONDS.sleep(20);
|
||||
return new StringReader("Hello");
|
||||
}
|
||||
throw new IOException(name);
|
||||
}
|
||||
};
|
||||
|
||||
Template template = Mustache.compiler().withLoader(loader).
|
||||
compile("{{stuff}}\n\t{{> partial }}");
|
||||
ExecutorService executor = Executors.newFixedThreadPool(64);
|
||||
ConcurrentLinkedDeque<Exception> q = new ConcurrentLinkedDeque<>();
|
||||
|
||||
Map<String, Object> m = new HashMap<>();
|
||||
m.put("stuff", "Foo");
|
||||
for (int i = 100; i > 0; i--) {
|
||||
int ii = i;
|
||||
executor.execute(() -> {
|
||||
try {
|
||||
TimeUnit.MILLISECONDS.sleep(ii % 10);
|
||||
template.execute(m);
|
||||
} catch (Exception e) {
|
||||
q.add(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
executor.shutdown();
|
||||
executor.awaitTermination(10_000, TimeUnit.MILLISECONDS);
|
||||
if (!q.isEmpty()) {
|
||||
System.out.println(q);
|
||||
}
|
||||
assertTrue(q.isEmpty());
|
||||
assertEquals(1, loadCount.get());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user