/** * Run with simulated time * @deprecated use ``` * try (Time.SimulatedTime time = new Time.SimulatedTime()) { * ... * } * ``` * @param code what to run */ @Deprecated public static void withSimulatedTime(Runnable code) { try (SimulatedTime st = new SimulatedTime()) { code.run(); } }
@Test public void testLogResetResetsRootLoggerIfSet() { try (SimulatedTime t = new SimulatedTime()) { long past = Time.currentTimeMillis() - 1000; TreeMap<String, LogLevel> config = new TreeMap<>(); config.put(LogManager.ROOT_LOGGER_NAME, ll("DEBUG", "WARN", past)); AtomicReference<TreeMap<String, LogLevel>> atomConf = new AtomicReference<>(config); LogConfigManager underTest = spy(new LogConfigManagerUnderTest(atomConf)); underTest.resetLogLevels(); assertEquals(new TreeMap<>(), atomConf.get()); verify(underTest).setLoggerLevel(anyObject(), eq(LogManager.ROOT_LOGGER_NAME), eq("WARN")); } }
@Test public void testLogResetsNamedLoggersWithPastTimeout() { try (SimulatedTime t = new SimulatedTime()) { long past = Time.currentTimeMillis() - 1000; TreeMap<String, LogLevel> config = new TreeMap<>(); config.put("my_debug_logger", ll("DEBUG", "INFO", past)); config.put("my_info_logger", ll("INFO", "WARN", past)); config.put("my_error_logger", ll("ERROR", "INFO", past)); AtomicReference<TreeMap<String, LogLevel>> atomConf = new AtomicReference<>(config); LogConfigManager underTest = spy(new LogConfigManagerUnderTest(atomConf)); underTest.resetLogLevels(); assertEquals(new TreeMap<>(), atomConf.get()); verify(underTest).setLoggerLevel(anyObject(), eq("my_debug_logger"), eq("INFO")); verify(underTest).setLoggerLevel(anyObject(), eq("my_info_logger"), eq("WARN")); verify(underTest).setLoggerLevel(anyObject(), eq("my_error_logger"), eq("INFO")); } }
@Test public void testEmptyToEmpty() throws Exception { try (SimulatedTime t = new SimulatedTime(1010)) { AsyncLocalizer localizer = mock(AsyncLocalizer.class); LocalState state = mock(LocalState.class); BlobChangingCallback cb = mock(BlobChangingCallback.class); ContainerLauncher containerLauncher = mock(ContainerLauncher.class); ISupervisor iSuper = mock(ISupervisor.class); SlotMetrics slotMetrics = new SlotMetrics(new StormMetricsRegistry()); StaticState staticState = new StaticState(localizer, 1000, 1000, 1000, 1000, containerLauncher, "localhost", 8080, iSuper, state, cb, null, null, slotMetrics); DynamicState dynamicState = new DynamicState(null, null, null, slotMetrics); DynamicState nextState = Slot.handleEmpty(dynamicState, staticState); assertEquals(MachineState.EMPTY, nextState.state); assertTrue(Time.currentTimeMillis() > 1000); } }
@Test public void uploadedBlobPersistsMinimumTime() { Set<String> idleTopologies = new HashSet<>(); idleTopologies.add("topology1"); Map<String, Object> conf = new HashMap<>(); conf.put(DaemonConfig.NIMBUS_TOPOLOGY_BLOBSTORE_DELETION_DELAY_MS, 300000); try (Time.SimulatedTime t = new Time.SimulatedTime(null)) { Set<String> toDelete = Nimbus.getExpiredTopologyIds(idleTopologies, conf); Assert.assertTrue(toDelete.isEmpty()); Time.advanceTime(10 * 60 * 1000L); toDelete = Nimbus.getExpiredTopologyIds(idleTopologies, conf); Assert.assertTrue(toDelete.contains("topology1")); Assert.assertEquals(1, toDelete.size()); } } }
@Test public void testCannotContainMultipleSchedulesForId() { try (SimulatedTime time = new SimulatedTime()) { KafkaSpoutRetryExponentialBackoff retryService = createOneSecondWaitRetryService(); long offset = 0; KafkaSpoutMessageId msgId = retryService.getMessageId(testTopic, offset); msgId.incrementNumFails(); retryService.schedule(msgId); Time.advanceTime(500); boolean scheduled = retryService.schedule(msgId); retryService.remove(msgId); assertThat("The message should no longer be scheduled", retryService.isScheduled(msgId), is(false)); Time.advanceTime(500); assertThat("The message should not be ready for retry because it isn't scheduled", retryService.isReady(msgId), is(false)); } }
@Test public void testMaxDelay() { try (SimulatedTime time = new SimulatedTime()) { int maxDelaySecs = 2; KafkaSpoutRetryExponentialBackoff retryService = new KafkaSpoutRetryExponentialBackoff(TimeInterval.seconds(500), TimeInterval.seconds(0), 1, TimeInterval.seconds(maxDelaySecs)); long offset = 0; KafkaSpoutMessageId msgId = retryService.getMessageId(testTopic, offset); msgId.incrementNumFails(); retryService.schedule(msgId); Time.advanceTimeSecs(maxDelaySecs); assertThat("The message should be ready for retry after the max delay", retryService.isReady(msgId), is(true)); } }
@Test public void deltaSecsConvertsToSecondsTest() { try (SimulatedTime t = new SimulatedTime()) { int current = Time.currentTimeSecs(); Time.advanceTime(1000); Assert.assertEquals(Time.deltaSecs(current), 1); } }
@Test public void shouldNotAdvanceTimeTest() { try (SimulatedTime t = new SimulatedTime()) { long current = Time.currentTimeMillis(); Time.advanceTime(0); Assert.assertEquals(Time.deltaMs(current), 0); } }
@Test public void testSimulatedTime() throws Exception { assertThat(Time.isSimulating(), is(false)); try(SimulatedTime time = new SimulatedTime()) { assertThat(Time.isSimulating(), is(true)); } }
@Test public void testLogResetTriggersForPastTime() { try (SimulatedTime t = new SimulatedTime()) { long past = Time.currentTimeMillis() - 1000; TreeMap<String, LogLevel> config = new TreeMap<>(); config.put("foo", ll("INFO", "WARN", past)); AtomicReference<TreeMap<String, LogLevel>> atomConf = new AtomicReference<>(config); LogConfigManager underTest = new LogConfigManagerUnderTest(atomConf); underTest.resetLogLevels(); assertEquals(new TreeMap<>(), atomConf.get()); } }
@Test(expected = IllegalArgumentException.class) public void shouldThrowIfAttemptToAdvanceBackwardsTest() { try (SimulatedTime t = new SimulatedTime()) { Time.advanceTime(-1500); } }
@Test public void shouldAdvanceForwardTest() { try (SimulatedTime t = new SimulatedTime()) { long current = Time.currentTimeMillis(); Time.advanceTime(1000); Assert.assertEquals(Time.deltaMs(current), 1000); Time.advanceTime(500); Assert.assertEquals(Time.deltaMs(current), 1500); } }
@Test public void isSimulatingReturnsTrueDuringSimulationTest() { Assert.assertFalse(Time.isSimulating()); try (SimulatedTime t = new SimulatedTime()) { Assert.assertTrue(Time.isSimulating()); } }
@Test public void deltaSecsTruncatesFractionalSecondsTest() { try (SimulatedTime t = new SimulatedTime()) { int current = Time.currentTimeSecs(); Time.advanceTime(1500); Assert.assertEquals(Time.deltaSecs(current), 1, 0); } }
@BeforeEach public void setUp() { spoutConfig = createSpoutConfig(); consumerSpy = createConsumerSpy(); spout = new KafkaSpout<>(spoutConfig, createConsumerFactory(), new TopicAssigner()); simulatedTime = new Time.SimulatedTime(); }
@Test public void testLogResetShouldNotTriggerForFutureTime() { try (SimulatedTime t = new SimulatedTime()) { long theFuture = Time.currentTimeMillis() + 1000; TreeMap<String, LogLevel> config = new TreeMap<>(); config.put("foo", ll(theFuture)); AtomicReference<TreeMap<String, LogLevel>> atomConf = new AtomicReference<>(config); LogConfigManager underTest = new LogConfigManagerUnderTest(atomConf); underTest.resetLogLevels(); assertNotNull(atomConf.get()); } }
@Test public void testCanGetGroups() throws Exception { try (SimulatedTime t = new SimulatedTime()) { groupsMapping.prepare(topoConf); when(mockShell.execCommand(ShellUtils.getGroupsForUserCommand(TEST_USER_1))).thenReturn(TEST_TWO_GROUPS); Set<String> groups = groupsMapping.getGroups(TEST_USER_1); assertThat(groups, containsInAnyOrder(TEST_TWO_GROUPS.split(GROUP_SEPARATOR_REGEX))); } }
@Test public void testWillExpireCache() throws Exception { try (SimulatedTime t = new SimulatedTime()) { groupsMapping.prepare(topoConf); when(mockShell.execCommand(ShellUtils.getGroupsForUserCommand(TEST_USER_1))).thenReturn(TEST_TWO_GROUPS, TEST_NO_GROUPS); Set<String> firstGroups = groupsMapping.getGroups(TEST_USER_1); Time.advanceTimeSecs(CACHE_EXPIRATION_SECS * 2); Set<String> secondGroups = groupsMapping.getGroups(TEST_USER_1); assertThat(firstGroups, not(secondGroups)); assertThat(secondGroups, contains(TEST_NO_GROUPS)); } }
@Test public void testWillCacheGroups() throws Exception { try (SimulatedTime t = new SimulatedTime()) { groupsMapping.prepare(topoConf); when(mockShell.execCommand(ShellUtils.getGroupsForUserCommand(TEST_USER_1))).thenReturn(TEST_TWO_GROUPS, TEST_NO_GROUPS); Set<String> firstGroups = groupsMapping.getGroups(TEST_USER_1); Set<String> secondGroups = groupsMapping.getGroups(TEST_USER_1); assertThat(firstGroups, is(secondGroups)); } }