-
Notifications
You must be signed in to change notification settings - Fork 212
Adding tests for Resource Cluster Actor #169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 165 additions & 0 deletions
165
...ane-server/src/test/java/io/mantisrx/master/resourcecluster/ResourceClusterActorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| /* | ||
| * Copyright 2022 Netflix, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package io.mantisrx.master.resourcecluster; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import akka.actor.ActorRef; | ||
| import akka.actor.ActorSystem; | ||
| import akka.actor.Props; | ||
| import akka.testkit.javadsl.TestKit; | ||
| import io.mantisrx.common.Ack; | ||
| import io.mantisrx.common.WorkerPorts; | ||
| import io.mantisrx.runtime.MachineDefinition; | ||
| import io.mantisrx.server.core.TestingRpcService; | ||
| import io.mantisrx.server.core.domain.WorkerId; | ||
| import io.mantisrx.server.master.persistence.MantisJobStore; | ||
| import io.mantisrx.server.master.resourcecluster.ClusterID; | ||
| import io.mantisrx.server.master.resourcecluster.ResourceCluster; | ||
| import io.mantisrx.server.master.resourcecluster.ResourceClusterTaskExecutorMapper; | ||
| import io.mantisrx.server.master.resourcecluster.TaskExecutorHeartbeat; | ||
| import io.mantisrx.server.master.resourcecluster.TaskExecutorID; | ||
| import io.mantisrx.server.master.resourcecluster.TaskExecutorRegistration; | ||
| import io.mantisrx.server.master.resourcecluster.TaskExecutorReport; | ||
| import io.mantisrx.server.worker.TaskExecutorGateway; | ||
| import io.mantisrx.shaded.com.google.common.collect.ImmutableList; | ||
| import java.time.Clock; | ||
| import java.time.Duration; | ||
| import org.junit.AfterClass; | ||
| import org.junit.Before; | ||
| import org.junit.BeforeClass; | ||
| import org.junit.Test; | ||
| import org.mockito.Matchers; | ||
|
|
||
| public class ResourceClusterActorTest { | ||
| private static final TaskExecutorID TASK_EXECUTOR_ID = TaskExecutorID.of("taskExecutorId"); | ||
| private static final String TASK_EXECUTOR_ADDRESS = "address"; | ||
| private static final ClusterID CLUSTER_ID = ClusterID.of("clusterId"); | ||
| private static final Duration heartbeatTimeout = Duration.ofSeconds(10); | ||
| private static final Duration assignmentTimeout = Duration.ofSeconds(1); | ||
| private static final String HOST_NAME = "hostname"; | ||
| private static final WorkerPorts WORKER_PORTS = new WorkerPorts(1, 2, 3, 4, 5); | ||
| private static final MachineDefinition MACHINE_DEFINITION = | ||
| new MachineDefinition(2f, 2014, 128.0, 1024, 1); | ||
| private static final TaskExecutorRegistration TASK_EXECUTOR_REGISTRATION = | ||
| new TaskExecutorRegistration( | ||
| TASK_EXECUTOR_ID, | ||
| CLUSTER_ID, | ||
| TASK_EXECUTOR_ADDRESS, | ||
| HOST_NAME, | ||
| WORKER_PORTS, | ||
| MACHINE_DEFINITION); | ||
| private static final WorkerId WORKER_ID = | ||
| WorkerId.fromIdUnsafe("late-sine-function-tutorial-1-worker-0-1"); | ||
|
|
||
| static ActorSystem actorSystem; | ||
|
|
||
| private final TestingRpcService rpcService = new TestingRpcService(); | ||
| private final TaskExecutorGateway gateway = mock(TaskExecutorGateway.class); | ||
|
|
||
| private MantisJobStore mantisJobStore; | ||
| private ResourceClusterTaskExecutorMapper mapper; | ||
| private ActorRef resourceClusterActor; | ||
| private ResourceCluster resourceCluster; | ||
|
|
||
| @BeforeClass | ||
| public static void setup() { | ||
| actorSystem = ActorSystem.create(); | ||
| } | ||
|
|
||
| @AfterClass | ||
| public static void teardown() { | ||
| TestKit.shutdownActorSystem(actorSystem); | ||
| actorSystem = null; | ||
| } | ||
|
|
||
| @Before | ||
| public void setupRpcService() { | ||
| rpcService.registerGateway(TASK_EXECUTOR_ADDRESS, gateway); | ||
| mantisJobStore = mock(MantisJobStore.class); | ||
| mapper = ResourceClusterTaskExecutorMapper.inMemory(); | ||
| } | ||
|
|
||
| @Before | ||
| public void setupActor() { | ||
| final Props props = | ||
| ResourceClusterActor.props( | ||
| CLUSTER_ID, | ||
| heartbeatTimeout, | ||
| assignmentTimeout, | ||
| Clock.systemDefaultZone(), | ||
| rpcService, | ||
| mantisJobStore); | ||
|
|
||
| resourceClusterActor = actorSystem.actorOf(props); | ||
| resourceCluster = | ||
| new ResourceClusterAkkaImpl( | ||
| resourceClusterActor, | ||
| Duration.ofSeconds(1), | ||
| CLUSTER_ID, | ||
| mapper); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRegistration() throws Exception { | ||
| assertEquals(Ack.getInstance(), resourceCluster.registerTaskExecutor(TASK_EXECUTOR_REGISTRATION).get()); | ||
| assertEquals(ImmutableList.of(TASK_EXECUTOR_ID), resourceCluster.getRegisteredTaskExecutors().get()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testInitializationAfterRestart() throws Exception { | ||
| when(mantisJobStore.getTaskExecutor(Matchers.eq(TASK_EXECUTOR_ID))).thenReturn(TASK_EXECUTOR_REGISTRATION); | ||
| assertEquals( | ||
| Ack.getInstance(), | ||
| resourceCluster.initializeTaskExecutor(TASK_EXECUTOR_ID, WORKER_ID).get()); | ||
| assertEquals(ImmutableList.of(TASK_EXECUTOR_ID), resourceCluster.getBusyTaskExecutors().get()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetFreeTaskExecutors() throws Exception { | ||
| assertEquals(Ack.getInstance(), resourceCluster.registerTaskExecutor(TASK_EXECUTOR_REGISTRATION).get()); | ||
| assertEquals(Ack.getInstance(), | ||
| resourceCluster | ||
| .heartBeatFromTaskExecutor( | ||
| new TaskExecutorHeartbeat( | ||
| TASK_EXECUTOR_ID, | ||
| CLUSTER_ID, | ||
| TaskExecutorReport.available())).get()); | ||
| assertEquals(TASK_EXECUTOR_ID, resourceCluster.getTaskExecutorFor(MACHINE_DEFINITION, WORKER_ID).get()); | ||
| assertEquals(ImmutableList.of(), resourceCluster.getAvailableTaskExecutors().get()); | ||
| assertEquals(ImmutableList.of(TASK_EXECUTOR_ID), resourceCluster.getRegisteredTaskExecutors().get()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testAssignmentTimeout() throws Exception { | ||
| assertEquals(Ack.getInstance(), resourceCluster.registerTaskExecutor(TASK_EXECUTOR_REGISTRATION).get()); | ||
| assertEquals(Ack.getInstance(), | ||
| resourceCluster | ||
| .heartBeatFromTaskExecutor( | ||
| new TaskExecutorHeartbeat( | ||
| TASK_EXECUTOR_ID, | ||
| CLUSTER_ID, | ||
| TaskExecutorReport.available())).get()); | ||
| assertEquals(TASK_EXECUTOR_ID, resourceCluster.getTaskExecutorFor(MACHINE_DEFINITION, WORKER_ID).get()); | ||
| assertEquals(ImmutableList.of(), resourceCluster.getAvailableTaskExecutors().get()); | ||
| Thread.sleep(2000); | ||
| assertEquals(ImmutableList.of(TASK_EXECUTOR_ID), resourceCluster.getAvailableTaskExecutors().get()); | ||
| assertEquals(TASK_EXECUTOR_ID, resourceCluster.getTaskExecutorFor(MACHINE_DEFINITION, WORKER_ID).get()); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this be done using a mock Clock?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if I can make Akka schedule something (such as the assignment timeout in this case) based on a mock clock. Let me take a look into this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be great if we could. All these
Thread.sleep()start to add up over time.