How to use call method of org.jmock.lib.concurrent.DeterministicScheduler class

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

Source:DeterministicSchedulerTests.java Github

copy

Full Screen

...24 Runnable commandC = mock(Runnable.class, "commandC");25 Runnable commandD = mock(Runnable.class, "commandD");26 27 @SuppressWarnings("unchecked")28 Callable<String> callableA = mock(Callable.class, "callableA");29 30 public void testRunsPendingCommandsUntilIdle() {31 scheduler.execute(commandA);32 scheduler.execute(commandB);33 34 final Sequence executionOrder = sequence("executionOrder");35 36 checking(new Expectations() {{37 oneOf (commandA).run(); inSequence(executionOrder);38 oneOf (commandB).run(); inSequence(executionOrder);39 }});40 41 assertFalse(scheduler.isIdle());42 43 scheduler.runUntilIdle();44 45 assertTrue(scheduler.isIdle());46 }47 48 public void testCanRunCommandsSpawnedByExecutedCommandsUntilIdle() {49 scheduler.execute(commandA);50 scheduler.execute(commandB);51 52 final Sequence executionOrder = sequence("executionOrder");53 54 checking(new Expectations() {{55 oneOf (commandA).run(); inSequence(executionOrder); will(schedule(commandC));56 oneOf (commandB).run(); inSequence(executionOrder); will(schedule(commandD));57 oneOf (commandC).run(); inSequence(executionOrder);58 oneOf (commandD).run(); inSequence(executionOrder);59 }});60 61 scheduler.runUntilIdle();62 }63 64 public void testCanScheduleCommandAndReturnFuture() throws InterruptedException, ExecutionException {65 Future<?> future = scheduler.submit(commandA);66 67 checking(new Expectations() {{68 oneOf (commandA).run();69 }});70 71 assertTrue("future should not be done before running the task", !future.isDone());72 73 scheduler.runUntilIdle();74 75 assertTrue("future should be done after running the task", future.isDone());76 assertNull("result of future should be null", future.get());77 }78 79 public void testCanScheduleCommandAndResultAndReturnFuture() throws InterruptedException, ExecutionException {80 Future<String> future = scheduler.submit(commandA, "result1");81 82 checking(new Expectations() {{83 oneOf (commandA).run();84 }});85 86 scheduler.runUntilIdle();87 88 assertThat(future.get(), equalTo("result1"));89 }90 public void testCanScheduleCallableAndReturnFuture() throws Exception {91 Future<String> future = scheduler.submit(callableA);92 93 checking(new Expectations() {{94 oneOf (callableA).call(); will(returnValue("result2"));95 }});96 97 scheduler.runUntilIdle();98 99 assertThat(future.get(), equalTo("result2"));100 }101 public void testScheduledCallablesCanReturnNull() throws Exception {102 checking(new Expectations() {{103 oneOf (callableA).call(); will(returnValue(null));104 }});105 106 Future<String> future = scheduler.submit(callableA);107 108 scheduler.runUntilIdle();109 110 assertNull(future.get());111 }112 public void testSimpleGetDelay() throws Exception {113 ScheduledFuture<?> task1 = scheduler.schedule(commandA, 10, TimeUnit.SECONDS);114 scheduler.tick(1, TimeUnit.SECONDS);115 assertEquals(9, task1.getDelay(TimeUnit.SECONDS));116 }117 public void testGetDelayWithManyScheduledTasks() throws Exception {118 ScheduledFuture<?> task1 = scheduler.schedule(commandA, 10, TimeUnit.SECONDS);119 ScheduledFuture<?> task2 = scheduler.schedule(commandA, 20, TimeUnit.SECONDS);120 ScheduledFuture<?> task3 = scheduler.schedule(commandA, 15, TimeUnit.SECONDS);121 scheduler.tick(5, TimeUnit.SECONDS);122 assertEquals(5, task1.getDelay(TimeUnit.SECONDS));123 assertEquals(15, task2.getDelay(TimeUnit.SECONDS));124 assertEquals(10, task3.getDelay(TimeUnit.SECONDS));125 }126 public void testGetDelayOnPassedTasks() throws Exception {127 final Throwable thrown = new IllegalStateException();128 ScheduledFuture<?> task1 = scheduler.schedule(commandA, 1, TimeUnit.MILLISECONDS);129 checking(new Expectations() {{130 oneOf (commandA).run(); will(throwException(thrown));131 }});132 scheduler.tick(2, TimeUnit.MILLISECONDS);133 assertEquals(-1, task1.getDelay(TimeUnit.MILLISECONDS));134 }135 136 public class ExampleException extends Exception {}137 138 public void testExceptionThrownByScheduledCallablesIsThrownFromFuture() throws Exception {139 final Throwable thrown = new ExampleException();140 141 checking(new Expectations() {{142 oneOf (callableA).call(); will(throwException(thrown));143 }});144 145 Future<String> future = scheduler.submit(callableA);146 147 scheduler.runUntilIdle();148 149 try {150 future.get();151 fail("should have thrown ExecutionException");152 }153 catch (ExecutionException expected) {154 assertThat(expected.getCause(), sameInstance(thrown));155 }156 }157 public void testCanScheduleCommandsToBeExecutedAfterADelay() {158 scheduler.schedule(commandA, 10, TimeUnit.SECONDS);159 160 scheduler.tick(9, TimeUnit.SECONDS);161 162 checking(new Expectations() {{163 oneOf (commandA).run();164 }});165 166 scheduler.tick(1, TimeUnit.SECONDS);167 }168 169 public void testTickingTimeForwardRunsAllCommandsScheduledDuringThatTimePeriod() {170 scheduler.schedule(commandA, 1, TimeUnit.MILLISECONDS);171 scheduler.schedule(commandB, 2, TimeUnit.MILLISECONDS);172 173 checking(new Expectations() {{174 oneOf (commandA).run();175 oneOf (commandB).run();176 }});177 178 scheduler.tick(3, TimeUnit.MILLISECONDS);179 }180 181 public void testTickingTimeForwardRunsCommandsExecutedByScheduledCommands() {182 scheduler.schedule(commandA, 1, TimeUnit.MILLISECONDS);183 scheduler.schedule(commandD, 2, TimeUnit.MILLISECONDS);184 185 checking(new Expectations() {{186 oneOf (commandA).run(); will(schedule(commandB));187 oneOf (commandB).run(); will(schedule(commandC));188 oneOf (commandC).run();189 oneOf (commandD).run();190 }});191 192 scheduler.tick(3, TimeUnit.MILLISECONDS);193 }194 195 public void testCanExecuteCommandsThatRepeatWithFixedDelay() {196 scheduler.scheduleWithFixedDelay(commandA, 2L, 3L, TimeUnit.SECONDS);197 198 checking(new Expectations() {{199 exactly(3).of(commandA).run();200 }});201 202 scheduler.tick(8L, TimeUnit.SECONDS);203 }204 public void testCanExecuteCommandsThatRepeatAtFixedRateButAssumesThatCommandsTakeNoTimeToExecute() {205 scheduler.scheduleAtFixedRate(commandA, 2L, 3L, TimeUnit.SECONDS);206 207 checking(new Expectations() {{208 exactly(3).of(commandA).run();209 }});210 211 scheduler.tick(8L, TimeUnit.SECONDS);212 }213 214 public void testCanCancelScheduledCommands() {215 final boolean dontCare = true;216 ScheduledFuture<?> future = scheduler.schedule(commandA, 1, TimeUnit.SECONDS);217 218 assertFalse(future.isCancelled());219 future.cancel(dontCare);220 assertTrue(future.isCancelled());221 222 checking(new Expectations() {{223 never (commandA);224 }});225 226 scheduler.tick(2, TimeUnit.SECONDS);227 }228 public void testCancellingARunningCommandStopsItFromRunningAgain() {229 DeterministicScheduler deterministicScheduler = new DeterministicScheduler();230 ObjectThatCancelsARepeatingTask objectThatCancelsARepeatingTask = new ObjectThatCancelsARepeatingTask(deterministicScheduler);231 objectThatCancelsARepeatingTask.scheduleATaskThatWillCancelItself(1, TimeUnit.SECONDS);232 deterministicScheduler.tick(2,TimeUnit.SECONDS);233 assertThat("cancelling runnable run count", objectThatCancelsARepeatingTask.runCount(), is(1));234 }235 public static class ObjectThatCancelsARepeatingTask {236 private final ScheduledExecutorService scheduler;237 private ScheduledFuture<?> scheduledFuture;238 private final AtomicInteger counter = new AtomicInteger();239 public ObjectThatCancelsARepeatingTask(ScheduledExecutorService scheduler) {240 this.scheduler = scheduler;241 }242 public void scheduleATaskThatWillCancelItself(int interval, TimeUnit unit){243 scheduledFuture = scheduler.scheduleAtFixedRate(244 new CancellingRunnable(), interval,interval,unit);245 }246 public int runCount(){247 return counter.get();248 }249 private class CancellingRunnable implements Runnable{250 @Override251 public void run() {252 scheduledFuture.cancel(true);253 counter.incrementAndGet();254 }255 }256 }257 static final int TIMEOUT_IGNORED = 1000;258 259 public void testCanScheduleCallablesAndGetTheirResultAfterTheyHaveBeenExecuted() throws Exception {260 checking(new Expectations() {{261 oneOf (callableA).call(); will(returnValue("A"));262 }});263 264 ScheduledFuture<String> future = scheduler.schedule(callableA, 1, TimeUnit.SECONDS);265 266 assertTrue("is not done", !future.isDone());267 268 scheduler.tick(1, TimeUnit.SECONDS);269 270 assertTrue("is done", future.isDone());271 assertThat(future.get(), equalTo("A"));272 assertThat(future.get(TIMEOUT_IGNORED, TimeUnit.SECONDS), equalTo("A"));273 }274 public void testCannotBlockWaitingForFutureResultOfScheduledCallable() throws Exception {275 ScheduledFuture<String> future = scheduler.schedule(callableA, 1, TimeUnit.SECONDS);276 277 try {278 future.get();279 fail("should have thrown UnsupportedSynchronousOperationException");280 }281 catch (UnsupportedSynchronousOperationException expected) {}282 283 try {284 future.get(TIMEOUT_IGNORED, TimeUnit.SECONDS);285 fail("should have thrown UnsupportedSynchronousOperationException");286 }287 catch (UnsupportedSynchronousOperationException expected) {}288 }289 ...

