akka.testkit.TestActorRef Java Examples

The following examples show how to use akka.testkit.TestActorRef. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FlinkUntypedActorTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that LeaderSessionMessage messages with a wrong leader session ID are filtered
 * out.
 */
@Test
public void testLeaderSessionMessageFilteringOfFlinkUntypedActor() {
	final UUID leaderSessionID = UUID.randomUUID();
	final UUID oldSessionID = UUID.randomUUID();

	TestActorRef<PlainFlinkUntypedActor> actor = null;

	try {
		actor = TestActorRef.create(
				actorSystem, Props.create(PlainFlinkUntypedActor.class, leaderSessionID));

		final PlainFlinkUntypedActor underlyingActor = actor.underlyingActor();

		actor.tell(new JobManagerMessages.LeaderSessionMessage(leaderSessionID, 1), ActorRef.noSender());
		actor.tell(new JobManagerMessages.LeaderSessionMessage(oldSessionID, 2), ActorRef.noSender());
		actor.tell(new JobManagerMessages.LeaderSessionMessage(leaderSessionID, 2), ActorRef.noSender());
		actor.tell(1, ActorRef.noSender());

		assertEquals(3, underlyingActor.getMessageCounter());

	} finally {
		stopActor(actor);
	}
}
 
Example #2
Source File: AkkademyDbTest.java    From learning-akka with Apache License 2.0 6 votes vote down vote up
@Test
public void itShouldPlaceKeyValuesFromListOfSetMessageIntoMap() {
    TestProbe testProbe = TestProbe.apply(system);
    TestActorRef<AkkademyDb> actorRef = TestActorRef.create(system, Props.create(AkkademyDb.class));
    AkkademyDb akkademyDb = actorRef.underlyingActor();

    List list = Arrays.asList(
            new SetRequest("key2", "value2", testProbe.ref()),
            new SetRequest("key3", "value3", testProbe.ref()));
    actorRef.tell(list, ActorRef.noSender());

    assertEquals(akkademyDb.map.get("key2"), "value2");
    assertEquals(akkademyDb.map.get("key3"), "value3");

    testProbe.expectMsg(new Status.Success("key2"));
    testProbe.expectMsg(new Status.Success("key3"));
}
 
Example #3
Source File: ChatroomTest.java    From learning-akka with Apache License 2.0 6 votes vote down vote up
@Test
public void testShouldAddUserToJoinedUsersWhenJoiningTest() {
    //Given a Chatroom has no users
    Props props = Props.create(Chatroom.class);
    TestActorRef<Chatroom> ref = TestActorRef.create(system, props);
    Chatroom chatroom = ref.underlyingActor();
    assertEquals(chatroom.joinedUsers.size(), 0);

    //When it receives a request from a user to join the chatroom
    UserRef userRef = new UserRef(system.deadLetters(), "user");
    Messages.JoinChatroom request = new Messages.JoinChatroom(userRef);
    ref.tell(request, system.deadLetters());

    //It should add the UserRef to its list of joined users
    assertEquals(chatroom.joinedUsers.get(0), userRef);
}
 
Example #4
Source File: ChatroomTest.java    From learning-akka with Apache License 2.0 6 votes vote down vote up
@Test
public void testShouldSendHistoryWhenUserJoin() {
    new JavaTestKit(system) {{
        //Given
        Props props = Props.create(Chatroom.class);
        TestActorRef<Chatroom> ref = TestActorRef.create(system, props);
        Chatroom chatroom = ref.underlyingActor();
        Messages.PostToChatroom msg = new Messages.PostToChatroom("test", "user");
        chatroom.chatHistory.add(msg);

        //When
        UserRef userRef = new UserRef(system.deadLetters(), "user");
        Messages.JoinChatroom request = new Messages.JoinChatroom(userRef);
        ref.tell(request, getRef());

        //Then
        List expected = new ArrayList<Messages.PostToChatroom>();
        expected.add(msg);
        expectMsgEquals(duration("1 second"), expected);
    }};
}
 
Example #5
Source File: ChatroomTest.java    From learning-akka with Apache License 2.0 6 votes vote down vote up
@Test
public void testShouldSendUpdateWhenUserPosts() {
    //Given
    Props props = Props.create(Chatroom.class);
    TestActorRef<Chatroom> ref = TestActorRef.create(system, props);
    Chatroom chatroom = ref.underlyingActor();

    final TestProbe probe = new TestProbe(system);
    UserRef userRef = new UserRef(probe.ref(), "user");
    chatroom.joinChatroom(new Messages.JoinChatroom(userRef));

    //When
    Messages.PostToChatroom msg = new Messages.PostToChatroom("test", "user");
    ref.tell(msg, probe.ref());

    //Then
    probe.expectMsg(msg);
}
 
