How to use method of org.jmock.test.unit.lib.concurrent.DeterministicSchedulerTests class

Best Jmock-library code snippet using org.jmock.test.unit.lib.concurrent.DeterministicSchedulerTests.

Run Jmock-library automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1package org.jmock.test.unit.lib.concurrent;
2
3
4import static org.hamcrest.Matchers.equalTo;
5import static org.hamcrest.Matchers.sameInstance;
6import static org.junit.Assert.assertThat;
7
8import java.util.concurrent.Callable;
9import java.util.concurrent.ExecutionException;
10import java.util.concurrent.Future;
11import java.util.concurrent.ScheduledFuture;
12import java.util.concurrent.TimeUnit;
13
14import org.jmock.Expectations;
15import org.jmock.Sequence;
16import org.jmock.api.Action;
17import org.jmock.integration.junit3.MockObjectTestCase;
18import org.jmock.lib.concurrent.DeterministicScheduler;
19import org.jmock.lib.concurrent.UnsupportedSynchronousOperationException;
20
21public class DeterministicSchedulerTests extends MockObjectTestCase {
22    DeterministicScheduler scheduler = new DeterministicScheduler();
23    
24    Runnable commandA = mock(Runnable.class, "commandA");
25    Runnable commandB = mock(Runnable.class, "commandB");
26    Runnable commandC = mock(Runnable.class, "commandC");
27    Runnable commandD = mock(Runnable.class, "commandD");
28    
29    @SuppressWarnings("unchecked")
30    Callable<String> callableA = mock(Callable.class, "callableA");
31    
32    public void testRunsPendingCommandsUntilIdle() {
33        scheduler.execute(commandA);
34        scheduler.execute(commandB);
35        
36        final Sequence executionOrder = sequence("executionOrder");
37        
38        checking(new Expectations() {{
39            oneOf (commandA).run(); inSequence(executionOrder);
40            oneOf (commandB).run(); inSequence(executionOrder);
41        }});
42        
43        assertFalse(scheduler.isIdle());
44        
45        scheduler.runUntilIdle();
46        
47        assertTrue(scheduler.isIdle());
48    }
49    
50    public void testCanRunCommandsSpawnedByExecutedCommandsUntilIdle() {
51        scheduler.execute(commandA);
52        scheduler.execute(commandB);
53        
54        final Sequence executionOrder = sequence("executionOrder");
55        
56        checking(new Expectations() {{
57            oneOf (commandA).run(); inSequence(executionOrder); will(schedule(commandC));
58            oneOf (commandB).run(); inSequence(executionOrder); will(schedule(commandD));
59            oneOf (commandC).run(); inSequence(executionOrder);
60            oneOf (commandD).run(); inSequence(executionOrder);
61        }});
62        
63        scheduler.runUntilIdle();
64    }
65    
66    public void testCanScheduleCommandAndReturnFuture() throws InterruptedException, ExecutionException {
67        Future<?> future = scheduler.submit(commandA);
68        
69        checking(new Expectations() {{
70            oneOf (commandA).run();
71        }});
72        
73        assertTrue("future should not be done before running the task", !future.isDone());
74        
75        scheduler.runUntilIdle();
76        
77        assertTrue("future should be done after running the task", future.isDone());
78        assertNull("result of future should be null", future.get());
79    }
80    
81    public void testCanScheduleCommandAndResultAndReturnFuture() throws InterruptedException, ExecutionException {
82        Future<String> future = scheduler.submit(commandA, "result1");
83        
84        checking(new Expectations() {{
85            oneOf (commandA).run();
86        }});
87       
88        scheduler.runUntilIdle();
89        
90        assertThat(future.get(), equalTo("result1"));
91    }
92
93    public void testCanScheduleCallableAndReturnFuture() throws Exception {
94        Future<String> future = scheduler.submit(callableA);
95        
96        checking(new Expectations() {{
97            oneOf (callableA).call(); will(returnValue("result2"));
98        }});
99        
100        scheduler.runUntilIdle();
101        
102        assertThat(future.get(), equalTo("result2"));
103    }
104
105    public void testScheduledCallablesCanReturnNull() throws Exception {
106        checking(new Expectations() {{
107            oneOf (callableA).call(); will(returnValue(null));
108        }});
109        
110        Future<String> future = scheduler.submit(callableA);
111        
112        scheduler.runUntilIdle();
113        
114        assertNull(future.get());
115    }
116    
117    public class ExampleException extends Exception {}
118    
119    public void testExceptionThrownByScheduledCallablesIsThrownFromFuture() throws Exception {
120        final Throwable thrown = new ExampleException();
121        
122        checking(new Expectations() {{
123            oneOf (callableA).call(); will(throwException(thrown));
124        }});
125        
126        Future<String> future = scheduler.submit(callableA);
127        
128        scheduler.runUntilIdle();
129        
130        try {
131            future.get();
132            fail("should have thrown ExecutionException");
133        }
134        catch (ExecutionException expected) {
135            assertThat(expected.getCause(), sameInstance(thrown));
136        }
137    }
138
139    public void testCanScheduleCommandsToBeExecutedAfterADelay() {
140        scheduler.schedule(commandA, 10, TimeUnit.SECONDS);
141        
142        scheduler.tick(9, TimeUnit.SECONDS);
143        
144        checking(new Expectations() {{
145            oneOf (commandA).run();
146        }});
147        
148        scheduler.tick(1, TimeUnit.SECONDS);
149    }
150    
151    public void testTickingTimeForwardRunsAllCommandsScheduledDuringThatTimePeriod() {
152        scheduler.schedule(commandA, 1, TimeUnit.MILLISECONDS);
153        scheduler.schedule(commandB, 2, TimeUnit.MILLISECONDS);
154        
155        checking(new Expectations() {{
156            oneOf (commandA).run();
157            oneOf (commandB).run();
158        }});
159        
160        scheduler.tick(3, TimeUnit.MILLISECONDS);
161    }
162    
163    public void testTickingTimeForwardRunsCommandsExecutedByScheduledCommands() {
164        scheduler.schedule(commandA, 1, TimeUnit.MILLISECONDS);
165        scheduler.schedule(commandD, 2, TimeUnit.MILLISECONDS);
166        
167        checking(new Expectations() {{
168            oneOf (commandA).run(); will(schedule(commandB));
169            oneOf (commandB).run(); will(schedule(commandC));
170            oneOf (commandC).run();
171            oneOf (commandD).run();
172        }});
173        
174        scheduler.tick(3, TimeUnit.MILLISECONDS);
175    }
176    
177    public void testCanExecuteCommandsThatRepeatWithFixedDelay() {
178        scheduler.scheduleWithFixedDelay(commandA, 2L, 3L, TimeUnit.SECONDS);
179        
180        checking(new Expectations() {{
181            exactly(3).of(commandA).run();
182        }});
183        
184        scheduler.tick(8L, TimeUnit.SECONDS);
185    }
186
187    public void testCanExecuteCommandsThatRepeatAtFixedRateButAssumesThatCommandsTakeNoTimeToExecute() {
188        scheduler.scheduleAtFixedRate(commandA, 2L, 3L, TimeUnit.SECONDS);
189        
190        checking(new Expectations() {{
191            exactly(3).of(commandA).run();
192        }});
193        
194        scheduler.tick(8L, TimeUnit.SECONDS);
195    }
196    
197    public void testCanCancelScheduledCommands() {
198        final boolean dontCare = true;
199        ScheduledFuture<?> future = scheduler.schedule(commandA, 1, TimeUnit.SECONDS);
200        
201        assertFalse(future.isCancelled());
202        future.cancel(dontCare);
203        assertTrue(future.isCancelled());
204        
205        checking(new Expectations() {{
206            never (commandA);
207        }});
208        
209        scheduler.tick(2, TimeUnit.SECONDS);
210    }
211    
212    static final int TIMEOUT_IGNORED = 1000;
213    
214    public void testCanScheduleCallablesAndGetTheirResultAfterTheyHaveBeenExecuted() throws Exception {
215        checking(new Expectations() {{
216            oneOf (callableA).call(); will(returnValue("A"));
217        }});
218        
219        ScheduledFuture<String> future = scheduler.schedule(callableA, 1, TimeUnit.SECONDS);
220        
221        assertTrue("is not done", !future.isDone());
222        
223        scheduler.tick(1, TimeUnit.SECONDS);
224        
225        assertTrue("is done", future.isDone());
226        assertThat(future.get(), equalTo("A"));
227        assertThat(future.get(TIMEOUT_IGNORED, TimeUnit.SECONDS), equalTo("A"));
228    }
229
230    public void testCannotBlockWaitingForFutureResultOfScheduledCallable() throws Exception {
231        ScheduledFuture<String> future = scheduler.schedule(callableA, 1, TimeUnit.SECONDS);
232        
233        try {
234            future.get();
235            fail("should have thrown UnsupportedSynchronousOperationException");
236        }
237        catch (UnsupportedSynchronousOperationException expected) {}
238        
239        try {
240            future.get(TIMEOUT_IGNORED, TimeUnit.SECONDS);
241            fail("should have thrown UnsupportedSynchronousOperationException");
242        }
243        catch (UnsupportedSynchronousOperationException expected) {}
244    }
245    
246    private Action schedule(final Runnable command) {
247        return ScheduleOnExecutorAction.schedule(scheduler, command);
248    }
249}
250
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)