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

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

Run Jmock-library automation tests on LambdaTest cloud grid

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

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

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)