Search in sources :

Example 1 with IncomingData

use of com.yahoo.processing.response.IncomingData in project vespa by vespa-engine.

the class StreamingTestCase method testStreamingData.

/**
 * Tests adding a chain which is called every time new data is added to a data list
 */
@SuppressWarnings("unchecked")
@Test
public void testStreamingData() throws InterruptedException, ExecutionException, TimeoutException {
    // Set up
    StreamProcessor streamProcessor = new StreamProcessor();
    Chain<Processor> streamProcessing = new Chain<Processor>(streamProcessor);
    ProcessorLibrary.FutureDataSource futureDataSource = new ProcessorLibrary.FutureDataSource();
    Chain<Processor> main = new Chain<>(new ProcessorLibrary.DataCounter(), new ProcessorLibrary.StreamProcessingInitiator(streamProcessing), futureDataSource);
    // Execute
    Request request = new Request();
    Response response = Execution.createRoot(main, 0, Execution.Environment.createEmpty()).process(request);
    IncomingData incomingData = futureDataSource.incomingData.get(0);
    // State prior to receiving any additional data
    assertEquals(1, response.data().asList().size());
    assertEquals("Data count: 0", response.data().get(0).toString());
    assertEquals("Add data listener invoked also for DataCounter", 1, streamProcessor.invocationCount);
    assertEquals("Initial data count", 1, response.data().asList().size());
    // add first data - we have no listener so the data is held in the incoming buffer
    incomingData.add(new ProcessorLibrary.StringData(request, "d1"));
    assertEquals("Data add listener not invoked as we are not listening on new data yet", 1, streamProcessor.invocationCount);
    assertEquals("New data is not consumed", 1, response.data().asList().size());
    // start listening on incoming data - this is what a renderer will do
    incomingData.addNewDataListener(new MockNewDataListener(incomingData), MoreExecutors.directExecutor());
    assertEquals("We got a data add event for the data which was already added", 2, streamProcessor.invocationCount);
    assertEquals("New data is consumed", 2, response.data().asList().size());
    incomingData.add(new ProcessorLibrary.StringData(request, "d2"));
    assertEquals("We are now getting data add events each time", 3, streamProcessor.invocationCount);
    assertEquals("New data is consumed", 3, response.data().asList().size());
    incomingData.addLast(new ProcessorLibrary.StringData(request, "d3"));
    assertEquals("We are getting data add events also the last time", 4, streamProcessor.invocationCount);
    assertEquals("New data is consumed", 4, response.data().asList().size());
    // no-op here
    response.data().complete().get(1000, TimeUnit.MILLISECONDS);
    assertEquals("d1", response.data().get(1).toString().toString());
    assertEquals("d2", response.data().get(2).toString().toString());
    assertEquals("d3", response.data().get(3).toString().toString());
}
Also used : Chain(com.yahoo.component.chain.Chain) Processor(com.yahoo.processing.Processor) Request(com.yahoo.processing.Request) ProcessorLibrary(com.yahoo.processing.test.ProcessorLibrary) Response(com.yahoo.processing.Response) IncomingData(com.yahoo.processing.response.IncomingData) Test(org.junit.Test)

Aggregations

Chain (com.yahoo.component.chain.Chain)1 Processor (com.yahoo.processing.Processor)1 Request (com.yahoo.processing.Request)1 Response (com.yahoo.processing.Response)1 IncomingData (com.yahoo.processing.response.IncomingData)1 ProcessorLibrary (com.yahoo.processing.test.ProcessorLibrary)1 Test (org.junit.Test)1