use of io.cdap.cdap.test.ServiceManager in project cdap by caskdata.
the class TestFrameworkTestRun method testAppWithServices.
@Category(SlowTests.class)
@Test
public void testAppWithServices() throws Exception {
ApplicationManager applicationManager = deployApplication(AppWithServices.class);
LOG.info("Deployed.");
ServiceManager serviceManager = applicationManager.getServiceManager(AppWithServices.SERVICE_NAME).start();
serviceManager.waitForRun(ProgramRunStatus.RUNNING, 10, TimeUnit.SECONDS);
LOG.info("Service Started");
URL serviceURL = serviceManager.getServiceURL(15, TimeUnit.SECONDS);
Assert.assertNotNull(serviceURL);
// Call the ping endpoint
URL url = new URL(serviceURL, "ping2");
HttpRequest request = HttpRequest.get(url).build();
HttpResponse response = executeHttp(request);
Assert.assertEquals(200, response.getResponseCode());
// Call the failure endpoint
url = new URL(serviceURL, "failure");
request = HttpRequest.get(url).build();
response = executeHttp(request);
Assert.assertEquals(500, response.getResponseCode());
Assert.assertTrue(response.getResponseBodyAsString().contains("Exception"));
// Call the verify ClassLoader endpoint
url = new URL(serviceURL, "verifyClassLoader");
request = HttpRequest.get(url).build();
response = executeHttp(request);
Assert.assertEquals(200, response.getResponseCode());
RuntimeMetrics serviceMetrics = serviceManager.getMetrics();
serviceMetrics.waitForinput(3, 5, TimeUnit.SECONDS);
Assert.assertEquals(3, serviceMetrics.getInput());
Assert.assertEquals(2, serviceMetrics.getProcessed());
Assert.assertEquals(1, serviceMetrics.getException());
// in the AppWithServices the handlerName is same as the serviceName - "ServerService" handler
RuntimeMetrics handlerMetrics = getMetricsManager().getServiceHandlerMetrics(NamespaceId.DEFAULT.getNamespace(), AppWithServices.APP_NAME, AppWithServices.SERVICE_NAME, AppWithServices.SERVICE_NAME);
handlerMetrics.waitForinput(3, 5, TimeUnit.SECONDS);
Assert.assertEquals(3, handlerMetrics.getInput());
Assert.assertEquals(2, handlerMetrics.getProcessed());
Assert.assertEquals(1, handlerMetrics.getException());
// we can verify metrics, by adding getServiceMetrics in MetricsManager and then disabling the system scope test in
// TestMetricsCollectionService
LOG.info("DatasetUpdateService Started");
Map<String, String> args = ImmutableMap.of(AppWithServices.WRITE_VALUE_RUN_KEY, AppWithServices.DATASET_TEST_VALUE, AppWithServices.WRITE_VALUE_STOP_KEY, AppWithServices.DATASET_TEST_VALUE_STOP);
ServiceManager datasetWorkerServiceManager = applicationManager.getServiceManager(AppWithServices.DATASET_WORKER_SERVICE_NAME).start(args);
WorkerManager datasetWorker = applicationManager.getWorkerManager(AppWithServices.DATASET_UPDATE_WORKER).start(args);
datasetWorker.waitForRun(ProgramRunStatus.RUNNING, 10, TimeUnit.SECONDS);
datasetWorkerServiceManager.waitForRun(ProgramRunStatus.RUNNING, 10, TimeUnit.SECONDS);
ServiceManager noopManager = applicationManager.getServiceManager("NoOpService").start();
noopManager.waitForRun(ProgramRunStatus.RUNNING, 10, TimeUnit.SECONDS);
// We don't know when the datasetWorker run() method executed, hence need to retry on the service call
// until it can get a value from a dataset, which was written out by the datasetWorker.
AtomicInteger called = new AtomicInteger(0);
AtomicInteger failed = new AtomicInteger(0);
Tasks.waitFor(AppWithServices.DATASET_TEST_VALUE, new Callable<String>() {
@Override
public String call() throws Exception {
URL url = noopManager.getServiceURL();
String path = "ping/" + AppWithServices.DATASET_TEST_KEY;
try {
called.incrementAndGet();
return new Gson().fromJson(callServiceGet(url, path), String.class);
} catch (IOException e) {
failed.incrementAndGet();
LOG.debug("Exception when reading from service {}/{}", url, path, e);
}
return null;
}
}, 30, TimeUnit.SECONDS);
// Validates the metrics emitted by the service call.
handlerMetrics = getMetricsManager().getServiceHandlerMetrics(NamespaceId.DEFAULT.getNamespace(), AppWithServices.APP_NAME, "NoOpService", "NoOpHandler");
handlerMetrics.waitForinput(called.get(), 5, TimeUnit.SECONDS);
handlerMetrics.waitForProcessed(1, 5, TimeUnit.SECONDS);
handlerMetrics.waitForException(failed.get(), 5, TimeUnit.SECONDS);
// Test that a service can discover another service
String path = String.format("discover/%s/%s", AppWithServices.APP_NAME, AppWithServices.DATASET_WORKER_SERVICE_NAME);
url = new URL(serviceURL, path);
request = HttpRequest.get(url).build();
response = executeHttp(request);
Assert.assertEquals(200, response.getResponseCode());
datasetWorker.stop();
datasetWorker.waitForStopped(10, TimeUnit.SECONDS);
datasetWorkerServiceManager.stop();
datasetWorkerServiceManager.waitForStopped(10, TimeUnit.SECONDS);
LOG.info("DatasetUpdateService Stopped");
serviceManager.stop();
serviceManager.waitForStopped(10, TimeUnit.SECONDS);
LOG.info("ServerService Stopped");
// Since all worker are stopped, we can just hit the service to read the dataset. No retry needed.
String result = callServiceGet(noopManager.getServiceURL(), "ping/" + AppWithServices.DATASET_TEST_KEY_STOP);
String decodedResult = new Gson().fromJson(result, String.class);
Assert.assertEquals(AppWithServices.DATASET_TEST_VALUE_STOP, decodedResult);
result = callServiceGet(noopManager.getServiceURL(), "ping/" + AppWithServices.DATASET_TEST_KEY_STOP_2);
decodedResult = new Gson().fromJson(result, String.class);
Assert.assertEquals(AppWithServices.DATASET_TEST_VALUE_STOP_2, decodedResult);
}
use of io.cdap.cdap.test.ServiceManager in project cdap by caskdata.
the class TestFrameworkTestRun method testTransactionHandlerService.
@Test
public void testTransactionHandlerService() throws Exception {
ApplicationManager applicationManager = deployApplication(testSpace, AppWithServices.class);
LOG.info("Deployed.");
ServiceManager serviceManager = applicationManager.getServiceManager(AppWithServices.TRANSACTIONS_SERVICE_NAME).start();
serviceManager.waitForRun(ProgramRunStatus.RUNNING, 10, TimeUnit.SECONDS);
LOG.info("Service Started");
final URL baseUrl = serviceManager.getServiceURL(15, TimeUnit.SECONDS);
Assert.assertNotNull(baseUrl);
// Make a request to write in a separate thread and wait for it to return.
ExecutorService executorService = Executors.newSingleThreadExecutor();
Future<Integer> requestFuture = executorService.submit(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
try {
URL url = new URL(String.format("%s/write/%s/%s/%d", baseUrl, AppWithServices.DATASET_TEST_KEY, AppWithServices.DATASET_TEST_VALUE, 10000));
HttpRequest request = HttpRequest.get(url).build();
HttpResponse response = executeHttp(request);
return response.getResponseCode();
} catch (Exception e) {
LOG.error("Request thread got exception.", e);
throw Throwables.propagate(e);
}
}
});
// The dataset should not be written by the time this request is made, since the transaction to write
// has not been committed yet.
URL url = new URL(String.format("%s/read/%s", baseUrl, AppWithServices.DATASET_TEST_KEY));
HttpRequest request = HttpRequest.get(url).build();
HttpResponse response = executeHttp(request);
Assert.assertEquals(204, response.getResponseCode());
// Wait for the transaction to commit.
Integer writeStatusCode = requestFuture.get();
Assert.assertEquals(200, writeStatusCode.intValue());
// Make the same request again. By now the transaction should've completed.
request = HttpRequest.get(url).build();
response = executeHttp(request);
Assert.assertEquals(200, response.getResponseCode());
Assert.assertEquals(AppWithServices.DATASET_TEST_VALUE, new Gson().fromJson(response.getResponseBodyAsString(), String.class));
executorService.shutdown();
serviceManager.stop();
serviceManager.waitForStopped(10, TimeUnit.SECONDS);
DataSetManager<KeyValueTable> dsManager = getDataset(testSpace.dataset(AppWithServices.TRANSACTIONS_DATASET_NAME));
String value = Bytes.toString(dsManager.get().read(AppWithServices.DESTROY_KEY));
Assert.assertEquals(AppWithServices.VALUE, value);
}
use of io.cdap.cdap.test.ServiceManager in project cdap by cdapio.
the class TestAppWithCube method testApp.
@Category(SlowTests.class)
@Test
public void testApp() throws Exception {
// Deploy the application
ApplicationManager appManager = deployApplication(AppWithCube.class);
ServiceManager serviceManager = appManager.getServiceManager(AppWithCube.SERVICE_NAME).start();
try {
serviceManager.waitForRun(ProgramRunStatus.RUNNING, 10, TimeUnit.SECONDS);
URL url = serviceManager.getServiceURL();
long tsInSec = System.currentTimeMillis() / 1000;
// round to a minute for testing minute resolution
tsInSec = (tsInSec / 60) * 60;
// add couple facts
add(url, ImmutableList.of(new CubeFact(tsInSec).addDimensionValue("user", "alex").addDimensionValue("action", "click").addMeasurement("count", MeasureType.COUNTER, 1)));
add(url, ImmutableList.of(new CubeFact(tsInSec).addDimensionValue("user", "alex").addDimensionValue("action", "click").addMeasurement("count", MeasureType.COUNTER, 1), new CubeFact(tsInSec + 1).addDimensionValue("user", "alex").addDimensionValue("action", "back").addMeasurement("count", MeasureType.COUNTER, 1), new CubeFact(tsInSec + 2).addDimensionValue("user", "alex").addDimensionValue("action", "click").addMeasurement("count", MeasureType.COUNTER, 1)));
// search for tags
Collection<DimensionValue> tags = searchDimensionValue(url, new CubeExploreQuery(tsInSec - 60, tsInSec + 60, 1, 100, new ArrayList<DimensionValue>()));
Assert.assertEquals(1, tags.size());
DimensionValue tv = tags.iterator().next();
Assert.assertEquals("user", tv.getName());
Assert.assertEquals("alex", tv.getValue());
tags = searchDimensionValue(url, CubeExploreQuery.builder().from().resolution(1, TimeUnit.SECONDS).where().dimension("user", "alex").timeRange(tsInSec - 60, tsInSec + 60).limit(100).build());
Assert.assertEquals(2, tags.size());
Iterator<DimensionValue> iterator = tags.iterator();
tv = iterator.next();
Assert.assertEquals("action", tv.getName());
Assert.assertEquals("back", tv.getValue());
tv = iterator.next();
Assert.assertEquals("action", tv.getName());
Assert.assertEquals("click", tv.getValue());
// search for measures
Collection<String> measures = searchMeasure(url, new CubeExploreQuery(tsInSec - 60, tsInSec + 60, 1, 100, ImmutableList.of(new DimensionValue("user", "alex"))));
Assert.assertEquals(1, measures.size());
String measure = measures.iterator().next();
Assert.assertEquals("count", measure);
// query for data
// 1-sec resolution
Collection<TimeSeries> data = query(url, CubeQuery.builder().select().measurement("count", AggregationFunction.SUM).from(null).resolution(1, TimeUnit.SECONDS).where().dimension("action", "click").timeRange(tsInSec - 60, tsInSec + 60).limit(100).build());
Assert.assertEquals(1, data.size());
TimeSeries series = data.iterator().next();
List<TimeValue> timeValues = series.getTimeValues();
Assert.assertEquals(2, timeValues.size());
TimeValue timeValue = timeValues.get(0);
Assert.assertEquals(tsInSec, timeValue.getTimestamp());
Assert.assertEquals(2, timeValue.getValue());
timeValue = timeValues.get(1);
Assert.assertEquals(tsInSec + 2, timeValue.getTimestamp());
Assert.assertEquals(1, timeValue.getValue());
// 60-sec resolution
data = query(url, new CubeQuery(null, tsInSec - 60, tsInSec + 60, 60, 100, ImmutableMap.of("count", AggregationFunction.SUM), ImmutableMap.of("action", "click"), new ArrayList<String>(), null, null));
Assert.assertEquals(1, data.size());
series = data.iterator().next();
timeValues = series.getTimeValues();
Assert.assertEquals(1, timeValues.size());
timeValue = timeValues.get(0);
Assert.assertEquals(tsInSec, timeValue.getTimestamp());
Assert.assertEquals(3, timeValue.getValue());
} finally {
serviceManager.stop();
serviceManager.waitForStopped(10, TimeUnit.SECONDS);
}
}
use of io.cdap.cdap.test.ServiceManager in project cdap by cdapio.
the class TestFrameworkTestRun method testAppWithServices.
@Category(SlowTests.class)
@Test
public void testAppWithServices() throws Exception {
ApplicationManager applicationManager = deployApplication(AppWithServices.class);
LOG.info("Deployed.");
ServiceManager serviceManager = applicationManager.getServiceManager(AppWithServices.SERVICE_NAME).start();
serviceManager.waitForRun(ProgramRunStatus.RUNNING, 10, TimeUnit.SECONDS);
LOG.info("Service Started");
URL serviceURL = serviceManager.getServiceURL(15, TimeUnit.SECONDS);
Assert.assertNotNull(serviceURL);
// Call the ping endpoint
URL url = new URL(serviceURL, "ping2");
HttpRequest request = HttpRequest.get(url).build();
HttpResponse response = executeHttp(request);
Assert.assertEquals(200, response.getResponseCode());
// Call the failure endpoint
url = new URL(serviceURL, "failure");
request = HttpRequest.get(url).build();
response = executeHttp(request);
Assert.assertEquals(500, response.getResponseCode());
Assert.assertTrue(response.getResponseBodyAsString().contains("Exception"));
// Call the verify ClassLoader endpoint
url = new URL(serviceURL, "verifyClassLoader");
request = HttpRequest.get(url).build();
response = executeHttp(request);
Assert.assertEquals(200, response.getResponseCode());
RuntimeMetrics serviceMetrics = serviceManager.getMetrics();
serviceMetrics.waitForinput(3, 5, TimeUnit.SECONDS);
Assert.assertEquals(3, serviceMetrics.getInput());
Assert.assertEquals(2, serviceMetrics.getProcessed());
Assert.assertEquals(1, serviceMetrics.getException());
// in the AppWithServices the handlerName is same as the serviceName - "ServerService" handler
RuntimeMetrics handlerMetrics = getMetricsManager().getServiceHandlerMetrics(NamespaceId.DEFAULT.getNamespace(), AppWithServices.APP_NAME, AppWithServices.SERVICE_NAME, AppWithServices.SERVICE_NAME);
handlerMetrics.waitForinput(3, 5, TimeUnit.SECONDS);
Assert.assertEquals(3, handlerMetrics.getInput());
Assert.assertEquals(2, handlerMetrics.getProcessed());
Assert.assertEquals(1, handlerMetrics.getException());
// we can verify metrics, by adding getServiceMetrics in MetricsManager and then disabling the system scope test in
// TestMetricsCollectionService
LOG.info("DatasetUpdateService Started");
Map<String, String> args = ImmutableMap.of(AppWithServices.WRITE_VALUE_RUN_KEY, AppWithServices.DATASET_TEST_VALUE, AppWithServices.WRITE_VALUE_STOP_KEY, AppWithServices.DATASET_TEST_VALUE_STOP);
ServiceManager datasetWorkerServiceManager = applicationManager.getServiceManager(AppWithServices.DATASET_WORKER_SERVICE_NAME).start(args);
WorkerManager datasetWorker = applicationManager.getWorkerManager(AppWithServices.DATASET_UPDATE_WORKER).start(args);
datasetWorker.waitForRun(ProgramRunStatus.RUNNING, 10, TimeUnit.SECONDS);
datasetWorkerServiceManager.waitForRun(ProgramRunStatus.RUNNING, 10, TimeUnit.SECONDS);
ServiceManager noopManager = applicationManager.getServiceManager("NoOpService").start();
noopManager.waitForRun(ProgramRunStatus.RUNNING, 10, TimeUnit.SECONDS);
// We don't know when the datasetWorker run() method executed, hence need to retry on the service call
// until it can get a value from a dataset, which was written out by the datasetWorker.
AtomicInteger called = new AtomicInteger(0);
AtomicInteger failed = new AtomicInteger(0);
Tasks.waitFor(AppWithServices.DATASET_TEST_VALUE, new Callable<String>() {
@Override
public String call() throws Exception {
URL url = noopManager.getServiceURL();
String path = "ping/" + AppWithServices.DATASET_TEST_KEY;
try {
called.incrementAndGet();
return new Gson().fromJson(callServiceGet(url, path), String.class);
} catch (IOException e) {
failed.incrementAndGet();
LOG.debug("Exception when reading from service {}/{}", url, path, e);
}
return null;
}
}, 30, TimeUnit.SECONDS);
// Validates the metrics emitted by the service call.
handlerMetrics = getMetricsManager().getServiceHandlerMetrics(NamespaceId.DEFAULT.getNamespace(), AppWithServices.APP_NAME, "NoOpService", "NoOpHandler");
handlerMetrics.waitForinput(called.get(), 5, TimeUnit.SECONDS);
handlerMetrics.waitForProcessed(1, 5, TimeUnit.SECONDS);
handlerMetrics.waitForException(failed.get(), 5, TimeUnit.SECONDS);
// Test that a service can discover another service
String path = String.format("discover/%s/%s", AppWithServices.APP_NAME, AppWithServices.DATASET_WORKER_SERVICE_NAME);
url = new URL(serviceURL, path);
request = HttpRequest.get(url).build();
response = executeHttp(request);
Assert.assertEquals(200, response.getResponseCode());
datasetWorker.stop();
datasetWorker.waitForStopped(10, TimeUnit.SECONDS);
datasetWorkerServiceManager.stop();
datasetWorkerServiceManager.waitForStopped(10, TimeUnit.SECONDS);
LOG.info("DatasetUpdateService Stopped");
serviceManager.stop();
serviceManager.waitForStopped(10, TimeUnit.SECONDS);
LOG.info("ServerService Stopped");
// Since all worker are stopped, we can just hit the service to read the dataset. No retry needed.
String result = callServiceGet(noopManager.getServiceURL(), "ping/" + AppWithServices.DATASET_TEST_KEY_STOP);
String decodedResult = new Gson().fromJson(result, String.class);
Assert.assertEquals(AppWithServices.DATASET_TEST_VALUE_STOP, decodedResult);
result = callServiceGet(noopManager.getServiceURL(), "ping/" + AppWithServices.DATASET_TEST_KEY_STOP_2);
decodedResult = new Gson().fromJson(result, String.class);
Assert.assertEquals(AppWithServices.DATASET_TEST_VALUE_STOP_2, decodedResult);
}
use of io.cdap.cdap.test.ServiceManager in project cdap by cdapio.
the class TestFrameworkTestRun method testServiceManager.
@Test
public void testServiceManager() throws Exception {
ApplicationManager applicationManager = deployApplication(AppWithServices.class);
final ServiceManager serviceManager = applicationManager.getServiceManager(AppWithServices.SERVICE_NAME);
serviceManager.setInstances(2);
Assert.assertEquals(0, serviceManager.getProvisionedInstances());
Assert.assertEquals(2, serviceManager.getRequestedInstances());
Assert.assertFalse(serviceManager.isRunning());
List<RunRecord> history = serviceManager.getHistory();
Assert.assertEquals(0, history.size());
serviceManager.start();
serviceManager.waitForRun(ProgramRunStatus.RUNNING, 10, TimeUnit.SECONDS);
Assert.assertEquals(2, serviceManager.getProvisionedInstances());
// requesting with ProgramRunStatus.KILLED returns empty list
history = serviceManager.getHistory(ProgramRunStatus.KILLED);
Assert.assertEquals(0, history.size());
// requesting with either RUNNING or ALL will return one record
Tasks.waitFor(1, new Callable<Integer>() {
@Override
public Integer call() throws Exception {
return serviceManager.getHistory(ProgramRunStatus.RUNNING).size();
}
}, 5, TimeUnit.SECONDS);
history = serviceManager.getHistory(ProgramRunStatus.RUNNING);
Assert.assertEquals(ProgramRunStatus.RUNNING, history.get(0).getStatus());
history = serviceManager.getHistory(ProgramRunStatus.ALL);
Assert.assertEquals(1, history.size());
Assert.assertEquals(ProgramRunStatus.RUNNING, history.get(0).getStatus());
}
Aggregations