Search in sources :

Example 16 with Message

use of io.cdap.cdap.api.messaging.Message in project cdap by cdapio.

the class TetheringServerHandlerTest method testProcessControlChannelProgramUpdates.

@Test
public void testProcessControlChannelProgramUpdates() throws Exception {
    // Create and accept tethering
    createTethering("xyz", NAMESPACES, REQUEST_TIME, DESCRIPTION);
    acceptTethering();
    expectTetheringControlResponse("xyz", HttpResponseStatus.OK);
    // Add program update Notifications to body
    HttpRequest.Builder builder = HttpRequest.builder(HttpMethod.POST, config.resolveURL("tethering/controlchannels/xyz"));
    ProgramRunId programRunId = new ProgramRunId("system", "app", ProgramType.SPARK, "program", "run");
    Notification programUpdate = new Notification(Notification.Type.PROGRAM_STATUS, ImmutableMap.of(ProgramOptionConstants.PROGRAM_RUN_ID, GSON.toJson(programRunId)));
    TetheringControlChannelRequest content = new TetheringControlChannelRequest(null, ImmutableList.of(programUpdate));
    builder.withBody(GSON.toJson(content));
    HttpResponse response = HttpRequests.execute(builder.build());
    Assert.assertEquals(HttpResponseStatus.OK.code(), response.getResponseCode());
    // Check that program update was persisted as a state transition in TMS
    try (CloseableIterator<Message> iterator = new MultiThreadMessagingContext(messagingService).getMessageFetcher().fetch(NamespaceId.SYSTEM.getNamespace(), cConf.get(Constants.AppFabric.PROGRAM_STATUS_EVENT_TOPIC), 1, null)) {
        Assert.assertTrue(iterator.hasNext());
        Notification notification = iterator.next().decodePayload(r -> GSON.fromJson(r, Notification.class));
        Assert.assertEquals(programUpdate, notification);
        Map<String, String> properties = notification.getProperties();
        Assert.assertEquals(GSON.toJson(programRunId), properties.get(ProgramOptionConstants.PROGRAM_RUN_ID));
    }
    // Delete tethering
    deleteTethering();
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) Message(io.cdap.cdap.api.messaging.Message) HttpResponse(io.cdap.common.http.HttpResponse) MultiThreadMessagingContext(io.cdap.cdap.messaging.context.MultiThreadMessagingContext) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) Notification(io.cdap.cdap.proto.Notification) Test(org.junit.Test)

Example 17 with Message

use of io.cdap.cdap.api.messaging.Message in project cdap by cdapio.

the class TetheringRuntimeJobManagerTest method testPublishToControlChannel.

@Test
public void testPublishToControlChannel() throws Exception {
    TetheringControlMessage message = new TetheringControlMessage(TetheringControlMessage.Type.START_PROGRAM, "payload".getBytes(StandardCharsets.UTF_8));
    runtimeJobManager.publishToControlChannel(message);
    try (CloseableIterator<Message> iterator = messageFetcher.fetch(topicId.getNamespace(), topicId.getTopic(), 1, 0)) {
        Assert.assertTrue(iterator.hasNext());
        Assert.assertEquals(GSON.toJson(message), iterator.next().getPayloadAsString());
    }
}
Also used : TetheringControlMessage(io.cdap.cdap.internal.tethering.TetheringControlMessage) Message(io.cdap.cdap.api.messaging.Message) TetheringControlMessage(io.cdap.cdap.internal.tethering.TetheringControlMessage) Test(org.junit.Test)

Example 18 with Message

use of io.cdap.cdap.api.messaging.Message in project cdap by cdapio.

the class RuntimeClientServerTest method testSmallMessage.

