use of org.apache.kafka.common.utils.MockScheduler in project apache-kafka-on-k8s by banzaicloud.
the class CoordinatorTest method testCreateTask.
@Test
public void testCreateTask() throws Exception {
MockTime time = new MockTime(0, 0, 0);
Scheduler scheduler = new MockScheduler(time);
try (MiniTrogdorCluster cluster = new MiniTrogdorCluster.Builder().addCoordinator("node01").addAgent("node02").scheduler(scheduler).build()) {
new ExpectedTasks().waitFor(cluster.coordinatorClient());
NoOpTaskSpec fooSpec = new NoOpTaskSpec(1, 2);
cluster.coordinatorClient().createTask(new CreateTaskRequest("foo", fooSpec));
new ExpectedTasks().addTask(new ExpectedTaskBuilder("foo").taskState(new TaskPending(fooSpec)).build()).waitFor(cluster.coordinatorClient());
time.sleep(2);
new ExpectedTasks().addTask(new ExpectedTaskBuilder("foo").taskState(new TaskRunning(fooSpec, 2)).workerState(new WorkerRunning(fooSpec, 2, "")).build()).waitFor(cluster.coordinatorClient()).waitFor(cluster.agentClient("node02"));
time.sleep(3);
new ExpectedTasks().addTask(new ExpectedTaskBuilder("foo").taskState(new TaskDone(fooSpec, 2, 5, "", false)).build()).waitFor(cluster.coordinatorClient());
}
}
use of org.apache.kafka.common.utils.MockScheduler 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.MockScheduler 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.MockScheduler in project kafka by apache.
the class CoordinatorTest method testTasksRequest.
@Test
public void testTasksRequest() throws Exception {
MockTime time = new MockTime(0, 0, 0);
Scheduler scheduler = new MockScheduler(time);
try (MiniTrogdorCluster cluster = new MiniTrogdorCluster.Builder().addCoordinator("node01").addAgent("node02").scheduler(scheduler).build()) {
CoordinatorClient coordinatorClient = cluster.coordinatorClient();
new ExpectedTasks().waitFor(coordinatorClient);
NoOpTaskSpec fooSpec = new NoOpTaskSpec(1, 10);
NoOpTaskSpec barSpec = new NoOpTaskSpec(3, 1);
coordinatorClient.createTask(new CreateTaskRequest("foo", fooSpec));
coordinatorClient.createTask(new CreateTaskRequest("bar", barSpec));
new ExpectedTasks().addTask(new ExpectedTaskBuilder("foo").taskState(new TaskPending(fooSpec)).build()).addTask(new ExpectedTaskBuilder("bar").taskState(new TaskPending(barSpec)).build()).waitFor(coordinatorClient);
assertEquals(0, coordinatorClient.tasks(new TasksRequest(null, 10, 0, 10, 0, Optional.empty())).tasks().size());
TasksResponse resp1 = coordinatorClient.tasks(new TasksRequest(Arrays.asList("foo", "baz"), 0, 0, 0, 0, Optional.empty()));
assertTrue(resp1.tasks().containsKey("foo"));
assertFalse(resp1.tasks().containsKey("bar"));
assertEquals(1, resp1.tasks().size());
time.sleep(2);
new ExpectedTasks().addTask(new ExpectedTaskBuilder("foo").taskState(new TaskRunning(fooSpec, 2, new TextNode("active"))).workerState(new WorkerRunning("foo", fooSpec, 2, new TextNode("active"))).build()).addTask(new ExpectedTaskBuilder("bar").taskState(new TaskPending(barSpec)).build()).waitFor(coordinatorClient).waitFor(cluster.agentClient("node02"));
TasksResponse resp2 = coordinatorClient.tasks(new TasksRequest(null, 1, 0, 0, 0, Optional.empty()));
assertTrue(resp2.tasks().containsKey("foo"));
assertFalse(resp2.tasks().containsKey("bar"));
assertEquals(1, resp2.tasks().size());
assertEquals(0, coordinatorClient.tasks(new TasksRequest(null, 3, 0, 0, 0, Optional.empty())).tasks().size());
}
}
use of org.apache.kafka.common.utils.MockScheduler in project kafka by apache.
the class CoordinatorTest method testTaskRequestWithOldStartMsGetsUpdated.
@Test
public void testTaskRequestWithOldStartMsGetsUpdated() throws Exception {
MockTime time = new MockTime(0, 0, 0);
Scheduler scheduler = new MockScheduler(time);
try (MiniTrogdorCluster cluster = new MiniTrogdorCluster.Builder().addCoordinator("node01").addAgent("node02").scheduler(scheduler).build()) {
NoOpTaskSpec fooSpec = new NoOpTaskSpec(1, 500);
time.sleep(552);
CoordinatorClient coordinatorClient = cluster.coordinatorClient();
NoOpTaskSpec updatedSpec = new NoOpTaskSpec(552, 500);
coordinatorClient.createTask(new CreateTaskRequest("fooSpec", fooSpec));
TaskState expectedState = new ExpectedTaskBuilder("fooSpec").taskState(new TaskRunning(updatedSpec, 552, new TextNode("receiving"))).build().taskState();
TaskState resp = coordinatorClient.task(new TaskRequest("fooSpec"));
assertEquals(expectedState, resp);
}
}
Aggregations