How to use SynchroniserTests class of org.jmock.test.unit.lib.concurrent.internal package

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

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.internal;
2
3import static org.hamcrest.Matchers.containsString;
4import static org.junit.Assert.assertThat;
5import static org.junit.Assert.fail;
6
7import java.util.concurrent.atomic.AtomicInteger;
8
9import org.jmock.Expectations;
10import org.jmock.Mockery;
11import org.jmock.States;
12import org.jmock.integration.junit4.JUnit4Mockery;
13import org.jmock.lib.concurrent.Blitzer;
14import org.jmock.lib.concurrent.Synchroniser;
15import org.junit.After;
16import org.junit.Test;
17
18public class SynchroniserTests {
19    public interface Events {
20        void action();
21        void finished();
22    }
23    
24    Synchroniser synchroniser = new Synchroniser();
25
26    // Not a rule, to allow checking the mockery is *not* satisfied
27    Mockery mockery = new JUnit4Mockery() {{
28        setThreadingPolicy(synchroniser);
29    }};
30    
31    Blitzer blitzer = new Blitzer(16, 4);
32    
33    Events mockObject = mockery.mock(Events.class, "mockObject");
34    
35    @Test(timeout=250)
36    public void allowsMultipleThreadsToCallMockObjects() throws InterruptedException {
37        mockery.checking(new Expectations() {{
38            exactly(blitzer.totalActionCount()).of(mockObject).action();
39        }});
40        
41        blitzer.blitz(new Runnable() {
42            public void run() {
43                mockObject.action();
44            }
45        });
46
47        mockery.assertIsSatisfied();
48    }
49    
50    @Test(timeout=250)
51    public void canWaitForAStateMachineToEnterAGivenState() throws InterruptedException {
52        final AtomicInteger counter = new AtomicInteger(blitzer.totalActionCount());
53        
54        final States threads = mockery.states("threads");
55        
56        mockery.checking(new Expectations() {{
57            exactly(blitzer.totalActionCount()).of(mockObject).action();
58                when(threads.isNot("finished"));
59                
60            oneOf(mockObject).finished();
61                then(threads.is("finished"));
62        }});
63        
64        blitzer.blitz(new Runnable() {
65            public void run() {
66                mockObject.action();
67                if (counter.decrementAndGet() == 0) {
68                    mockObject.finished();
69                }
70            }
71        });
72        
73        synchroniser.waitUntil(threads.is("finished"));
74        mockery.assertIsSatisfied();
75    }
76
77    @Test(timeout=250)
78    public void canWaitForAStateMachineToEnterAGivenStateWithinSomeTimeout() throws InterruptedException {
79        final States threads = mockery.states("threads");
80        
81        mockery.checking(new Expectations() {{
82            exactly(blitzer.totalActionCount()).of(mockObject).action();
83                when(threads.isNot("finished"));
84                
85            oneOf(mockObject).finished();
86                then(threads.is("finished"));
87        }});
88        
89        blitzer.blitz(new Runnable() {
90            AtomicInteger counter = new AtomicInteger(blitzer.totalActionCount());
91            
92            public void run() {
93                mockObject.action();
94                if (counter.decrementAndGet() == 0) {
95                    mockObject.finished();
96                }
97            }
98        });
99        
100        synchroniser.waitUntil(threads.is("finished"), 100);
101        mockery.assertIsSatisfied();
102    }
103
104    @Test(timeout=250)
105    public void failsTheTestIfStateMachineDoesNotEnterExpectedStateWithinTimeout() throws InterruptedException {
106        States threads = mockery.states("threads");
107        
108        try {
109            synchroniser.waitUntil(threads.is("finished"), 100);
110        }
111        catch (AssertionError e) {
112            return;
113        }
114        
115        fail("should have thrown AssertionError");
116    }
117    
118    @Test
119    public void throwsExpectationErrorIfExpectationFailsWhileWaitingForStateMachine() throws InterruptedException {
120        final States threads = mockery.states("threads");
121        
122        // This will cause an expectation error, and nothing will make
123        // the "threads" state machine transition to "finished" 
124        
125        blitzer.blitz(new Runnable() {
126            public void run() {
127                mockObject.action();
128            }
129        });
130        
131        try {
132            synchroniser.waitUntil(threads.is("finished"), 100);
133            fail("should have thrown AssertionError");
134        }
135        catch (AssertionError e) {
136            assertThat(e.getMessage(), containsString("action()"));
137        }
138    }
139
140    @Test
141    public void throwsExpectationErrorIfExpectationFailsWhileWaitingForStateMachineEvenIfWaitSucceeds() throws InterruptedException {
142        final States threads = mockery.states("threads");
143        
144        mockery.checking(new Expectations() {{
145            oneOf(mockObject).finished();
146                then(threads.is("finished"));
147        }});
148        
149        blitzer.blitz(new Runnable() {
150            AtomicInteger counter = new AtomicInteger(blitzer.totalActionCount());
151            
152            public void run() {
153                if (counter.decrementAndGet() == 0) {
154                    mockObject.finished();
155                }
156                else {
157                    mockObject.action();
158                }
159            }
160        });
161        
162        try {
163            synchroniser.waitUntil(threads.is("finished"), 100);
164            fail("should have thrown AssertionError");
165        }
166        catch (AssertionError e) {
167            assertThat(e.getMessage(), containsString("action()"));
168        }
169    }
170    
171    @After
172    public void cleanUp() {
173        blitzer.shutdown();
174    }
175}
176
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 popular Stackoverflow questions on SynchroniserTests

    No relevant questions found for this class 😞

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

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)