Search in sources :

Example 1 with ServiceManager

use of io.cdap.cdap.test.ServiceManager in project cdap by caskdata.

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());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RunRecord(io.cdap.cdap.proto.RunRecord) ApplicationManager(io.cdap.cdap.test.ApplicationManager) ServiceManager(io.cdap.cdap.test.ServiceManager) IOException(java.io.IOException) ConflictException(io.cdap.cdap.common.ConflictException) Test(org.junit.Test)

Example 2 with ServiceManager

use of io.cdap.cdap.test.ServiceManager in project cdap by caskdata.

the class TestFrameworkTestRun method testAppWithPlugin.

@Test
public void testAppWithPlugin() throws Exception {
    ArtifactId artifactId = NamespaceId.DEFAULT.artifact("app-with-plugin", "1.0.0-SNAPSHOT");
    addAppArtifact(artifactId, AppWithPlugin.class);
    ArtifactId pluginArtifactId = NamespaceId.DEFAULT.artifact("test-plugin", "1.0.0-SNAPSHOT");
    addPluginArtifact(pluginArtifactId, artifactId, ToStringPlugin.class);
    ApplicationId appId = NamespaceId.DEFAULT.app("AppWithPlugin");
    AppRequest createRequest = new AppRequest(new ArtifactSummary(artifactId.getArtifact(), artifactId.getVersion()));
    ApplicationManager appManager = deployApplication(appId, createRequest);
    final WorkerManager workerManager = appManager.getWorkerManager(AppWithPlugin.WORKER);
    workerManager.start();
    workerManager.waitForRun(ProgramRunStatus.COMPLETED, 10, TimeUnit.SECONDS);
    final ServiceManager serviceManager = appManager.getServiceManager(AppWithPlugin.SERVICE);
    serviceManager.start();
    serviceManager.waitForRun(ProgramRunStatus.RUNNING, 10, TimeUnit.SECONDS);
    URL serviceURL = serviceManager.getServiceURL(5, TimeUnit.SECONDS);
    callServiceGet(serviceURL, "dummy");
    serviceManager.stop();
    serviceManager.waitForStopped(10, TimeUnit.SECONDS);
    WorkflowManager workflowManager = appManager.getWorkflowManager(AppWithPlugin.WORKFLOW);
    workflowManager.start();
    workflowManager.waitForRun(ProgramRunStatus.COMPLETED, 5, TimeUnit.MINUTES);
    List<RunRecord> runRecords = workflowManager.getHistory();
    Assert.assertNotEquals(ProgramRunStatus.FAILED, runRecords.get(0).getStatus());
    DataSetManager<KeyValueTable> workflowTableManager = getDataset(AppWithPlugin.WORKFLOW_TABLE);
    String value = Bytes.toString(workflowTableManager.get().read("val"));
    Assert.assertEquals(AppWithPlugin.TEST, value);
    Map<String, String> workflowTags = ImmutableMap.of(Constants.Metrics.Tag.NAMESPACE, NamespaceId.DEFAULT.getNamespace(), Constants.Metrics.Tag.APP, "AppWithPlugin", Constants.Metrics.Tag.WORKFLOW, AppWithPlugin.WORKFLOW, Constants.Metrics.Tag.RUN_ID, runRecords.get(0).getPid());
    getMetricsManager().waitForTotalMetricCount(workflowTags, String.format("user.destroy.%s", AppWithPlugin.WORKFLOW), 1, 60, TimeUnit.SECONDS);
    // Testing Spark Plugins. First send some data to fileset for the Spark program to process
    DataSetManager<FileSet> fileSetManager = getDataset(AppWithPlugin.SPARK_INPUT);
    FileSet fileSet = fileSetManager.get();
    try (PrintStream out = new PrintStream(fileSet.getLocation("input").append("file.txt").getOutputStream(), true, "UTF-8")) {
        for (int i = 0; i < 5; i++) {
            out.println("Message " + i);
        }
    }
    Map<String, String> sparkArgs = new HashMap<>();
    FileSetArguments.setInputPath(sparkArgs, "input");
    SparkManager sparkManager = appManager.getSparkManager(AppWithPlugin.SPARK).start(sparkArgs);
    sparkManager.waitForRun(ProgramRunStatus.COMPLETED, 2, TimeUnit.MINUTES);
    // Verify the Spark result.
    DataSetManager<Table> dataSetManager = getDataset(AppWithPlugin.SPARK_TABLE);
    Table table = dataSetManager.get();
    try (Scanner scanner = table.scan(null, null)) {
        for (int i = 0; i < 5; i++) {
            Row row = scanner.next();
            Assert.assertNotNull(row);
            String expected = "Message " + i + " " + AppWithPlugin.TEST;
            Assert.assertEquals(expected, Bytes.toString(row.getRow()));
            Assert.assertEquals(expected, Bytes.toString(row.get(expected)));
        }
        // There shouldn't be any more rows in the table.
        Assert.assertNull(scanner.next());
    }
}
Also used : Scanner(io.cdap.cdap.api.dataset.table.Scanner) ApplicationManager(io.cdap.cdap.test.ApplicationManager) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) HashMap(java.util.HashMap) WorkflowManager(io.cdap.cdap.test.WorkflowManager) URL(java.net.URL) ServiceManager(io.cdap.cdap.test.ServiceManager) PrintStream(java.io.PrintStream) SparkManager(io.cdap.cdap.test.SparkManager) Table(io.cdap.cdap.api.dataset.table.Table) KeyValueTable(io.cdap.cdap.api.dataset.lib.KeyValueTable) FileSet(io.cdap.cdap.api.dataset.lib.FileSet) AppRequest(io.cdap.cdap.proto.artifact.AppRequest) WorkerManager(io.cdap.cdap.test.WorkerManager) RunRecord(io.cdap.cdap.proto.RunRecord) ArtifactSummary(io.cdap.cdap.api.artifact.ArtifactSummary) KeyValueTable(io.cdap.cdap.api.dataset.lib.KeyValueTable) Row(io.cdap.cdap.api.dataset.table.Row) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) Test(org.junit.Test)

