summaryrefslogtreecommitdiffstats
path: root/container-core/src/test/java/com/yahoo/container/handler/VipStatusHandlerTestCase.java
blob: a4f7bec07f68fb39d51a56bb697fc74d28b3553b (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.handler;

import com.google.inject.Key;
import com.yahoo.container.core.VipStatusConfig;
import com.yahoo.jdisc.Container;
import com.yahoo.jdisc.Metric;
import com.yahoo.jdisc.References;
import com.yahoo.jdisc.ResourceReference;
import com.yahoo.jdisc.handler.BufferedContentChannel;
import com.yahoo.jdisc.handler.ContentChannel;
import com.yahoo.jdisc.handler.ReadableContentChannel;
import com.yahoo.jdisc.handler.RequestHandler;
import com.yahoo.jdisc.handler.ResponseHandler;
import com.yahoo.jdisc.http.HttpRequest;
import com.yahoo.jdisc.service.CurrentContainer;
import com.yahoo.text.Utf8;
import org.junit.Test;
import org.mockito.Mockito;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.util.concurrent.Executors;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

/**
 * Check semantics of VIP status handler. Do note this handler does not need to
 * care about the incoming URI, that's 100% handled in JDIsc by the binding
 * pattern.
 *
 * @author <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a>
 */
public class VipStatusHandlerTestCase {

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

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

    Metric metric = Mockito.mock(Metric.class);

    @Test
    public final void testHandleRequest() {
        final VipStatusConfig config = new VipStatusConfig(new VipStatusConfig.Builder().accessdisk(false)
                .noSearchBackendsImpliesOutOfService(false));
        final VipStatusHandler handler = new VipStatusHandler(Executors.newCachedThreadPool(), config, metric);
        final MockResponseHandler responseHandler = new MockResponseHandler();
        final HttpRequest request = createRequest();
        final BufferedContentChannel requestContent = createChannel();
        handler.handleRequest(request, requestContent, responseHandler);
        final ByteBuffer b = responseHandler.channel.read();
        final byte[] asBytes = new byte[b.remaining()];
        b.get(asBytes);
        assertEquals(VipStatusHandler.OK_MESSAGE, Utf8.toString(asBytes));
    }

    public static final class NotFoundResponseHandler implements
            ResponseHandler {
        final ReadableContentChannel channel = new ReadableContentChannel();

        @Override
        public ContentChannel handleResponse(
                final com.yahoo.jdisc.Response response) {
            assertEquals(com.yahoo.jdisc.Response.Status.NOT_FOUND,
                    response.getStatus());
            return channel;
        }
    }

    @Test
    public final void testFileNotFound() {
        final VipStatusConfig config = new VipStatusConfig(new VipStatusConfig.Builder().accessdisk(true)
                .statusfile("/VipStatusHandlerTestCaseFileThatReallyReallyShouldNotExist")
                .noSearchBackendsImpliesOutOfService(false));
        final VipStatusHandler handler = new VipStatusHandler(Executors.newCachedThreadPool(), config, metric);
        final NotFoundResponseHandler responseHandler = new NotFoundResponseHandler();
        final HttpRequest request = createRequest();
        final BufferedContentChannel requestContent = createChannel();
        handler.handleRequest(request, requestContent, responseHandler);
        final ByteBuffer b = responseHandler.channel.read();
        final byte[] asBytes = new byte[b.remaining()];
        b.get(asBytes);
        assertEquals(
                VipStatusHandler.StatusResponse.COULD_NOT_FIND_STATUS_FILE,
                Utf8.toString(asBytes));
    }

    @Test
    public final void testFileFound() throws IOException {
        final File statusFile = File.createTempFile("VipStatusHandlerTestCase",
                null);
        try {
            final FileWriter writer = new FileWriter(statusFile);
            final String OK = "OK\n";
            writer.write(OK);
            writer.close();
            final VipStatusConfig config = new VipStatusConfig(new VipStatusConfig.Builder().accessdisk(true)
                    .statusfile(statusFile.getAbsolutePath()).noSearchBackendsImpliesOutOfService(false));
            final VipStatusHandler handler = new VipStatusHandler(Executors.newCachedThreadPool(), config, metric);
            final MockResponseHandler responseHandler = new MockResponseHandler();
            final HttpRequest request = createRequest();
            final BufferedContentChannel requestContent = createChannel();
            handler.handleRequest(request, requestContent, responseHandler);
            final ByteBuffer b = responseHandler.channel.read();
            final byte[] asBytes = new byte[b.remaining()];
            b.get(asBytes);
            assertEquals(OK, Utf8.toString(asBytes));
        } finally {
            statusFile.delete();
        }
    }

    @Test
    public final void testProgrammaticallyRemovedFromRotation() throws IOException {
        VipStatus vipStatus = new VipStatus();
        final VipStatusConfig config = new VipStatusConfig(new VipStatusConfig.Builder().accessdisk(false)
                .noSearchBackendsImpliesOutOfService(true));
        final VipStatusHandler handler = new VipStatusHandler(Executors.newCachedThreadPool(), config,  metric, vipStatus);

        vipStatus.removeFromRotation(this);

        {
            final MockResponseHandler responseHandler = new MockResponseHandler();
            final HttpRequest request = createRequest();
            final BufferedContentChannel requestContent = createChannel();
            handler.handleRequest(request, requestContent, responseHandler);
            final ByteBuffer b = responseHandler.channel.read();
            final byte[] asBytes = new byte[b.remaining()];
            b.get(asBytes);
            assertEquals(VipStatusHandler.StatusResponse.NO_SEARCH_BACKENDS, Utf8.toString(asBytes));
        }

        vipStatus.addToRotation(this);

        {
            final MockResponseHandler responseHandler = new MockResponseHandler();
            final HttpRequest request = createRequest();
            final BufferedContentChannel requestContent = createChannel();
            handler.handleRequest(request, requestContent, responseHandler);
            final ByteBuffer b = responseHandler.channel.read();
            final byte[] asBytes = new byte[b.remaining()];
            b.get(asBytes);
            assertEquals(VipStatusHandler.OK_MESSAGE, Utf8.toString(asBytes));
        }
    }

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

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

    public static BufferedContentChannel createChannel() {
        BufferedContentChannel channel = new BufferedContentChannel();
        channel.close(null);
        return channel;
    }

    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 0;
                }
            };
        }
    }

}