use of co.cask.cdap.test.FlowManager in project cdap by caskdata.
the class TestFrameworkTestRun method testClusterName.
@Test
public void testClusterName() throws Exception {
String clusterName = getConfiguration().get(Constants.CLUSTER_NAME);
ApplicationManager appManager = deployApplication(ClusterNameTestApp.class);
final DataSetManager<KeyValueTable> datasetManager = getDataset(ClusterNameTestApp.CLUSTER_NAME_TABLE);
final KeyValueTable clusterNameTable = datasetManager.get();
// A callable for reading the cluster name from the ClusterNameTable.
// It is used for Tasks.waitFor call down below.
final AtomicReference<String> key = new AtomicReference<>();
Callable<String> readClusterName = new Callable<String>() {
@Nullable
@Override
public String call() throws Exception {
datasetManager.flush();
byte[] bytes = clusterNameTable.read(key.get());
return bytes == null ? null : new String(bytes, StandardCharsets.UTF_8);
}
};
// Service
ServiceManager serviceManager = appManager.getServiceManager(ClusterNameTestApp.ClusterNameServiceHandler.class.getSimpleName()).start();
Assert.assertEquals(clusterName, callServiceGet(serviceManager.getServiceURL(10, TimeUnit.SECONDS), "clusterName"));
serviceManager.stop();
// Worker
WorkerManager workerManager = appManager.getWorkerManager(ClusterNameTestApp.ClusterNameWorker.class.getSimpleName()).start();
key.set("worker.cluster.name");
Tasks.waitFor(clusterName, readClusterName, 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
// The worker will stop by itself. No need to call stop
workerManager.waitForRun(ProgramRunStatus.COMPLETED, 10, TimeUnit.SECONDS);
// Flow
FlowManager flowManager = appManager.getFlowManager(ClusterNameTestApp.ClusterNameFlow.class.getSimpleName()).start();
key.set("flow.cluster.name");
Tasks.waitFor(clusterName, readClusterName, 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
flowManager.stop();
// MapReduce
// Setup the input file used by MR
Location location = this.<FileSet>getDataset(ClusterNameTestApp.INPUT_FILE_SET).get().getLocation("input");
try (PrintStream printer = new PrintStream(location.getOutputStream(), true, "UTF-8")) {
for (int i = 0; i < 10; i++) {
printer.println("Hello World " + i);
}
}
// Setup input and output dataset arguments
Map<String, String> inputArgs = new HashMap<>();
FileSetArguments.setInputPath(inputArgs, "input");
Map<String, String> outputArgs = new HashMap<>();
FileSetArguments.setOutputPath(outputArgs, "output");
Map<String, String> args = new HashMap<>();
args.putAll(RuntimeArguments.addScope(Scope.DATASET, ClusterNameTestApp.INPUT_FILE_SET, inputArgs));
args.putAll(RuntimeArguments.addScope(Scope.DATASET, ClusterNameTestApp.OUTPUT_FILE_SET, outputArgs));
MapReduceManager mrManager = appManager.getMapReduceManager(ClusterNameTestApp.ClusterNameMapReduce.class.getSimpleName()).start(args);
key.set("mr.client.cluster.name");
Tasks.waitFor(clusterName, readClusterName, 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
key.set("mapper.cluster.name");
Tasks.waitFor(clusterName, readClusterName, 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
key.set("reducer.cluster.name");
Tasks.waitFor(clusterName, readClusterName, 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
mrManager.waitForRun(ProgramRunStatus.COMPLETED, 60, TimeUnit.SECONDS);
// Spark
SparkManager sparkManager = appManager.getSparkManager(ClusterNameTestApp.ClusterNameSpark.class.getSimpleName()).start();
key.set("spark.cluster.name");
Tasks.waitFor(clusterName, readClusterName, 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
sparkManager.waitForRun(ProgramRunStatus.COMPLETED, 60, TimeUnit.SECONDS);
// Workflow
// Cleanup the output path for the MR job in the workflow first
this.<FileSet>getDataset(ClusterNameTestApp.OUTPUT_FILE_SET).get().getLocation("output").delete(true);
args = RuntimeArguments.addScope(Scope.MAPREDUCE, ClusterNameTestApp.ClusterNameMapReduce.class.getSimpleName(), args);
WorkflowManager workflowManager = appManager.getWorkflowManager(ClusterNameTestApp.ClusterNameWorkflow.class.getSimpleName()).start(args);
String prefix = ClusterNameTestApp.ClusterNameWorkflow.class.getSimpleName() + ".";
key.set(prefix + "mr.client.cluster.name");
Tasks.waitFor(clusterName, readClusterName, 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
key.set(prefix + "mapper.cluster.name");
Tasks.waitFor(clusterName, readClusterName, 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
key.set(prefix + "reducer.cluster.name");
Tasks.waitFor(clusterName, readClusterName, 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
key.set(prefix + "spark.cluster.name");
Tasks.waitFor(clusterName, readClusterName, 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
key.set(prefix + "action.cluster.name");
Tasks.waitFor(clusterName, readClusterName, 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
workflowManager.waitForRun(ProgramRunStatus.COMPLETED, 120, TimeUnit.SECONDS);
}
use of co.cask.cdap.test.FlowManager in project cdap by caskdata.
the class TestFrameworkTestRun method testByteCodeClassLoader.
@Category(XSlowTests.class)
@Test
public void testByteCodeClassLoader() throws Exception {
// This test verify bytecode generated classes ClassLoading
ApplicationManager appManager = deployApplication(testSpace, ClassLoaderTestApp.class);
FlowManager flowManager = appManager.getFlowManager("BasicFlow").start();
// Wait for at least 10 records being generated
RuntimeMetrics flowMetrics = flowManager.getFlowletMetrics("Sink");
flowMetrics.waitForProcessed(10, 5000, TimeUnit.MILLISECONDS);
flowManager.stop();
ServiceManager serviceManager = appManager.getServiceManager("RecordQuery").start();
URL serviceURL = serviceManager.getServiceURL(15, TimeUnit.SECONDS);
Assert.assertNotNull(serviceURL);
// Query record
URL url = new URL(serviceURL, "query?type=public");
HttpRequest request = HttpRequest.get(url).build();
HttpResponse response = HttpRequests.execute(request);
Assert.assertEquals(200, response.getResponseCode());
long count = Long.parseLong(response.getResponseBodyAsString());
serviceManager.stop();
// Verify the record count with dataset
DataSetManager<KeyValueTable> recordsManager = getDataset(testSpace.dataset("records"));
KeyValueTable records = recordsManager.get();
Assert.assertTrue(count == Bytes.toLong(records.read("PUBLIC")));
}
use of co.cask.cdap.test.FlowManager in project cdap by caskdata.
the class TestFrameworkTestRun method testFlowletMetricsReset.
@Test(timeout = 60000L)
public void testFlowletMetricsReset() throws Exception {
ApplicationManager appManager = deployApplication(DataSetInitApp.class);
FlowManager flowManager = appManager.getFlowManager("DataSetFlow").start();
RuntimeMetrics flowletMetrics = flowManager.getFlowletMetrics("Consumer");
flowletMetrics.waitForProcessed(1, 5, TimeUnit.SECONDS);
flowManager.stop();
Assert.assertEquals(1, flowletMetrics.getProcessed());
getMetricsManager().resetAll();
// check the metrics were deleted after reset
Assert.assertEquals(0, flowletMetrics.getProcessed());
}
use of co.cask.cdap.test.FlowManager in project cdap by caskdata.
the class TestFrameworkTestRun method testMultiInput.
@Category(XSlowTests.class)
@Test(timeout = 240000)
public void testMultiInput() throws Exception {
ApplicationManager applicationManager = deployApplication(JoinMultiStreamApp.class);
FlowManager flowManager = applicationManager.getFlowManager("JoinMultiFlow").start();
StreamManager s1 = getStreamManager("s1");
StreamManager s2 = getStreamManager("s2");
StreamManager s3 = getStreamManager("s3");
s1.send("testing 1");
s2.send("testing 2");
s3.send("testing 3");
RuntimeMetrics terminalMetrics = flowManager.getFlowletMetrics("Terminal");
terminalMetrics.waitForProcessed(3, 60, TimeUnit.SECONDS);
TimeUnit.SECONDS.sleep(1);
ServiceManager queryManager = applicationManager.getServiceManager("QueryService").start();
queryManager.waitForStatus(true, 2, 1);
URL serviceURL = queryManager.getServiceURL();
Gson gson = new Gson();
Assert.assertEquals("testing 1", gson.fromJson(callServiceGet(serviceURL, "input1"), String.class));
Assert.assertEquals("testing 2", gson.fromJson(callServiceGet(serviceURL, "input2"), String.class));
Assert.assertEquals("testing 3", gson.fromJson(callServiceGet(serviceURL, "input3"), String.class));
}
use of co.cask.cdap.test.FlowManager in project cdap by caskdata.
the class TestFrameworkTestRun method testApp.
// todo: passing stream name as a workaround for not cleaning up streams during reset()
private void testApp(Class<? extends Application> app, String streamName) throws Exception {
ApplicationManager applicationManager = deployApplication(app);
FlowManager flowManager = applicationManager.getFlowManager("WordCountFlow").start();
// Send some inputs to streams
StreamManager streamManager = getStreamManager(streamName);
for (int i = 0; i < 100; i++) {
streamManager.send(ImmutableMap.of("title", "title " + i), "testing message " + i);
}
// Check the flowlet metrics
RuntimeMetrics flowletMetrics = flowManager.getFlowletMetrics("CountByField");
flowletMetrics.waitForProcessed(500, 10, TimeUnit.SECONDS);
Assert.assertEquals(0L, flowletMetrics.getException());
// Query the result
ServiceManager serviceManager = applicationManager.getServiceManager("WordFrequency").start();
serviceManager.waitForStatus(true, 2, 1);
// Verify the query result
Type resultType = new TypeToken<Map<String, Long>>() {
}.getType();
Map<String, Long> result = new Gson().fromJson(callServiceGet(serviceManager.getServiceURL(), "wordfreq/" + streamName + ":testing"), resultType);
Assert.assertNotNull(result);
Assert.assertEquals(100L, result.get(streamName + ":testing").longValue());
// check the metrics
RuntimeMetrics serviceMetrics = serviceManager.getMetrics();
serviceMetrics.waitForProcessed(1, 5, TimeUnit.SECONDS);
Assert.assertEquals(0L, serviceMetrics.getException());
// Run mapreduce job
MapReduceManager mrManager = applicationManager.getMapReduceManager("countTotal").start();
mrManager.waitForRun(ProgramRunStatus.COMPLETED, 1800L, TimeUnit.SECONDS);
long totalCount = Long.valueOf(callServiceGet(serviceManager.getServiceURL(), "total"));
// every event has 5 tokens
Assert.assertEquals(5 * 100L, totalCount);
// Run mapreduce from stream
mrManager = applicationManager.getMapReduceManager("countFromStream").start();
mrManager.waitForRun(ProgramRunStatus.COMPLETED, 120L, TimeUnit.SECONDS);
totalCount = Long.valueOf(callServiceGet(serviceManager.getServiceURL(), "stream_total"));
// The stream MR only consume the body, not the header.
Assert.assertEquals(3 * 100L, totalCount);
DataSetManager<MyKeyValueTableDefinition.KeyValueTable> mydatasetManager = getDataset("mydataset");
Assert.assertEquals(100L, Long.valueOf(mydatasetManager.get().get("title:title")).longValue());
// also test the deprecated version of getDataset(). This can be removed when we remove the method
mydatasetManager = getDataset("mydataset");
Assert.assertEquals(100L, Long.valueOf(mydatasetManager.get().get("title:title")).longValue());
}
Aggregations