Example #6
Source File: ThingPersistenceActorTest.java    From ditto with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * The ThingPersistenceActor is created with a Thing ID. Any command it receives which belongs to a Thing with a
 * different ID should lead to an exception as the command was obviously sent to the wrong ThingPersistenceActor.
 */
@Test
public void tryToCreateThingWithDifferentThingId() {
    final ThingId thingIdOfActor = ThingId.of("test.ns", "23420815");
    final Thing thing = createThingV2WithRandomId();
    final CreateThing createThing = CreateThing.of(thing, null, dittoHeadersV2);

    final Props props = ThingPersistenceActor.props(thingIdOfActor, getDistributedPub());
    final TestActorRef<ThingPersistenceActor> underTest = TestActorRef.create(actorSystem, props);
    final ThingPersistenceActor thingPersistenceActor = underTest.underlyingActor();
    final PartialFunction<Object, BoxedUnit> receiveCommand = thingPersistenceActor.receiveCommand();

    try {
        receiveCommand.apply(createThing);
        fail("Expected IllegalArgumentException to be thrown.");
    } catch (final Exception e) {
        assertThat(e).isInstanceOf(IllegalArgumentException.class);
    }
}
 
Example #7
Source File: FSMClientActorTest.java    From learning-akka with Apache License 2.0 5 votes vote down vote up
@Test
public void itShouldFlushMessagesInConnectedAndPending() throws Exception {
    TestActorRef<FSMClientActor> fsmClientRef =
            TestActorRef.create(system, Props.create(FSMClientActor.class, dbRef.path().toString()));

    fsmClientRef.tell(new SetRequest("testkey", "testvalue", probe.ref()), probe.ref());

    assert(fsmClientRef.underlyingActor().stateName() == State.CONNECTED_AND_PENDING);

    fsmClientRef.tell(new FlushMsg(), probe.ref());

    probe.expectMsgClass(Status.Success.class);
    assert(fsmClientRef.underlyingActor().stateName() == State.CONNECTED);
}
 
Example #8
Source File: AkkademyDbTest.java    From learning-akka with Apache License 2.0 5 votes vote down vote up
@Test
public void itShouldPlaceKeyValueFromSetMessageIntoMap() {
    TestProbe testProbe = TestProbe.apply(system);
    TestActorRef<AkkademyDb> actorRef = TestActorRef.create(system, Props.create(AkkademyDb.class));
    AkkademyDb akkademyDb = actorRef.underlyingActor();

    actorRef.tell(new SetRequest("key", "value", testProbe.ref()), ActorRef.noSender());

    assertEquals(akkademyDb.map.get("key"), "value");
}
 
Example #9
Source File: ChatroomTest.java    From learning-akka with Apache License 2.0 5 votes vote down vote up
@Test
public void testShouldAddUserToJoinedUsersWhenJoiningUnitTest() {
    Props props = Props.create(Chatroom.class);
    TestActorRef<Chatroom> ref = TestActorRef.create(system, props);
    Chatroom chatroom = ref.underlyingActor();

    UserRef userRef = new UserRef(system.deadLetters(), "user");
    Messages.JoinChatroom request = new Messages.JoinChatroom(userRef);
    chatroom.joinChatroom(request);

    assertEquals(chatroom.joinedUsers.get(0), userRef);
}
 
