public BlockedOperator(Duration unblockAfter) { this.unblockAfter = requireNonNull(unblockAfter, "unblockAfter is null"); this.operatorContext = TestingOperatorContext.create(executor); }
private static AggregatedMemoryContext mockMemoryContext(ScheduledExecutorService scheduledExecutor) { // It's important to use OperatorContext's system memory context, because it does additional bookkeeping. return TestingOperatorContext.create(scheduledExecutor).newAggregateSystemMemoryContext(); }
@Test(expectedExceptions = IllegalStateException.class, expectedExceptionsMessageRegExp = "listener already set") public void testSingleListenerEnforcement() { OperatorContext operatorContext = TestingOperatorContext.create(scheduledExecutor); operatorContext.setMemoryRevocationRequestListener(() -> {}); operatorContext.setMemoryRevocationRequestListener(() -> {}); } }
@Test public void testRevocationAlreadyRequested() { AtomicInteger counter = new AtomicInteger(); OperatorContext operatorContext = TestingOperatorContext.create(scheduledExecutor); LocalMemoryContext revocableMemoryContext = operatorContext.localRevocableMemoryContext(); revocableMemoryContext.setBytes(1000); // when memory revocation is already requested setting a listener should immediately execute it operatorContext.requestMemoryRevoking(); operatorContext.setMemoryRevocationRequestListener(() -> counter.incrementAndGet()); assertTrue(operatorContext.isMemoryRevokingRequested()); assertEquals(counter.get(), 1); }
@Test public void testOperatorMemoryRevocation() { AtomicInteger counter = new AtomicInteger(); OperatorContext operatorContext = TestingOperatorContext.create(scheduledExecutor); LocalMemoryContext revocableMemoryContext = operatorContext.localRevocableMemoryContext(); revocableMemoryContext.setBytes(1000); operatorContext.setMemoryRevocationRequestListener(() -> counter.incrementAndGet()); operatorContext.requestMemoryRevoking(); assertTrue(operatorContext.isMemoryRevokingRequested()); assertEquals(counter.get(), 1); // calling resetMemoryRevokingRequested() should clear the memory revoking requested flag operatorContext.resetMemoryRevokingRequested(); assertFalse(operatorContext.isMemoryRevokingRequested()); operatorContext.requestMemoryRevoking(); assertEquals(counter.get(), 2); assertTrue(operatorContext.isMemoryRevokingRequested()); }