Search in sources :

Example 41 with ServiceManager

use of co.cask.cdap.test.ServiceManager in project cdap by caskdata.

the class PartitionConsumingTestRun method testWordCountOnFileSet.

private void testWordCountOnFileSet(Function<ApplicationManager, ProgramManager> runProgram, boolean produceOutputPartitionEachRun) throws Exception {
    ApplicationManager applicationManager = deployApplication(AppWithPartitionConsumers.class);
    ServiceManager serviceManager = applicationManager.getServiceManager("DatasetService").start();
    serviceManager.waitForStatus(true);
    URL serviceURL = serviceManager.getServiceURL();
    // write a file to the file set using the service and run the WordCount MapReduce job on that one partition
    createPartition(serviceURL, LINE1, "1");
    ProgramManager programManager = runProgram.apply(applicationManager);
    programManager.waitForRun(ProgramRunStatus.COMPLETED, 5, TimeUnit.MINUTES);
    Assert.assertEquals(new Long(2), getCount(serviceURL, "a"));
    Assert.assertEquals(new Long(1), getCount(serviceURL, "b"));
    Assert.assertEquals(new Long(0), getCount(serviceURL, "c"));
    // create two additional partitions
    createPartition(serviceURL, LINE2, "2");
    createPartition(serviceURL, LINE3, "3");
    // running the program job now processes these two new partitions (LINE2 and LINE3) and updates the counts
    // dataset accordingly
    programManager = runProgram.apply(applicationManager);
    programManager.waitForRuns(ProgramRunStatus.COMPLETED, 2, 5, TimeUnit.MINUTES);
    Assert.assertEquals(new Long(3), getCount(serviceURL, "a"));
    Assert.assertEquals(new Long(3), getCount(serviceURL, "b"));
    Assert.assertEquals(new Long(3), getCount(serviceURL, "c"));
    // running the program without adding new partitions does not affect the counts dataset
    programManager = runProgram.apply(applicationManager);
    programManager.waitForRuns(ProgramRunStatus.COMPLETED, 3, 5, TimeUnit.MINUTES);
    Assert.assertEquals(new Long(3), getCount(serviceURL, "a"));
    Assert.assertEquals(new Long(3), getCount(serviceURL, "b"));
    Assert.assertEquals(new Long(3), getCount(serviceURL, "c"));
    DataSetManager<PartitionedFileSet> outputLines = getDataset("outputLines");
    Set<PartitionDetail> partitions = outputLines.get().getPartitions(PartitionFilter.ALWAYS_MATCH);
    // each of the three MapReduce runs produces an output partition (even if there's no input data)
    // however, Worker run doesn't produce a new output partition if there's no new input partition
    Assert.assertEquals(produceOutputPartitionEachRun ? 3 : 2, partitions.size());
    // we only store the counts to the "outputLines" dataset
    List<String> expectedCounts = Lists.newArrayList("1", "1", "2", "2", "3");
    List<String> outputRecords = getDataFromExplore("outputLines");
    Collections.sort(outputRecords);
    Assert.assertEquals(expectedCounts, outputRecords);
}
Also used : ApplicationManager(co.cask.cdap.test.ApplicationManager) ServiceManager(co.cask.cdap.test.ServiceManager) PartitionedFileSet(co.cask.cdap.api.dataset.lib.PartitionedFileSet) PartitionDetail(co.cask.cdap.api.dataset.lib.PartitionDetail) URL(java.net.URL) ProgramManager(co.cask.cdap.test.ProgramManager)

Example 42 with ServiceManager

use of co.cask.cdap.test.ServiceManager in project cdap by caskdata.

the class TestBundleJarApp method testBundleJar.

