aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_core/src/test/java/com/yahoo/jdisc/handler/BlockingContentWriterTestCase.java
blob: af48e1fe9b4c812de2ba5c53efbfeba8946f956d (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.handler;

import org.junit.jupiter.api.Test;

import java.nio.ByteBuffer;
import java.util.concurrent.*;

import static org.junit.jupiter.api.Assertions.assertNotSame;
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 BlockingContentWriterTestCase {

    @Test
    void requireThatContentChannelIsNotNull() {
        try {
            new BlockingContentWriter(null);
            fail();
        } catch (NullPointerException e) {

        }
    }

    @Test
    void requireThatWriteDeliversBuffer() throws InterruptedException {
        MyContent content = MyContent.newNonBlockingContent();
        BlockingContentWriter writer = new BlockingContentWriter(content);
        ByteBuffer buf = ByteBuffer.allocate(69);
        writer.write(buf);
        assertSame(buf, content.writeBuf);
    }

    @Test
    void requireThatWriteIsBlocking() throws Exception {
        MyContent content = MyContent.newBlockingContent();
        BlockingContentWriter writer = new BlockingContentWriter(content);
        FutureTask<Boolean> task = new FutureTask<>(new WriteTask(writer, ByteBuffer.allocate(69)));
        Executors.newSingleThreadExecutor().submit(task);
        content.writeLatch.await(600, TimeUnit.SECONDS);
        try {
            task.get(100, TimeUnit.MILLISECONDS);
            fail();
        } catch (TimeoutException e) {

        }
        content.writeCompletion.completed();
        assertTrue(task.get(600, TimeUnit.SECONDS));
    }

    @Test
    void requireThatWriteExceptionIsThrown() throws Exception {
        Throwable throwMe = new RuntimeException();
        try {
            new BlockingContentWriter(MyContent.newFailedContent(throwMe)).write(ByteBuffer.allocate(69));
        } catch (Throwable t) {
            assertSame(throwMe, t);
        }
        throwMe = new Error();
        try {
            new BlockingContentWriter(MyContent.newFailedContent(throwMe)).write(ByteBuffer.allocate(69));
        } catch (Throwable t) {
            assertSame(throwMe, t);
        }
        throwMe = new Exception();
        try {
            new BlockingContentWriter(MyContent.newFailedContent(throwMe)).write(ByteBuffer.allocate(69));
        } catch (Throwable t) {
            assertNotSame(throwMe, t);
            assertSame(throwMe, t.getCause());
        }
    }

    @Test
    void requireThatCloseIsBlocking() throws Exception {
        MyContent content = MyContent.newBlockingContent();
        BlockingContentWriter writer = new BlockingContentWriter(content);
        FutureTask<Boolean> task = new FutureTask<>(new CloseTask(writer));
        Executors.newSingleThreadExecutor().submit(task);
        content.closeLatch.await(600, TimeUnit.SECONDS);
        try {
            task.get(100, TimeUnit.MILLISECONDS);
            fail();
        } catch (TimeoutException e) {

        }
        content.closeCompletion.completed();
        assertTrue(task.get(600, TimeUnit.SECONDS));
    }

    @Test
    void requireThatCloseExceptionIsThrown() throws Exception {
        Throwable throwMe = new RuntimeException();
        try {
            new BlockingContentWriter(MyContent.newFailedContent(throwMe)).close();
        } catch (Throwable t) {
            assertSame(throwMe, t);
        }
        throwMe = new Error();
        try {
            new BlockingContentWriter(MyContent.newFailedContent(throwMe)).close();
        } catch (Throwable t) {
            assertSame(throwMe, t);
        }
        throwMe = new Exception();
        try {
            new BlockingContentWriter(MyContent.newFailedContent(throwMe)).close();
        } catch (Throwable t) {
            assertNotSame(throwMe, t);
            assertSame(throwMe, t.getCause());
        }
    }

    private static class MyContent implements ContentChannel {

        final CountDownLatch writeLatch = new CountDownLatch(1);
        final CountDownLatch closeLatch = new CountDownLatch(1);
        final Throwable eagerFailure;
        final boolean eagerCompletion;
        CompletionHandler writeCompletion;
        CompletionHandler closeCompletion;
        ByteBuffer writeBuf;

        MyContent(boolean eagerCompletion, Throwable eagerFailure) {
            this.eagerCompletion = eagerCompletion;
            this.eagerFailure = eagerFailure;
        }

        @Override
        public void write(ByteBuffer buf, CompletionHandler handler) {
            writeBuf = buf;
            if (eagerFailure != null) {
                handler.failed(eagerFailure);
            } else if (eagerCompletion) {
                handler.completed();
            } else {
                writeCompletion = handler;
            }
            writeLatch.countDown();
        }

        @Override
        public void close(CompletionHandler handler) {
            if (eagerFailure != null) {
                handler.failed(eagerFailure);
            } else if (eagerCompletion) {
                handler.completed();
            } else {
                closeCompletion = handler;
            }
            closeLatch.countDown();
        }

        static MyContent newBlockingContent() {
            return new MyContent(false, null);
        }

        static MyContent newNonBlockingContent() {
            return new MyContent(true, null);
        }

        static MyContent newFailedContent(Throwable e) {
            return new MyContent(false, e);
        }
    }

    private static class WriteTask implements Callable<Boolean> {

        final BlockingContentWriter writer;
        final ByteBuffer buf;

        WriteTask(BlockingContentWriter writer, ByteBuffer buf) {
            this.writer = writer;
            this.buf = buf;
        }

        @Override
        public Boolean call() {
            try {
                writer.write(buf);
            } catch (Throwable t) {
                return false;
            }
            return true;
        }
    }

    private static class CloseTask implements Callable<Boolean> {

        final BlockingContentWriter writer;

        CloseTask(BlockingContentWriter writer) {
            this.writer = writer;
        }

        @Override
        public Boolean call() {
            try {
                writer.close();
            } catch (Throwable t) {
                return false;
            }
            return true;
        }
    }
}