aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_core/src/test/java/com/yahoo/jdisc/handler/UnsafeContentInputStreamTestCase.java
blob: 50d695dbb275360d4d003948fae75c2e734305b9 (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
// 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.text.Utf8;
import org.junit.jupiter.api.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;

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

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

    @Test
    void requireThatBytesCanBeRead() throws IOException {
        BufferedContentChannel channel = new BufferedContentChannel();
        FastContentWriter writer = new FastContentWriter(channel);
        writer.write("Hello ");
        writer.write("World!");
        writer.close();

        BufferedReader reader = asBufferedReader(channel);
        assertEquals("Hello World!", reader.readLine());
        assertNull(reader.readLine());
    }

    @Test
    void testMark() throws IOException {
        BufferedContentChannel channel = new BufferedContentChannel();
        FastContentWriter writer = new FastContentWriter(channel);
        writer.write("Hello ");
        writer.write("World!");
        writer.close();

        InputStream stream = asInputStream(channel);
        assertTrue(stream.markSupported());
        int first = stream.read();
        assertEquals('H', first);
        stream.mark(10);
        byte [] buf = new byte[8];
        stream.read(buf);
        assertEquals("ello Wor", Utf8.toString(buf));
        stream.reset();
        stream.mark(8);
        buf = new byte[8];
        stream.read(buf);
        assertEquals("ello Wor", Utf8.toString(buf));
        stream.reset();
        stream.mark(5);
        buf = new byte [9];
        stream.read(buf);
        assertEquals("ello Worl", Utf8.toString(buf));
        try {
            stream.reset();
            fail("UnsafeContentInputStream.reset expected to fail when your read past readLimit.");
        } catch (IOException e) {
            assertEquals("mark has not been called, or too much has been read since marked.", e.getMessage());
        } catch (Throwable t) {
            fail("Did not expect " + t);
        }
    }

    @Test
    void requireThatReadAfterResetIncludesDataAfterMark() throws IOException {
        ReadableContentChannel content = new ReadableContentChannel();
        UnsafeContentInputStream in = new UnsafeContentInputStream(content);
        byte[] outBuf = new byte[]{1, 2, 3};
        content.write(ByteBuffer.wrap(outBuf), null);
        in.mark(4);
        assertEquals(3, in.read(new byte[]{101, 102, 103, 104}));
        in.reset();
        byte[] inBuf = new byte[4];
        int read = in.read(inBuf);
        assertEquals(3, read);
        assertArrayEquals(new byte[]{1, 2, 3, 0}, inBuf);
    }

    @Test
    void requireThatCompletionsAreCalledWithDeprecatedContentWriter() throws IOException {
        BufferedContentChannel channel = new BufferedContentChannel();
        FastContentWriter writer = new FastContentWriter(channel);
        writer.write("foo");
        writer.close();

        InputStream stream = asInputStream(channel);
        assertEquals('f', stream.read());
        assertEquals('o', stream.read());
        assertEquals('o', stream.read());
        assertEquals(-1, stream.read());
        assertTrue(writer.isDone());
    }

    @Test
    void requireThatCompletionsAreCalled() throws IOException {
        BufferedContentChannel channel = new BufferedContentChannel();
        FastContentWriter writer = new FastContentWriter(channel);
        writer.write("foo");
        writer.close();

        InputStream stream = asInputStream(channel);
        assertEquals('f', stream.read());
        assertEquals('o', stream.read());
        assertEquals('o', stream.read());
        assertEquals(-1, stream.read());
        assertTrue(writer.isDone());
    }

    @Test
    void requireThatCloseDrainsStreamWithDeprecatedContentWriter() {
        BufferedContentChannel channel = new BufferedContentChannel();
        FastContentWriter writer = new FastContentWriter(channel);
        writer.write("foo");
        writer.close();

        asInputStream(channel).close();
        assertTrue(writer.isDone());
    }

    @Test
    void requireThatCloseDrainsStream() {
        BufferedContentChannel channel = new BufferedContentChannel();
        FastContentWriter writer = new FastContentWriter(channel);
        writer.write("foo");
        writer.close();

        asInputStream(channel).close();
        assertTrue(writer.isDone());
    }

    @Test
    void requireThatAvailableIsNotBlocking() throws IOException {
        BufferedContentChannel channel = new BufferedContentChannel();
        InputStream stream = asInputStream(channel);
        assertEquals(0, stream.available());
        channel.write(ByteBuffer.wrap(new byte[]{6, 9}), null);
        assertTrue(stream.available() > 0);
        assertEquals(6, stream.read());
        assertTrue(stream.available() > 0);
        assertEquals(9, stream.read());
        assertEquals(0, stream.available());
        channel.close(null);
        assertEquals(-1, stream.read());
        assertEquals(0, stream.available());
    }

    @Test
    void requireThatReadLargeArrayIsNotBlocking() throws IOException {
        BufferedContentChannel channel = new BufferedContentChannel();
        InputStream stream = asInputStream(channel);
        assertEquals(0, stream.available());
        channel.write(ByteBuffer.wrap(new byte[]{6, 9}), null);
        assertTrue(stream.available() > 0);
        byte[] buf = new byte[69];
        assertEquals(2, stream.read(buf));
        assertEquals(6, buf[0]);
        assertEquals(9, buf[1]);
        assertEquals(0, stream.available());
        channel.close(null);
        assertEquals(-1, stream.read(buf));
        assertEquals(0, stream.available());
    }

    @Test
    void requireThatAllByteValuesCanBeRead() throws IOException {
        ReadableContentChannel content = new ReadableContentChannel();
        InputStream in = new UnsafeContentInputStream(content);
        for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; ++i) {
            content.write(ByteBuffer.wrap(new byte[]{(byte) i}), null);
            assertEquals(i, (byte) in.read());
        }
    }

    private static BufferedReader asBufferedReader(BufferedContentChannel channel) {
        return new BufferedReader(new InputStreamReader(asInputStream(channel)));
    }

    private static UnsafeContentInputStream asInputStream(BufferedContentChannel channel) {
        return new UnsafeContentInputStream(channel.toReadable());
    }
}