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");
}
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);
}
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")));
}
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()));
}
}
}
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());
}
Aggregations