public void testGetWaitQueueLengthWithWrongMonitorThrowsIMSE() { Monitor monitor1 = new Monitor(); Monitor monitor2 = new Monitor(); FlagGuard guard = new FlagGuard(monitor2); try { monitor1.getWaitQueueLength(guard); fail("expected IllegalMonitorStateException"); } catch (IllegalMonitorStateException expected) { } }
private GeneratedMonitorTest( Method method, Scenario scenario, boolean fair, Timeout timeout, Outcome expectedOutcome) { super(nameFor(method, scenario, fair, timeout, expectedOutcome)); this.method = method; this.scenario = scenario; this.timeout = timeout; this.expectedOutcome = expectedOutcome; this.monitor = new Monitor(fair); this.guard = new FlagGuard(monitor); this.tearDownLatch = new CountDownLatch(1); this.doingCallLatch = new CountDownLatch(1); this.callCompletedLatch = new CountDownLatch(1); }
public void testHasWaitersWithWrongMonitorThrowsIMSE() { Monitor monitor1 = new Monitor(); Monitor monitor2 = new Monitor(); FlagGuard guard = new FlagGuard(monitor2); try { monitor1.hasWaiters(guard); fail("expected IllegalMonitorStateException"); } catch (IllegalMonitorStateException expected) { } }
public void testLeaveWithoutEnterThrowsIMSE() { Monitor monitor = new Monitor(); try { monitor.leave(); fail("expected IllegalMonitorStateException"); } catch (IllegalMonitorStateException expected) { } }
@Override protected void runTest() throws Throwable { Monitor monitor = new Monitor(fair); FlagGuard guard = new FlagGuard(monitor); Object[] arguments = (timed ? new Object[] {guard, 0L, TimeUnit.MILLISECONDS} : new Object[] {guard}); try { method.invoke(monitor, arguments); fail("expected IllegalMonitorStateException"); } catch (InvocationTargetException e) { assertEquals(IllegalMonitorStateException.class, e.getTargetException().getClass()); } } };
@Override protected final void setUp() throws Exception { boolean fair = new Random().nextBoolean(); monitor = new Monitor(fair); tearDownStack.addTearDown(thread1 = new TestThread<>(monitor, "TestThread #1")); tearDownStack.addTearDown(thread2 = new TestThread<>(monitor, "TestThread #2")); }
@Override protected void runTest() throws Throwable { Monitor monitor1 = new Monitor(fair1); Monitor monitor2 = new Monitor(fair2); FlagGuard guard = new FlagGuard(monitor2); Object[] arguments = (timed ? new Object[] {guard, 0L, TimeUnit.MILLISECONDS} : new Object[] {guard}); boolean occupyMonitor = isWaitFor(method); if (occupyMonitor) { // If we don't already occupy the monitor, we'll get an IMSE regardless of the guard (see // generateWaitForWhenNotOccupyingTestCase). monitor1.enter(); } try { method.invoke(monitor1, arguments); fail("expected IllegalMonitorStateException"); } catch (InvocationTargetException e) { assertEquals(IllegalMonitorStateException.class, e.getTargetException().getClass()); } finally { if (occupyMonitor) { monitor1.leave(); } } } };
/** * Generates a test case verifying that calling any waitForXxx method when not occupying the * monitor produces an IllegalMonitorStateException. */ private static TestCase generateWaitForWhenNotOccupyingTestCase( final Method method, final boolean fair) { final boolean timed = isTimed(method); // Not going to bother with all timeouts, just 0ms. String testName = method.getName() + (fair ? "(fair)" : "(nonfair)") + (timed ? "(0ms)" : "()") + "/NotOccupying->IMSE"; return new TestCase(testName) { @Override protected void runTest() throws Throwable { Monitor monitor = new Monitor(fair); FlagGuard guard = new FlagGuard(monitor); Object[] arguments = (timed ? new Object[] {guard, 0L, TimeUnit.MILLISECONDS} : new Object[] {guard}); try { method.invoke(monitor, arguments); fail("expected IllegalMonitorStateException"); } catch (InvocationTargetException e) { assertEquals(IllegalMonitorStateException.class, e.getTargetException().getClass()); } } }; }
public void testOccupiedMethods() { Monitor monitor = new Monitor(); verifyOccupiedMethodsInCurrentThread(monitor, false, false, 0); verifyOccupiedMethodsInAnotherThread(monitor, false, false, 0); monitor.enter(); try { verifyOccupiedMethodsInCurrentThread(monitor, true, true, 1); verifyOccupiedMethodsInAnotherThread(monitor, true, false, 0); monitor.enter(); try { verifyOccupiedMethodsInCurrentThread(monitor, true, true, 2); verifyOccupiedMethodsInAnotherThread(monitor, true, false, 0); } finally { monitor.leave(); } verifyOccupiedMethodsInCurrentThread(monitor, true, true, 1); verifyOccupiedMethodsInAnotherThread(monitor, true, false, 0); } finally { monitor.leave(); } verifyOccupiedMethodsInCurrentThread(monitor, false, false, 0); verifyOccupiedMethodsInAnotherThread(monitor, false, false, 0); }
@Test public void testAcquireMonitor() { Monitor monitor = new Monitor(); ForkJoinPool.commonPool().invoke(new RecursiveAction() { AtomicInteger running = new AtomicInteger();
@Override public Monitor load(Object key) throws Exception { return new Monitor(); } }
private final Monitor monitor = new Monitor(); private final Monitor.Guard paused = new Monitor.Guard(monitor) { @Override
public void testHasWaitersWithWrongMonitorThrowsIMSE() { Monitor monitor1 = new Monitor(); Monitor monitor2 = new Monitor(); FlagGuard guard = new FlagGuard(monitor2); try { monitor1.hasWaiters(guard); fail("expected IllegalMonitorStateException"); } catch (IllegalMonitorStateException expected) { } }
public void testGetWaitQueueLengthWithWrongMonitorThrowsIMSE() { Monitor monitor1 = new Monitor(); Monitor monitor2 = new Monitor(); FlagGuard guard = new FlagGuard(monitor2); try { monitor1.getWaitQueueLength(guard); fail("expected IllegalMonitorStateException"); } catch (IllegalMonitorStateException expected) { } }
@Override protected void runTest() throws Throwable { Monitor monitor = new Monitor(fair); FlagGuard guard = new FlagGuard(monitor); Object[] arguments = (timed ? new Object[] {guard, 0L, TimeUnit.MILLISECONDS} : new Object[] {guard}); try { method.invoke(monitor, arguments); fail("expected IllegalMonitorStateException"); } catch (InvocationTargetException e) { assertEquals(IllegalMonitorStateException.class, e.getTargetException().getClass()); } } };
public void testLeaveWithoutEnterThrowsIMSE() { Monitor monitor = new Monitor(); try { monitor.leave(); fail("expected IllegalMonitorStateException"); } catch (IllegalMonitorStateException expected) { } }
@Override public void onSessionInitiated(ProviderContext session) { this.rpcReg = session.addRpcImplementation(Onem2mMqttClientService.class, this); this.dataBroker = session.getSALService(DataBroker.class); this.notifierService = session.getSALService(NotificationProviderService.class); crudMonitor = new Monitor(); stats = Onem2mStats.getInstance(); db = Onem2mDb.getInstance(); db.initializeDatastore(dataBroker); onem2mService = session.getRpcService(Onem2mService.class); onem2mMqttClient = new Onem2mMqttAsyncClient(); LOG.info("Onem2mMqttProvider Session Initiated ..."); }
@Override protected final void setUp() throws Exception { boolean fair = new Random().nextBoolean(); monitor = new Monitor(fair); tearDownStack.addTearDown(thread1 = new TestThread<>(monitor, "TestThread #1")); tearDownStack.addTearDown(thread2 = new TestThread<>(monitor, "TestThread #2")); }