ServiceJWTManager extends AbstractService { public String getMasterToken() { return masterToken.get(); } ServiceJWTManager(String name); void setHTTPClient(CloseableHttpClient client); String getMasterToken(); static final String JWT_MANAGER_PREFIX; static final String JWT_MANAGER_MASTER_TOKEN_KEY; static final String JWT_MANAGER_RENEW_TOKEN_PATTERN; }
@Test public void onlyOneRenewerShouldRun() throws Exception { int numOfRenewTokens = 5; LocalDateTime now = DateUtils.getNow(); LocalDateTime expiresAt = now.plus(10L, ChronoUnit.MINUTES); Pair<String, String[]> tokens = generateMasterAndRenewTokens(numOfRenewTokens, now, expiresAt); conf.set(CommonConfigurationKeys.JWT_MANAGER_MASTER_TOKEN_VALIDITY_PERIOD, "2s"); ServiceJWTManager jwtManager0 = new TestingServiceJWTManager("TestingServiceJWTMgm0", tokens.getFirst(), tokens.getSecond(), expiresAt); jwtManager0.init(conf); jwtManager = new TestingServiceJWTManager("TestingServiceJWTMgm1", tokens.getFirst(), tokens.getSecond(), expiresAt); jwtManager.init(conf); String initialJWTManagerMasterToken = jwtManager.getMasterToken(); Assert.assertNotEquals("", initialJWTManagerMasterToken); jwtManager0.start(); int secondsWaited = 0; while (!((TestingServiceJWTManager) jwtManager0).renewed && secondsWaited++ < 5) { TimeUnit.SECONDS.sleep(1); } Assert.assertTrue(((TestingServiceJWTManager) jwtManager0).renewed); jwtManager.start(); secondsWaited = 0; while (!((TestingServiceJWTManager)jwtManager).tried2lock && secondsWaited++ < 5) { TimeUnit.SECONDS.sleep(1); } Assert.assertTrue(((TestingServiceJWTManager) jwtManager).tried2lock); Assert.assertFalse(((TestingServiceJWTManager) jwtManager).renewed); TimeUnit.SECONDS.sleep(conf.getTimeDuration(CommonConfigurationKeys.JWT_MANAGER_MASTER_TOKEN_VALIDITY_PERIOD, 2L, TimeUnit.SECONDS)); Assert.assertNotEquals(initialJWTManagerMasterToken, jwtManager.getMasterToken()); Assert.assertEquals(tokens.getFirst(), jwtManager.getMasterToken()); jwtManager0.stop(); secondsWaited = 0; while (!((TestingServiceJWTManager) jwtManager).renewed && secondsWaited++ < 5) { TimeUnit.SECONDS.sleep(1); } Assert.assertTrue(((TestingServiceJWTManager) jwtManager).renewed); }