/** Test of resetWindow functionality of WindowGenerator. */ @Test public void testResetWindow() { ManualScheduledExecutorService msse = new ManualScheduledExecutorService(1); msse.setCurrentTimeMillis(0x0afebabe * 1000L); WindowGenerator generator = new WindowGenerator(msse, WindowGenerator.MAX_WINDOW_ID + 1024); final long currentTIme = msse.getCurrentTimeMillis(); final int windowWidth = 0x1234abcd; generator.setFirstWindow(currentTIme); generator.setResetWindow(currentTIme); generator.setWindowWidth(windowWidth); SweepableReservoir reservoir = generator.acquireReservoir(Node.OUTPUT, 1024); reservoir.setSink( new Sink<Object>() { boolean firsttime = true; @Override public int getCount(boolean reset) { return 0; } @Override public void put(Object payload) { assert (false); if (firsttime) { assert (payload instanceof ResetWindowTuple); firsttime = false; } else { assert (payload instanceof Tuple); } } }); generator.activate(null); msse.tick(1); Assert.assertNull(reservoir.sweep()); ResetWindowTuple rwt = (ResetWindowTuple) reservoir.sweep(); reservoir.remove(); assert (rwt.getWindowId() == 0x0afebabe00000000L); assert (rwt.getBaseSeconds() * 1000L == currentTIme); assert (rwt.getIntervalMillis() == windowWidth); Tuple t = reservoir.sweep(); reservoir.remove(); assert (t.getType() == MessageType.BEGIN_WINDOW); assert (t.getWindowId() == 0x0afebabe00000000L); assert (reservoir.sweep() == null); }
@Test public void testWindowGen() throws Exception { final AtomicLong currentWindow = new AtomicLong(); final AtomicInteger beginWindowCount = new AtomicInteger(); final AtomicInteger endWindowCount = new AtomicInteger(); final AtomicLong windowXor = new AtomicLong(); Sink<Object> s = new Sink<Object>() { @Override public int getCount(boolean reset) { return 0; } @Override public void put(Object payload) { logger.debug("unexpected payload {}", payload); } }; ScheduledThreadPoolExecutor stpe = new ScheduledThreadPoolExecutor(1, "WindowGenerator"); int windowWidth = 200; long firstWindowMillis = stpe.getCurrentTimeMillis(); firstWindowMillis -= firstWindowMillis % 1000L; WindowGenerator wg = new WindowGenerator( new ScheduledThreadPoolExecutor(1, "WindowGenerator"), WindowGenerator.MAX_WINDOW_ID + 1024); wg.setResetWindow(firstWindowMillis); wg.setFirstWindow(firstWindowMillis); wg.setWindowWidth(windowWidth); SweepableReservoir reservoir = wg.acquireReservoir("GeneratorTester", windowWidth); reservoir.setSink(s); wg.activate(null); Thread.sleep(200); wg.deactivate(); reservoir.sweep(); /* just transfer over all the control tuples */ Tuple t; while ((t = reservoir.sweep()) != null) { reservoir.remove(); long windowId = t.getWindowId(); switch (t.getType()) { case BEGIN_WINDOW: currentWindow.set(windowId); beginWindowCount.incrementAndGet(); windowXor.set(windowXor.get() ^ windowId); break; case END_WINDOW: endWindowCount.incrementAndGet(); windowXor.set(windowXor.get() ^ windowId); break; case RESET_WINDOW: break; default: currentWindow.set(0); break; } } long lastWindowMillis = System.currentTimeMillis(); Assert.assertEquals("only last window open", currentWindow.get(), windowXor.get()); long expectedCnt = (lastWindowMillis - firstWindowMillis) / windowWidth; Assert.assertTrue("Minimum begin window count", expectedCnt + 1 <= beginWindowCount.get()); Assert.assertEquals("end window count", beginWindowCount.get() - 1, endWindowCount.get()); }