Clover Coverage Report - EasyMock 3.0
Coverage timestamp: sam. mai 8 2010 14:37:27 CEST
68   205   13   6,8
0   127   0,19   10
10     1,3  
1    
 
  ThreadingTest       Line # 38 68 0% 13 1 98,7% 0.98717946
 
  (12)
 
1    /**
2    * Copyright 2001-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10    * Unless required by applicable law or agreed to in writing, software
11    * distributed under the License is distributed on an "AS IS" BASIS,
12    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    * See the License for the specific language governing permissions and
14    * limitations under the License.
15    */
16   
17    package org.easymock.tests2;
18   
19    import static org.easymock.EasyMock.*;
20    import static org.junit.Assert.*;
21   
22    import java.util.Collections;
23    import java.util.List;
24    import java.util.concurrent.*;
25   
26    import org.easymock.internal.AssertionErrorWrapper;
27    import org.easymock.internal.MocksBehavior;
28    import org.easymock.tests.IMethods;
29    import org.junit.Test;
30   
31    /**
32    * Test that EasyMock works in replay state in a multithreaded environment. Note
33    * that sadly this test isn't sure to fail all the time. Only if there's a
34    * concurrency issue and we're lucky enough to fell on it during testing.
35    *
36    * @author Henri Tremblay
37    */
 
38    public class ThreadingTest {
39   
40    private static final int THREAD_COUNT = 10;
41   
 
42  2 toggle @Test
43    public void testThreadSafe() throws Throwable {
44   
45  2 final IMethods mock = createMock(IMethods.class);
46  2 expect(mock.oneArg("test")).andReturn("result").times(THREAD_COUNT);
47   
48  2 replay(mock);
49   
50  2 final Callable<String> replay = new Callable<String>() {
 
51  20 toggle public String call() throws Exception {
52  20 return mock.oneArg("test");
53    }
54    };
55   
56  2 final ExecutorService service = Executors.newFixedThreadPool(THREAD_COUNT);
57   
58  2 final List<Callable<String>> tasks = Collections.nCopies(THREAD_COUNT, replay);
59   
60  2 final List<Future<String>> results = service.invokeAll(tasks);
61   
62  2 for (final Future<String> future : results) {
63  20 assertEquals("result", future.get());
64    }
65   
66  2 verify(mock);
67    }
68   
 
69  2 toggle @Test
70    public void testThreadNotSafe() throws Throwable {
71   
72  2 final IMethods mock = createMock(IMethods.class);
73  2 expect(mock.oneArg("test")).andReturn("result").times(THREAD_COUNT);
74   
75  2 makeThreadSafe(mock, false);
76   
77  2 checkIsUsedInOneThread(mock, true);
78   
79  2 replay(mock);
80   
81  2 final Callable<String> replay = new Callable<String>() {
 
82  20 toggle public String call() throws Exception {
83  20 return mock.oneArg("test");
84    }
85    };
86   
87  2 final ExecutorService service = Executors.newFixedThreadPool(THREAD_COUNT);
88   
89  2 final List<Callable<String>> tasks = Collections.nCopies(THREAD_COUNT, replay);
90   
91  2 final List<Future<String>> results = service.invokeAll(tasks);
92   
93  2 boolean exceptionThrown = false;
94   
95  2 for (final Future<String> future : results) {
96  20 try {
97  20 assertEquals("result", future.get());
98    } catch (final ExecutionException e) {
99    // Since I don't know which one the lastThread is, that's the
100    // best assert I can do except doing
101    // a regular exception and I don't think it worth it
102  18 assertTrue(e.getCause().getMessage().startsWith(
103    "\n Mock isn't supposed to be called from multiple threads. Last: "));
104  18 exceptionThrown = true;
105    }
106    }
107   
108  2 assertTrue(exceptionThrown);
109    }
110   
 
111  2 toggle @Test
112    public void testMockUsedCorrectly() {
113  2 final IMethods mock = createMock(IMethods.class);
114  2 expect(mock.oneArg("test")).andReturn("result").times(2);
115   
116  2 checkIsUsedInOneThread(mock, true);
117   
118  2 replay(mock);
119   
120  2 mock.oneArg("test");
121  2 mock.oneArg("test");
122   
123  2 verify(mock);
124    }
125   
 
126  2 toggle @Test
127    public void testChangeDefault() throws Throwable {
128  2 final String previousThreadSafetyCheck = setEasyMockProperty(ENABLE_THREAD_SAFETY_CHECK_BY_DEFAULT,
129    Boolean.TRUE.toString());
130  2 final String previousThreadSafe = setEasyMockProperty(NOT_THREAD_SAFE_BY_DEFAULT, Boolean.TRUE
131    .toString());
132  2 try {
133  2 final MocksBehavior behavior = new MocksBehavior(true);
134  2 assertFalse(behavior.isThreadSafe());
135   
136  2 final Thread t = new Thread() {
 
137  2 toggle @Override
138    public void run() {
139  2 behavior.checkThreadSafety();
140    }
141    };
142  2 t.start();
143  2 t.join();
144  2 try {
145  2 behavior.checkThreadSafety();
146  0 fail("Shouldn't work");
147    } catch (final AssertionErrorWrapper e) {
148   
149    }
150   
151    } finally {
152  2 setEasyMockProperty(ENABLE_THREAD_SAFETY_CHECK_BY_DEFAULT, previousThreadSafetyCheck);
153  2 setEasyMockProperty(NOT_THREAD_SAFE_BY_DEFAULT, previousThreadSafe);
154    }
155    }
156   
 
157  2 toggle @Test
158    public void testRecordingInMultipleThreads() throws InterruptedException, ExecutionException {
159   
160  2 final Callable<String> replay = new Callable<String>() {
 
161  20 toggle public String call() throws Exception {
162  20 final IMethods mock = createMock(IMethods.class);
163  20 expect(mock.oneArg("test")).andReturn("result");
164   
165  20 replay(mock);
166   
167  20 final String s = mock.oneArg("test");
168   
169  20 verify(mock);
170   
171  20 return s;
172    }
173    };
174   
175  2 final ExecutorService service = Executors.newFixedThreadPool(THREAD_COUNT);
176   
177  2 final List<Callable<String>> tasks = Collections.nCopies(THREAD_COUNT, replay);
178   
179  2 final List<Future<String>> results = service.invokeAll(tasks);
180   
181  2 for (final Future<String> future : results) {
182  20 assertEquals("result", future.get());
183    }
184    }
185   
 
186  2 toggle @SuppressWarnings("unchecked")
187    @Test
188    public void testCleanupAfterFailureInRecordPhase() {
189  2 Comparable<String> mock = createNiceMock(Comparable.class);
190   
191    // Mocking equals() doesn't work
192  2 try {
193  2 expect(mock.equals(eq(mock))).andReturn(true);
194    } catch (final IllegalStateException e) {
195   
196    }
197   
198    // However, the recorded matchers should be cleaned to prevent impacting
199    // other tests
200  2 mock = createNiceMock(Comparable.class);
201  2 expect(mock.compareTo((String) isNull())).andReturn(1);
202  2 replay(mock);
203  2 assertEquals(1, mock.compareTo(null));
204    }
205    }