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

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

Source:InvocationDispatcherTests.java Github

copy

Full Screen

...12import org.jmock.api.Invocation;13import org.jmock.api.InvocationDispatcher;14import org.jmock.internal.StateMachine;15import org.jmock.lib.concurrent.SynchronisingInvocationDispatcherWrapper;16import org.jmock.lib.concurrent.UnsynchronisedInvocationDispatcher;17import org.jmock.test.unit.support.MethodFactory;18import org.jmock.test.unit.support.MockExpectation;19import junit.framework.TestCase;20public class InvocationDispatcherTests extends TestCase {21 // Avoid multi threaeding tests deadlocking22 // Adjust timeout for debugging23 private static final TimeUnit TIMEOUT_UNIT = TimeUnit.SECONDS;24 private static final int TIMEOUT = 2;25 MethodFactory methodFactory = new MethodFactory();26 Invocation invocation = new Invocation(27 "invokedObject",28 methodFactory.newMethod("invokedMethod"),29 Invocation.NO_PARAMETERS);30 static final boolean NOT_RELEVANT = true;31 public void testInvokesFirstMatchingExpectationInGroup() throws Throwable {32 MockExpectation expectation1 = new MockExpectation(false, NOT_RELEVANT, NOT_RELEVANT);33 MockExpectation expectation2 = new MockExpectation(true, NOT_RELEVANT, NOT_RELEVANT);34 MockExpectation expectation3 = new MockExpectation(true, NOT_RELEVANT, NOT_RELEVANT);35 UnsynchronisedInvocationDispatcher dispatcher = new UnsynchronisedInvocationDispatcher();36 dispatcher.add(expectation1);37 dispatcher.add(expectation2);38 dispatcher.add(expectation3);39 expectation1.shouldNotBeInvoked();40 expectation2.shouldBeInvokedWith(invocation);41 expectation3.shouldNotBeInvoked();42 dispatcher.dispatch(invocation);43 assertTrue("expectation2 should have been invoked",44 expectation2.wasInvoked);45 }46 public void testThrowsExpectationErrorIfNoExpectationsMatchAnInvocation() throws Throwable {47 MockExpectation expectation1 = new MockExpectation(false, NOT_RELEVANT, NOT_RELEVANT);48 MockExpectation expectation2 = new MockExpectation(false, NOT_RELEVANT, NOT_RELEVANT);49 MockExpectation expectation3 = new MockExpectation(false, NOT_RELEVANT, NOT_RELEVANT);50 UnsynchronisedInvocationDispatcher dispatcher = new UnsynchronisedInvocationDispatcher();51 dispatcher.add(expectation1);52 dispatcher.add(expectation2);53 dispatcher.add(expectation3);54 expectation1.shouldNotBeInvoked();55 expectation2.shouldNotBeInvoked();56 expectation3.shouldNotBeInvoked();57 try {58 dispatcher.dispatch(invocation);59 fail("should have thrown ExpectationError");60 } catch (ExpectationError e) {61 // expected62 }63 }64 public void testIsSatisfiedOnlyIfAllExpectationsAreSatisfied() {65 UnsynchronisedInvocationDispatcher dispatcherAll = new UnsynchronisedInvocationDispatcher();66 dispatcherAll.add(new MockExpectation(NOT_RELEVANT, true, NOT_RELEVANT));67 dispatcherAll.add(new MockExpectation(NOT_RELEVANT, true, NOT_RELEVANT));68 assertTrue("should be satisfied if all expectations are satisfied",69 dispatcherAll.isSatisfied());70 UnsynchronisedInvocationDispatcher dispatcher1 = new UnsynchronisedInvocationDispatcher();71 dispatcher1.add(new MockExpectation(NOT_RELEVANT, true, NOT_RELEVANT));72 dispatcher1.add(new MockExpectation(NOT_RELEVANT, false, NOT_RELEVANT));73 assertFalse("should not be satisfied if first expectation is not satisfied",74 dispatcher1.isSatisfied());75 UnsynchronisedInvocationDispatcher dispatcher2 = new UnsynchronisedInvocationDispatcher();76 dispatcher2.add(new MockExpectation(NOT_RELEVANT, false, NOT_RELEVANT));77 dispatcher2.add(new MockExpectation(NOT_RELEVANT, true, NOT_RELEVANT));78 assertFalse("should not be satisfied if second expectation is not satisfied",79 dispatcher2.isSatisfied());80 UnsynchronisedInvocationDispatcher dispatcherNone = new UnsynchronisedInvocationDispatcher();81 dispatcherNone.add(new MockExpectation(NOT_RELEVANT, false, NOT_RELEVANT));82 dispatcherNone.add(new MockExpectation(NOT_RELEVANT, true, NOT_RELEVANT));83 assertFalse("should not be satisfied if no expectations are satisfied",84 dispatcherNone.isSatisfied());85 }86 /**87 * Resolves issue 10488 * 89 * @throws Throwable90 */91 public void testUnsynchronisedInvocationDispatcherHandlesAddingExpectationsWhileOtherTestsDispatch()92 throws Throwable {93 final CyclicBarrier barrier = new CyclicBarrier(2);94 MockExpectation expectation1 = new MockExpectation(true, NOT_RELEVANT, NOT_RELEVANT);95 MockExpectation expectation2 = new MockExpectation(false, NOT_RELEVANT, NOT_RELEVANT);96 Collection<Expectation> expectations = new CriticalSectionForcingCollectionWrapper<>(97 new CopyOnWriteArrayList<Expectation>(), barrier);98 Collection<StateMachine> stateMachines = new CriticalSectionForcingCollectionWrapper<>(99 new ArrayList<StateMachine>(), barrier);100 final UnsynchronisedInvocationDispatcher dispatcher = new UnsynchronisedInvocationDispatcher(expectations,101 stateMachines);102 new Thread(new Runnable() {103 @Override104 public void run() {105 try {106 barrier.await(TIMEOUT, TIMEOUT_UNIT);107 barrier.await(TIMEOUT, TIMEOUT_UNIT);108 // now the expectation one has been added109 dispatcher.dispatch(invocation);110 barrier.await(TIMEOUT, TIMEOUT_UNIT);111 } catch (Throwable e) {112 // will throw a ConcurrentModification Exception unless a multithreaded strategy113 // is used114 throw new RuntimeException(e);115 }116 }117 }, "Concurrent Dispatch").start();118 // expect dispatch119 dispatcher.add(expectation1);120 // await is satisfied check121 dispatcher.add(expectation2);122 barrier.await(TIMEOUT, TIMEOUT_UNIT);123 expectation1.shouldBeInvokedWith(invocation);124 assertTrue("expectation1 should have been invoked",125 expectation1.wasInvoked);126 }127 public void testSynchronisedInvocationDispatcherBlocksAddingExpectationsWhileOtherTestsDispatch()128 throws Throwable {129 final CyclicBarrier barrier = new CyclicBarrier(2);130 131 MockExpectation expectation1 = new MockExpectation(true, NOT_RELEVANT, NOT_RELEVANT);132 MockExpectation expectation2 = new MockExpectation(false, NOT_RELEVANT, NOT_RELEVANT);133 // intentionally use array list as the wrapper should synchronise access134 final InvocationDispatcher dispatcher = new SynchronisingInvocationDispatcherWrapper(135 new UnsynchronisedInvocationDispatcher(new ArrayList<Expectation>(), new ArrayList<StateMachine>()));136 // expect dispatch137 dispatcher.add(expectation1);138 new Thread(new Runnable() {139 @Override140 public void run() {141 try {142 dispatcher.dispatch(invocation);143 barrier.await();144 } catch (Throwable e) {145 throw new RuntimeException(e);146 }147 }148 }, "Concurrent Dispatch").start();149 // await until dispatch...

Full Screen

Full Screen

Source:PortalSynchroniser.java Github

copy

Full Screen

...3import org.jmock.api.Invocation;4import org.jmock.api.InvocationDispatcher;5import org.jmock.api.Invokable;6import org.jmock.api.ThreadingPolicy;7import org.jmock.lib.concurrent.UnsynchronisedInvocationDispatcher;8/**9 * Extension to the base JMock synchroniser to support some edge cases10 * with our custom JMock extensions11 * @author Josh Vote (CSIRO)12 *13 */14public class PortalSynchroniser implements ThreadingPolicy {15 private final ReentrantLock lock = new ReentrantLock(true);16 private final InvocationDispatcher dispatcher = new UnsynchronisedInvocationDispatcher();17 18 public Invokable synchroniseAccessTo(final Invokable mockObject) {19 return new Invokable() {20 public Object invoke(Invocation invocation) throws Throwable {21 return synchroniseInvocation(mockObject, invocation);22 }23 };24 }25 /**26 * Acquires (or reacquires) the lock used by this ThreadingPolicy27 * for the current thread. Blocks if the lock cannot be acquired.28 *29 * Ensure that a call to releaseLock is made if acquring locks30 * @throws InterruptedException...

Full Screen

Full Screen

Source:SingleThreadedPolicy.java Github

copy

Full Screen

...4import org.jmock.api.InvocationDispatcher;5import org.jmock.api.Invokable;6import org.jmock.api.ThreadingPolicy;7import org.jmock.lib.concurrent.Synchroniser;8import org.jmock.lib.concurrent.UnsynchronisedInvocationDispatcher;9public class SingleThreadedPolicy implements ThreadingPolicy {10 private final Thread testThread;11 12 public SingleThreadedPolicy() {13 this.testThread = Thread.currentThread();14 }15 public Invokable synchroniseAccessTo(final Invokable mockObject) {16 return new Invokable() {17 public Object invoke(Invocation invocation) throws Throwable {18 checkRunningOnTestThread();19 return mockObject.invoke(invocation);20 }21 };22 }23 24 private void checkRunningOnTestThread() {25 if (Thread.currentThread() != testThread) {26 reportError("the Mockery is not thread-safe: use a " + 27 Synchroniser.class.getSimpleName() + " to ensure thread safety");28 }29 }30 31 private void reportError(String error) {32 System.err.println(error);33 throw new ConcurrentModificationException(error);34 }35 public InvocationDispatcher dispatcher() {36 return new UnsynchronisedInvocationDispatcher();37 }38}...

Full Screen

Full Screen

UnsynchronisedInvocationDispatcher

Using AI Code Generation

copy

Full Screen

1import org.jmock.MockObjectTestCase;2import org.jmock.Mock;3import org.jmock.core.Invocation;4import org.jmock.core.InvocationDispatcher;5import org.jmock.core.InvocationMatcher;6import org.jmock.core.Invokable;7import org.jmock.core.Stub;8import org.jmock.core.stub.DefaultResultStub;9import org.jmock.core.stub.ReturnStub;10import org.jmock.core.stub.ThrowStub;11import org.jmock.core.constraint.IsAnything;12import org.jmock.core.constraint.IsEqual;13import org.jmock.core.matcher.InvokeOnceMatcher;14import org.jmock.core.matcher.InvokeAtLeastOnceMatcher;15import org.jmock.core.matcher.InvokeAtMostOnceMatcher;16import org.jmock.core.matcher.InvokeAtLeastCountMatcher;17import org.jmock.core.matcher.InvokeAtMostCountMatcher;18import org.jmock.core.matcher.InvokeCountMatcher;19import org.jmock.core.matcher.InvokeBetweenCountMatcher;20import org.jmock.core.matcher.InvokeExactlyCountMatcher;21import org.jmock.core.matcher.InvokeAtLeastOnceAndAtMostCountMatcher;22import org.jmock.core.matcher.InvokeAtLeastCountAndAtMo

Full Screen

Full Screen

UnsynchronisedInvocationDispatcher

Using AI Code Generation

copy

Full Screen

1import org.jmock.Mock;2import org.jmock.MockObjectTestCase;3import org.jmock.lib.concurrent.UnsynchronizedInvocationDispatcher;4{5 public void testUnsynchronizedInvocationDispatcher() throws Exception6 {7 Mock mock = mock(Interface.class);8 Interface i = (Interface)mock.proxy();9 UnsynchronizedInvocationDispatcher dispatcher = new UnsynchronizedInvocationDispatcher(i);10 mock.expects(once()).method("doSomething");11 dispatcher.invoke(mockObject, method, args)12 }13}14 at org.jmock.lib.concurrent.UnsynchronizedInvocationDispatcher.invoke(UnsynchronizedInvocationDispatcher.java:24)15 at org.jmock.lib.concurrent.UnsynchronizedInvocationDispatcher.invoke(UnsynchronizedInvocationDispatcher.java:1)16 at org.jmock.lib.legacy.ClassImposteriser$MockMethodDispatcher.invoke(ClassImposteriser.java:179)17 at $Proxy0.doSomething(Unknown Source)18 at 1.testUnsynchronizedInvocationDispatcher(1.java:16)19 at 1.main(1.java:22)

Full Screen

Full Screen

UnsynchronisedInvocationDispatcher

Using AI Code Generation

copy

Full Screen

1import org.jmock.Mock;2import org.jmock.MockObjectTestCase;3import org.jmock.lib.concurrent.UnsynchronisedInvocationDispatcher;4import org.jmock.core.Invocation;5{6 public void testUnsynchronisedInvocationDispatcher()7 {8 Mock mock = mock(Runnable.class);9 mock.expects(once()).method("run");10 UnsynchronisedInvocationDispatcher dispatcher = new UnsynchronisedInvocationDispatcher();11 dispatcher.dispatch((Runnable) mock.proxy());12 }13}14import org.jmock.Mock;15import org.jmock.MockObjectTestCase;16import org.jmock.lib.concurrent.UnsynchronisedInvocationDispatcher;17import org.jmock.core.Invocation;18{19 public void testUnsynchronisedInvocationDispatcher()20 {21 Mock mock = mock(Runnable.class);22 mock.expects(once()).method("run");23 UnsynchronisedInvocationDispatcher dispatcher = new UnsynchronisedInvocationDispatcher();24 dispatcher.dispatch((Runnable) mock.proxy());25 }26}27import org.jmock.Mock;28import org.jmock.MockObjectTestCase;29import org.jmock.lib.concurrent.UnsynchronisedInvocationDispatcher;30import org.jmock.core.Invocation;31{32 public void testUnsynchronisedInvocationDispatcher()33 {34 Mock mock = mock(Runnable.class);35 mock.expects(once()).method("run");36 UnsynchronisedInvocationDispatcher dispatcher = new UnsynchronisedInvocationDispatcher();37 dispatcher.dispatch((Runnable) mock.proxy());38 }39}

Full Screen

Full Screen

UnsynchronisedInvocationDispatcher

Using AI Code Generation

copy

Full Screen

1import org.jmock.*;2import org.jmock.lib.*;3import org.jmock.lib.concurrent.*;4import org.jmock.core.*;5import org.jmock.core.constraint.*;6import org.jmock.core.matcher.*;7import org.jmock.util.*;8import junit.framework.*;9{10 public TestUnsynchronisedInvocationDispatcher(String name)11 {12 super(name);13 }14 public void testUnsynchronisedInvocationDispatcher()15 {16 Mock mock = new Mock(Interface1.class);17 Interface1 i1 = (Interface1)mock.proxy();18 UnsynchronisedInvocationDispatcher uid = new UnsynchronisedInvocationDispatcher();19 uid.setThreadName("TestUnsynchronisedInvocationDispatcher");20 uid.setTarget(i1);21 uid.start();22 i1.method1();23 i1.method2();24 i1.method3();25 i1.method4();26 i1.method5();27 i1.method6();28 i1.method7();29 i1.method8();30 i1.method9();31 i1.method10();32 i1.method11();33 i1.method12();34 i1.method13();35 i1.method14();36 i1.method15();37 i1.method16();38 i1.method17();39 i1.method18();40 i1.method19();41 i1.method20();42 i1.method21();43 i1.method22();44 i1.method23();45 i1.method24();46 i1.method25();47 i1.method26();48 i1.method27();49 i1.method28();50 i1.method29();51 i1.method30();52 i1.method31();53 i1.method32();54 i1.method33();55 i1.method34();56 i1.method35();57 i1.method36();58 i1.method37();59 i1.method38();60 i1.method39();61 i1.method40();62 i1.method41();63 i1.method42();64 i1.method43();65 i1.method44();66 i1.method45();67 i1.method46();68 i1.method47();69 i1.method48();70 i1.method49();71 i1.method50();72 i1.method51();73 i1.method52();74 i1.method53();75 i1.method54();

Full Screen

Full Screen

UnsynchronisedInvocationDispatcher

Using AI Code Generation

copy

Full Screen

1package org.jmock.lib.concurrent;2import junit.framework.TestCase;3import org.jmock.Mock;4import org.jmock.MockObjectTestCase;5import org.jmock.core.Invocation;6import org.jmock.core.InvocationDispatcher;7import org.jmock.core.InvocationDispatcherFactory;8import org.jmock.core.Stub;9import org.jmock.core.StubFactory;10import org.jmock.core.stub.DefaultResultStub;11import org.jmock.core.stub.ReturnStub;12import org.jmock.core.stub.ThrowStub;13import org.jmock.util.SynchronizedList;14import java.util.ArrayList;15import java.util.List;16import java.util.Iterator;17public class UnsynchronisedInvocationDispatcherTest extends MockObjectTestCase {18 public void testCanCreateDispatcherForStub() {19 Mock mockStubFactory = mock(StubFactory.class);20 Stub stub = new DefaultResultStub();21 mockStubFactory.expects(once()).method("createStub").will(returnValue(stub));22 StubFactory stubFactory = (StubFactory) mockStubFactory.proxy();23 new UnsynchronisedInvocationDispatcher(stubFactory);24 assertSame("should return stub created by stub factory", stub, dispatcher.getStub());25 }26 public void testCanCreateDispatcherForStubFactory() {27 Mock mockStubFactory = mock(StubFactory.class);28 Stub stub = new DefaultResultStub();29 mockStubFactory.expects(once()).method("createStub").will(returnValue(stub));30 StubFactory stubFactory = (StubFactory) mockStubFactory.proxy();31 new UnsynchronisedInvocationDispatcher(stubFactory);32 assertSame("should return stub created by stub factory", stub, dispatcher.getStub());33 }34 public void testCanCreateDispatcherForInvocationDispatcherFactory() {35 Mock mockStubFactory = mock(StubFactory.class);36 Mock mockInvocationDispatcherFactory = mock(InvocationDispatcherFactory.class);37 InvocationDispatcher invocationDispatcher = new InvocationDispatcher() {38 public void dispatch(Invocation invocation) {39 invocation.invoked(new DefaultResultStub());40 }41 };42 mockInvocationDispatcherFactory.expects(once()).method("createDispatcher").will(returnValue(invocationDispatcher));43 InvocationDispatcherFactory invocationDispatcherFactory = (InvocationDispatcherFactory) mockInvocationDispatcherFactory.proxy();44 new UnsynchronisedInvocationDispatcher(invocationDispatcherFactory);45 assertSame("should

Full Screen

Full Screen

UnsynchronisedInvocationDispatcher

Using AI Code Generation

copy

Full Screen

1import org.jmock.Mockery;2import org.jmock.lib.concurrent.UnsynchronizedInvocationDispatcher;3{4 public static void main(String[] args)5 {6 Mockery context = new Mockery();7 final MyInterface mock = context.mock(MyInterface.class);8 UnsynchronizedInvocationDispatcher dispatcher = new UnsynchronizedInvocationDispatcher(mock);9 MyInterface proxy = (MyInterface) dispatcher.proxy();10 proxy.method1();11 proxy.method2();12 context.assertIsSatisfied();13 }14}15import org.jmock.Mockery;16import org.jmock.lib.concurrent.SynchronizedInvocationDispatcher;17{18 public static void main(String[] args)19 {20 Mockery context = new Mockery();21 final MyInterface mock = context.mock(MyInterface.class);22 SynchronizedInvocationDispatcher dispatcher = new SynchronizedInvocationDispatcher(mock);23 MyInterface proxy = (MyInterface) dispatcher.proxy();24 proxy.method1();25 proxy.method2();26 context.assertIsSatisfied();27 }28}29import org.jmock.Mockery;30import org.jmock.lib.concurrent.Synchroniser;31{32 public static void main(String[] args)33 {34 Mockery context = new Mockery();35 final MyInterface mock = context.mock(MyInterface.class);36 Synchroniser synchroniser = new Synchroniser();37 MyInterface proxy = (MyInterface) synchroniser.proxy(mock);38 proxy.method1();39 proxy.method2();40 context.assertIsSatisfied();41 }42}43import org.jmock.Mockery;44import org.jmock.lib.concurrent.Synchroniser;45{46 public static void main(String[] args)47 {48 Mockery context = new Mockery();49 final MyInterface mock = context.mock(MyInterface.class);50 Synchroniser synchroniser = new Synchroniser();51 MyInterface proxy = (MyInterface) synchroniser.proxy(mock);52 proxy.method1();

Full Screen

Full Screen

UnsynchronisedInvocationDispatcher

Using AI Code Generation

copy

Full Screen

1public void testUnsynchronisedInvocationDispatcher() throws Exception2{3 final UnsynchronisedInvocationDispatcher dispatcher = new UnsynchronisedInvocationDispatcher();4 final Mock mock = new Mock(Interface.class, "mock", dispatcher);5 final Interface proxy = (Interface)mock.proxy();6 final Thread thread = new Thread(new Runnable()7 {8 public void run()9 {10 proxy.method();11 }12 });13 thread.start();14 thread.join();15 mock.verify();16}17public void testSynchronisedInvocationDispatcher() throws Exception18{19 final SynchronisedInvocationDispatcher dispatcher = new SynchronisedInvocationDispatcher();20 final Mock mock = new Mock(Interface.class, "mock", dispatcher);21 final Interface proxy = (Interface)mock.proxy();22 final Thread thread = new Thread(new Runnable()23 {24 public void run()25 {26 proxy.method();27 }28 });29 thread.start();30 thread.join();31 mock.verify();32}33public void testMultiThreadedTestRunner() throws Exception34{35 final Mock mock = new Mock(Interface.class, "mock");36 final Interface proxy = (Interface)mock.proxy();37 final MultiThreadedTestRunner runner = new MultiThreadedTestRunner();38 runner.run(new Runnable()39 {40 public void run()41 {42 proxy.method();43 }44 });45 mock.verify();46}47public void testMultiThreadedTestRunnerWithCount() throws Exception48{49 final Mock mock = new Mock(Interface.class, "mock");50 final Interface proxy = (Interface)mock.proxy();51 final MultiThreadedTestRunner runner = new MultiThreadedTestRunner(2);52 runner.run(new Runnable()53 {54 public void run()55 {56 proxy.method();57 }58 });59 mock.verify();60}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Jmock-library automation tests on LambdaTest cloud grid

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

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful