summaryrefslogtreecommitdiffstats
path: root/container-core/src/test/java/com/yahoo/container/jdisc/LoggingRequestHandlerTestCase.java
blob: 92fe347f7d1e83bd242ab5fee87fa0e0c701d548 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.jdisc;

import static org.junit.Assert.*;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import com.google.inject.Key;
import com.yahoo.container.logging.HitCounts;
import com.yahoo.jdisc.Container;
import com.yahoo.jdisc.References;
import com.yahoo.jdisc.ResourceReference;
import com.yahoo.jdisc.handler.RequestHandler;
import com.yahoo.jdisc.service.CurrentContainer;
import java.net.URI;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.yahoo.component.ComponentId;
import com.yahoo.component.provider.ComponentRegistry;
import com.yahoo.container.handler.Coverage;
import com.yahoo.container.handler.Timing;
import com.yahoo.container.logging.AccessLog;
import com.yahoo.container.logging.AccessLogEntry;
import com.yahoo.container.logging.AccessLogInterface;
import com.yahoo.jdisc.handler.BufferedContentChannel;
import com.yahoo.jdisc.handler.CompletionHandler;
import com.yahoo.jdisc.handler.ContentChannel;
import com.yahoo.jdisc.handler.ResponseHandler;

/**
 * Test contracts in LoggingRequestHandler.
 *
 * @author <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a>
 */
public class LoggingRequestHandlerTestCase {

    StartTimePusher accessLogging;
    AccessLogTestHandler handler;
    ExecutorService executor;

    public static final class NoTimingResponse extends ExtendedResponse {

        public NoTimingResponse() {
            super(200);
        }


        @Override
        public HitCounts getHitCounts() {
            return new HitCounts(1, 1, 1, 1, 1,
                    getCoverage().toLoggingCoverage());
        }

        @Override
        public Timing getTiming() {
            return null;
        }

        @Override
        public Coverage getCoverage() {
            return new Coverage(1, 1, true);
        }


        @Override
        public void render(OutputStream output, ContentChannel networkChannel,
                CompletionHandler handler) throws IOException {
            networkChannel.close(handler);
        }
    }

    static class CloseableContentChannel implements ContentChannel {

        @Override
        public void write(ByteBuffer buf, CompletionHandler handler) {
            if (handler != null) {
                handler.completed();
            }
        }

        @Override
        public void close(CompletionHandler handler) {
            if (handler != null) {
                handler.completed();
            }
        }

    }

    public static final class MockResponseHandler implements ResponseHandler {
        public final ContentChannel channel = new CloseableContentChannel();

        @Override
        public ContentChannel handleResponse(
                final com.yahoo.jdisc.Response response) {
            return channel;
        }
    }

    static final class AccessLogTestHandler extends LoggingRequestHandler {

        public AccessLogTestHandler(Executor executor, AccessLog accessLog) {
            super(executor, accessLog);
        }

        @Override
        public HttpResponse handle(HttpRequest request) {
            return new NoTimingResponse();
        }

    }

    static final class StartTimePusher implements AccessLogInterface {

        public final ArrayBlockingQueue<Long> starts = new ArrayBlockingQueue<>(1);

        @Override
        public void log(final AccessLogEntry accessLogEntry) {
            starts.offer(Long.valueOf(accessLogEntry.getTimeStampMillis()));
        }
    }

    @Before
    public void setUp() throws Exception {
        accessLogging = new StartTimePusher();
        ComponentRegistry<AccessLogInterface> implementers = new ComponentRegistry<>();
        implementers.register(new ComponentId("nalle"), accessLogging);
        implementers.freeze();
        executor = Executors.newCachedThreadPool();
        handler = new AccessLogTestHandler(executor, new AccessLog(implementers));
    }

    @After
    public void tearDown() throws Exception {
        accessLogging = null;
        handler = null;
        executor.shutdown();
        executor = null;
    }

    @Test
    public final void checkStartIsNotZeroWithoutTimingInstance() throws InterruptedException {
        Long startTime;

        MockResponseHandler responseHandler = new MockResponseHandler();
        com.yahoo.jdisc.http.HttpRequest request = createRequest();
        BufferedContentChannel requestContent = new BufferedContentChannel();
        requestContent.close(null);
        handler.handleRequest(request, requestContent, responseHandler);
        startTime = accessLogging.starts.poll(5, TimeUnit.MINUTES);
        if (startTime == null) {
            // test timed out, ignoring
        } else {
            assertFalse(
                    "Start time was 0,  that should never happen after the first millisecond of 1970.",
                    startTime.longValue() == 0L);
        }
    }

    public static com.yahoo.jdisc.http.HttpRequest createRequest() {
        return createRequest("http://localhost/search/?query=geewhiz");
    }

    public static com.yahoo.jdisc.http.HttpRequest createRequest(String uri) {
        com.yahoo.jdisc.http.HttpRequest request = null;
        try {
            request = com.yahoo.jdisc.http.HttpRequest.newClientRequest(new com.yahoo.jdisc.Request(new MockCurrentContainer(), new URI(uri)), new URI(uri),
                                                   com.yahoo.jdisc.http.HttpRequest.Method.GET, com.yahoo.jdisc.http.HttpRequest.Version.HTTP_1_1);
            request.setRemoteAddress(new InetSocketAddress(0));
        } catch (URISyntaxException e) {
            fail("Illegal URI string in test?");
        }
        return request;
    }

    private static class MockCurrentContainer implements CurrentContainer {
        @Override
        public Container newReference(java.net.URI uri) {
            return new Container() {

                @Override
                public RequestHandler resolveHandler(com.yahoo.jdisc.Request request) {
                    return null;
                }

                @Override
                public <T> T getInstance(Key<T> tKey) {
                    return null;
                }

                @Override
                public <T> T getInstance(Class<T> tClass) {
                    return null;
                }

                @Override
                public ResourceReference refer() {
                    return References.NOOP_REFERENCE;
                }

                @Override
                public void release() {
                    // NOP
                }

                @Override
                public long currentTimeMillis() {
                    return 37;
                }
            };
        }
    }

}