Search in sources :

Example 11 with RuntimeMetrics

use of co.cask.cdap.api.metrics.RuntimeMetrics in project cdap by caskdata.

the class PurchaseAppTest method test.

@Test
public void test() throws Exception {
    // Deploy the PurchaseApp application
    ApplicationManager appManager = deployApplication(PurchaseApp.class);
    // Start PurchaseFlow
    FlowManager flowManager = appManager.getFlowManager("PurchaseFlow").start();
    // Send stream events to the "purchaseStream" Stream
    StreamManager streamManager = getStreamManager("purchaseStream");
    streamManager.send("bob bought 3 apples for $30");
    streamManager.send("joe bought 1 apple for $100");
    streamManager.send("joe bought 10 pineapples for $20");
    streamManager.send("cat bought 3 bottles for $12");
    streamManager.send("cat bought 2 pops for $14");
    try {
        // Wait for the last Flowlet processing 5 events, or at most 15 seconds
        RuntimeMetrics metrics = flowManager.getFlowletMetrics("collector");
        metrics.waitForProcessed(5, 15, TimeUnit.SECONDS);
    } finally {
        flowManager.stop();
    }
    ServiceManager userProfileServiceManager = getUserProfileServiceManager(appManager);
    // Add customer's profile information
    URL userProfileUrl = new URL(userProfileServiceManager.getServiceURL(15, TimeUnit.SECONDS), UserProfileServiceHandler.USER_ENDPOINT);
    HttpURLConnection userProfileConnection = (HttpURLConnection) userProfileUrl.openConnection();
    String userProfileJson = "{'id' : 'joe', 'firstName': 'joe', 'lastName':'bernard', 'categories': ['fruits']}";
    try {
        userProfileConnection.setDoOutput(true);
        userProfileConnection.setRequestMethod("POST");
        userProfileConnection.getOutputStream().write(userProfileJson.getBytes(Charsets.UTF_8));
        Assert.assertEquals(HttpURLConnection.HTTP_OK, userProfileConnection.getResponseCode());
    } finally {
        userProfileConnection.disconnect();
    }
    // Test service to retrieve customer's profile information
    userProfileUrl = new URL(userProfileServiceManager.getServiceURL(15, TimeUnit.SECONDS), UserProfileServiceHandler.USER_ENDPOINT + "/joe");
    userProfileConnection = (HttpURLConnection) userProfileUrl.openConnection();
    Assert.assertEquals(HttpURLConnection.HTTP_OK, userProfileConnection.getResponseCode());
    String customerJson;
    try {
        customerJson = new String(ByteStreams.toByteArray(userProfileConnection.getInputStream()), Charsets.UTF_8);
    } finally {
        userProfileConnection.disconnect();
    }
    UserProfile profileFromService = GSON.fromJson(customerJson, UserProfile.class);
    Assert.assertEquals(profileFromService.getFirstName(), "joe");
    Assert.assertEquals(profileFromService.getLastName(), "bernard");
    // Run PurchaseHistoryWorkflow which will process the data
    MapReduceManager mapReduceManager = appManager.getMapReduceManager(PurchaseHistoryBuilder.class.getSimpleName()).start();
    mapReduceManager.waitForRun(ProgramRunStatus.COMPLETED, 3, TimeUnit.MINUTES);
    // Start PurchaseHistoryService
    ServiceManager purchaseHistoryServiceManager = appManager.getServiceManager(PurchaseHistoryService.SERVICE_NAME).start();
    // Wait for service startup
    purchaseHistoryServiceManager.waitForStatus(true);
    // Test service to retrieve a customer's purchase history
    URL url = new URL(purchaseHistoryServiceManager.getServiceURL(15, TimeUnit.SECONDS), "history/joe");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
    String historyJson;
    try {
        historyJson = new String(ByteStreams.toByteArray(conn.getInputStream()), Charsets.UTF_8);
    } finally {
        conn.disconnect();
    }
    PurchaseHistory history = GSON.fromJson(historyJson, PurchaseHistory.class);
    Assert.assertEquals("joe", history.getCustomer());
    Assert.assertEquals(2, history.getPurchases().size());
    UserProfile profileFromPurchaseHistory = history.getUserProfile();
    Assert.assertEquals(profileFromPurchaseHistory.getFirstName(), "joe");
    Assert.assertEquals(profileFromPurchaseHistory.getLastName(), "bernard");
}
Also used : FlowManager(co.cask.cdap.test.FlowManager) ApplicationManager(co.cask.cdap.test.ApplicationManager) HttpURLConnection(java.net.HttpURLConnection) MapReduceManager(co.cask.cdap.test.MapReduceManager) StreamManager(co.cask.cdap.test.StreamManager) RuntimeMetrics(co.cask.cdap.api.metrics.RuntimeMetrics) ServiceManager(co.cask.cdap.test.ServiceManager) URL(java.net.URL) Test(org.junit.Test)

