use of io.cdap.cdap.api.service.worker.RunnableTaskRequest in project cdap by caskdata.
the class RunnableTaskLauncherTest method testRunnableTaskRequestJsonConversion.
@Test
public void testRunnableTaskRequestJsonConversion() {
ArtifactId artifactId = new ArtifactId("test-artifact", new ArtifactVersion("1.0"), ArtifactScope.SYSTEM);
RunnableTaskRequest request = RunnableTaskRequest.getBuilder("taskClassName").withParam("param").withNamespace("n1").withArtifact(artifactId).build();
Gson gson = new Gson();
String requestJson = gson.toJson(request);
RunnableTaskRequest requestFromJson = gson.fromJson(requestJson, RunnableTaskRequest.class);
Assert.assertEquals(request.getClassName(), requestFromJson.getClassName());
Assert.assertEquals(request.getNamespace(), requestFromJson.getNamespace());
Assert.assertEquals(request.getParam(), requestFromJson.getParam());
Assert.assertEquals(request.getArtifactId(), requestFromJson.getArtifactId());
}
use of io.cdap.cdap.api.service.worker.RunnableTaskRequest in project cdap by caskdata.
the class TaskWorkerMetricsTest method testWrappedRequest.
@Test
public void testWrappedRequest() throws IOException {
String taskClassName = TaskWorkerServiceTest.TestRunnableClass.class.getName();
String wrappedClassName = "testClassName";
RunnableTaskRequest req = RunnableTaskRequest.getBuilder(taskClassName).withParam("100").withEmbeddedTaskRequest(RunnableTaskRequest.getBuilder(wrappedClassName).build()).build();
String reqBody = GSON.toJson(req);
HttpResponse response = HttpRequests.execute(HttpRequest.post(uri.resolve("/v3Internal/worker/run").toURL()).withBody(reqBody).build(), new DefaultHttpRequestConfig(false));
TaskWorkerTestUtil.waitForServiceCompletion(taskWorkerStateFuture);
Assert.assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
Assert.assertSame(1, published.size());
// check the metrics are present
MetricValues metricValues = published.get(0);
Assert.assertTrue(hasMetric(metricValues, Constants.Metrics.TaskWorker.REQUEST_COUNT));
Assert.assertTrue(hasMetric(metricValues, Constants.Metrics.TaskWorker.REQUEST_LATENCY_MS));
// check the clz tag is set correctly
Assert.assertEquals(wrappedClassName, metricValues.getTags().get(Constants.Metrics.Tag.CLASS));
}
use of io.cdap.cdap.api.service.worker.RunnableTaskRequest in project cdap by caskdata.
the class TaskWorkerMetricsTest method testSimpleRequest.
@Test
public void testSimpleRequest() throws IOException {
String taskClassName = TaskWorkerServiceTest.TestRunnableClass.class.getName();
RunnableTaskRequest req = RunnableTaskRequest.getBuilder(taskClassName).withParam("100").build();
String reqBody = GSON.toJson(req);
HttpResponse response = HttpRequests.execute(HttpRequest.post(uri.resolve("/v3Internal/worker/run").toURL()).withBody(reqBody).build(), new DefaultHttpRequestConfig(false));
TaskWorkerTestUtil.waitForServiceCompletion(taskWorkerStateFuture);
Assert.assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
Assert.assertSame(1, published.size());
// check the metrics are present
MetricValues metricValues = published.get(0);
Assert.assertTrue(hasMetric(metricValues, Constants.Metrics.TaskWorker.REQUEST_LATENCY_MS));
Assert.assertTrue(hasMetric(metricValues, Constants.Metrics.TaskWorker.REQUEST_COUNT));
// check the clz tag is set correctly
Assert.assertEquals(taskClassName, metricValues.getTags().get("clz"));
}
use of io.cdap.cdap.api.service.worker.RunnableTaskRequest in project cdap by caskdata.
the class TaskWorkerServiceTest method testStartAndStopWithInvalidRequest.
@Test
public void testStartAndStopWithInvalidRequest() throws Exception {
InetSocketAddress addr = taskWorkerService.getBindAddress();
URI uri = URI.create(String.format("http://%s:%s", addr.getHostName(), addr.getPort()));
// Post invalid request
RunnableTaskRequest noClassReq = RunnableTaskRequest.getBuilder("NoClass").build();
String reqBody = GSON.toJson(noClassReq);
HttpResponse response = HttpRequests.execute(HttpRequest.post(uri.resolve("/v3Internal/worker/run").toURL()).withBody(reqBody).build(), new DefaultHttpRequestConfig(false));
Assert.assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, response.getResponseCode());
BasicThrowable basicThrowable;
basicThrowable = GSON.fromJson(response.getResponseBodyAsString(), BasicThrowable.class);
Assert.assertTrue(basicThrowable.getClassName().contains("java.lang.ClassNotFoundException"));
Assert.assertNotNull(basicThrowable.getMessage());
Assert.assertTrue(basicThrowable.getMessage().contains("NoClass"));
Assert.assertNotEquals(basicThrowable.getStackTraces().length, 0);
}
use of io.cdap.cdap.api.service.worker.RunnableTaskRequest in project cdap by caskdata.
the class TaskWorkerServiceTest method testStartAndStopWithValidRequest.
@Test
public void testStartAndStopWithValidRequest() throws IOException {
InetSocketAddress addr = taskWorkerService.getBindAddress();
URI uri = URI.create(String.format("http://%s:%s", addr.getHostName(), addr.getPort()));
// Post valid request
String want = "100";
RunnableTaskRequest req = RunnableTaskRequest.getBuilder(TestRunnableClass.class.getName()).withParam(want).build();
String reqBody = GSON.toJson(req);
HttpResponse response = HttpRequests.execute(HttpRequest.post(uri.resolve("/v3Internal/worker/run").toURL()).withBody(reqBody).build(), new DefaultHttpRequestConfig(false));
TaskWorkerTestUtil.waitForServiceCompletion(serviceCompletionFuture);
Assert.assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
Assert.assertEquals(want, response.getResponseBodyAsString());
Assert.assertEquals(Service.State.TERMINATED, taskWorkerService.state());
}
Aggregations