Full Screen

Full Screen

Source:ConfigManagerImplTest.java Github

copy

Full Screen

...34 configManager.restart();35 36 tr1.setCount(100);37 38 scheduledExecutorService.tick(6, TimeUnit.SECONDS); // call save39 40 tr1.setCount(10);41 42 configService.configure(tr1);43 Assert.assertEquals(100, tr1.getCount());44 configManager.unregister(tr1);45 46 }47 48 @Test49 public void testConfigure() throws InterruptedException {50 TestRule1 tr1 = new TestRule1();51 configManager.register(tr1);52 53 configManager.setConfigurePeroidInSeconds(5);54 configManager.setSavePeroidInSeconds(20);55 configManager.restart();56 57 tr1.setCount(100);58 configService.save(tr1);59 60 tr1.setCount(10);61 scheduledExecutorService.tick(6, TimeUnit.SECONDS); // call configure62 63 Assert.assertEquals(100, tr1.getCount());64 configManager.unregister(tr1); 65 }66 67} ...

Full Screen

Full Screen

call

Using AI Code Generation

copy

Full Screen

1import org.jmock.Mockery;2import org.jmock.lib.concurrent.DeterministicScheduler;3import org.jmock.lib.concurrent.Synchroniser;4import org.jmock.lib.legacy.ClassImposteriser;5public class 1 {6 public static void main(String[] args) {7 Mockery context = new Mockery() {8 {9 setImposteriser(ClassImposteriser.INSTANCE);10 }11 };12 final DeterministicScheduler scheduler = new DeterministicScheduler();13 context.setThreadingPolicy(new Synchroniser());14 context.checking(new Expectations() {15 {16 oneOf(mock).method();17 will(scheduler);18 }19 });20 mock.method();21 scheduler.run();22 }23}24import org.jmock.Mockery;25import org.jmock.lib.concurrent.DeterministicScheduler;26import org.jmock.lib.concurrent.Synchroniser;27import org.jmock.lib.legacy.ClassImposteriser;28public class 2 {29 public static void main(String[] args) {30 Mockery context = new Mockery() {31 {32 setImposteriser(ClassImposteriser.INSTANCE);33 }34 };35 final DeterministicScheduler scheduler = new DeterministicScheduler();36 context.setThreadingPolicy(new Synchroniser());37 context.checking(new Expectations() {38 {39 oneOf(mock).method();40 will(scheduler);41 }42 });43 mock.method();44 scheduler.run();45 }46}