@Test
public void testBundleJar() throws Exception {
    File helloWorldJar = new File(TestBundleJarApp.class.getClassLoader().getResource("helloworld.jar").toURI());
    ApplicationManager applicationManager = deployApplication(BundleJarApp.class, helloWorldJar);
    FlowManager flowManager = applicationManager.getFlowManager("SimpleFlow").start();
    StreamManager streamManager = getStreamManager("simpleInputStream");
    for (int i = 0; i < 5; i++) {
        streamManager.send("test" + i + ":" + i);
    }
    // Check the flowlet metrics
    RuntimeMetrics flowletMetrics = flowManager.getFlowletMetrics("simpleFlowlet");
    flowletMetrics.waitForProcessed(5, 5, TimeUnit.SECONDS);
    Assert.assertEquals(0L, flowletMetrics.getException());
    flowManager.stop();
    // Query the result
    ServiceManager serviceManager = applicationManager.getServiceManager("SimpleGetInput").start();
    // Verify the query result
    String queryResult = callServiceGet(serviceManager.getServiceURL(), "/get/test1");
    String expectedQueryResult = new Gson().toJson(ImmutableMap.of("test1", "1" + BundleJarApp.EXPECTED_LOAD_TEST_CLASSES_OUTPUT));
    Assert.assertEquals(expectedQueryResult, queryResult);
    serviceManager.stop();
    serviceManager = applicationManager.getServiceManager("PrintService").start();
    String helloWorldClassName = "hello.HelloWorld";
    String result = callServiceGet(serviceManager.getServiceURL(), "/load/" + helloWorldClassName);
    String expected = new Gson().toJson(ImmutableMap.of("Class.forName", helloWorldClassName));
    Assert.assertEquals(expected, result);
}
Also used : FlowManager(co.cask.cdap.test.FlowManager) ApplicationManager(co.cask.cdap.test.ApplicationManager) StreamManager(co.cask.cdap.test.StreamManager) RuntimeMetrics(co.cask.cdap.api.metrics.RuntimeMetrics) ServiceManager(co.cask.cdap.test.ServiceManager) Gson(com.google.gson.Gson) File(java.io.File) Test(org.junit.Test)

Example 43 with ServiceManager

use of co.cask.cdap.test.ServiceManager in project cdap by caskdata.

the class ServiceLifeCycleTestRun method testLifecycleWithGC.

@Test
public void testLifecycleWithGC() throws Exception {
    // Set the http server properties to speed up test
    System.setProperty(ServiceHttpServer.THREAD_POOL_SIZE, "1");
    System.setProperty(ServiceHttpServer.THREAD_KEEP_ALIVE_SECONDS, "1");
    System.setProperty(ServiceHttpServer.HANDLER_CLEANUP_PERIOD_MILLIS, "100");
    try {
        ApplicationManager appManager = deployWithArtifact(ServiceLifecycleApp.class, artifactJar);
        final ServiceManager serviceManager = appManager.getServiceManager("test").start();
        // Make 5 consecutive calls, there should be one handler instance being created,
        // since there is only one handler thread.
        Multimap<Integer, String> states = null;
        for (int i = 0; i < 5; i++) {
            states = getStates(serviceManager);
            // There should only be one instance created
            Assert.assertEquals(1, states.size());
            // For the instance, there should only be INIT state.
            Assert.assertEquals(ImmutableList.of("INIT"), ImmutableList.copyOf(states.get(states.keySet().iterator().next())));
        }
        // Capture the current state
        final Multimap<Integer, String> lastStates = states;
        // TTL for the thread is 1 second, hence sleep for 2 second to make sure the thread is gone
        TimeUnit.SECONDS.sleep(2);
        Tasks.waitFor(true, new Callable<Boolean>() {

            @Override
            public Boolean call() throws Exception {
                // Force a gc to have weak references cleanup
                System.gc();
                Multimap<Integer, String> newStates = getStates(serviceManager);
                // and an INIT for the new handler that just handle the getState call
                if (newStates.size() != 3) {
                    return false;
                }
                // A INIT and a DESTROY is expected for the old handler
                return ImmutableList.of("INIT", "DESTROY").equals(ImmutableList.copyOf(newStates.get(lastStates.keySet().iterator().next())));
            }
        }, 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
    } finally {
        // Reset the http server properties to speed up test
        System.clearProperty(ServiceHttpServer.THREAD_POOL_SIZE);
        System.clearProperty(ServiceHttpServer.THREAD_KEEP_ALIVE_SECONDS);
        System.clearProperty(ServiceHttpServer.HANDLER_CLEANUP_PERIOD_MILLIS);
    }
}
Also used : Multimap(com.google.common.collect.Multimap) LinkedListMultimap(com.google.common.collect.LinkedListMultimap) ApplicationManager(co.cask.cdap.test.ApplicationManager) ServiceManager(co.cask.cdap.test.ServiceManager) IOException(java.io.IOException) Test(org.junit.Test)

Example 44 with ServiceManager

use of co.cask.cdap.test.ServiceManager in project cdap by caskdata.

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.waitForStatus(true);
        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));
        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.waitForStatus(false);
    }
}
Also used : ApplicationManager(co.cask.cdap.test.ApplicationManager) TimeSeries(co.cask.cdap.api.dataset.lib.cube.TimeSeries) ArrayList(java.util.ArrayList) CubeQuery(co.cask.cdap.api.dataset.lib.cube.CubeQuery) URL(java.net.URL) CubeFact(co.cask.cdap.api.dataset.lib.cube.CubeFact) DimensionValue(co.cask.cdap.api.dataset.lib.cube.DimensionValue) ServiceManager(co.cask.cdap.test.ServiceManager) CubeExploreQuery(co.cask.cdap.api.dataset.lib.cube.CubeExploreQuery) TimeValue(co.cask.cdap.api.dataset.lib.cube.TimeValue) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Example 45 with ServiceManager