@Test
public void testSmallMessage() throws Exception {
    ProgramRunId programRunId = NamespaceId.DEFAULT.app("app").workflow("workflow").run(RunIds.generate());
    TopicId topicId = NamespaceId.SYSTEM.topic("topic");
    List<Message> messages = new ArrayList<>();
    messages.add(createMessage(Math.max(1, RuntimeClient.CHUNK_SIZE / 4)));
    runtimeClient.sendMessages(programRunId, topicId, messages.iterator());
    assertMessages(topicId, messages);
}
Also used : Message(io.cdap.cdap.api.messaging.Message) ArrayList(java.util.ArrayList) TopicId(io.cdap.cdap.proto.id.TopicId) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) Test(org.junit.Test)

Example 19 with Message

use of io.cdap.cdap.api.messaging.Message in project cdap by cdapio.

the class RuntimeClientServerTest method testFutureIsNotBlockingWhenValueIsSet.

@Test
public void testFutureIsNotBlockingWhenValueIsSet() throws Exception {
    CountDownLatch countDownLatch = new CountDownLatch(1);
    runtimeClient.onProgramStopRequested(() -> countDownLatch.countDown());
    // Now call sendMessages which will set the future
    ProgramRunId programRunId = NamespaceId.DEFAULT.app("app").workflow("workflow").run(RunIds.generate());
    TopicId topicId = NamespaceId.SYSTEM.topic("topic");
    List<Message> messages = new ArrayList<>();
    messages.add(createMessage(Math.max(1, RuntimeClient.CHUNK_SIZE / 4)));
    runtimeClient.sendMessages(programRunId, topicId, messages.iterator());
    // Assert that future is not blocking anymore
    Assert.assertTrue(countDownLatch.await(5, TimeUnit.SECONDS));
}
Also used : Message(io.cdap.cdap.api.messaging.Message) ArrayList(java.util.ArrayList) TopicId(io.cdap.cdap.proto.id.TopicId) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 20 with Message

use of io.cdap.cdap.api.messaging.Message in project cdap by cdapio.

the class RuntimeClientServerTest method testLargeMessage.

@Test
public void testLargeMessage() throws Exception {
    ProgramRunId programRunId = NamespaceId.DEFAULT.app("app").workflow("workflow").run(RunIds.generate());
    TopicId topicId = NamespaceId.SYSTEM.topic(TEST_TOPIC);
    List<Message> messages = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        messages.add(createMessage(RuntimeClient.CHUNK_SIZE * 2));
    }
    runtimeClient.sendMessages(programRunId, topicId, messages.iterator());
    assertMessages(topicId, messages);
}
Also used : Message(io.cdap.cdap.api.messaging.Message) ArrayList(java.util.ArrayList) TopicId(io.cdap.cdap.proto.id.TopicId) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) Test(org.junit.Test)

Aggregations

Message (io.cdap.cdap.api.messaging.Message)43 Test (org.junit.Test)25 MessageFetcher (io.cdap.cdap.api.messaging.MessageFetcher)19 ProgramRunId (io.cdap.cdap.proto.id.ProgramRunId)18 ArrayList (java.util.ArrayList)18 TopicNotFoundException (io.cdap.cdap.api.messaging.TopicNotFoundException)14 MultiThreadMessagingContext (io.cdap.cdap.messaging.context.MultiThreadMessagingContext)12 TopicId (io.cdap.cdap.proto.id.TopicId)12 ApplicationManager (io.cdap.cdap.test.ApplicationManager)11 CloseableIterator (io.cdap.cdap.api.dataset.lib.CloseableIterator)10 MessagingContext (io.cdap.cdap.api.messaging.MessagingContext)10 Notification (io.cdap.cdap.proto.Notification)10 TimeoutException (java.util.concurrent.TimeoutException)10 MessagingService (io.cdap.cdap.messaging.MessagingService)8 IOException (java.io.IOException)8 StructuredRecord (io.cdap.cdap.api.data.format.StructuredRecord)7 Schema (io.cdap.cdap.api.data.schema.Schema)7 Table (io.cdap.cdap.api.dataset.table.Table)7 ETLStage (io.cdap.cdap.etl.proto.v2.ETLStage)7 HashMap (java.util.HashMap)7