use of co.cask.cdap.api.metrics.RuntimeMetrics in project cdap by caskdata.
the class SparkKMeansAppTest method test.
@Test
public void test() throws Exception {
// Deploy the Application
ApplicationManager appManager = deployApplication(SparkKMeansApp.class);
// Start the Flow
FlowManager flowManager = appManager.getFlowManager("PointsFlow").start();
// Send a few points to the stream
StreamManager streamManager = getStreamManager("pointsStream");
// one cluster around (0, 500, 0) and another around (100, 0, 0)
for (int i = 0; i < 100; i++) {
double diff = Math.random() / 100;
streamManager.send(String.format("%f %f %f", diff, 500 + diff, diff));
streamManager.send(String.format("%f %f %f", 100 + diff, diff, diff));
}
// Wait for the events to be processed, or at most 5 seconds
RuntimeMetrics metrics = flowManager.getFlowletMetrics("reader");
metrics.waitForProcessed(200, 10, TimeUnit.SECONDS);
// Start a Spark Program
SparkManager sparkManager = appManager.getSparkManager("SparkKMeansProgram").start();
sparkManager.waitForRun(ProgramRunStatus.COMPLETED, 60, TimeUnit.SECONDS);
flowManager.stop();
// Start CentersService
ServiceManager serviceManager = appManager.getServiceManager(SparkKMeansApp.CentersService.SERVICE_NAME).start();
// Wait service startup
serviceManager.waitForStatus(true);
// Request data and verify it
String response = requestService(new URL(serviceManager.getServiceURL(15, TimeUnit.SECONDS), "centers/0"));
String[] coordinates = response.split(",");
int x0 = Double.valueOf(coordinates[0]).intValue();
int y0 = Double.valueOf(coordinates[1]).intValue();
int z0 = Double.valueOf(coordinates[2]).intValue();
response = requestService(new URL(serviceManager.getServiceURL(15, TimeUnit.SECONDS), "centers/1"));
coordinates = response.split(",");
int x1 = Double.valueOf(coordinates[0]).intValue();
int y1 = Double.valueOf(coordinates[1]).intValue();
int z1 = Double.valueOf(coordinates[2]).intValue();
// one cluster should be around (0, 500, 0) and the other around (100, 0, 0)
if (x0 == 100) {
Assert.assertEquals(0, y0);
Assert.assertEquals(0, z0);
Assert.assertEquals(0, x1);
Assert.assertEquals(500, y1);
Assert.assertEquals(0, z1);
} else {
Assert.assertEquals(0, x0);
Assert.assertEquals(500, y0);
Assert.assertEquals(0, z0);
Assert.assertEquals(100, x1);
Assert.assertEquals(0, y1);
Assert.assertEquals(0, z1);
}
// Request data by incorrect index and verify response
URL url = new URL(serviceManager.getServiceURL(15, TimeUnit.SECONDS), "centers/10");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
try {
Assert.assertEquals(HttpURLConnection.HTTP_NO_CONTENT, conn.getResponseCode());
} finally {
conn.disconnect();
}
}
Aggregations