Java Code Examples for akka.testkit.TestActorRef#create()
The following examples show how to use
akka.testkit.TestActorRef#create() .
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: ChatroomTest.java From learning-akka with Apache License 2.0 | 6 votes |
@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 2
Source File: FlinkUntypedActorTest.java From Flink-CEPplus with Apache License 2.0 | 6 votes |
/** * 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 3
Source File: ChatroomTest.java From learning-akka with Apache License 2.0 | 6 votes |
@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: AkkademyDbTest.java From learning-akka with Apache License 2.0 | 6 votes |
@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 5
Source File: ChatroomTest.java From learning-akka with Apache License 2.0 | 6 votes |
@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: SetupDocumentTypeWorkerActorTest.java From searchanalytics-bigdata with MIT License | 5 votes |
@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 7
Source File: ChatroomTest.java From learning-akka with Apache License 2.0 | 5 votes |
@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 8
Source File: SetupDocumentTypeWorkerActorTest.java From searchanalytics-bigdata with MIT License | 5 votes |
@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 9
Source File: FSMClientActorTest.java From learning-akka with Apache License 2.0 | 5 votes |
@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 10
Source File: FSMClientActorTest.java From learning-akka with Apache License 2.0 | 5 votes |
@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 11
Source File: SetupDocumentTypeWorkerActorTest.java From searchanalytics-bigdata with MIT License | 5 votes |
@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: AkkademyDbTest.java From learning-akka with Apache License 2.0 | 5 votes |
@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 13
Source File: WorkFlowExecutionControllerTest.java From flux with Apache License 2.0 | 5 votes |
@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 14
Source File: DataGeneratorWorkerActorTest.java From searchanalytics-bigdata with MIT License | 5 votes |
@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 15
Source File: DataGeneratorWorkerActorTest.java From searchanalytics-bigdata with MIT License | 5 votes |
@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 16
Source File: FlinkUntypedActorTest.java From Flink-CEPplus with Apache License 2.0 | 5 votes |
/** * 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 17
Source File: DataGeneratorWorkerActorTest.java From searchanalytics-bigdata with MIT License | 4 votes |
@Test public void testProductPropertyDataGeneration() { 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_PROPERTY; String indexName = "trialindexName"; IndexDocumentTypeMessageVO indexDocumentTypeMessageVO = new IndexDocumentTypeMessageVO() .config(config).documentType(documentType) .newIndexName(indexName); Set<ProductProperty> productPropertList = new HashSet<ProductProperty>(); ProductProperty productProperty = new ProductProperty(); // For now ids hard coded in generator for testing Long productPropertyId = 1l; productProperty.setId(productPropertyId); productProperty .setColor(SampleDataGeneratorService.PRODUCTPROPERTY_COLOR_BLACK); productProperty .setSize(SampleDataGeneratorService.PRODUCTPROPERTY_SIZE_13_INCH); productPropertList.add(productProperty); expect(sampleDataGeneratorService.generateProductPropertySampleData()) .andReturn(productPropertList); 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(productPropertyId, resultMsg.getDocumentId()); assertEquals(config, resultMsg.getConfig()); assertEquals(documentType, resultMsg.getDocumentType()); assertEquals(indexName, resultMsg.getNewIndexName()); }
Example 18
Source File: SetupDocumentTypeWorkerActorTest.java From searchanalytics-bigdata with MIT License | 4 votes |
@Test public void handleFullFlow() { 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); TestProbe testProbeDocumentGeneratorWorker = TestProbe.apply(system); TestProbe testProbeIndexDataWorker = TestProbe.apply(system); actor.setDataGeneratorWorkerRouter(testProbeDataGeneratorWorker.ref()); actor.setDocumentGeneratorWorkerRouter(testProbeDocumentGeneratorWorker .ref()); actor.setIndexDocumentWorkerRouter(testProbeIndexDataWorker.ref()); ElasticSearchIndexConfig config = ElasticSearchIndexConfig.COM_WEBSITE; IndexDocumentType documentType = IndexDocumentType.PRODUCT; String indexName = "trialindexName"; IndexDocumentTypeMessageVO indexDocumentTypeMessageVO = new IndexDocumentTypeMessageVO() .config(config).documentType(documentType) .newIndexName(indexName); assertEquals(0, actor.getTotalDocumentsToIndex()); assertEquals(0, actor.getTotalDocumentsToIndexDone()); assertEquals(null, actor.getIndexDocumentType()); TestProbe testProbeOriginalSender = TestProbe.apply(system); 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()); // updated actor state assertEquals(0, actor.getTotalDocumentsToIndex()); assertEquals(0, actor.getTotalDocumentsToIndexDone()); assertEquals(documentType, actor.getIndexDocumentType()); // Let's say total data to generate to 1 ref.tell(Integer.valueOf(1), testProbeDataGeneratorWorker.ref()); assertEquals(1, actor.getTotalDocumentsToIndex()); assertEquals(0, actor.getTotalDocumentsToIndexDone()); assertEquals(documentType, actor.getIndexDocumentType()); Long documentId = 1l; IndexDocumentVO indexDocumentVO = new IndexDocumentVO() .config(resultMsg.getConfig()) .documentType(indexDocumentTypeMessageVO.getIndexDocumentType()) .newIndexName(indexDocumentTypeMessageVO.getNewIndexName()) .documentId(documentId); // Send data back to the sender, generate document ref.tell(indexDocumentVO, testProbeDataGeneratorWorker.ref()); 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(1, actor.getTotalDocumentsToIndex()); assertEquals(0, actor.getTotalDocumentsToIndexDone()); assertEquals(documentType, actor.getIndexDocumentType()); // send data back, index document. Product product = new Product(); product.setId(documentId); resultMsgDoc.product(product); ref.tell(resultMsgDoc, testProbeDocumentGeneratorWorker.ref()); 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(1, actor.getTotalDocumentsToIndex()); assertEquals(0, actor.getTotalDocumentsToIndexDone()); assertEquals(documentType, actor.getIndexDocumentType()); // Send back message, indexdone. resultMsgDocIndex.indexDone(true); TestProbe testProbeParent = TestProbe.apply(system); String string = testProbeParent.ref().path().toString(); actor.setParentActorPathString(string); ref.tell(resultMsgDocIndex, null); // state is set back to initial and message sent to parent. testProbeParent.expectMsgClass(IndexDocumentType.class); TestActor.Message messageDocIndexDone = testProbeParent.lastMessage(); IndexDocumentType resultMsgDocIndexDone = (IndexDocumentType) messageDocIndexDone .msg(); // state reset assertEquals(documentType, resultMsgDocIndexDone); assertEquals(0, actor.getTotalDocumentsToIndex()); assertEquals(0, actor.getTotalDocumentsToIndexDone()); assertEquals(null, actor.getIndexDocumentType()); }
Example 19
Source File: MetricQueryServiceTest.java From Flink-CEPplus with Apache License 2.0 | 4 votes |
@Test public void testCreateDump() throws Exception { ActorSystem s = AkkaUtils.createLocalActorSystem(new Configuration()); try { ActorRef serviceActor = MetricQueryService.startMetricQueryService(s, null, Long.MAX_VALUE); TestActorRef testActorRef = TestActorRef.create(s, Props.create(TestActor.class)); TestActor testActor = (TestActor) testActorRef.underlyingActor(); final Counter c = new SimpleCounter(); final Gauge<String> g = () -> "Hello"; final Histogram h = new TestHistogram(); final Meter m = new TestMeter(); final TaskManagerMetricGroup tm = UnregisteredMetricGroups.createUnregisteredTaskManagerMetricGroup(); MetricQueryService.notifyOfAddedMetric(serviceActor, c, "counter", tm); MetricQueryService.notifyOfAddedMetric(serviceActor, g, "gauge", tm); MetricQueryService.notifyOfAddedMetric(serviceActor, h, "histogram", tm); MetricQueryService.notifyOfAddedMetric(serviceActor, m, "meter", tm); serviceActor.tell(MetricQueryService.getCreateDump(), testActorRef); testActor.waitForResult(); MetricDumpSerialization.MetricSerializationResult dump = testActor.getSerializationResult(); assertTrue(dump.serializedCounters.length > 0); assertTrue(dump.serializedGauges.length > 0); assertTrue(dump.serializedHistograms.length > 0); assertTrue(dump.serializedMeters.length > 0); MetricQueryService.notifyOfRemovedMetric(serviceActor, c); MetricQueryService.notifyOfRemovedMetric(serviceActor, g); MetricQueryService.notifyOfRemovedMetric(serviceActor, h); MetricQueryService.notifyOfRemovedMetric(serviceActor, m); serviceActor.tell(MetricQueryService.getCreateDump(), testActorRef); testActor.waitForResult(); MetricDumpSerialization.MetricSerializationResult emptyDump = testActor.getSerializationResult(); assertEquals(0, emptyDump.serializedCounters.length); assertEquals(0, emptyDump.serializedGauges.length); assertEquals(0, emptyDump.serializedHistograms.length); assertEquals(0, emptyDump.serializedMeters.length); } finally { s.terminate(); } }
Example 20
Source File: MetricQueryServiceTest.java From Flink-CEPplus with Apache License 2.0 | 4 votes |
@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(); } }