How to use SynchronisingInvocationDispatcherWrapper class of org.jmock.lib.concurrent package

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

Run Jmock-library automation tests on LambdaTest cloud grid

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

copy
1package org.jmock.lib.concurrent;
2
3import org.hamcrest.Description;
4import org.jmock.api.Expectation;
5import org.jmock.api.Invocation;
6import org.jmock.api.InvocationDispatcher;
7import org.jmock.internal.StateMachine;
8
9/**
10 * I synchronise my delegate.
11 * This serialises access to the internal Expectation and StateMachine collections.
12 * @author oliverbye
13 *
14 */
15public class SynchronisingInvocationDispatcherWrapper implements InvocationDispatcher {
16
17    private final InvocationDispatcher delegate;
18
19    public SynchronisingInvocationDispatcherWrapper(InvocationDispatcher dispatcher) {
20        delegate = dispatcher;
21    }
22    
23    public synchronized StateMachine newStateMachine(String name) {
24        return delegate.newStateMachine(name);
25    }
26
27    public synchronized void add(Expectation expectation) {
28        delegate.add(expectation);
29    }
30
31    public synchronized void describeTo(Description description) {
32        delegate.describeTo(description);
33    }
34
35    public synchronized void describeMismatch(Invocation invocation, Description description) {
36        delegate.describeMismatch(invocation, description);
37    }
38
39    public synchronized boolean isSatisfied() {
40        return delegate.isSatisfied();
41    }
42
43    public synchronized Object dispatch(Invocation invocation) throws Throwable {
44        return delegate.dispatch(invocation);
45    }
46}
47
Full Screen
copy
1package org.jmock.test.unit.internal;
2
3import java.util.ArrayList;
4import java.util.Collection;
5import java.util.Iterator;
6import java.util.concurrent.BrokenBarrierException;
7import java.util.concurrent.CopyOnWriteArrayList;
8import java.util.concurrent.CyclicBarrier;
9import java.util.concurrent.TimeUnit;
10import java.util.concurrent.TimeoutException;
11
12import org.jmock.api.Expectation;
13import org.jmock.api.ExpectationError;
14import org.jmock.api.Invocation;
15import org.jmock.api.InvocationDispatcher;
16import org.jmock.internal.StateMachine;
17import org.jmock.lib.concurrent.SynchronisingInvocationDispatcherWrapper;
18import org.jmock.lib.concurrent.UnsynchronisedInvocationDispatcher;
19import org.jmock.test.unit.support.MethodFactory;
20import org.jmock.test.unit.support.MockExpectation;
21
22import junit.framework.TestCase;
23
24public class InvocationDispatcherTests extends TestCase {
25
26    // Avoid multi threaeding tests deadlocking
27    // Adjust timeout for debugging
28    private static final TimeUnit TIMEOUT_UNIT = TimeUnit.SECONDS;
29    private static final int TIMEOUT = 2;
30
31    MethodFactory methodFactory = new MethodFactory();
32    Invocation invocation = new Invocation(
33            "invokedObject",
34            methodFactory.newMethod("invokedMethod"),
35            Invocation.NO_PARAMETERS);
36
37    static final boolean NOT_RELEVANT = true;
38
39    public void testInvokesFirstMatchingExpectationInGroup() throws Throwable {
40        MockExpectation expectation1 = new MockExpectation(false, NOT_RELEVANT, NOT_RELEVANT);
41        MockExpectation expectation2 = new MockExpectation(true, NOT_RELEVANT, NOT_RELEVANT);
42        MockExpectation expectation3 = new MockExpectation(true, NOT_RELEVANT, NOT_RELEVANT);
43
44        UnsynchronisedInvocationDispatcher dispatcher = new UnsynchronisedInvocationDispatcher();
45        dispatcher.add(expectation1);
46        dispatcher.add(expectation2);
47        dispatcher.add(expectation3);
48
49        expectation1.shouldNotBeInvoked();
50        expectation2.shouldBeInvokedWith(invocation);
51        expectation3.shouldNotBeInvoked();
52
53        dispatcher.dispatch(invocation);
54
55        assertTrue("expectation2 should have been invoked",
56                expectation2.wasInvoked);
57    }
58
59    public void testThrowsExpectationErrorIfNoExpectationsMatchAnInvocation() throws Throwable {
60        MockExpectation expectation1 = new MockExpectation(false, NOT_RELEVANT, NOT_RELEVANT);
61        MockExpectation expectation2 = new MockExpectation(false, NOT_RELEVANT, NOT_RELEVANT);
62        MockExpectation expectation3 = new MockExpectation(false, NOT_RELEVANT, NOT_RELEVANT);
63
64        UnsynchronisedInvocationDispatcher dispatcher = new UnsynchronisedInvocationDispatcher();
65        dispatcher.add(expectation1);
66        dispatcher.add(expectation2);
67        dispatcher.add(expectation3);
68
69        expectation1.shouldNotBeInvoked();
70        expectation2.shouldNotBeInvoked();
71        expectation3.shouldNotBeInvoked();
72
73        try {
74            dispatcher.dispatch(invocation);
75            fail("should have thrown ExpectationError");
76        } catch (ExpectationError e) {
77            // expected
78        }
79    }
80
81    public void testIsSatisfiedOnlyIfAllExpectationsAreSatisfied() {
82        UnsynchronisedInvocationDispatcher dispatcherAll = new UnsynchronisedInvocationDispatcher();
83        dispatcherAll.add(new MockExpectation(NOT_RELEVANT, true, NOT_RELEVANT));
84        dispatcherAll.add(new MockExpectation(NOT_RELEVANT, true, NOT_RELEVANT));
85        assertTrue("should be satisfied if all expectations are satisfied",
86                dispatcherAll.isSatisfied());
87
88        UnsynchronisedInvocationDispatcher dispatcher1 = new UnsynchronisedInvocationDispatcher();
89        dispatcher1.add(new MockExpectation(NOT_RELEVANT, true, NOT_RELEVANT));
90        dispatcher1.add(new MockExpectation(NOT_RELEVANT, false, NOT_RELEVANT));
91        assertFalse("should not be satisfied if first expectation is not satisfied",
92                dispatcher1.isSatisfied());
93
94        UnsynchronisedInvocationDispatcher dispatcher2 = new UnsynchronisedInvocationDispatcher();
95        dispatcher2.add(new MockExpectation(NOT_RELEVANT, false, NOT_RELEVANT));
96        dispatcher2.add(new MockExpectation(NOT_RELEVANT, true, NOT_RELEVANT));
97        assertFalse("should not be satisfied if second expectation is not satisfied",
98                dispatcher2.isSatisfied());
99
100        UnsynchronisedInvocationDispatcher dispatcherNone = new UnsynchronisedInvocationDispatcher();
101        dispatcherNone.add(new MockExpectation(NOT_RELEVANT, false, NOT_RELEVANT));
102        dispatcherNone.add(new MockExpectation(NOT_RELEVANT, true, NOT_RELEVANT));
103        assertFalse("should not be satisfied if no expectations are satisfied",
104                dispatcherNone.isSatisfied());
105    }
106
107    /**
108     * Resolves issue 104
109     * 
110     * @throws Throwable
111     */
112    public void testUnsynchronisedInvocationDispatcherHandlesAddingExpectationsWhileOtherTestsDispatch()
113            throws Throwable {
114
115        final CyclicBarrier barrier = new CyclicBarrier(2);
116
117        MockExpectation expectation1 = new MockExpectation(true, NOT_RELEVANT, NOT_RELEVANT);
118        MockExpectation expectation2 = new MockExpectation(false, NOT_RELEVANT, NOT_RELEVANT);
119
120        Collection<Expectation> expectations = new CriticalSectionForcingCollectionWrapper<>(
121                new CopyOnWriteArrayList<Expectation>(), barrier);
122        Collection<StateMachine> stateMachines = new CriticalSectionForcingCollectionWrapper<>(
123                new ArrayList<StateMachine>(), barrier);
124        final UnsynchronisedInvocationDispatcher dispatcher = new UnsynchronisedInvocationDispatcher(expectations,
125                stateMachines);
126
127        new Thread(new Runnable() {
128            @Override
129            public void run() {
130                try {
131                    barrier.await(TIMEOUT, TIMEOUT_UNIT);
132                    barrier.await(TIMEOUT, TIMEOUT_UNIT);
133                    // now the expectation one has been added
134
135                    dispatcher.dispatch(invocation);
136                    barrier.await(TIMEOUT, TIMEOUT_UNIT);
137                } catch (Throwable e) {
138                    // will throw a ConcurrentModification Exception unless a multithreaded strategy
139                    // is used
140                    throw new RuntimeException(e);
141                }
142            }
143        }, "Concurrent Dispatch").start();
144
145        // expect dispatch
146        dispatcher.add(expectation1);
147        // await is satisfied check
148
149        dispatcher.add(expectation2);
150        barrier.await(TIMEOUT, TIMEOUT_UNIT);
151
152        expectation1.shouldBeInvokedWith(invocation);
153        assertTrue("expectation1 should have been invoked",
154                expectation1.wasInvoked);
155    }
156
157    public void testSynchronisedInvocationDispatcherBlocksAddingExpectationsWhileOtherTestsDispatch()
158            throws Throwable {
159
160        final CyclicBarrier barrier = new CyclicBarrier(2);
161        
162        MockExpectation expectation1 = new MockExpectation(true, NOT_RELEVANT, NOT_RELEVANT);
163        MockExpectation expectation2 = new MockExpectation(false, NOT_RELEVANT, NOT_RELEVANT);
164
165        // intentionally use array list as the wrapper should synchronise access
166        final InvocationDispatcher dispatcher = new SynchronisingInvocationDispatcherWrapper(
167                new UnsynchronisedInvocationDispatcher(new ArrayList<Expectation>(), new ArrayList<StateMachine>()));
168
169        // expect dispatch
170        dispatcher.add(expectation1);
171
172        new Thread(new Runnable() {
173            @Override
174            public void run() {
175                try {
176                    dispatcher.dispatch(invocation);
177                    barrier.await();
178                } catch (Throwable e) {
179                    throw new RuntimeException(e);
180                }
181            }
182        }, "Concurrent Dispatch").start();
183
184        // await until dispatch
185        barrier.await();
186
187        dispatcher.add(expectation2);
188
189        expectation1.shouldBeInvokedWith(invocation);
190        assertTrue("expectation1 should have been invoked",
191                expectation1.wasInvoked);
192    }
193
194    private class CriticalSectionForcingCollectionWrapper<T> implements Collection<T> {
195        private final Collection<T> delegate;
196        private final CyclicBarrier barrier;
197
198        CriticalSectionForcingCollectionWrapper(Collection<T> delegate, CyclicBarrier barrier) {
199            this.delegate = delegate;
200            this.barrier = barrier;
201        }
202
203        private void await() {
204            try {
205                // we want the expectation check to have got the iterator
206                // but not progressed checking
207                barrier.await(TIMEOUT, TIMEOUT_UNIT);
208            } catch (InterruptedException e) {
209                throw new RuntimeException(e);
210            } catch (BrokenBarrierException e) {
211                throw new RuntimeException(e);
212            } catch (TimeoutException e) {
213                throw new RuntimeException(e);
214            }
215        }
216
217        public int size() {
218            return delegate.size();
219        }
220
221        public boolean isEmpty() {
222            return delegate.isEmpty();
223        }
224
225        public boolean contains(Object o) {
226            return delegate.contains(o);
227        }
228
229        public Iterator<T> iterator() {
230            Iterator<T> reply = delegate.iterator();
231            await(); // expectation add follows this
232            await(); // wait for add to complete
233            return reply;
234        }
235
236        public Object[] toArray() {
237            return delegate.toArray();
238        }
239
240        public <U> U[] toArray(U[] a) {
241            return delegate.toArray(a);
242        }
243
244        public boolean add(T e) {
245            // Make sure iterator is called before adding
246            await();
247            boolean reply = delegate.add(e);
248            // Make sure iterator returns after adding
249            await();
250            return reply;
251        }
252
253        public boolean remove(Object o) {
254            return delegate.remove(o);
255        }
256
257        public boolean containsAll(Collection<?> c) {
258            return delegate.containsAll(c);
259        }
260
261        public boolean addAll(Collection<? extends T> c) {
262            return delegate.addAll(c);
263        }
264
265        public boolean removeAll(Collection<?> c) {
266            return delegate.removeAll(c);
267        }
268
269        public boolean retainAll(Collection<?> c) {
270            return delegate.retainAll(c);
271        }
272
273        public void clear() {
274            delegate.clear();
275        }
276
277        public boolean equals(Object o) {
278            return delegate.equals(o);
279        }
280
281        public int hashCode() {
282            return delegate.hashCode();
283        }
284    }
285
286}
287
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 SynchronisingInvocationDispatcherWrapper

    No relevant questions found for this class 😞

Most used methods in SynchronisingInvocationDispatcherWrapper

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)