Best Jmock-library code snippet using org.jmock.test.unit.lib.concurrent.DeterministicSchedulerTests.AtomicInteger
Source:DeterministicSchedulerTests.java
...8import java.util.concurrent.Future;9import java.util.concurrent.ScheduledExecutorService;10import java.util.concurrent.ScheduledFuture;11import java.util.concurrent.TimeUnit;12import java.util.concurrent.atomic.AtomicInteger;13import org.jmock.Expectations;14import org.jmock.Sequence;15import org.jmock.api.Action;16import org.jmock.lib.concurrent.DeterministicScheduler;17import org.jmock.lib.concurrent.UnsupportedSynchronousOperationException;18import org.jmock.test.unit.internal.MockObjectTestCase;19public class DeterministicSchedulerTests extends MockObjectTestCase {20 DeterministicScheduler scheduler = new DeterministicScheduler();21 22 Runnable commandA = mock(Runnable.class, "commandA");23 Runnable commandB = mock(Runnable.class, "commandB");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);...
AtomicInteger
Using AI Code Generation
1final DeterministicScheduler scheduler = new DeterministicScheduler();2final AtomicInteger counter = new AtomicInteger(0);3final Task task = scheduler.schedule(1, new Runnable() {4 public void run() {5 counter.incrementAndGet();6 }7});8scheduler.tick();9assertThat(counter.get(), equalTo(1));10assertThat(scheduler.getTasks(), not(hasItem(task)));11final DeterministicScheduler scheduler = new DeterministicScheduler();12final AtomicInteger counter = new AtomicInteger(0);13final Task task = scheduler.schedule(1, new Runnable() {14 public void run() {15 counter.incrementAndGet();16 }17});18scheduler.tick();19assertThat(counter.get(), equalTo(1));20assertThat(scheduler.getTasks(), not(hasItem(task)));21final DeterministicScheduler scheduler = new DeterministicScheduler();22final AtomicInteger counter = new AtomicInteger(0);23final Task task = scheduler.schedule(1, new Runnable() {24 public void run() {25 counter.incrementAndGet();26 }27});28scheduler.tick();29assertThat(counter.get(), equalTo(1));30assertThat(scheduler.getTasks(), not(hasItem(task)));31final DeterministicScheduler scheduler = new DeterministicScheduler();32final AtomicInteger counter = new AtomicInteger(0);33final Task task = scheduler.schedule(1, new Runnable() {34 public void run() {35 counter.incrementAndGet();36 }37});38scheduler.tick();39assertThat(counter.get(), equalTo(1));40assertThat(scheduler.getTasks(), not(hasItem(task)));
AtomicInteger
Using AI Code Generation
1public void testAtomicIntegerWithDeterministicScheduler() throws Exception {2 final AtomicInteger atomicInteger = new AtomicInteger(0);3 scheduler.schedule(new Runnable() {4 public void run() {5 atomicInteger.incrementAndGet();6 }7 }, 0);8 scheduler.run();9 assertEquals(1, atomicInteger.get());10}11public void testAtomicLongWithDeterministicScheduler() throws Exception {12 final AtomicLong atomicLong = new AtomicLong(0);13 scheduler.schedule(new Runnable() {14 public void run() {15 atomicLong.incrementAndGet();16 }17 }, 0);18 scheduler.run();19 assertEquals(1, atomicLong.get());20}21public void testAtomicBooleanWithDeterministicScheduler() throws Exception {22 final AtomicBoolean atomicBoolean = new AtomicBoolean(false);23 scheduler.schedule(new Runnable() {24 public void run() {25 atomicBoolean.set(true);26 }27 }, 0);28 scheduler.run();29 assertTrue(atomicBoolean.get());30}31public void testAtomicIntegerWithDeterministicScheduler() throws Exception {32 final AtomicInteger atomicInteger = new AtomicInteger(0);33 scheduler.schedule(new Runnable() {34 public void run() {35 atomicInteger.set(1);36 }37 }, 0);38 scheduler.run();39 assertEquals(1, atomicInteger.get());40}41public void testAtomicLongWithDeterministicScheduler() throws Exception {42 final AtomicLong atomicLong = new AtomicLong(0);43 scheduler.schedule(new Runnable() {44 public void run() {45 atomicLong.set(1);46 }47 }, 0);48 scheduler.run();49 assertEquals(1, atomicLong.get());50}51public void testAtomicBooleanWithDeterministicScheduler() throws Exception {52 final AtomicBoolean atomicBoolean = new AtomicBoolean(false);53 scheduler.schedule(new Runnable() {54 public void run() {
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!