Full Screen

Full Screen

call

Using AI Code Generation

copy

Full Screen

1import org.jmock.Mockery;2import org.jmock.lib.concurrent.DeterministicScheduler;3import org.jmock.lib.concurrent.Synchroniser;4public class Test1 {5 public static void main(String[] args) {6 Mockery context = new Mockery();7 context.setThreadingPolicy(new Synchroniser());8 DeterministicScheduler scheduler = new DeterministicScheduler();9 context.setThreadingPolicy(scheduler);10 scheduler.call(1000);11 }12}13 at org.jmock.lib.concurrent.DeterministicScheduler.call(DeterministicScheduler.java:116)14 at Test1.main(1.java:16)15public void call(long delay);16import org.jmock.Mockery;17import org.jmock.lib.concurrent.DeterministicScheduler;18import org.jmock.lib.concurrent.Synchroniser;19public class Test2 {20 public static void main(String[] args) {21 Mockery context = new Mockery();22 context.setThreadingPolicy(new Synchroniser());23 DeterministicScheduler scheduler = new DeterministicScheduler();24 context.setThreadingPolicy(scheduler);25 scheduler.call(1000);26 System.out.println("The task is executed");27 }28}

Full Screen

Full Screen

call

Using AI Code Generation

copy

Full Screen