Example #10
Source File: SetupDocumentTypeWorkerActorTest.java    From searchanalytics-bigdata with MIT License 5 votes vote down vote up
@Test
public void handleIndexDocumentTypeMessageVO() {
	final Props props = Props.create(SetupDocumentTypeWorkerActor.class,
			null, null);
	final TestActorRef<SetupDocumentTypeWorkerActor> ref = TestActorRef
			.create(system, props);
	final SetupDocumentTypeWorkerActor actor = ref.underlyingActor();
	// Mock the behavior of child/worker actors.
	TestProbe testProbeDataGeneratorWorker = TestProbe.apply(system);
	actor.setDataGeneratorWorkerRouter(testProbeDataGeneratorWorker.ref());
	ElasticSearchIndexConfig config = ElasticSearchIndexConfig.COM_WEBSITE;
	IndexDocumentType documentType = IndexDocumentType.PRODUCT;
	String indexName = "trialindexName";
	IndexDocumentTypeMessageVO indexDocumentTypeMessageVO = new IndexDocumentTypeMessageVO()
			.config(config).documentType(documentType)
			.newIndexName(indexName);
	TestProbe testProbeOriginalSender = TestProbe.apply(system);
	// Actor state check
	assertEquals(null, actor.getIndexDocumentType());
	ref.tell(indexDocumentTypeMessageVO, testProbeOriginalSender.ref());
	testProbeDataGeneratorWorker
			.expectMsgClass(IndexDocumentTypeMessageVO.class);
	TestActor.Message message = testProbeDataGeneratorWorker.lastMessage();
	IndexDocumentTypeMessageVO resultMsg = (IndexDocumentTypeMessageVO) message
			.msg();
	assertEquals(config, resultMsg.getConfig());
	assertEquals(documentType, resultMsg.getIndexDocumentType());
	assertEquals(indexName, resultMsg.getNewIndexName());
	assertEquals(documentType, actor.getIndexDocumentType());
}
 
Example #11
Source File: SetupDocumentTypeWorkerActorTest.java    From searchanalytics-bigdata with MIT License 5 votes vote down vote up
@Test
public void handleIndexDocumentVODocumentGeneration() {
	final Props props = Props.create(SetupDocumentTypeWorkerActor.class,
			null, null);
	final TestActorRef<SetupDocumentTypeWorkerActor> ref = TestActorRef
			.create(system, props);
	final SetupDocumentTypeWorkerActor actor = ref.underlyingActor();
	// Mock the behavior of child/worker actors.
	TestProbe testProbeDocumentGeneratorWorker = TestProbe.apply(system);
	actor.setDocumentGeneratorWorkerRouter(testProbeDocumentGeneratorWorker
			.ref());
	ElasticSearchIndexConfig config = ElasticSearchIndexConfig.COM_WEBSITE;
	IndexDocumentType documentType = IndexDocumentType.PRODUCT;
	String indexName = "trialindexName";
	Long documentId = 1l;
	IndexDocumentVO indexDocumentVO = new IndexDocumentVO().config(config)
			.documentType(documentType).newIndexName(indexName)
			.documentId(documentId);
	// Send data back to the sender, generate document
	assertEquals(0, actor.getTotalDocumentsToIndex());
	ref.tell(indexDocumentVO, null);
	testProbeDocumentGeneratorWorker.expectMsgClass(IndexDocumentVO.class);
	TestActor.Message messageDoc = testProbeDocumentGeneratorWorker
			.lastMessage();
	IndexDocumentVO resultMsgDoc = (IndexDocumentVO) messageDoc.msg();
	assertEquals(config, resultMsgDoc.getConfig());
	assertEquals(documentType, resultMsgDoc.getDocumentType());
	assertEquals(indexName, resultMsgDoc.getNewIndexName());
	assertEquals(documentId, resultMsgDoc.getDocumentId());
	assertEquals(0, actor.getTotalDocumentsToIndex());
}
 
Example #12
Source File: SetupDocumentTypeWorkerActorTest.java    From searchanalytics-bigdata with MIT License 5 votes vote down vote up
@Test
public void handleIndexDataSizeToIndex() {
	final Props props = Props.create(SetupDocumentTypeWorkerActor.class,
			null, null);
	final TestActorRef<SetupDocumentTypeWorkerActor> ref = TestActorRef
			.create(system, props);
	final SetupDocumentTypeWorkerActor actor = ref.underlyingActor();
	// Mock the behavior of child/worker actors.
	TestProbe testProbeDataGeneratorWorker = TestProbe.apply(system);
	actor.setDataGeneratorWorkerRouter(testProbeDataGeneratorWorker.ref());
	// Let's say total data to generate to 1
	ref.tell(Integer.valueOf(1), testProbeDataGeneratorWorker.ref());
	assertEquals(1, actor.getTotalDocumentsToIndex());
	assertEquals(0, actor.getTotalDocumentsToIndexDone());
}
 
Example #13
Source File: SetupDocumentTypeWorkerActorTest.java    From searchanalytics-bigdata with MIT License 5 votes vote down vote up
@Test
public void handleIndexDocumentVOIndexData() {
	final Props props = Props.create(SetupDocumentTypeWorkerActor.class,
			null, null);
	final TestActorRef<SetupDocumentTypeWorkerActor> ref = TestActorRef
			.create(system, props);
	final SetupDocumentTypeWorkerActor actor = ref.underlyingActor();
	// Mock the behavior of child/worker actors.
	TestProbe testProbeIndexDataWorker = TestProbe.apply(system);
	actor.setIndexDocumentWorkerRouter(testProbeIndexDataWorker.ref());
	ElasticSearchIndexConfig config = ElasticSearchIndexConfig.COM_WEBSITE;
	IndexDocumentType documentType = IndexDocumentType.PRODUCT;
	String indexName = "trialindexName";
	Long documentId = 1l;
	Product product = new Product();
	product.setId(documentId);
	IndexDocumentVO indexDocumentVO = new IndexDocumentVO().config(config)
			.documentType(documentType).newIndexName(indexName)
			.documentId(documentId);
	// send data back, index document.
	indexDocumentVO.product(product);
	// This is controlled for doc generation, won;t change here.
	assertEquals(0, actor.getTotalDocumentsToIndex());
	ref.tell(indexDocumentVO, null);
	testProbeIndexDataWorker.expectMsgClass(IndexDocumentVO.class);
	TestActor.Message messageDocIndex = testProbeIndexDataWorker
			.lastMessage();
	IndexDocumentVO resultMsgDocIndex = (IndexDocumentVO) messageDocIndex
			.msg();
	assertEquals(config, resultMsgDocIndex.getConfig());
	assertEquals(documentType, resultMsgDocIndex.getDocumentType());
	assertEquals(indexName, resultMsgDocIndex.getNewIndexName());
	assertEquals(documentId, resultMsgDocIndex.getDocumentId());
	assertEquals(product, resultMsgDocIndex.getProduct());
	assertEquals(0, actor.getTotalDocumentsToIndex());
}
 
Example #14
Source File: SetupDocumentTypeWorkerActorTest.java    From searchanalytics-bigdata with MIT License 5 votes vote down vote up
@Test
public void handleIndexDocumentVOIndexDone() {
	final Props props = Props.create(SetupDocumentTypeWorkerActor.class,
			null, null);
	final TestActorRef<SetupDocumentTypeWorkerActor> ref = TestActorRef
			.create(system, props);
	final SetupDocumentTypeWorkerActor actor = ref.underlyingActor();
	// Mock the behavior of child/worker actors.
	TestProbe testProbeIndexDataWorker = TestProbe.apply(system);
	actor.setIndexDocumentWorkerRouter(testProbeIndexDataWorker.ref());
	ElasticSearchIndexConfig config = ElasticSearchIndexConfig.COM_WEBSITE;
	IndexDocumentType documentType = IndexDocumentType.PRODUCT;
	String indexName = "trialindexName";
	Long documentId = 1l;
	Product product = new Product();
	product.setId(documentId);
	IndexDocumentVO indexDocumentVO = new IndexDocumentVO().config(config)
			.documentType(documentType).newIndexName(indexName)
			.documentId(documentId);
	// send data back, index document.
	indexDocumentVO.product(product);
	indexDocumentVO.indexDone(true);
	// Actor state check
	assertEquals(0, actor.getTotalDocumentsToIndexDone());
	ref.tell(indexDocumentVO, null);
	testProbeIndexDataWorker.expectNoMsg();
	;
	assertEquals(1, actor.getTotalDocumentsToIndexDone());
}
 
Example #15
Source File: SetupDocumentTypeWorkerActorTest.java    From searchanalytics-bigdata with MIT License 5 votes vote down vote up
@Test
public void handleUnhandledMessage() {
	final Props props = Props.create(SetupDocumentTypeWorkerActor.class,
			null, null);
	final TestActorRef<SetupDocumentTypeWorkerActor> ref = TestActorRef
			.create(system, props);
	final SetupDocumentTypeWorkerActor actor = ref.underlyingActor();
	// Mock the behavior of child/worker actors.
	TestProbe testProbeIndexDataWorker = TestProbe.apply(system);
	actor.setIndexDocumentWorkerRouter(testProbeIndexDataWorker.ref());
	// Actor state check
	assertEquals(null, actor.getIndexDocumentType());
	// Subscribe first
	TestProbe subscriber = TestProbe.apply(system);
	system.eventStream()
			.subscribe(subscriber.ref(), UnhandledMessage.class);
	// garbage
	String invalidMessage = "blah blah";
	ref.tell(invalidMessage, null);
	// Expect the unhandled message now.
	FiniteDuration duration = Duration.create(1, TimeUnit.SECONDS);
	subscriber.expectMsgClass(duration, UnhandledMessage.class);
	TestActor.Message message = subscriber.lastMessage();
	UnhandledMessage resultMsg = (UnhandledMessage) message.msg();
	assertEquals(invalidMessage, resultMsg.getMessage());
	// Actor state check
	assertEquals(null, actor.getIndexDocumentType());
}
 
