Clover Coverage Report - EasyMock 2.4
Coverage timestamp: mer. juil. 2 2008 02:17:38 CEST
27   91   7   6,75
0   55   0,26   4
4     1,75  
1    
 
 
  ThreadingTest       Line # 23 27 7 100% 1.0
 
  (2)
 
1    /*
2    * Copyright (c) 2001-2008 OFFIS, Henri Tremblay.
3    * This program is made available under the terms of the MIT License.
4    */
5    package org.easymock.tests2;
6   
7    import static org.easymock.EasyMock.*;
8    import static org.junit.Assert.*;
9   
10    import java.util.Collections;
11    import java.util.List;
12    import java.util.concurrent.*;
13   
14    import org.easymock.tests.IMethods;
15    import org.junit.Test;
16   
17    /**
18    * Test that EasyMock works in replay state in a multithreaded
19    * environment. Note that sadly this test isn't sure to fail all
20    * the time. Only if there's a concurrency issue and we're lucky
21    * enough to fell on it during testing.
22    */
 
23    public class ThreadingTest {
24   
25    private static final int THREAD_COUNT = 10;
26   
 
27  1 toggle @Test
28    public void testThreadSafe() throws Throwable {
29   
30  1 final IMethods mock = createMock(IMethods.class);
31  1 expect(mock.oneArg("test")).andReturn("result").times(THREAD_COUNT);
32   
33  1 makeThreadSafe(mock, true);
34   
35  1 replay(mock);
36   
37  1 Callable<String> replay = new Callable<String>() {
 
38  10 toggle public String call() throws Exception {
39  10 return mock.oneArg("test");
40    }
41    };
42   
43  1 ExecutorService service = Executors.newFixedThreadPool(THREAD_COUNT);
44   
45  1 List<Callable<String>> tasks = Collections.nCopies(THREAD_COUNT, replay);
46   
47  1 List<Future<String>> results = service.invokeAll(tasks);
48   
49  1 for (Future<String> future : results) {
50  10 assertEquals("result", future.get());
51    }
52   
53  1 verify(mock);
54    }
55   
 
56  1 toggle @Test
57    public void testThreadNotSafe() throws Throwable {
58   
59  1 final IMethods mock = createMock(IMethods.class);
60  1 expect(mock.oneArg("test")).andReturn("result").times(THREAD_COUNT);
61   
62  1 replay(mock);
63   
64  1 Callable<String> replay = new Callable<String>() {
 
65  10 toggle public String call() throws Exception {
66  10 return mock.oneArg("test");
67    }
68    };
69   
70  1 ExecutorService service = Executors.newFixedThreadPool(THREAD_COUNT);
71   
72  1 List<Callable<String>> tasks = Collections.nCopies(THREAD_COUNT, replay);
73   
74  1 List<Future<String>> results = service.invokeAll(tasks);
75   
76  1 boolean exceptionThrown = false;
77   
78  1 for (Future<String> future : results) {
79  10 try {
80  10 assertEquals("result", future.get());
81    }
82    catch(ExecutionException e) {
83  9 assertEquals("\n Un-thread-safe mock called from multiple threads", e.getCause().getMessage());
84  9 exceptionThrown = true;
85    }
86    }
87   
88  1 assertTrue(exceptionThrown);
89    }
90    }
91