aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_core/src/test/java/com/yahoo/jdisc/handler/ResponseDispatchTestCase.java
blob: 2c00689c3f49ef6f07fb5498f1d8ac63bebae63e (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
// 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.Response;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
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 ResponseDispatchTestCase {

    @Test
    void requireThatFactoryMethodsWork() throws Exception {
        {
            FutureResponse handler = new FutureResponse();
            ResponseDispatch.newInstance(69).dispatch(handler);
            Response response = handler.get(600, TimeUnit.SECONDS);
            assertNotNull(response);
            assertEquals(69, response.getStatus());
        }
        {
            FutureResponse handler = new FutureResponse();
            Response sentResponse = new Response(69);
            ResponseDispatch.newInstance(sentResponse).dispatch(handler);
            Response receivedResponse = handler.get(600, TimeUnit.SECONDS);
            assertSame(sentResponse, receivedResponse);
        }
        {
            ReadableContentChannel content = new ReadableContentChannel();
            FutureResponse handler = new FutureResponse(content);
            ByteBuffer buf = ByteBuffer.allocate(69);
            ResponseDispatch.newInstance(69, Arrays.asList(buf)).dispatch(handler);
            Response response = handler.get(600, TimeUnit.SECONDS);
            assertNotNull(response);
            assertEquals(69, response.getStatus());
            assertSame(buf, content.read());
            assertNull(content.read());
        }
        {
            ReadableContentChannel content = new ReadableContentChannel();
            FutureResponse handler = new FutureResponse(content);
            ByteBuffer buf = ByteBuffer.allocate(69);
            ResponseDispatch.newInstance(69, Arrays.asList(buf)).dispatch(handler);
            Response response = handler.get(600, TimeUnit.SECONDS);
            assertNotNull(response);
            assertEquals(69, response.getStatus());
            assertSame(buf, content.read());
            assertNull(content.read());
        }
        {
            ReadableContentChannel content = new ReadableContentChannel();
            FutureResponse handler = new FutureResponse(content);
            ByteBuffer buf = ByteBuffer.allocate(69);
            Response sentResponse = new Response(69);
            ResponseDispatch.newInstance(sentResponse, Arrays.asList(buf)).dispatch(handler);
            Response receivedResponse = handler.get(600, TimeUnit.SECONDS);
            assertSame(sentResponse, receivedResponse);
            assertSame(buf, content.read());
            assertNull(content.read());
        }
    }

    @Test
    void requireThatResponseCanBeDispatched() throws Exception {
        final Response response = new Response(Response.Status.OK);
        final List<ByteBuffer> writtenContent = Arrays.asList(ByteBuffer.allocate(6), ByteBuffer.allocate(9));
        ResponseDispatch dispatch = new ResponseDispatch() {

            @Override
            protected Response newResponse() {
                return response;
            }

            @Override
            protected Iterable<ByteBuffer> responseContent() {
                return writtenContent;
            }
        };
        ReadableContentChannel receivedContent = new ReadableContentChannel();
        MyResponseHandler responseHandler = new MyResponseHandler(receivedContent);
        dispatch.dispatch(responseHandler);
        assertFalse(dispatch.isDone());
        assertSame(response, responseHandler.response);
        assertSame(writtenContent.get(0), receivedContent.read());
        assertFalse(dispatch.isDone());
        assertSame(writtenContent.get(1), receivedContent.read());
        assertFalse(dispatch.isDone());
        assertNull(receivedContent.read());
        assertTrue(dispatch.isDone());
        assertTrue(dispatch.get(600, TimeUnit.SECONDS));
        assertTrue(dispatch.get());
    }

    @Test
    void requireThatStreamCanBeConnected() throws IOException {
        ReadableContentChannel responseContent = new ReadableContentChannel();
        OutputStream out = new FastContentOutputStream(new ResponseDispatch() {

            @Override
            protected Response newResponse() {
                return new Response(Response.Status.OK);
            }
        }.connect(new MyResponseHandler(responseContent)));
        out.write(6);
        out.write(9);
        out.close();

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

    @Test
    void requireThatCancelIsUnsupported() {
        ResponseDispatch dispatch = ResponseDispatch.newInstance(69);
        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());
    }

    @Test
    void requireThatDispatchClosesContentIfWriteThrowsException() {
        final AtomicBoolean closed = new AtomicBoolean(false);
        try {
            ResponseDispatch.newInstance(6, ByteBuffer.allocate(9)).dispatch(
                    new MyResponseHandler(new ContentChannel() {

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

                        @Override
                        public void close(CompletionHandler handler) {
                            closed.set(true);
                        }
                    }));
            fail();
        } catch (UnsupportedOperationException e) {

        }
        assertTrue(closed.get());
    }

    @Test
    void requireThatDispatchCanBeListenedTo() throws InterruptedException {
        RunnableLatch listener = new RunnableLatch();
        ReadableContentChannel responseContent = new ReadableContentChannel();
        ResponseDispatch.newInstance(6, ByteBuffer.allocate(9))
                .dispatch(new MyResponseHandler(responseContent))
                .whenComplete((__, ___) -> listener.run());
        assertFalse(listener.await(100, TimeUnit.MILLISECONDS));
        assertNotNull(responseContent.read());
        assertFalse(listener.await(100, TimeUnit.MILLISECONDS));
        assertNull(responseContent.read());
        assertTrue(listener.await(600, TimeUnit.SECONDS));
    }

    private static class MyResponseHandler implements ResponseHandler {

        final ContentChannel content;
        Response response;

        MyResponseHandler(ContentChannel content) {
            this.content = content;
        }

        @Override
        public ContentChannel handleResponse(Response response) {
            this.response = response;
            return content;
        }
    }

}