1import org.jmock.Mock;2import org.jmock.MockObjectTestCase;3import org.jmock.lib.concurrent.DeterministicScheduler;4public class Example extends MockObjectTestCase {5 public void testDeterministicScheduler() {6 Mock mock = mock(Runnable.class);7 mock.expects(once()).method("run");

Full Screen

Full Screen

call

Using AI Code Generation

copy

Full Screen

1package org.jmock.example;2import java.util.concurrent.Callable;3import junit.framework.TestCase;4import org.jmock.Mockery;5import org.jmock.example.Example;6import org.jmock.example.ExampleTest;7import org.jmock.example.ExampleTest.ExampleTask;8import org.jmock.lib.concurrent.DeterministicScheduler;9public class ExampleTest extends TestCase {10 public static class ExampleTask implements Callable<String> {11 private final Example example;12 public ExampleTask(Example example) {13 this.example = example;14 }15 public String call() throws Exception {16 return example.doSomething();17 }18 }19 public void testExample() {20 Mockery context = new Mockery();21 final Example example = context.mock(Example.class);22 DeterministicScheduler scheduler = new DeterministicScheduler();23 scheduler.schedule(new ExampleTask(example), 1);24 context.checking(new Expectations() {{25 oneOf (example).doSomething();26 will(returnValue("foo"));27 }});28 assertEquals("foo", scheduler.call());29 }30}31package org.jmock.example;32import java.util.concurrent.Callable;33import junit.framework.TestCase;34import org.jmock.Mockery;35import org.jmock.example.Example;36import org.jmock.example.ExampleTest;37import org.jmock.example.ExampleTest.ExampleTask;38import org.jmock.lib.concurrent.DeterministicScheduler;39public class ExampleTest extends TestCase {40 public static class ExampleTask implements Callable<String> {41 private final Example example;42 public ExampleTask(Example example) {43 this.example = example;44 }45 public String call() throws Exception {46 return example.doSomething();47 }48 }49 public void testExample() {50 Mockery context = new Mockery();51 final Example example = context.mock(Example.class);52 DeterministicScheduler scheduler = new DeterministicScheduler();53 scheduler.schedule(new ExampleTask(example), 1);54 context.checking(new Expectations() {{55 oneOf (example).doSomething();56 will(returnValue("foo"));57 }});58 assertEquals("foo", scheduler.call());59 }60}61package org.jmock.example;62import java.util.concurrent.Callable;63import junit.framework.TestCase;64import org.jmock.Mockery

Full Screen

Full Screen

call

Using AI Code Generation

copy

Full Screen

1import org.jmock.Mockery;2import org.jmock.Sequence;3import org.jmock.lib.concurrent.DeterministicScheduler;4import org.jmock.lib.concurrent.Synchroniser;5import org.jmock.lib.legacy.ClassImposteriser;6public class DelayedCall {7 public static void main(String[] args) {8 final Mockery context = new Mockery() {9 {10 setImposteriser(ClassImposteriser.INSTANCE);11 }12 };13 final Sequence seq = context.sequence("seq");14 final Runnable runnable = context.mock(Runnable.class, "runnable");15 context.checking(new Expectations() {16 {17 oneOf(runnable).run();18 }19 });20 DeterministicScheduler scheduler = new DeterministicScheduler();21 scheduler.setSynchroniser(new Synchroniser());22 scheduler.call(runnable, 1000);23 scheduler.run();24 context.assertIsSatisfied();25 }26}27import org.jmock.Mockery;28import org.jmock.Sequence;29import org.jmock.lib.concurrent.DeterministicScheduler;30import org.jmock.lib.concurrent.Synchroniser;31import org.jmock.lib.legacy.ClassImposteriser;32public class DelayedCall {33 public static void main(String[] args) {34 final Mockery context = new Mockery() {35 {36 setImposteriser(ClassImposteriser.INSTANCE);37 }38 };39 final Sequence seq = context.sequence("seq");40 final Runnable runnable = context.mock(Runnable.class, "runnable");41 context.checking(new Expectations() {42 {43 oneOf(runnable).run();44 }45 });46 DeterministicScheduler scheduler = new DeterministicScheduler();47 scheduler.setSynchroniser(new Synchroniser());48 scheduler.call(runnable, 1000);49 scheduler.run();50 context.assertIsSatisfied();51 }52}53import org.jmock.Mockery;54import org.jmock.Sequence;55import org.jmock.lib.concurrent.DeterministicScheduler;56import org.jmock.lib.concurrent.Synchroniser;57import

Full Screen

Full Screen

call

Using AI Code Generation

copy

Full Screen

1import org.jmock.Mockery;2import org.jmock.lib.concurrent.DeterministicScheduler;3import org.jmock.lib.concurrent.Synchroniser;4import org.jmock.lib.legacy.ClassImposteriser;5import org.junit.After;6import org.junit.Before;7import org.junit.Test;8public class TestJMock {9 private Mockery context = new Mockery() {{10 setImposteriser(ClassImposteriser.INSTANCE);11 setThreadingPolicy(new Synchroniser());12 }};13 private DeterministicScheduler scheduler;14 public void setUp() {15 scheduler = new DeterministicScheduler();16 }17 public void tearDown() {18 context.assertIsSatisfied();19 }20 public void test() {21 final Runnable task = context.mock(Runnable.class);22 scheduler.schedule(task, 1000);23 context.checking(new Expectations() {{24 oneOf(task).run();25 }});26 scheduler.run();27 }28}29at org.jmock.internal.InvocationExpectation.check(InvocationExpectation.java:104)30at org.jmock.internal.ExpectationGroup.check(ExpectationGroup.java:75)31at org.jmock.internal.StatePredicate.assertIsSatisfied(StatePredicate.java:42)32at org.jmock.internal.StatePredicate.assertIsSatisfied(StatePredicate.java:32)33at org.jmock.Mockery.assertIsSatisfied(Mockery.java:183)34at TestJMock.tearDown(TestJMock.java:36)35at org.jmock.internal.InvocationExpectation.check(InvocationExpectation.java:104)36at org.jmock.internal.ExpectationGroup.check(ExpectationGroup.java:75)37at org.jmock.internal.StatePredicate.assertIsSatisfied(StatePredicate.java:42)38at org.jmock.internal.StatePredicate.assertIsSatisfied(StatePredicate.java:32)39at org.jmock.Mockery.assertIsSatisfied(Mockery.java:183)40at TestJMock.tearDown(TestJMock.java:36)41at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)42at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)43at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethod

