private static RenewAction<?> getActionFromTokenAspect( TokenAspect<DummyFs> tokenAspect) { return (RenewAction<?>) Whitebox.getInternalState(tokenAspect, "action"); }
@Test public void testRenewal() throws Exception { Configuration conf = new Configuration(); Token<?> token1 = mock(Token.class); Token<?> token2 = mock(Token.class); final long renewCycle = 100; DelegationTokenRenewer.renewCycle = renewCycle; UserGroupInformation ugi = UserGroupInformation.createUserForTesting("foo", new String[] { "bar" }); DummyFs fs = spy(new DummyFs()); doReturn(token1).doReturn(token2).when(fs).getDelegationToken(null); doReturn(token1).when(fs).getRenewToken(); // cause token renewer to abandon the token doThrow(new IOException("renew failed")).when(token1).renew(conf); doThrow(new IOException("get failed")).when(fs).addDelegationTokens(null, null); final URI uri = new URI("dummyfs://127.0.0.1:1234"); TokenAspect<DummyFs> tokenAspect = new TokenAspect<DummyFs>(fs, SecurityUtil.buildTokenService(uri), DummyFs.TOKEN_KIND); fs.initialize(uri, conf); tokenAspect.initDelegationToken(ugi); // trigger token acquisition tokenAspect.ensureTokenInitialized(); DelegationTokenRenewer.RenewAction<?> action = getActionFromTokenAspect(tokenAspect); verify(fs).setDelegationToken(token1); assertTrue(action.isValid()); // upon renewal, token will go bad based on above stubbing Thread.sleep(renewCycle * 2); assertSame(action, getActionFromTokenAspect(tokenAspect)); assertFalse(action.isValid()); // now that token is invalid, should get a new one tokenAspect.ensureTokenInitialized(); verify(fs, times(2)).getDelegationToken(anyString()); verify(fs).setDelegationToken(token2); assertNotSame(action, getActionFromTokenAspect(tokenAspect)); action = getActionFromTokenAspect(tokenAspect); assertTrue(action.isValid()); }
@Test public void testGetTokenAfterFailure() throws Exception { Configuration conf = mock(Configuration.class); Token<?> token1 = mock(Token.class); Token<?> token2 = mock(Token.class); long renewCycle = 1000; DelegationTokenRenewer.renewCycle = renewCycle; WebHdfsFileSystem fs = spy(new WebHdfsFileSystem()); doReturn(conf).when(fs).getConf(); doReturn(token1).doReturn(token2).when(fs).getDelegationToken(null); // cause token renewer to abandon the token doThrow(new IOException("renew failed")).when(token1).renew(conf); doThrow(new IOException("get failed")).when(fs).addDelegationTokens(null, null); // trigger token acquisition Token<?> token = fs.getDelegationToken(); RenewAction<?> action = fs.action; assertSame(token1, token); assertTrue(action.isValid()); // fetch again and make sure it's the same as before token = fs.getDelegationToken(); assertSame(token1, token); assertSame(action, fs.action); assertTrue(fs.action.isValid()); // upon renewal, token will go bad based on above stubbing Thread.sleep(renewCycle); assertSame(action, fs.action); assertFalse(fs.action.isValid()); // now that token is invalid, should get a new one token = fs.getDelegationToken(); assertSame(token2, token); assertNotSame(action, fs.action); assertTrue(fs.action.isValid()); action = fs.action; // should get same one again token = fs.getDelegationToken(); assertSame(token2, token); assertSame(action, fs.action); assertTrue(fs.action.isValid()); }