Example 12 with RuntimeMetrics

use of co.cask.cdap.api.metrics.RuntimeMetrics in project cdap by caskdata.

the class HelloWorldTest method test.

@Test
public void test() throws Exception {
    // Deploy the HelloWorld application
    ApplicationManager appManager = deployApplication(HelloWorld.class);
    // Start WhoFlow
    FlowManager flowManager = appManager.getFlowManager("WhoFlow").start();
    Assert.assertTrue(flowManager.isRunning());
    // Send stream events to the "who" Stream
    StreamManager streamManager = getStreamManager("who");
    streamManager.send("1");
    streamManager.send("2");
    streamManager.send("3");
    streamManager.send("4");
    streamManager.send("5");
    try {
        // Wait for the last Flowlet processing 5 events, or at most 5 seconds
        RuntimeMetrics metrics = flowManager.getFlowletMetrics("saver");
        metrics.waitForProcessed(5, 5, TimeUnit.SECONDS);
    } finally {
        flowManager.stop();
        Assert.assertFalse(flowManager.isRunning());
    }
    // Start Greeting service and use it
    ServiceManager serviceManager = appManager.getServiceManager(HelloWorld.Greeting.SERVICE_NAME).start();
    // Wait service startup
    serviceManager.waitForStatus(true);
    URL url = new URL(serviceManager.getServiceURL(), "greet");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    Assert.assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode());
    String response;
    try {
        response = new String(ByteStreams.toByteArray(connection.getInputStream()), Charsets.UTF_8);
    } finally {
        connection.disconnect();
    }
    Assert.assertEquals("Hello 5!", response);
}
Also used : FlowManager(co.cask.cdap.test.FlowManager) ApplicationManager(co.cask.cdap.test.ApplicationManager) HttpURLConnection(java.net.HttpURLConnection) StreamManager(co.cask.cdap.test.StreamManager) RuntimeMetrics(co.cask.cdap.api.metrics.RuntimeMetrics) ServiceManager(co.cask.cdap.test.ServiceManager) URL(java.net.URL) Test(org.junit.Test)

Example 13 with RuntimeMetrics

use of co.cask.cdap.api.metrics.RuntimeMetrics in project cdap by caskdata.

the class TestFrameworkTestRun method testAppConfig.

private void testAppConfig(String appName, ApplicationManager appManager, ConfigTestApp.ConfigClass conf) throws Exception {
    String streamName = conf == null ? ConfigTestApp.DEFAULT_STREAM : conf.getStreamName();
    String datasetName = conf == null ? ConfigTestApp.DEFAULT_TABLE : conf.getTableName();
    FlowManager flowManager = appManager.getFlowManager(ConfigTestApp.FLOW_NAME).start();
    StreamManager streamManager = getStreamManager(streamName);
    streamManager.send("abcd");
    streamManager.send("xyz");
    RuntimeMetrics metrics = flowManager.getFlowletMetrics(ConfigTestApp.FLOWLET_NAME);
    metrics.waitForProcessed(2, 1, TimeUnit.MINUTES);
    flowManager.stop();
    DataSetManager<KeyValueTable> dsManager = getDataset(datasetName);
    KeyValueTable table = dsManager.get();
    Assert.assertEquals("abcd", Bytes.toString(table.read(appName + ".abcd")));
    Assert.assertEquals("xyz", Bytes.toString(table.read(appName + ".xyz")));
}
Also used : FlowManager(co.cask.cdap.test.FlowManager) StreamManager(co.cask.cdap.test.StreamManager) RuntimeMetrics(co.cask.cdap.api.metrics.RuntimeMetrics) KeyValueTable(co.cask.cdap.api.dataset.lib.KeyValueTable)

Example 14 with RuntimeMetrics

use of co.cask.cdap.api.metrics.RuntimeMetrics in project cdap by caskdata.

the class TestFrameworkTestRun method testDynamicBatchSize.

