Search in sources :

Example 11 with WorkerManager

use of io.cdap.cdap.test.WorkerManager in project cdap by caskdata.

the class TestFrameworkTestRun method testAppWithWorker.

@Category(SlowTests.class)
@Test
public void testAppWithWorker() throws Exception {
    ApplicationManager applicationManager = deployApplication(testSpace, AppWithWorker.class);
    LOG.info("Deployed.");
    WorkerManager manager = applicationManager.getWorkerManager(AppWithWorker.WORKER).start();
    // Wait for initialize and run states
    Tasks.waitFor(true, new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            DataSetManager<KeyValueTable> dataSetManager = getDataset(testSpace.dataset(AppWithWorker.DATASET));
            KeyValueTable table = dataSetManager.get();
            return AppWithWorker.INITIALIZE.equals(Bytes.toString(table.read(AppWithWorker.INITIALIZE))) && AppWithWorker.RUN.equals(Bytes.toString(table.read(AppWithWorker.RUN)));
        }
    }, 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
    manager.stop();
    applicationManager.stopAll();
    // Wait for stop state
    Tasks.waitFor(true, new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            DataSetManager<KeyValueTable> dataSetManager = getDataset(testSpace.dataset(AppWithWorker.DATASET));
            KeyValueTable table = dataSetManager.get();
            return AppWithWorker.STOP.equals(Bytes.toString(table.read(AppWithWorker.STOP)));
        }
    }, 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
}
Also used : WorkerManager(io.cdap.cdap.test.WorkerManager) ApplicationManager(io.cdap.cdap.test.ApplicationManager) KeyValueTable(io.cdap.cdap.api.dataset.lib.KeyValueTable) DataSetManager(io.cdap.cdap.test.DataSetManager) IOException(java.io.IOException) ConflictException(io.cdap.cdap.common.ConflictException) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Example 12 with WorkerManager

use of io.cdap.cdap.test.WorkerManager in project cdap by caskdata.

the class MessagingAppTestRun method testWithWorker.

@Test
public void testWithWorker() throws Exception {
    ApplicationManager appManager = deployWithArtifact(NAMESPACE, MessagingApp.class, artifactJar);
    final WorkerManager workerManager = appManager.getWorkerManager(MessagingApp.MessagingWorker.class.getSimpleName()).start();
    MessagingContext messagingContext = getMessagingContext();
    final MessagingAdmin messagingAdmin = getMessagingAdmin(NAMESPACE);
    // Wait for the worker to create the topic
    Tasks.waitFor(true, new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            try {
                messagingAdmin.getTopicProperties(MessagingApp.TOPIC);
                return true;
            } catch (TopicNotFoundException e) {
                return false;
            }
        }
    }, 5, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
    // Publish a message
    String message = "message";
    MessagePublisher messagePublisher = messagingContext.getMessagePublisher();
    messagePublisher.publish(NAMESPACE.getNamespace(), MessagingApp.TOPIC, message);
    // The worker will publish back a message with payload as concat(message, message)
    final MessageFetcher messageFetcher = messagingContext.getMessageFetcher();
    Tasks.waitFor(message + message, new Callable<String>() {

        @Override
        public String call() throws Exception {
            try (CloseableIterator<Message> iterator = messageFetcher.fetch(NAMESPACE.getNamespace(), MessagingApp.TOPIC, Integer.MAX_VALUE, 0L)) {
                Message message = Iterators.getLast(iterator, null);
                return message == null ? null : message.getPayloadAsString();
            }
        }
    }, 5, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
    // Publish concat(message + message) to the app
    messagePublisher.publish(NAMESPACE.getNamespace(), MessagingApp.TOPIC, message + message);
    // timeout.
    try {
        Tasks.waitFor(message + message + message + message, new Callable<String>() {

            @Override
            public String call() throws Exception {
                try (CloseableIterator<Message> iterator = messageFetcher.fetch(NAMESPACE.getNamespace(), MessagingApp.TOPIC, Integer.MAX_VALUE, 0L)) {
                    Message message = Iterators.getLast(iterator, null);
                    return message == null ? null : message.getPayloadAsString();
                }
            }
        }, 2, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
        Assert.fail("Expected timeout exception");
    } catch (TimeoutException e) {
    // expected
    }
    // Now publish a message to the control topic, to unblock the transaction block.
    messagePublisher.publish(NAMESPACE.getNamespace(), MessagingApp.CONTROL_TOPIC, message);
    // Should expect a new message as concat(message, message, message, message)
    Tasks.waitFor(message + message + message + message, new Callable<String>() {

        @Override
        public String call() throws Exception {
            try (CloseableIterator<Message> iterator = messageFetcher.fetch(NAMESPACE.getNamespace(), MessagingApp.TOPIC, Integer.MAX_VALUE, 0L)) {
                Message message = Iterators.getLast(iterator, null);
                return message == null ? null : message.getPayloadAsString();
            }
        }
    }, 5, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
    // Wait for the worker to finish and verify that it completes successfully.
    workerManager.waitForRun(ProgramRunStatus.COMPLETED, 5, TimeUnit.SECONDS);
}
Also used : ApplicationManager(io.cdap.cdap.test.ApplicationManager) MessageFetcher(io.cdap.cdap.api.messaging.MessageFetcher) CloseableIterator(io.cdap.cdap.api.dataset.lib.CloseableIterator) Message(io.cdap.cdap.api.messaging.Message) TopicNotFoundException(io.cdap.cdap.api.messaging.TopicNotFoundException) MessagePublisher(io.cdap.cdap.api.messaging.MessagePublisher) TimeoutException(java.util.concurrent.TimeoutException) TopicNotFoundException(io.cdap.cdap.api.messaging.TopicNotFoundException) WorkerManager(io.cdap.cdap.test.WorkerManager) MessagingAdmin(io.cdap.cdap.api.messaging.MessagingAdmin) MessagingContext(io.cdap.cdap.api.messaging.MessagingContext) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 13 with WorkerManager

use of io.cdap.cdap.test.WorkerManager in project cdap by caskdata.

the class MessagingAppTestRun method testTxPublishFetch.

@Test
public void testTxPublishFetch() throws Exception {
    ApplicationManager appManager = deployWithArtifact(NAMESPACE, MessagingApp.class, artifactJar);
    MessagingAdmin messagingAdmin = getMessagingAdmin(NAMESPACE);
    final WorkerManager workerManager = appManager.getWorkerManager(MessagingApp.TransactionalMessagingWorker.class.getSimpleName());
    // Run the TransactionalMessagingWorker twice, one with getting publisher/fetcher inside TX, one outside.
    for (boolean getInTx : Arrays.asList(true, false)) {
        messagingAdmin.createTopic(MessagingApp.TOPIC);
        workerManager.start(Collections.singletonMap("get.in.tx", Boolean.toString(getInTx)));
        // Wait for the worker to finish and verify that it completes successfully.
        int workerRunCount = getInTx ? 1 : 2;
        workerManager.waitForRuns(ProgramRunStatus.COMPLETED, workerRunCount, 60, TimeUnit.SECONDS);
        messagingAdmin.deleteTopic(MessagingApp.TOPIC);
    }
}
Also used : WorkerManager(io.cdap.cdap.test.WorkerManager) ApplicationManager(io.cdap.cdap.test.ApplicationManager) MessagingAdmin(io.cdap.cdap.api.messaging.MessagingAdmin) Test(org.junit.Test)

Aggregations

ApplicationManager (io.cdap.cdap.test.ApplicationManager)13 WorkerManager (io.cdap.cdap.test.WorkerManager)13 Test (org.junit.Test)13 ServiceManager (io.cdap.cdap.test.ServiceManager)5 Category (org.junit.experimental.categories.Category)5 KeyValueTable (io.cdap.cdap.api.dataset.lib.KeyValueTable)4 WorkflowManager (io.cdap.cdap.test.WorkflowManager)4 ConflictException (io.cdap.cdap.common.ConflictException)3 SparkManager (io.cdap.cdap.test.SparkManager)3 IOException (java.io.IOException)3 Gson (com.google.gson.Gson)2 FileSet (io.cdap.cdap.api.dataset.lib.FileSet)2 Table (io.cdap.cdap.api.dataset.table.Table)2 MessagingAdmin (io.cdap.cdap.api.messaging.MessagingAdmin)2 MapReduceManager (io.cdap.cdap.test.MapReduceManager)2 PrintStream (java.io.PrintStream)2 URL (java.net.URL)2 HashMap (java.util.HashMap)2 ArtifactSummary (io.cdap.cdap.api.artifact.ArtifactSummary)1 CloseableIterator (io.cdap.cdap.api.dataset.lib.CloseableIterator)1