How to use schedule method of org.jmock.test.unit.lib.concurrent.DeterministicSchedulerTests class

Best Jmock-library code snippet using org.jmock.test.unit.lib.concurrent.DeterministicSchedulerTests.schedule

Source:DeterministicSchedulerTests.java Github

copy

Full Screen

...13import org.jmock.integration.junit3.MockObjectTestCase;14import org.jmock.lib.concurrent.DeterministicScheduler;15import org.jmock.lib.concurrent.UnsupportedSynchronousOperationException;16public class DeterministicSchedulerTests extends MockObjectTestCase {17 DeterministicScheduler scheduler = new DeterministicScheduler();18 19 Runnable commandA = mock(Runnable.class, "commandA");20 Runnable commandB = mock(Runnable.class, "commandB");21 Runnable commandC = mock(Runnable.class, "commandC");22 Runnable commandD = mock(Runnable.class, "commandD");23 24 @SuppressWarnings("unchecked")25 Callable<String> callableA = mock(Callable.class, "callableA");26 27 public void testRunsPendingCommandsUntilIdle() {28 scheduler.execute(commandA);29 scheduler.execute(commandB);30 31 final Sequence executionOrder = sequence("executionOrder");32 33 checking(new Expectations() {{34 oneOf (commandA).run(); inSequence(executionOrder);35 oneOf (commandB).run(); inSequence(executionOrder);36 }});37 38 assertFalse(scheduler.isIdle());39 40 scheduler.runUntilIdle();41 42 assertTrue(scheduler.isIdle());43 }44 45 public void testCanRunCommandsSpawnedByExecutedCommandsUntilIdle() {46 scheduler.execute(commandA);47 scheduler.execute(commandB);48 49 final Sequence executionOrder = sequence("executionOrder");50 51 checking(new Expectations() {{52 oneOf (commandA).run(); inSequence(executionOrder); will(schedule(commandC));53 oneOf (commandB).run(); inSequence(executionOrder); will(schedule(commandD));54 oneOf (commandC).run(); inSequence(executionOrder);55 oneOf (commandD).run(); inSequence(executionOrder);56 }});57 58 scheduler.runUntilIdle();59 }60 61 public void testCanScheduleCommandAndReturnFuture() throws InterruptedException, ExecutionException {62 Future<?> future = scheduler.submit(commandA);63 64 checking(new Expectations() {{65 oneOf (commandA).run();66 }});67 68 assertTrue("future should not be done before running the task", !future.isDone());69 70 scheduler.runUntilIdle();71 72 assertTrue("future should be done after running the task", future.isDone());73 assertNull("result of future should be null", future.get());74 }75 76 public void testCanScheduleCommandAndResultAndReturnFuture() throws InterruptedException, ExecutionException {77 Future<String> future = scheduler.submit(commandA, "result1");78 79 checking(new Expectations() {{80 oneOf (commandA).run();81 }});82 83 scheduler.runUntilIdle();84 85 assertThat(future.get(), equalTo("result1"));86 }87 public void testCanScheduleCallableAndReturnFuture() throws Exception {88 Future<String> future = scheduler.submit(callableA);89 90 checking(new Expectations() {{91 oneOf (callableA).call(); will(returnValue("result2"));92 }});93 94 scheduler.runUntilIdle();95 96 assertThat(future.get(), equalTo("result2"));97 }98 public void testScheduledCallablesCanReturnNull() throws Exception {99 checking(new Expectations() {{100 oneOf (callableA).call(); will(returnValue(null));101 }});102 103 Future<String> future = scheduler.submit(callableA);104 105 scheduler.runUntilIdle();106 107 assertNull(future.get());108 }109 110 public class ExampleException extends Exception {}111 112 public void testExceptionThrownByScheduledCallablesIsThrownFromFuture() throws Exception {113 final Throwable thrown = new ExampleException();114 115 checking(new Expectations() {{116 oneOf (callableA).call(); will(throwException(thrown));117 }});118 119 Future<String> future = scheduler.submit(callableA);120 121 scheduler.runUntilIdle();122 123 try {124 future.get();125 fail("should have thrown ExecutionException");126 }127 catch (ExecutionException expected) {128 assertThat(expected.getCause(), sameInstance(thrown));129 }130 }131 public void testCanScheduleCommandsToBeExecutedAfterADelay() {132 scheduler.schedule(commandA, 10, TimeUnit.SECONDS);133 134 scheduler.tick(9, TimeUnit.SECONDS);135 136 checking(new Expectations() {{137 oneOf (commandA).run();138 }});139 140 scheduler.tick(1, TimeUnit.SECONDS);141 }142 143 public void testTickingTimeForwardRunsAllCommandsScheduledDuringThatTimePeriod() {144 scheduler.schedule(commandA, 1, TimeUnit.MILLISECONDS);145 scheduler.schedule(commandB, 2, TimeUnit.MILLISECONDS);146 147 checking(new Expectations() {{148 oneOf (commandA).run();149 oneOf (commandB).run();150 }});151 152 scheduler.tick(3, TimeUnit.MILLISECONDS);153 }154 155 public void testTickingTimeForwardRunsCommandsExecutedByScheduledCommands() {156 scheduler.schedule(commandA, 1, TimeUnit.MILLISECONDS);157 scheduler.schedule(commandD, 2, TimeUnit.MILLISECONDS);158 159 checking(new Expectations() {{160 oneOf (commandA).run(); will(schedule(commandB));161 oneOf (commandB).run(); will(schedule(commandC));162 oneOf (commandC).run();163 oneOf (commandD).run();164 }});165 166 scheduler.tick(3, TimeUnit.MILLISECONDS);167 }168 169 public void testCanExecuteCommandsThatRepeatWithFixedDelay() {170 scheduler.scheduleWithFixedDelay(commandA, 2L, 3L, TimeUnit.SECONDS);171 172 checking(new Expectations() {{173 exactly(3).of(commandA).run();174 }});175 176 scheduler.tick(8L, TimeUnit.SECONDS);177 }178 public void testCanExecuteCommandsThatRepeatAtFixedRateButAssumesThatCommandsTakeNoTimeToExecute() {179 scheduler.scheduleAtFixedRate(commandA, 2L, 3L, TimeUnit.SECONDS);180 181 checking(new Expectations() {{182 exactly(3).of(commandA).run();183 }});184 185 scheduler.tick(8L, TimeUnit.SECONDS);186 }187 188 public void testCanCancelScheduledCommands() {189 final boolean dontCare = true;190 ScheduledFuture<?> future = scheduler.schedule(commandA, 1, TimeUnit.SECONDS);191 192 assertFalse(future.isCancelled());193 future.cancel(dontCare);194 assertTrue(future.isCancelled());195 196 checking(new Expectations() {{197 never (commandA);198 }});199 200 scheduler.tick(2, TimeUnit.SECONDS);201 }202 203 static final int TIMEOUT_IGNORED = 1000;204 205 public void testCanScheduleCallablesAndGetTheirResultAfterTheyHaveBeenExecuted() throws Exception {206 checking(new Expectations() {{207 oneOf (callableA).call(); will(returnValue("A"));208 }});209 210 ScheduledFuture<String> future = scheduler.schedule(callableA, 1, TimeUnit.SECONDS);211 212 assertTrue("is not done", !future.isDone());213 214 scheduler.tick(1, TimeUnit.SECONDS);215 216 assertTrue("is done", future.isDone());217 assertThat(future.get(), equalTo("A"));218 assertThat(future.get(TIMEOUT_IGNORED, TimeUnit.SECONDS), equalTo("A"));219 }220 public void testCannotBlockWaitingForFutureResultOfScheduledCallable() throws Exception {221 ScheduledFuture<String> future = scheduler.schedule(callableA, 1, TimeUnit.SECONDS);222 223 try {224 future.get();225 fail("should have thrown UnsupportedSynchronousOperationException");226 }227 catch (UnsupportedSynchronousOperationException expected) {}228 229 try {230 future.get(TIMEOUT_IGNORED, TimeUnit.SECONDS);231 fail("should have thrown UnsupportedSynchronousOperationException");232 }233 catch (UnsupportedSynchronousOperationException expected) {}234 }235 236 private Action schedule(final Runnable command) {237 return ScheduleOnExecutorAction.schedule(scheduler, command);238 }239}...

