summaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/com/yahoo/io/GrowableBufferOutputStreamTestCase.java
blob: 001023f62a4d2849ae53da7d240a8889a1f6f9a1 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.io;

import java.nio.channels.WritableByteChannel;
import java.nio.ByteBuffer;
import java.io.IOException;
import com.yahoo.io.GrowableBufferOutputStream;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;


/**
 * Tests the GrowableBufferOutputStream
 *
 * @author Bjorn Borud
 */
public class GrowableBufferOutputStreamTestCase {

    private byte[] testData;

    static class DummyWritableByteChannel implements WritableByteChannel {
        private ByteBuffer buffer;

        public DummyWritableByteChannel(ByteBuffer buffer) {
            this.buffer = buffer;
        }

        public int write(ByteBuffer src) {
            int written = Math.min(src.remaining(), buffer.remaining());

            if (buffer.remaining() < src.remaining()) {
                ByteBuffer tmp = src.slice();

                tmp.limit(written);
                src.position(src.position() + written);
            } else {
                buffer.put(src);
            }
            return written;
        }

        public boolean isOpen() {
            return true;
        }

        public void close() {}
    }

    @Before
    public void setUp() {
        testData = new byte[100];
        for (int i = 0; i < 100; ++i) {
            testData[i] = (byte) i;
        }
    }

    @Test
    public void testSimple() throws IOException {
        GrowableBufferOutputStream g = new GrowableBufferOutputStream(10, 5);

        g.write(testData, 0, 100);
        g.flush();
        assertEquals(10, g.numWritableBuffers());
        assertEquals(100, g.writableSize());

        ByteBuffer sink = ByteBuffer.allocate(60);
        DummyWritableByteChannel channel = new DummyWritableByteChannel(sink);
        int written = g.channelWrite(channel);

        assertEquals(60, written);
        assertEquals(60, sink.position());
        assertEquals(40, g.writableSize());

        // there should be 4 buffers left now
        assertEquals(4, g.numWritableBuffers());

        // ensure that we got what we expected
        for (int i = 0; i < 60; ++i) {
            if (((int) sink.get(i)) != i) {
                fail();
            }
        }

        // then we write more data
        g.write(testData, 0, 100);
        g.flush();
        assertEquals(140, g.writableSize());

        // ...which implies that we should now have 14 writable buffers
        assertEquals(14, g.numWritableBuffers());

        // reset the sink so it can consume more data
        sink.clear();

        // then write more to the DummyWritableByteChannel
        written = g.channelWrite(channel);
        assertEquals(60, written);
        assertEquals(60, sink.position());
        assertEquals(80, g.writableSize());

        // now there should be 8 buffers
        assertEquals(8, g.numWritableBuffers());

        // ensure that we got what we expected
        for (int i = 0; i < 60; ++i) {
            int val = (int) sink.get(i);
            int expected = (i + 60) % 100;

            if (val != expected) {
                fail("Value was " + val + " and not " + i);
            }
        }

        // when we clear there should be no buffers
        g.clear();
        assertEquals(0, g.numWritableBuffers());
        assertEquals(0, g.writableSize());

        // ditto after flush after clear
        g.flush();
        assertEquals(0, g.numWritableBuffers());

        // flush the cache too
        g.clearAll();
        assertEquals(0, g.numWritableBuffers());
    }

}