aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_core/src/test/java/com/yahoo/jdisc/handler/RequestDispatchTestCase.java
blob: fdae7e8e701e10d84ca1c5b44e97105c2cf40717 (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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.handler;

import com.yahoo.jdisc.Request;
import com.yahoo.jdisc.Response;
import com.yahoo.jdisc.application.ContainerBuilder;
import com.yahoo.jdisc.test.TestDriver;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

/**
 * @author Simon Thoresen Hult
 */
public class RequestDispatchTestCase {

    @Test
    void requireThatRequestCanBeDispatched() throws Exception {
        final TestDriver driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi();
        final List<ByteBuffer> writtenContent = Arrays.asList(ByteBuffer.allocate(6), ByteBuffer.allocate(9));
        ReadableContentChannel receivedContent = new ReadableContentChannel();
        ContainerBuilder builder = driver.newContainerBuilder();
        Response response = new Response(Response.Status.OK);
        builder.serverBindings().bind("http://localhost/", new MyRequestHandler(receivedContent, response));
        driver.activateContainer(builder);
        RequestDispatch dispatch = new RequestDispatch() {

            @Override
            protected Request newRequest() {
                return new Request(driver, URI.create("http://localhost/"));
            }

            @Override
            protected Iterable<ByteBuffer> requestContent() {
                return writtenContent;
            }
        };
        dispatch.dispatch();
        assertFalse(dispatch.isDone());
        assertSame(writtenContent.get(0), receivedContent.read());
        assertFalse(dispatch.isDone());
        assertSame(writtenContent.get(1), receivedContent.read());
        assertFalse(dispatch.isDone());
        assertNull(receivedContent.read());
        assertTrue(dispatch.isDone());
        assertSame(response, dispatch.get(600, TimeUnit.SECONDS));
        assertTrue(driver.close());
    }

    @Test
    void requireThatStreamCanBeConnected() throws IOException {
        TestDriver driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi();
        ContainerBuilder builder = driver.newContainerBuilder();
        ReadableContentChannel content = new ReadableContentChannel();
        MyRequestHandler requestHandler = new MyRequestHandler(content, new Response(Response.Status.OK));
        builder.serverBindings().bind("http://localhost/", requestHandler);
        driver.activateContainer(builder);

        OutputStream out = new FastContentOutputStream(driver.newRequestDispatch("http://localhost/", new FutureResponse()).connect());
        out.write(6);
        out.write(9);
        out.close();

        InputStream in = content.toStream();
        assertEquals(6, in.read());
        assertEquals(9, in.read());
        assertEquals(-1, in.read());

        assertTrue(driver.close());
    }

    @Test
    void requireThatCancelIsUnsupported() {
        TestDriver driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi();
        RequestDispatch dispatch = driver.newRequestDispatch("http://localhost/", new FutureResponse());
        assertFalse(dispatch.isCancelled());
        try {
            dispatch.cancel(true);
            fail();
        } catch (UnsupportedOperationException e) {

        }
        assertFalse(dispatch.isCancelled());
        try {
            dispatch.cancel(false);
            fail();
        } catch (UnsupportedOperationException e) {

        }
        assertFalse(dispatch.isCancelled());
        assertTrue(driver.close());
    }

    @Test
    void requireThatDispatchHandlesConnectException() {
        TestDriver driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi();
        ContainerBuilder builder = driver.newContainerBuilder();
        builder.serverBindings().bind("http://localhost/", new AbstractRequestHandler() {

            @Override
            public ContentChannel handleRequest(Request request, ResponseHandler handler) {
                throw new RuntimeException();
            }
        });
        driver.activateContainer(builder);
        try {
            driver.newRequestDispatch("http://localhost/", new FutureResponse()).dispatch();
            fail();
        } catch (RuntimeException e) {

        }
        assertTrue(driver.close());
    }

    @Test
    void requireThatDispatchHandlesWriteException() {
        final TestDriver driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi();
        ContainerBuilder builder = driver.newContainerBuilder();
        Response response = new Response(Response.Status.OK);
        builder.serverBindings().bind("http://localhost/", new MyRequestHandler(new ContentChannel() {

            @Override
            public void write(ByteBuffer buf, CompletionHandler handler) {
                throw new RuntimeException();
            }

            @Override
            public void close(CompletionHandler handler) {
                handler.completed();
            }
        }, response));
        driver.activateContainer(builder);
        try {
            new RequestDispatch() {

                @Override
                protected Request newRequest() {
                    return new Request(driver, URI.create("http://localhost/"));
                }

                @Override
                protected Iterable<ByteBuffer> requestContent() {
                    return Arrays.asList(ByteBuffer.allocate(69));
                }
            }.dispatch();
            fail();
        } catch (RuntimeException e) {

        }
        assertTrue(driver.close());
    }

    @Test
    void requireThatDispatchHandlesCloseException() {
        final TestDriver driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi();
        ContainerBuilder builder = driver.newContainerBuilder();
        Response response = new Response(Response.Status.OK);
        builder.serverBindings().bind("http://localhost/", new MyRequestHandler(new ContentChannel() {

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

            @Override
            public void close(CompletionHandler handler) {
                throw new RuntimeException();
            }
        }, response));
        driver.activateContainer(builder);
        try {
            new RequestDispatch() {

                @Override
                protected Request newRequest() {
                    return new Request(driver, URI.create("http://localhost/"));
                }

                @Override
                protected Iterable<ByteBuffer> requestContent() {
                    return Arrays.asList(ByteBuffer.allocate(69));
                }
            }.dispatch();
            fail();
        } catch (RuntimeException e) {

        }
        assertTrue(driver.close());
    }

    @Test
    void requireThatDispatchCanBeListenedTo() throws InterruptedException {
        final TestDriver driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi();
        ContainerBuilder builder = driver.newContainerBuilder();
        ReadableContentChannel requestContent = new ReadableContentChannel();
        MyRequestHandler requestHandler = new MyRequestHandler(requestContent, null);
        builder.serverBindings().bind("http://localhost/", requestHandler);
        driver.activateContainer(builder);
        RunnableLatch listener = new RunnableLatch();
        new RequestDispatch() {

            @Override
            protected Request newRequest() {
                return new Request(driver, URI.create("http://localhost/"));
            }
        }.dispatch().whenComplete((__, ___) -> listener.run());
        assertFalse(listener.await(100, TimeUnit.MILLISECONDS));
        ContentChannel responseContent = ResponseDispatch.newInstance(Response.Status.OK)
                .connect(requestHandler.responseHandler);
        assertFalse(listener.await(100, TimeUnit.MILLISECONDS));
        assertNull(requestContent.read());
        assertTrue(listener.await(600, TimeUnit.SECONDS));
        responseContent.close(null);
        assertTrue(driver.close());
    }

    private static class MyRequestHandler extends AbstractRequestHandler {

        final ContentChannel content;
        final Response response;
        ResponseHandler responseHandler;

        MyRequestHandler(ContentChannel content, Response response) {
            this.content = content;
            this.response = response;
        }

        @Override
        public ContentChannel handleRequest(Request request, ResponseHandler handler) {
            if (response != null) {
                ResponseDispatch.newInstance(response).dispatch(handler);
            } else {
                responseHandler = handler;
            }
            return content;
        }
    }
}