use of org.apache.kafka.common.utils.MockTime in project kafka by apache.
the class RetryTest method testUseMaxTimeout.
@Test
public void testUseMaxTimeout() throws IOException {
Exception[] attempts = new Exception[] { new IOException("pretend connect error"), new IOException("pretend timeout error"), new IOException("pretend read error") };
long retryWaitMs = 5000;
long maxWaitMs = 5000;
Retryable<String> call = createRetryable(attempts);
Time time = new MockTime(0, 0, 0);
assertEquals(0L, time.milliseconds());
Retry<String> r = new Retry<>(time, retryWaitMs, maxWaitMs);
assertThrows(ExecutionException.class, () -> r.execute(call));
assertEquals(maxWaitMs, time.milliseconds());
}
use of org.apache.kafka.common.utils.MockTime in project kafka by apache.
the class ExpiringCredentialRefreshingLoginTest method testRefreshWithExpirationSmallerThanConfiguredBuffers.
@Test
public void testRefreshWithExpirationSmallerThanConfiguredBuffers() throws Exception {
int numExpectedRefreshes = 1;
boolean clientReloginAllowedBeforeLogout = true;
final LoginContext mockLoginContext = mock(LoginContext.class);
Subject subject = new Subject();
when(mockLoginContext.getSubject()).thenReturn(subject);
MockTime mockTime = new MockTime();
long startMs = mockTime.milliseconds();
/*
* Identify the lifetime of each expiring credential
*/
long lifetimeMinutes = 10L;
/*
* Identify the point at which refresh will occur in that lifetime
*/
long refreshEveryMinutes = 8L;
/*
* Set an absolute last refresh time that will cause the login thread to exit
* after a certain number of re-logins (by adding an extra half of a refresh
* interval).
*/
long absoluteLastRefreshMs = startMs + (1 + numExpectedRefreshes) * 1000 * 60 * refreshEveryMinutes - 1000 * 60 * refreshEveryMinutes / 2;
/*
* Identify buffer time on either side for the refresh algorithm that will cause
* the entire lifetime to be taken up. In other words, make sure there is no way
* to honor the buffers.
*/
short minPeriodSeconds = (short) (1 + lifetimeMinutes * 60 / 2);
short bufferSeconds = minPeriodSeconds;
/*
* Define some listeners so we can keep track of who gets done and when. All
* added listeners should end up done except the last, extra one, which should
* not.
*/
MockScheduler mockScheduler = new MockScheduler(mockTime);
List<KafkaFutureImpl<Long>> waiters = addWaiters(mockScheduler, 1000 * 60 * refreshEveryMinutes, numExpectedRefreshes + 1);
// Create the ExpiringCredentialRefreshingLogin instance under test
TestLoginContextFactory testLoginContextFactory = new TestLoginContextFactory();
TestExpiringCredentialRefreshingLogin testExpiringCredentialRefreshingLogin = new TestExpiringCredentialRefreshingLogin(refreshConfigThatPerformsReloginEveryGivenPercentageOfLifetime(1.0 * refreshEveryMinutes / lifetimeMinutes, minPeriodSeconds, bufferSeconds, clientReloginAllowedBeforeLogout), testLoginContextFactory, mockTime, 1000 * 60 * lifetimeMinutes, absoluteLastRefreshMs, clientReloginAllowedBeforeLogout);
testLoginContextFactory.configure(mockLoginContext, testExpiringCredentialRefreshingLogin);
/*
* Perform the login, wait up to a certain amount of time for the refresher
* thread to exit, and make sure the correct calls happened at the correct times
*/
long expectedFinalMs = startMs + numExpectedRefreshes * 1000 * 60 * refreshEveryMinutes;
assertFalse(testLoginContextFactory.refresherThreadStartedFuture().isDone());
assertFalse(testLoginContextFactory.refresherThreadDoneFuture().isDone());
testExpiringCredentialRefreshingLogin.login();
assertTrue(testLoginContextFactory.refresherThreadStartedFuture().isDone());
testLoginContextFactory.refresherThreadDoneFuture().get(1L, TimeUnit.SECONDS);
assertEquals(expectedFinalMs, mockTime.milliseconds());
for (int i = 0; i < numExpectedRefreshes; ++i) {
KafkaFutureImpl<Long> waiter = waiters.get(i);
assertTrue(waiter.isDone());
assertEquals((i + 1) * 1000 * 60 * refreshEveryMinutes, waiter.get().longValue() - startMs);
}
assertFalse(waiters.get(numExpectedRefreshes).isDone());
InOrder inOrder = inOrder(mockLoginContext);
inOrder.verify(mockLoginContext).login();
for (int i = 0; i < numExpectedRefreshes; ++i) {
inOrder.verify(mockLoginContext).login();
inOrder.verify(mockLoginContext).logout();
}
}
use of org.apache.kafka.common.utils.MockTime in project kafka by apache.
the class ExpiringCredentialRefreshingLoginTest method testRefreshWithMinPeriodIntrusion.
@Test
public void testRefreshWithMinPeriodIntrusion() throws Exception {
int numExpectedRefreshes = 1;
boolean clientReloginAllowedBeforeLogout = true;
Subject subject = new Subject();
final LoginContext mockLoginContext = mock(LoginContext.class);
when(mockLoginContext.getSubject()).thenReturn(subject);
MockTime mockTime = new MockTime();
long startMs = mockTime.milliseconds();
/*
* Identify the lifetime of each expiring credential
*/
long lifetimeMinutes = 10L;
/*
* Identify the point at which refresh will occur in that lifetime
*/
long refreshEveryMinutes = 8L;
/*
* Set an absolute last refresh time that will cause the login thread to exit
* after a certain number of re-logins (by adding an extra half of a refresh
* interval).
*/
long absoluteLastRefreshMs = startMs + (1 + numExpectedRefreshes) * 1000 * 60 * refreshEveryMinutes - 1000 * 60 * refreshEveryMinutes / 2;
/*
* Identify a minimum period that will cause the refresh time to be delayed a
* bit.
*/
int bufferIntrusionSeconds = 1;
short minPeriodSeconds = (short) (refreshEveryMinutes * 60 + bufferIntrusionSeconds);
short bufferSeconds = (short) 0;
/*
* Define some listeners so we can keep track of who gets done and when. All
* added listeners should end up done except the last, extra one, which should
* not.
*/
MockScheduler mockScheduler = new MockScheduler(mockTime);
List<KafkaFutureImpl<Long>> waiters = addWaiters(mockScheduler, 1000 * (60 * refreshEveryMinutes + bufferIntrusionSeconds), numExpectedRefreshes + 1);
// Create the ExpiringCredentialRefreshingLogin instance under test
TestLoginContextFactory testLoginContextFactory = new TestLoginContextFactory();
TestExpiringCredentialRefreshingLogin testExpiringCredentialRefreshingLogin = new TestExpiringCredentialRefreshingLogin(refreshConfigThatPerformsReloginEveryGivenPercentageOfLifetime(1.0 * refreshEveryMinutes / lifetimeMinutes, minPeriodSeconds, bufferSeconds, clientReloginAllowedBeforeLogout), testLoginContextFactory, mockTime, 1000 * 60 * lifetimeMinutes, absoluteLastRefreshMs, clientReloginAllowedBeforeLogout);
testLoginContextFactory.configure(mockLoginContext, testExpiringCredentialRefreshingLogin);
/*
* Perform the login, wait up to a certain amount of time for the refresher
* thread to exit, and make sure the correct calls happened at the correct times
*/
long expectedFinalMs = startMs + numExpectedRefreshes * 1000 * (60 * refreshEveryMinutes + bufferIntrusionSeconds);
assertFalse(testLoginContextFactory.refresherThreadStartedFuture().isDone());
assertFalse(testLoginContextFactory.refresherThreadDoneFuture().isDone());
testExpiringCredentialRefreshingLogin.login();
assertTrue(testLoginContextFactory.refresherThreadStartedFuture().isDone());
testLoginContextFactory.refresherThreadDoneFuture().get(1L, TimeUnit.SECONDS);
assertEquals(expectedFinalMs, mockTime.milliseconds());
for (int i = 0; i < numExpectedRefreshes; ++i) {
KafkaFutureImpl<Long> waiter = waiters.get(i);
assertTrue(waiter.isDone());
assertEquals((i + 1) * 1000 * (60 * refreshEveryMinutes + bufferIntrusionSeconds), waiter.get().longValue() - startMs);
}
assertFalse(waiters.get(numExpectedRefreshes).isDone());
InOrder inOrder = inOrder(mockLoginContext);
inOrder.verify(mockLoginContext).login();
for (int i = 0; i < numExpectedRefreshes; ++i) {
inOrder.verify(mockLoginContext).login();
inOrder.verify(mockLoginContext).logout();
}
}
use of org.apache.kafka.common.utils.MockTime in project kafka by apache.
the class OAuthBearerUnsecuredLoginCallbackHandlerTest method addsExtensions.
@Test
public void addsExtensions() throws IOException, UnsupportedCallbackException {
Map<String, String> options = new HashMap<>();
options.put("unsecuredLoginExtension_testId", "1");
OAuthBearerUnsecuredLoginCallbackHandler callbackHandler = createCallbackHandler(options, new MockTime());
SaslExtensionsCallback callback = new SaslExtensionsCallback();
callbackHandler.handle(new Callback[] { callback });
assertEquals("1", callback.extensions().map().get("testId"));
}
use of org.apache.kafka.common.utils.MockTime in project kafka by apache.
the class OAuthBearerUnsecuredLoginCallbackHandlerTest method throwsErrorOnInvalidExtensionValue.
@Test
public void throwsErrorOnInvalidExtensionValue() {
Map<String, String> options = new HashMap<>();
options.put("unsecuredLoginExtension_testId", "Çalifornia");
OAuthBearerUnsecuredLoginCallbackHandler callbackHandler = createCallbackHandler(options, new MockTime());
SaslExtensionsCallback callback = new SaslExtensionsCallback();
assertThrows(IOException.class, () -> callbackHandler.handle(new Callback[] { callback }));
}
Aggregations