Example #16
Source File: SetupDocumentTypeWorkerActorTest.java    From searchanalytics-bigdata with MIT License 5 votes vote down vote up
@Test
public void sendExceptionToParent() {
	final Props props = Props.create(SetupDocumentTypeWorkerActor.class,
			null, null);
	final TestActorRef<SetupDocumentTypeWorkerActor> ref = TestActorRef
			.create(system, props);
	final SetupDocumentTypeWorkerActor actor = ref.underlyingActor();
	// Mock the behavior of child/worker actors.
	TestProbe testProbeDocumentGeneratorWorker = TestProbe.apply(system);
	TestProbe testProbeIndexDataWorker = TestProbe.apply(system);
	// No data generator
	actor.setDataGeneratorWorkerRouter(null);
	actor.setDocumentGeneratorWorkerRouter(testProbeDocumentGeneratorWorker
			.ref());
	actor.setIndexDocumentWorkerRouter(testProbeIndexDataWorker.ref());
	ElasticSearchIndexConfig config = ElasticSearchIndexConfig.COM_WEBSITE;
	IndexDocumentType documentType = IndexDocumentType.PRODUCT;
	String indexName = "trialindexName";
	// no document type
	IndexDocumentTypeMessageVO indexDocumentTypeMessageVO = new IndexDocumentTypeMessageVO()
			.config(config).newIndexName(indexName)
			.documentType(documentType);
	assertEquals(0, actor.getTotalDocumentsToIndex());
	assertEquals(0, actor.getTotalDocumentsToIndexDone());
	assertEquals(null, actor.getIndexDocumentType());
	// parent Exception, NPE in data generator
	TestProbe testProbeParentActor = TestProbe.apply(system);
	String parentString = testProbeParentActor.ref().path().toString();
	actor.setParentActorPathString(parentString);
	ref.tell(indexDocumentTypeMessageVO, null);
	testProbeParentActor
			.expectMsgClass(DocumentTypeIndexingException.class);
	TestActor.Message testProbeParentActorMessage = testProbeParentActor
			.lastMessage();
	DocumentTypeIndexingException testProbeParentActorMessageType = (DocumentTypeIndexingException) testProbeParentActorMessage
			.msg();
	// doc type is not yet set
	assertEquals(documentType,
			testProbeParentActorMessageType.getIndexDocumentType());
}
 
Example #17
Source File: DataGeneratorWorkerActorTest.java    From searchanalytics-bigdata with MIT License 5 votes vote down vote up
@Test
public void testExceptionForInvalidDocumentTypeMessage() {
	final Props props = Props.create(DataGeneratorWorkerActor.class,
			sampleDataGeneratorService);
	final TestActorRef<DataGeneratorWorkerActor> ref = TestActorRef.create(
			system, props);

	ElasticSearchIndexConfig config = ElasticSearchIndexConfig.COM_WEBSITE;

	// invalid document type
	IndexDocumentType documentType = null;
	String indexName = "trialindexName";
	IndexDocumentTypeMessageVO indexDocumentTypeMessageVO = new IndexDocumentTypeMessageVO()
			.config(config).documentType(documentType)
			.newIndexName(indexName);

	replay(sampleDataGeneratorService);

	TestProbe testProbe = TestProbe.apply(system);
	ref.tell(indexDocumentTypeMessageVO, testProbe.ref());
	verify(sampleDataGeneratorService);

	testProbe.expectMsgClass(DocumentTypeDataGenerationException.class);
	TestActor.Message message = testProbe.lastMessage();
	DocumentTypeDataGenerationException resultMsg = (DocumentTypeDataGenerationException) message
			.msg();
	assertEquals(documentType, resultMsg.getIndexDocumentType());
}
 