Example 3 with ServiceManager

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);
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) ApplicationManager(io.cdap.cdap.test.ApplicationManager) RuntimeMetrics(io.cdap.cdap.api.metrics.RuntimeMetrics) HttpResponse(io.cdap.common.http.HttpResponse) Gson(com.google.gson.Gson) IOException(java.io.IOException) URL(java.net.URL) IOException(java.io.IOException) ConflictException(io.cdap.cdap.common.ConflictException) WorkerManager(io.cdap.cdap.test.WorkerManager) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServiceManager(io.cdap.cdap.test.ServiceManager) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Example 4 with ServiceManager

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);
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) ApplicationManager(io.cdap.cdap.test.ApplicationManager) HttpResponse(io.cdap.common.http.HttpResponse) Gson(com.google.gson.Gson) URL(java.net.URL) IOException(java.io.IOException) ConflictException(io.cdap.cdap.common.ConflictException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServiceManager(io.cdap.cdap.test.ServiceManager) KeyValueTable(io.cdap.cdap.api.dataset.lib.KeyValueTable) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Example 5 with ServiceManager

use of io.cdap.cdap.test.ServiceManager in project cdap by caskdata.

the class TestFrameworkTestRun method testAppWithDataset.

private void testAppWithDataset(Class<? extends Application> app, String serviceName) throws Exception {
    ApplicationManager applicationManager = deployApplication(app);
    // Query the result
    ServiceManager serviceManager = applicationManager.getServiceManager(serviceName).start();
    serviceManager.waitForRun(ProgramRunStatus.RUNNING, 10, TimeUnit.SECONDS);
    callServicePut(serviceManager.getServiceURL(), "key1", "value1");
    String response = callServiceGet(serviceManager.getServiceURL(), "key1");
    Assert.assertEquals("value1", new Gson().fromJson(response, String.class));
    serviceManager.stop();
    serviceManager.waitForRun(ProgramRunStatus.KILLED, 10, TimeUnit.SECONDS);
}
Also used : ApplicationManager(io.cdap.cdap.test.ApplicationManager) ServiceManager(io.cdap.cdap.test.ServiceManager) Gson(com.google.gson.Gson)

Aggregations

ServiceManager (io.cdap.cdap.test.ServiceManager)26 ApplicationManager (io.cdap.cdap.test.ApplicationManager)24 Test (org.junit.Test)22 URL (java.net.URL)14 KeyValueTable (io.cdap.cdap.api.dataset.lib.KeyValueTable)11 Gson (com.google.gson.Gson)8 Category (org.junit.experimental.categories.Category)8 HttpResponse (io.cdap.common.http.HttpResponse)7 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)6 PartitionedFileSet (io.cdap.cdap.api.dataset.lib.PartitionedFileSet)5 ArtifactId (io.cdap.cdap.proto.id.ArtifactId)5 WorkflowManager (io.cdap.cdap.test.WorkflowManager)5 HttpRequest (io.cdap.common.http.HttpRequest)5 IOException (java.io.IOException)5 FileSet (io.cdap.cdap.api.dataset.lib.FileSet)4 Table (io.cdap.cdap.api.dataset.table.Table)4 WorkerManager (io.cdap.cdap.test.WorkerManager)4 ImmutableSet (com.google.common.collect.ImmutableSet)3 ArtifactSummary (io.cdap.cdap.api.artifact.ArtifactSummary)3 ConflictException (io.cdap.cdap.common.ConflictException)3