use of co.cask.cdap.test.ServiceManager in project cdap by caskdata.

the class TestFrameworkTestRun method testGetServiceURL.

@Category(SlowTests.class)
@Test
public void testGetServiceURL() throws Exception {
    ApplicationManager applicationManager = deployApplication(AppUsingGetServiceURL.class);
    ServiceManager centralServiceManager = applicationManager.getServiceManager(AppUsingGetServiceURL.CENTRAL_SERVICE).start();
    centralServiceManager.waitForStatus(true);
    WorkerManager pingingWorker = applicationManager.getWorkerManager(AppUsingGetServiceURL.PINGING_WORKER).start();
    pingingWorker.waitForStatus(true);
    // Test service's getServiceURL
    ServiceManager serviceManager = applicationManager.getServiceManager(AppUsingGetServiceURL.FORWARDING).start();
    String result = callServiceGet(serviceManager.getServiceURL(), "ping");
    String decodedResult = new Gson().fromJson(result, String.class);
    // Verify that the service was able to hit the CentralService and retrieve the answer.
    Assert.assertEquals(AppUsingGetServiceURL.ANSWER, decodedResult);
    result = callServiceGet(serviceManager.getServiceURL(), "read/" + AppUsingGetServiceURL.DATASET_KEY);
    decodedResult = new Gson().fromJson(result, String.class);
    Assert.assertEquals(AppUsingGetServiceURL.ANSWER, decodedResult);
    serviceManager.stop();
    // JIRA - CDAP-3656
    try {
        pingingWorker.stop();
    } catch (Throwable e) {
        LOG.error("Got exception while stopping pinging worker", e);
    }
    pingingWorker.waitForStatus(false);
    centralServiceManager.stop();
    centralServiceManager.waitForStatus(false);
}
Also used : WorkerManager(co.cask.cdap.test.WorkerManager) ApplicationManager(co.cask.cdap.test.ApplicationManager) ServiceManager(co.cask.cdap.test.ServiceManager) Gson(com.google.gson.Gson) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Aggregations

ServiceManager (co.cask.cdap.test.ServiceManager)50 ApplicationManager (co.cask.cdap.test.ApplicationManager)47 Test (org.junit.Test)44 URL (java.net.URL)28 StreamManager (co.cask.cdap.test.StreamManager)15 KeyValueTable (co.cask.cdap.api.dataset.lib.KeyValueTable)13 FlowManager (co.cask.cdap.test.FlowManager)13 MapReduceManager (co.cask.cdap.test.MapReduceManager)13 RuntimeMetrics (co.cask.cdap.api.metrics.RuntimeMetrics)12 HttpResponse (co.cask.common.http.HttpResponse)12 Gson (com.google.gson.Gson)11 SparkManager (co.cask.cdap.test.SparkManager)10 IOException (java.io.IOException)10 HttpURLConnection (java.net.HttpURLConnection)10 HttpRequest (co.cask.common.http.HttpRequest)9 Category (org.junit.experimental.categories.Category)7 ApplicationId (co.cask.cdap.proto.id.ApplicationId)6 ArtifactSummary (co.cask.cdap.api.artifact.ArtifactSummary)5 Table (co.cask.cdap.api.dataset.table.Table)5 AppRequest (co.cask.cdap.proto.artifact.AppRequest)5