How to use method of org.jmock.lib.concurrent.DeterministicExecutor class

Best Jmock-library code snippet using org.jmock.lib.concurrent.DeterministicExecutor.

Run Jmock-library automation tests on LambdaTest cloud grid

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

copy
1/*
2 * @(#)MultithreadedTest.java     26 May 2009
3 */
4package com.xebia.aphillips.jmock;
5
6import java.util.concurrent.Executor;
7
8import org.jmock.Expectations;
9import org.jmock.Mockery;
10import org.jmock.integration.junit4.JMock;
11import org.jmock.integration.junit4.JUnit4Mockery;
12import org.jmock.lib.concurrent.DeterministicExecutor;
13import org.junit.Test;
14import org.junit.runner.RunWith;
15
16/**
17 * Demo multithreaded testing with JMock. See the 
18 * <a href="http://jmock.org/">JMock documentation</a> for more information.
19 * 
20 * @author aphillips
21 * @since 26 May 2009
22 *
23 */
24@RunWith(JMock.class)
25public class JMockMultithreadedDemo {
26    private final Mockery context = new JUnit4Mockery();
27    
28    interface Alarm {
29        void ring();
30    }
31    
32    class Guard {
33        protected final Alarm alarm;
34        
35        Guard( Alarm alarm ) {
36            this.alarm = alarm;
37        }
38        
39        void notice(Object burglar) {
40            startRingingTheAlarm();
41        }
42        
43        protected void startRingingTheAlarm() {
44            Runnable ringAlarmTask = new Runnable() {
45                public void run() {
46                    for (int i = 0; i < 10; i++) {
47                        alarm.ring();
48                    }
49                }
50            };
51            
52            Thread ringAlarmThread = new Thread(ringAlarmTask);
53            ringAlarmThread.start();
54        }
55    }
56    
57    @Test
58    public void ringsTheAlarmOnceWhenNoticesABurglar() throws InterruptedException {
59        final Alarm alarm = context.mock(Alarm.class);
60        Guard guard = new Guard(alarm);
61        
62        context.checking(new Expectations() {{
63            oneOf (alarm).ring();
64        }});
65        
66        guard.notice(new Object());
67        
68        // give the frantically ringing alarm thread a bit of time before validating
69        Thread.sleep(200);
70    }
71    
72    class ExecutorGuard extends Guard {
73        private final Executor executor;
74        
75        ExecutorGuard(Alarm alarm, Executor executor) {
76            super(alarm);
77            this.executor = executor;
78        }
79
80        @Override
81        protected void startRingingTheAlarm() {
82            Runnable ringAlarmTask = new Runnable() {
83                public void run() {
84                    for (int i = 0; i < 10; i++) {
85                        alarm.ring();
86                    }
87                }
88            };
89            executor.execute(ringAlarmTask);
90        }
91       
92    }
93    
94    @Test
95    public void ringsTheAlarmOnceWhenNoticesABurglar2() {
96        final Alarm alarm = context.mock(Alarm.class);
97        DeterministicExecutor executor = new DeterministicExecutor();
98        Guard guard = new ExecutorGuard(alarm, executor);
99        
100        guard.notice(new Object());
101        
102        context.checking(new Expectations() {{
103            oneOf (alarm).ring();
104        }});
105        
106        executor.runUntilIdle();    
107    }
108}
109
Full Screen
copy
1package org.jmock.test.unit.lib.concurrent;
2
3import org.jmock.Expectations;
4import org.jmock.Sequence;
5import org.jmock.api.Action;
6import org.jmock.integration.junit3.MockObjectTestCase;
7import org.jmock.lib.concurrent.DeterministicExecutor;
8
9public class DeterministicExecutorTests extends MockObjectTestCase {
10    DeterministicExecutor scheduler = new DeterministicExecutor();
11    
12    Runnable commandA = mock(Runnable.class, "commandA");
13    Runnable commandB = mock(Runnable.class, "commandB");
14    Runnable commandC = mock(Runnable.class, "commandC");
15    Runnable commandD = mock(Runnable.class, "commandD");
16    
17    public void testRunsPendingCommands() {
18        scheduler.execute(commandA);
19        scheduler.execute(commandB);
20        
21        final Sequence executionOrder = sequence("executionOrder");
22        
23        checking(new Expectations() {{
24            oneOf (commandA).run(); inSequence(executionOrder);
25            oneOf (commandB).run(); inSequence(executionOrder);
26        }});
27        
28        scheduler.runPendingCommands();
29    }
30    
31    public void testCanLeaveCommandsSpawnedByExecutedCommandsPendingForLaterExecution() {
32        scheduler.execute(commandA);
33        scheduler.execute(commandB);
34        
35        final Sequence executionOrder = sequence("executionOrder");
36        
37        checking(new Expectations() {{
38            oneOf (commandA).run(); inSequence(executionOrder); will(schedule(commandC));
39            oneOf (commandB).run(); inSequence(executionOrder); will(schedule(commandD));
40            never (commandC).run();
41            never (commandD).run();
42        }});
43        
44        scheduler.runPendingCommands();
45    }
46    
47    public void testCanRunCommandsSpawnedByExecutedCommandsUntilNoCommandsArePending() {
48        scheduler.execute(commandA);
49        scheduler.execute(commandB);
50        
51        final Sequence executionOrder = sequence("executionOrder");
52        
53        checking(new Expectations() {{
54            oneOf (commandA).run(); inSequence(executionOrder); will(schedule(commandC));
55            oneOf (commandB).run(); inSequence(executionOrder); will(schedule(commandD));
56            oneOf (commandC).run(); inSequence(executionOrder);
57            oneOf (commandD).run(); inSequence(executionOrder);
58        }});
59        
60        scheduler.runUntilIdle();
61    }
62
63    protected Action schedule(final Runnable command) {
64        return ScheduleOnExecutorAction.schedule(scheduler, command);
65    }
66}
67
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

Most used method in DeterministicExecutor

Trigger code on LambdaTest Cloud Grid

Execute automation tests with on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
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)