Example #18
Source File: DataGeneratorWorkerActorTest.java    From searchanalytics-bigdata with MIT License 5 votes vote down vote up
@Test
public void testExceptionForInvalidMessage() {
	final Props props = Props.create(DataGeneratorWorkerActor.class,
			sampleDataGeneratorService);
	final TestActorRef<DataGeneratorWorkerActor> ref = TestActorRef.create(
			system, props);

	// Subscribe first
	TestProbe subscriber = TestProbe.apply(system);
	system.eventStream()
			.subscribe(subscriber.ref(), UnhandledMessage.class);

	replay(sampleDataGeneratorService);

	TestProbe testProbe = TestProbe.apply(system);
	// Send message
	String invalidMessage = "blah blah";
	ref.tell(invalidMessage, testProbe.ref());
	verify(sampleDataGeneratorService);

	// Expect the unhandled message now.
	FiniteDuration duration = Duration.create(1, TimeUnit.SECONDS);
	subscriber.expectMsgClass(duration, UnhandledMessage.class);
	TestActor.Message message = subscriber.lastMessage();
	UnhandledMessage resultMsg = (UnhandledMessage) message.msg();
	assertEquals(invalidMessage, resultMsg.getMessage());
}
 
Example #19
Source File: FSMClientActorTest.java    From learning-akka with Apache License 2.0 5 votes vote down vote up
@Test
public void itShouldTransitionToConnectedAndPending() throws Exception {
    TestActorRef<FSMClientActor> fsmClientRef =
            TestActorRef.create(system, Props.create(FSMClientActor.class, dbRef.path().toString()));

    assert(fsmClientRef.underlyingActor().stateName() == State.DISCONNECTED);
    fsmClientRef.tell(new GetRequest("testkey"), probe.ref());

    assert(fsmClientRef.underlyingActor().stateName() == State.CONNECTED_AND_PENDING);
}
 
Example #20
Source File: HotswapClientActorTest.java    From learning-akka with Apache License 2.0 5 votes vote down vote up
@Test
public void itShouldGet() throws Exception {
    TestActorRef<AkkademyDb> dbRef = TestActorRef.create(system, Props.create(AkkademyDb.class));
    AkkademyDb db = dbRef.underlyingActor();
    db.map.put("testkey", "testvalue");

    TestProbe probe = TestProbe.apply(system);
    TestActorRef<HotswapClientActor> clientRef =
            TestActorRef.create(system, Props.create(HotswapClientActor.class, dbRef.path().toString()));


    clientRef.tell(new GetRequest("testkey"), probe.ref());
    probe.expectMsg("testvalue");
}
 
Example #21
Source File: HotswapClientActorTest.java    From learning-akka with Apache License 2.0 5 votes vote down vote up
@Test
public void itShouldSet() throws Exception {
    TestActorRef<AkkademyDb> dbRef = TestActorRef.create(system, Props.create(AkkademyDb.class));
    AkkademyDb db = dbRef.underlyingActor();

    TestProbe probe = TestProbe.apply(system);
    TestActorRef<HotswapClientActor> clientRef =
            TestActorRef.create(system, Props.create(HotswapClientActor.class, dbRef.path().toString()));

    clientRef.tell(new SetRequest("testkey", "testvalue", probe.ref()), probe.ref());

    probe.expectMsg(new Status.Success("testkey"));
    assert(db.map.get("testkey") == "testvalue");
}
 
Example #22
Source File: AkkademyDbTest.java    From learning-akka with Apache License 2.0 5 votes vote down vote up
@Test
public void itShouldPlaceKeyValueFromSetMessageIntoMap() {
    TestActorRef<AkkademyDb> actorRef = TestActorRef.create(system, Props.create(AkkademyDb.class));
    actorRef.tell(new SetRequest("key", "value"), ActorRef.noSender());

    AkkademyDb akkademyDb = actorRef.underlyingActor();
    assertEquals(akkademyDb.map.get("key"), "value");
}
 
Example #23
Source File: AkkademyDbTest.java    From learning-akka with Apache License 2.0 5 votes vote down vote up
@Test
public void itShouldPlaceKeyValueFromSetMessageIntoMap() {
    TestActorRef<AkkademyDb> actorRef = TestActorRef.create(system, Props.create(AkkademyDb.class));
    actorRef.tell(new SetRequest("key", "value"), ActorRef.noSender());

    AkkademyDb akkademyDb = actorRef.underlyingActor();
    assertEquals(akkademyDb.map.get("key"), "value");
}
 
