aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/test/java/com/yahoo/container/handler/VipStatusHandlerTestCase.java
blob: e95dbcbcb221b66291bae95bdf957d6f358b7ae0 (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
// Copyright Yahoo. 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 Steinar Knutsen
 */
public class VipStatusHandlerTestCase {

    public static class MockResponseHandler implements ResponseHandler {

        final ReadableContentChannel channel = new ReadableContentChannel();

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

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

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

    public static class NotFoundResponseHandler implements ResponseHandler {

        final ReadableContentChannel channel = new ReadableContentChannel();

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

    }

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

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

    @Test
    public void testExplicitlyRotationControl() {
        VipStatus vipStatus = new VipStatus();
        VipStatusConfig config = new VipStatusConfig(new VipStatusConfig.Builder().accessdisk(false));
        VipStatusHandler handler = new VipStatusHandler(Executors.newCachedThreadPool(), config,  metric, vipStatus);

        vipStatus.setInRotation(false);

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

        vipStatus.setInRotation(true);

        {
            MockResponseHandler responseHandler = new MockResponseHandler();
            HttpRequest request = createRequest();
            BufferedContentChannel requestContent = createChannel();
            handler.handleRequest(request, requestContent, responseHandler);
            ByteBuffer b = responseHandler.channel.read();
            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;
                }
            };
        }
    }

}