| 1 | package com.hammurapi.common.tests; |
| 2 | |
| 3 | import static org.junit.Assert.assertEquals; |
| 4 | import static org.junit.Assert.*; |
| 5 | |
| 6 | import java.util.ArrayList; |
| 7 | import java.util.Collection; |
| 8 | import java.util.Collections; |
| 9 | import java.util.List; |
| 10 | import java.util.Random; |
| 11 | import java.util.concurrent.Callable; |
| 12 | import java.util.concurrent.ExecutionException; |
| 13 | import java.util.concurrent.ExecutorService; |
| 14 | import java.util.concurrent.Executors; |
| 15 | import java.util.concurrent.Future; |
| 16 | import java.util.concurrent.atomic.AtomicBoolean; |
| 17 | import java.util.concurrent.atomic.AtomicInteger; |
| 18 | |
| 19 | import org.junit.After; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | |
| 23 | import com.hammurapi.common.FreezeableCollection; |
| 24 | import com.hammurapi.common.InvocationRecordingProxyFactory; |
| 25 | import com.hammurapi.common.InvocationRecordingProxyFactory.InvocationRecord; |
| 26 | import com.hammurapi.common.InvocationRecordingProxyFactory.ProblemListener; |
| 27 | import com.hammurapi.common.concurrent.LocalTrackingExecutorService; |
| 28 | |
| 29 | public class CommonTests { |
| 30 | |
| 31 | private ExecutorService executorService; |
| 32 | |
| 33 | @Before |
| 34 | public void setUp() throws Exception { |
| 35 | executorService = Executors.newFixedThreadPool(10); |
| 36 | } |
| 37 | |
| 38 | @After |
| 39 | public void tearDown() throws Exception { |
| 40 | executorService.shutdown(); |
| 41 | } |
| 42 | |
| 43 | |
| 44 | /** |
| 45 | * Tests that no tasks are executed after join() returns. |
| 46 | * @throws InterruptedException |
| 47 | */ |
| 48 | @Test |
| 49 | public void testLocalTrackingServiceJoin() throws InterruptedException { |
| 50 | |
| 51 | final AtomicBoolean afterJoin = new AtomicBoolean(false); |
| 52 | final Random random = new Random(System.currentTimeMillis()); |
| 53 | final AtomicInteger createdTasks = new AtomicInteger(); |
| 54 | |
| 55 | final LocalTrackingExecutorService ltes = new LocalTrackingExecutorService(executorService, true, null); |
| 56 | final int numberOfTasks = 5; |
| 57 | |
| 58 | class TestTask implements Callable<Boolean> { |
| 59 | |
| 60 | private int depth; |
| 61 | private Collection<Future<Boolean>> collector; |
| 62 | |
| 63 | public TestTask(long delay, int depth, Collection<Future<Boolean>> collector) { |
| 64 | createdTasks.incrementAndGet(); |
| 65 | this.delay = delay; |
| 66 | this.depth = depth; |
| 67 | this.collector = collector; |
| 68 | } |
| 69 | |
| 70 | long delay; |
| 71 | |
| 72 | @Override |
| 73 | public Boolean call() throws Exception { |
| 74 | // Thread.sleep(delay); |
| 75 | if (depth>0) { |
| 76 | for (int i=0; i<numberOfTasks; ++i) { |
| 77 | collector.add(ltes.submit(new TestTask(random.nextInt(100), depth-1, collector))); |
| 78 | } |
| 79 | } |
| 80 | return !afterJoin.get(); |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | FreezeableCollection<Future<Boolean>> collector = new FreezeableCollection<Future<Boolean>>(Collections.synchronizedCollection(new ArrayList<Future<Boolean>>())); |
| 85 | |
| 86 | int depth = 3; |
| 87 | for (int i=0; i<numberOfTasks; ++i) { |
| 88 | collector.add(ltes.submit(new TestTask(random.nextInt(100), depth, collector))); |
| 89 | } |
| 90 | |
| 91 | ltes.join(); |
| 92 | collector.freeze(); |
| 93 | |
| 94 | int counter = 0; |
| 95 | for (Future<Boolean> fb: collector) { |
| 96 | try { |
| 97 | assertNotNull(fb); |
| 98 | assertTrue(fb.get()); |
| 99 | ++counter; |
| 100 | } catch (ExecutionException e) { |
| 101 | e.printStackTrace(); |
| 102 | fail(e.toString()); |
| 103 | } |
| 104 | } |
| 105 | assertEquals(createdTasks.get(), counter); |
| 106 | |
| 107 | } |
| 108 | |
| 109 | @Test |
| 110 | public void testInvocationRecordingProxyFactoryTest() { |
| 111 | ArrayList<String> target = new ArrayList<String>(); |
| 112 | |
| 113 | InvocationRecordingProxyFactory.ProblemListener problemListener = new ProblemListener() { |
| 114 | |
| 115 | @Override |
| 116 | public void onProblem(Throwable th, List<InvocationRecord> records) { |
| 117 | for (InvocationRecord record: records) { |
| 118 | System.out.println(record); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | }; |
| 123 | |
| 124 | List<String> proxy = InvocationRecordingProxyFactory.wrap(List.class, target, problemListener); |
| 125 | |
| 126 | proxy.add("Hello"); |
| 127 | proxy.iterator().next(); |
| 128 | |
| 129 | assertEquals(3, InvocationRecordingProxyFactory.getInvocationRecords(proxy).size()); |
| 130 | |
| 131 | } |
| 132 | |
| 133 | } |