@Category(SlowTests.class)
@Test
public void testDynamicBatchSize() throws Exception {
    ApplicationManager applicationManager = deployApplication(testSpace, GenSinkApp2.class);
    DataSetManager<KeyValueTable> table = getDataset(testSpace.dataset("table"));
    // Start the flow with runtime argument. It should set the batch size to 1.
    FlowManager flowManager = applicationManager.getFlowManager("GenSinkFlow").start(Collections.singletonMap("flowlet.BatchSinkFlowlet.batch.size", "1"));
    RuntimeMetrics batchSinkMetrics = flowManager.getFlowletMetrics("BatchSinkFlowlet");
    // Batch sink only get the 99 batch events
    batchSinkMetrics.waitForProcessed(99, 5, TimeUnit.SECONDS);
    flowManager.stop();
    flowManager.waitForRun(ProgramRunStatus.KILLED, 10, TimeUnit.SECONDS);
    try (CloseableIterator<KeyValue<byte[], byte[]>> itor = table.get().scan(null, null)) {
        // Should only see batch size of 1.
        while (itor.hasNext()) {
            Assert.assertEquals(1, Bytes.toInt(itor.next().getKey()));
        }
    }
}
Also used : FlowManager(co.cask.cdap.test.FlowManager) ApplicationManager(co.cask.cdap.test.ApplicationManager) KeyValue(co.cask.cdap.api.dataset.lib.KeyValue) KeyValueTable(co.cask.cdap.api.dataset.lib.KeyValueTable) RuntimeMetrics(co.cask.cdap.api.metrics.RuntimeMetrics) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Example 15 with RuntimeMetrics

use of co.cask.cdap.api.metrics.RuntimeMetrics in project cdap by caskdata.

the class TestFrameworkTestRun method testGenerator.

@Category(SlowTests.class)
@Test
public void testGenerator() throws InterruptedException, IOException, TimeoutException {
    ApplicationManager applicationManager = deployApplication(testSpace, GenSinkApp2.class);
    FlowManager flowManager = applicationManager.getFlowManager("GenSinkFlow").start();
    // Check the flowlet metrics
    RuntimeMetrics genMetrics = flowManager.getFlowletMetrics("GenFlowlet");
    RuntimeMetrics sinkMetrics = flowManager.getFlowletMetrics("SinkFlowlet");
    RuntimeMetrics batchSinkMetrics = flowManager.getFlowletMetrics("BatchSinkFlowlet");
    // Generator generators 99 events + 99 batched events
    sinkMetrics.waitFor("system.process.events.in", 198, 5, TimeUnit.SECONDS);
    sinkMetrics.waitForProcessed(198, 5, TimeUnit.SECONDS);
    Assert.assertEquals(0L, sinkMetrics.getException());
    // Batch sink only get the 99 batch events
    batchSinkMetrics.waitFor("system.process.events.in", 99, 5, TimeUnit.SECONDS);
    batchSinkMetrics.waitForProcessed(99, 5, TimeUnit.SECONDS);
    Assert.assertEquals(0L, batchSinkMetrics.getException());
    Assert.assertEquals(1L, genMetrics.getException());
}
Also used : FlowManager(co.cask.cdap.test.FlowManager) ApplicationManager(co.cask.cdap.test.ApplicationManager) RuntimeMetrics(co.cask.cdap.api.metrics.RuntimeMetrics) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Aggregations

RuntimeMetrics (co.cask.cdap.api.metrics.RuntimeMetrics)21 ApplicationManager (co.cask.cdap.test.ApplicationManager)17 FlowManager (co.cask.cdap.test.FlowManager)17 Test (org.junit.Test)16 ServiceManager (co.cask.cdap.test.ServiceManager)12 StreamManager (co.cask.cdap.test.StreamManager)12 URL (java.net.URL)10 KeyValueTable (co.cask.cdap.api.dataset.lib.KeyValueTable)6 Gson (com.google.gson.Gson)6 HttpURLConnection (java.net.HttpURLConnection)6 Category (org.junit.experimental.categories.Category)5 Get (co.cask.cdap.api.dataset.table.Get)2 Table (co.cask.cdap.api.dataset.table.Table)2 MapReduceManager (co.cask.cdap.test.MapReduceManager)2 SparkManager (co.cask.cdap.test.SparkManager)2 HttpRequest (co.cask.common.http.HttpRequest)2 HttpResponse (co.cask.common.http.HttpResponse)2 Map (java.util.Map)2 KeyValue (co.cask.cdap.api.dataset.lib.KeyValue)1 Row (co.cask.cdap.api.dataset.table.Row)1