Search in sources :

Example 81 with MockTime

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());
}
Also used : MockTime(org.apache.kafka.common.utils.MockTime) Time(org.apache.kafka.common.utils.Time) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) IOException(java.io.IOException) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.jupiter.api.Test)

Example 82 with MockTime

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();
    }
}
Also used : MockScheduler(org.apache.kafka.common.utils.MockScheduler) InOrder(org.mockito.InOrder) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) Subject(javax.security.auth.Subject) LoginContext(javax.security.auth.login.LoginContext) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.jupiter.api.Test)

Example 83 with MockTime

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();
    }
}
Also used : MockScheduler(org.apache.kafka.common.utils.MockScheduler) InOrder(org.mockito.InOrder) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) Subject(javax.security.auth.Subject) LoginContext(javax.security.auth.login.LoginContext) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.jupiter.api.Test)

Example 84 with MockTime

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"));
}
Also used : SaslExtensionsCallback(org.apache.kafka.common.security.auth.SaslExtensionsCallback) HashMap(java.util.HashMap) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.jupiter.api.Test)

Example 85 with MockTime

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 }));
}
Also used : SaslExtensionsCallback(org.apache.kafka.common.security.auth.SaslExtensionsCallback) OAuthBearerTokenCallback(org.apache.kafka.common.security.oauthbearer.OAuthBearerTokenCallback) SaslExtensionsCallback(org.apache.kafka.common.security.auth.SaslExtensionsCallback) Callback(javax.security.auth.callback.Callback) HashMap(java.util.HashMap) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.jupiter.api.Test)

Aggregations

MockTime (org.apache.kafka.common.utils.MockTime)371 Test (org.junit.Test)158 Test (org.junit.jupiter.api.Test)134 HashMap (java.util.HashMap)103 Time (org.apache.kafka.common.utils.Time)102 Cluster (org.apache.kafka.common.Cluster)74 MockClient (org.apache.kafka.clients.MockClient)73 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)65 Node (org.apache.kafka.common.Node)57 Metrics (org.apache.kafka.common.metrics.Metrics)56 TopicPartition (org.apache.kafka.common.TopicPartition)55 LogContext (org.apache.kafka.common.utils.LogContext)50 StreamsConfig (org.apache.kafka.streams.StreamsConfig)49 Before (org.junit.Before)38 Properties (java.util.Properties)34 Metadata (org.apache.kafka.clients.Metadata)33 MockScheduler (org.apache.kafka.common.utils.MockScheduler)31 MetadataResponse (org.apache.kafka.common.requests.MetadataResponse)30 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)30 ProducerMetadata (org.apache.kafka.clients.producer.internals.ProducerMetadata)29