| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 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 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (31) |
Complexity: 7 |
Complexity Density: 0,26 |
|
| 23 |
|
public class ThreadingTest { |
| 24 |
|
|
| 25 |
|
private static final int THREAD_COUNT = 10; |
| 26 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 2 |
Complexity Density: 0,18 |
1
PASS
|
|
| 27 |
1
|
@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>() { |
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 38 |
10
|
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 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 3 |
Complexity Density: 0,21 |
1
PASS
|
|
| 56 |
1
|
@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>() { |
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 65 |
10
|
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 |
|
|