apex-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bhupeshchawda <...@git.apache.org>
Subject [GitHub] apex-malhar pull request #309: [APEXMALHAR-2106] Support multiple streams in...
Date Tue, 21 Jun 2016 10:16:01 GMT
Github user bhupeshchawda commented on a diff in the pull request:

    https://github.com/apache/apex-malhar/pull/309#discussion_r67842738
  
    --- Diff: library/src/test/java/com/datatorrent/lib/stream/MultipleStreamMergerTest.java
---
    @@ -0,0 +1,200 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *   http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +package com.datatorrent.lib.stream;
    +
    +import java.io.IOException;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.Map;
    +import java.util.concurrent.atomic.AtomicLong;
    +
    +import javax.validation.ConstraintViolationException;
    +
    +import org.junit.Assert;
    +import org.junit.Before;
    +import org.junit.Test;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import org.apache.hadoop.conf.Configuration;
    +
    +import com.datatorrent.api.DAG;
    +import com.datatorrent.api.DefaultInputPort;
    +import com.datatorrent.api.DefaultOutputPort;
    +import com.datatorrent.api.InputOperator;
    +import com.datatorrent.api.LocalMode;
    +import com.datatorrent.api.StreamingApplication;
    +import com.datatorrent.common.util.BaseOperator;
    +import com.datatorrent.lib.testbench.RandomWordGenerator;
    +
    +import static junit.framework.TestCase.assertTrue;
    +import static org.junit.Assert.assertEquals;
    +
    +public class MultipleStreamMergerTest
    +{
    +  private static Logger LOG = LoggerFactory.getLogger(MultipleStreamMergerTest.class);
    +
    +  private ArrayList<MultipleStreamMerger<byte[]>.Stream> streamsToAddToDag;
    +  private ArrayList<MultipleStreamMerger<byte[]>.NamedMerger> operatorsToAdd;
    +
    +  private static Counter counterOp = new Counter();
    +
    +  private static int tuplesToSend = 173;
    +  private static int streamsToMerge = 15;
    +
    +  private static Map<Long, AtomicLong> tuplesReceived = new HashMap<>();
    +
    +  @Before
    +  public void setUp() throws Exception
    +  {
    +    streamsToAddToDag = new ArrayList<>();
    +    operatorsToAdd = new ArrayList<>();
    +  }
    +
    +  @Test
    +  public void mergeTwoStreams()
    +  {
    +    RandomWordGenerator randomWordGenerator = new RandomWordGenerator();
    +    RandomWordGenerator randomWordGenerator2 = new RandomWordGenerator();
    +
    +    randomWordGenerator.setTuplesPerWindow(1);
    +    randomWordGenerator2.setTuplesPerWindow(1);
    +
    +    MultipleStreamMerger<byte[]> merger = new MultipleStreamMerger<>();
    +    merger.merge(randomWordGenerator.output)
    +        .merge(randomWordGenerator2.output);
    +
    +    merger.constructMergeTree(streamsToAddToDag, operatorsToAdd);
    +
    +    assertEquals("Count of created streams", 2, streamsToAddToDag.size());
    +    assertEquals("Count of created operators", 1, operatorsToAdd.size());
    +
    +    // Next check actual connections
    +    assertEquals("Generator 1 stream", randomWordGenerator.output,
    +        streamsToAddToDag.get(0).sourcePort.port);
    +
    +    assertEquals("Generator 2 stream", randomWordGenerator2.output,
    +        streamsToAddToDag.get(1).sourcePort.port);
    +
    +    assertEquals("Final operator input_1", operatorsToAdd.get(0).merger.data1, streamsToAddToDag.get(0).destPort);
    +    assertEquals("Final operator input_2", operatorsToAdd.get(0).merger.data2, streamsToAddToDag.get(1).destPort);
    +  }
    +
    +  @Test(expected = IllegalArgumentException.class)
    +  public void mergeOneStream()
    +  {
    +    RandomWordGenerator randomWordGenerator = new RandomWordGenerator();
    +    MultipleStreamMerger<byte[]> merger = new MultipleStreamMerger<>();
    +    merger.merge(randomWordGenerator.output);
    +    merger.constructMergeTree(streamsToAddToDag, operatorsToAdd);
    +  }
    +
    +  @Test(expected = IllegalArgumentException.class)
    +  public void mergeZeroStream()
    +  {
    +    MultipleStreamMerger<byte[]> merger = new MultipleStreamMerger<>();
    +    merger.constructMergeTree(streamsToAddToDag, operatorsToAdd);
    +  }
    +
    +  public static class Application implements StreamingApplication
    +  {
    +    @Override
    +    public void populateDAG(DAG dag, Configuration conf)
    +    {
    +      LOG.debug("Application - PopulateDAG");
    +      NumGenerator[] generators = new NumGenerator[streamsToMerge];
    +      MultipleStreamMerger<Long> merger = new MultipleStreamMerger<>();
    +
    +      for (int i = 0; i < streamsToMerge; i++) {
    +        generators[i] = new NumGenerator();
    +        dag.addOperator("Generator " + i, generators[i]);
    +        merger.merge(generators[i].out);
    +      }
    +
    +      DefaultOutputPort streamOutput = merger.mergeStreams(dag, conf);
    +
    +      // Count the number of tuples in the merger
    +      dag.addOperator("Counter", counterOp);
    +
    +      // And then we should see the output
    +      dag.addStream("merger-counter", streamOutput, counterOp.counter);
    +    }
    +  }
    +
    +  @Test
    +  public void testApplication() throws IOException, Exception
    +  {
    +    try {
    +      LocalMode lma = LocalMode.newInstance();
    +      Configuration conf = new Configuration(false);
    +      lma.prepareDAG(new Application(), conf);
    +      LocalMode.Controller lc = lma.getController();
    +      lc.run(15000); // runs for 10 seconds and quits
    --- End diff --
    
    This approach may run the app shorter or longer than required.
    May be this is okay for now, but the suggested approach for testing in local mode is to
use lc.runAsync(). See https://github.com/apache/apex-malhar/tree/79eeff782f5cba09bda633dc760cd34190b694c4/demos/iteration/src/test/java/com/datatorrent/demos/iteration
for an example.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message