Example #24
Source File: WorkFlowExecutionControllerTest.java    From flux with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    Thread.sleep(1000);
    workFlowExecutionController = new WorkFlowExecutionController(eventsDAO, stateMachinesDAO, statesDAO, auditDAO,
            executionNodeTaskDispatcher, redriverRegistry, metricsClient, clientElbPersistenceService);
    when(stateMachinesDAO.findById(anyString())).thenReturn(TestUtils.getStandardTestMachineWithId());
    when(clientElbPersistenceService.findByIdClientElb(anyString())).thenReturn("http://localhost:9997");
    actorSystem = ActorSystem.create("testActorSystem",ConfigFactory.load("testAkkaActorSystem"));
    mockActor = TestActorRef.create(actorSystem, Props.create(MockActorRef.class));
    when(routerRegistry.getRouter(anyString())).thenReturn(mockActor);
    objectMapper = new ObjectMapper();
}
 
Example #25
Source File: FlinkUntypedActorTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that an exception is thrown, when the FlinkUntypedActore receives a message which
 * extends {@link RequiresLeaderSessionID} and is not wrapped in a LeaderSessionMessage.
 */
@Test
public void testThrowingExceptionWhenReceivingNonWrappedRequiresLeaderSessionIDMessage() {
	final UUID leaderSessionID = UUID.randomUUID();

	TestActorRef<PlainFlinkUntypedActor> actor = null;

	try {
		final Props props = Props.create(PlainFlinkUntypedActor.class, leaderSessionID);
		actor = TestActorRef.create(actorSystem, props);

		actor.receive(new JobManagerMessages.LeaderSessionMessage(leaderSessionID, 1));

		try {
			actor.receive(new PlainRequiresLeaderSessionID());

			fail("Expected an exception to be thrown, because a RequiresLeaderSessionID" +
					"message was sent without being wrapped in LeaderSessionMessage.");
		} catch (Exception e) {
			assertEquals("Received a message PlainRequiresLeaderSessionID " +
					"without a leader session ID, even though the message requires a " +
					"leader session ID.",
					e.getMessage());
		}

	} finally {
		stopActor(actor);
	}
}
 
Example #26
Source File: ThingCommandEnforcementTest.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
@Before
public void init() {
    system = ActorSystem.create("test", ConfigFactory.load("test"));
    final TestActorRef<MockEntitiesActor> testActorRef =
            new TestActorRef<>(system, MockEntitiesActor.props(), system.guardian(), UUID.randomUUID().toString());
    mockEntitiesActorInstance = testActorRef.underlyingActor();
    mockEntitiesActor = testActorRef;
}
 
Example #27
Source File: LiveSignalEnforcementTest.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
@Before
public void init() {
    system = ActorSystem.create("test", ConfigFactory.load("test"));
    final TestActorRef<MockEntitiesActor> testActorRef =
            new TestActorRef<>(system, MockEntitiesActor.props(), system.guardian(), UUID.randomUUID().toString());
    mockEntitiesActorInstance = testActorRef.underlyingActor();
    mockEntitiesActor = testActorRef;
}
 
Example #28
Source File: PreEnforcementTest.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
@Before
public void init() {
    system = ActorSystem.create("test", ConfigFactory.load("test"));
    final TestActorRef<MockEntitiesActor> testActorRef =
            new TestActorRef<>(system, MockEntitiesActor.props(), system.guardian(), UUID
                    .randomUUID().toString());
    mockEntitiesActorInstance = testActorRef.underlyingActor();
    mockEntitiesActor = testActorRef;
}
 