Full Screen

Full Screen

schedule

Using AI Code Generation

copy

Full Screen

1DeterministicSchedulerTests scheduler = new DeterministicSchedulerTests();2scheduler.schedule(new Runnable() {3 public void run() {4 System.out.println("Task is running");5 }6}, 2000, TimeUnit.MILLISECONDS);7scheduler.runFor(2000, TimeUnit.MILLISECONDS);

Full Screen

Full Screen

schedule

Using AI Code Generation

copy

Full Screen

1import org.jmock.lib.concurrent.DeterministicScheduler2import org.jmock.lib.concurrent.DeterministicSchedulerTests3import org.jmock.lib.concurrent.Synchroniser4import org.jmock.lib.concurrent.TimeoutException5public class DeterministicSchedulerTest {6 def setup() {7 scheduler = new DeterministicScheduler()8 synchroniser = new Synchroniser()9 }10 def "test that the scheduler can be used to schedule a task"() {11 scheduler.schedule(synchroniser, 1000)12 synchroniser.waitUntilSignalled(1000)13 synchroniser.wasSignalled()14 }15 def "test that the scheduler can be used to schedule a task that is signalled"() {16 scheduler.schedule(synchroniser, 1000)17 synchroniser.waitUntilSignalled(1000)18 synchroniser.wasSignalled()19 }20 def "test that the scheduler can be used to schedule a task that is signalled within the timeout"() {21 scheduler.schedule(synchroniser, 1000)22 synchroniser.waitUntilSignalled(2000)23 synchroniser.wasSignalled()24 }25 def "test that the scheduler can be used to schedule a task that is signalled within the timeout"() {26 scheduler.schedule(synchroniser, 1000)27 synchroniser.waitUntilSignalled(2000)28 synchroniser.wasSignalled()29 }30 def "test that the scheduler can be used to schedule a task that times out"() {31 scheduler.schedule(synchroniser, 1000)32 !synchroniser.waitUntilSignalled(500)33 !synchroniser.wasSignalled()34 }35 def "test that the scheduler can be used to schedule a task that times out"() {36 scheduler.schedule(synchroniser, 1000)37 !synchroniser.waitUntilSignalled(500)38 !synchroniser.wasSignalled()39 }40 def "test that the scheduler can be used to schedule a task that times out"() {41 scheduler.schedule(synchroniser, 1000)

Full Screen

Full Screen

schedule

Using AI Code Generation

copy

Full Screen

1 public void testScheduleRunnable() {2 scheduler.schedule(runnable, 1, TimeUnit.NANOSECONDS);3 assertEquals(1, scheduler.getNumberOfScheduledTasks());4 scheduler.runNextTask();5 assertEquals(0, scheduler.getNumberOfScheduledTasks());6 context.assertIsSatisfied();7 }8}

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