aboutsummaryrefslogtreecommitdiffstats
path: root/vespaclient-container-plugin/src/test/java/com/yahoo/feedhandler/FeedHandlerTest.java
blob: c0cc907c6714c53076ed7e4613caa6f98b343f49 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.feedhandler;

import com.yahoo.container.jdisc.HttpRequest;
import com.yahoo.jdisc.HeaderFields;
import com.yahoo.jdisc.Metric;
import com.yahoo.container.logging.AccessLog;
import com.yahoo.metrics.simple.MetricReceiver;
import com.yahoo.vespa.http.client.core.Headers;
import com.yahoo.vespa.http.client.core.OperationStatus;
import com.yahoo.vespa.http.server.FeedHandler;
import com.yahoo.vespa.http.server.Feeder;
import org.junit.Test;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
 * Unit test for FeedHandler class.
 *
 * @author dybis
 */
public class FeedHandlerTest {

    /**
     * This class extends FeedHandler and allows to create a custom Feeder.
     */
    static class TestFeedHandler extends FeedHandler {
        private final CountDownLatch countDownLatch = new CountDownLatch(1);

        public TestFeedHandler() throws Exception {
            super(new FeedHandler.Context(Executors.newCachedThreadPool(),
                                          mock(AccessLog.class),
                                          mock(Metric.class)),
                  null, null, null, MetricReceiver.nullImplementation);
        }

        /**
         * Builds a feeder that blocks until countDownLatch is stepped down.
         */
        @Override
        protected Feeder createFeeder(
                com.yahoo.container.jdisc.HttpRequest request,
                InputStream requestInputStream,
                final BlockingQueue<OperationStatus> operations,
                String clientId,
                boolean sessionIdWasGeneratedJustNow,
                int protocolVersion) throws Exception {
            Feeder feeder = mock(Feeder.class);
            doAnswer(invocation -> {
                try {
                    countDownLatch.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                return null;
            }).when(feeder).waitForRequestReceived();
            return feeder;
        }
    }

    /**
     * nginx require that a post is finished before the server ack with a response. This behaviour is verified
     * in this test
     */
    @Test
    public void testResponseIsSentAfterWaitForRequestReceivedReturns() throws Exception {
        HttpRequest request = mock(HttpRequest.class);

        // Create a request with valid version.
        com.yahoo.jdisc.http.HttpRequest jdiscRequest = mock(com.yahoo.jdisc.http.HttpRequest.class);
        HeaderFields headerFields = mock(HeaderFields.class);
        List<String> version = new ArrayList<>();
        version.add("2");
        when(headerFields.get(Headers.VERSION)).thenReturn(version);
        when(jdiscRequest.headers()).thenReturn(headerFields);
        when(request.getJDiscRequest()).thenReturn(jdiscRequest);

        TestFeedHandler feedHandler = new TestFeedHandler();
        // After a short period, make the feed finish.
        new Thread(() -> {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            feedHandler.countDownLatch.countDown();
        }).start();
        // This should not return before countdown latch is stepped down.
        feedHandler.handle(request);
        // This should always returns after the countDownLatch has become zero. This might cause false positive,
        // but not false negatives. This is fine.
        assertThat(feedHandler.countDownLatch.getCount(), is(0L));

    }

}