Example #29
Source File: MetricQueryServiceTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Test
public void testHandleOversizedMetricMessage() throws Exception {
	ActorSystem s = AkkaUtils.createLocalActorSystem(new Configuration());
	try {
		final long sizeLimit = 200L;
		ActorRef serviceActor = MetricQueryService.startMetricQueryService(s, null, sizeLimit);
		TestActorRef testActorRef = TestActorRef.create(s, Props.create(TestActor.class));
		TestActor testActor = (TestActor) testActorRef.underlyingActor();

		final TaskManagerMetricGroup tm = UnregisteredMetricGroups.createUnregisteredTaskManagerMetricGroup();

		final String gaugeValue = "Hello";
		final long requiredGaugesToExceedLimit = sizeLimit / gaugeValue.length() + 1;
		List<Tuple2<String, Gauge<String>>> gauges = LongStream.range(0, requiredGaugesToExceedLimit)
			.mapToObj(x -> Tuple2.of("gauge" + x, (Gauge<String>) () -> "Hello" + x))
			.collect(Collectors.toList());
		gauges.forEach(gauge -> MetricQueryService.notifyOfAddedMetric(serviceActor, gauge.f1, gauge.f0, tm));

		MetricQueryService.notifyOfAddedMetric(serviceActor, new SimpleCounter(), "counter", tm);
		MetricQueryService.notifyOfAddedMetric(serviceActor, new TestHistogram(), "histogram", tm);
		MetricQueryService.notifyOfAddedMetric(serviceActor, new TestMeter(), "meter", tm);

		serviceActor.tell(MetricQueryService.getCreateDump(), testActorRef);
		testActor.waitForResult();

		MetricDumpSerialization.MetricSerializationResult dump = testActor.getSerializationResult();

		assertTrue(dump.serializedCounters.length > 0);
		assertEquals(1, dump.numCounters);
		assertTrue(dump.serializedMeters.length > 0);
		assertEquals(1, dump.numMeters);

		// gauges exceeded the size limit and will be excluded
		assertEquals(0, dump.serializedGauges.length);
		assertEquals(0, dump.numGauges);

		assertTrue(dump.serializedHistograms.length > 0);
		assertEquals(1, dump.numHistograms);

		// unregister all but one gauge to ensure gauges are reported again if the remaining fit
		for (int x = 1; x < gauges.size(); x++) {
			MetricQueryService.notifyOfRemovedMetric(serviceActor, gauges.get(x).f1);
		}

		serviceActor.tell(MetricQueryService.getCreateDump(), testActorRef);
		testActor.waitForResult();

		MetricDumpSerialization.MetricSerializationResult recoveredDump = testActor.getSerializationResult();

		assertTrue(recoveredDump.serializedCounters.length > 0);
		assertEquals(1, recoveredDump.numCounters);
		assertTrue(recoveredDump.serializedMeters.length > 0);
		assertEquals(1, recoveredDump.numMeters);
		assertTrue(recoveredDump.serializedGauges.length > 0);
		assertEquals(1, recoveredDump.numGauges);
		assertTrue(recoveredDump.serializedHistograms.length > 0);
		assertEquals(1, recoveredDump.numHistograms);
	} finally {
		s.terminate();
	}
}
 
Example #30
Source File: DataGeneratorWorkerActorTest.java    From searchanalytics-bigdata with MIT License 4 votes vote down vote up
@Test
public void testProductGroupDataGeneration() {
	final Props props = Props.create(DataGeneratorWorkerActor.class,
			sampleDataGeneratorService);
	final TestActorRef<DataGeneratorWorkerActor> ref = TestActorRef.create(
			system, props);

	ElasticSearchIndexConfig config = ElasticSearchIndexConfig.COM_WEBSITE;
	IndexDocumentType documentType = IndexDocumentType.PRODUCT_GROUP;
	String indexName = "trialindexName";
	IndexDocumentTypeMessageVO indexDocumentTypeMessageVO = new IndexDocumentTypeMessageVO()
			.config(config).documentType(documentType)
			.newIndexName(indexName);

	List<ProductGroup> productGroupList = new ArrayList<ProductGroup>();
	ProductGroup productGroup = new ProductGroup();
	// For now ids hard coded in generator for testing
	Long productGroupId = 1l;
	productGroup.setId(productGroupId);
	productGroupList.add(productGroup);

	expect(sampleDataGeneratorService.generateProductGroupSampleData())
			.andReturn(productGroupList);
	replay(sampleDataGeneratorService);

	TestProbe testProbe = TestProbe.apply(system);
	ref.tell(indexDocumentTypeMessageVO, testProbe.ref());
	verify(sampleDataGeneratorService);

	testProbe.expectMsgClass(Integer.class);
	TestActor.Message sizeMessage = testProbe.lastMessage();
	Integer resultMsgCount = (Integer) sizeMessage.msg();
	assertEquals(1, resultMsgCount.intValue());

	testProbe.expectMsgClass(IndexDocumentVO.class);
	TestActor.Message message = testProbe.lastMessage();
	IndexDocumentVO resultMsg = (IndexDocumentVO) message.msg();
	assertEquals(productGroupId, resultMsg.getDocumentId());
	assertEquals(config, resultMsg.getConfig());
	assertEquals(documentType, resultMsg.getDocumentType());
	assertEquals(indexName, resultMsg.getNewIndexName());
}