Full Screen

Full Screen

call

Using AI Code Generation

copy

Full Screen

1import junit.framework.TestCase;2import org.jmock.Mock;3import org.jmock.MockObjectTestCase;4import org.jmock.lib.concurrent.DeterministicScheduler;5public class TestDeterministicScheduler extends MockObjectTestCase {6 public void testDelay() throws InterruptedException {7 Mock mockRunnable = mock(Runnable.class);8 mockRunnable.expects(once()).method("run");9 DeterministicScheduler scheduler = new DeterministicScheduler();10 scheduler.callAfterDelay(1000, (Runnable) mockRunnable.proxy());11 scheduler.advanceTime(1000);12 }13}14import junit.framework.TestCase;15import org.jmock.Mock;16import org.jmock.MockObjectTestCase;17import org.jmock.lib.concurrent.DeterministicScheduler;18public class TestDeterministicScheduler extends MockObjectTestCase {19 public void testDelay() throws InterruptedException {20 Mock mockRunnable = mock(Runnable.class);21 mockRunnable.expects(once()).method("run");22 DeterministicScheduler scheduler = new DeterministicScheduler();23 scheduler.callAfterDelay(1000, (Runnable) mockRunnable.proxy());24 scheduler.advanceTime(1000);25 }26}27import junit.framework.TestCase;28import org.jmock.Mock;29import org.jmock.MockObjectTestCase;30import org.jmock.lib.concurrent.DeterministicScheduler;31public class TestDeterministicScheduler extends MockObjectTestCase {32 public void testDelay() throws InterruptedException {33 Mock mockRunnable = mock(Runnable.class);34 mockRunnable.expects(once()).method("run");35 DeterministicScheduler scheduler = new DeterministicScheduler();

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful