forked from morita-kuma/golog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer_buffered_test.go
165 lines (140 loc) · 5.27 KB
/
writer_buffered_test.go
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
package golog
import (
"testing"
"os"
"github.com/stretchr/testify/assert"
"fmt"
"io"
)
func BenchmarkWriter_Write(b *testing.B) {
bufferedFileWriter := newBufferedWriter(os.Stdout, withBufferSize(100))
data := []byte(`x5tT0dAb6ntcZIgn9danMXABNt8MfowerG87UgxjLODNibqt3H3zfzNZvzJr9DiLoRMqePUzot0UsgPM63mzcSQkgVOexrCpWyFR`)
for i := 0; i < b.N; i++ {
bufferedFileWriter.Write(data)
}
}
func TestNewWriter(t *testing.T) {
t.Run("expected initial value " , func(t *testing.T) {
writer := newBufferedWriter(os.Stdout)
assert.NotNil(t, writer)
assert.Equal(t, 0, writer.numberOfWrittenBytes)
assert.Equal(t, defaultBufferSize, writer.capacity())
})
t.Run("returns own instance " , func(t *testing.T) {
writer := newBufferedWriter(os.Stdout)
newWriter := newBufferedWriter(writer)
assert.Equal(t, fmt.Sprintf("%p", writer), fmt.Sprintf("%p", newWriter))
})
t.Run("returns buffer size overwritten by option parameter", func(t *testing.T) {
capacity := 100
writer := newBufferedWriter(os.Stdout, withBufferSize(capacity))
expected := capacity
assert.Equal(t, expected, writer.capacity())
})
t.Run("if capacity setted negative integer then default value will be used", func(t *testing.T) {
writer := newBufferedWriter(os.Stdout, withBufferSize(-1))
assert.Equal(t, defaultBufferSize, writer.capacity())
})
}
func TestWriter_Write(t *testing.T) {
t.Run("returns error if writer has error", func(t *testing.T) {
writer := bufferedWriter{
err: io.ErrShortWrite,
}
_, err := writer.Write([]byte(""))
assert.NotNil(t, err)
})
t.Run("not flushed for the same input as capacity", func(t *testing.T) {
input := []byte("test")
capacity := len(input)
bufferedFileWriter := newBufferedWriter(os.Stdout, withBufferSize(capacity))
bufferedFileWriter.Write(input)
expected := len(input)
assert.Equal(t, expected, bufferedFileWriter.buffered())
})
t.Run("If it exceeds the capacity, it will be output directly", func(t *testing.T) {
capacity := 4
bufferedFileWriter := newBufferedWriter(os.Stdout, withBufferSize(capacity))
n, err := bufferedFileWriter.Write([]byte("test123"))
assert.Nil(t, err)
assert.Equal(t, 0, bufferedFileWriter.buffered())
assert.Equal(t, 7, n)
})
t.Run("If it exceeds the available size of buffer(capacity >= input > available), it will be flush before buffering", func(t *testing.T) {
capacity := 10
bufferedFileWriter := newBufferedWriter(os.Stdout, withBufferSize(capacity))
n, err := bufferedFileWriter.Write([]byte("test123"))
n, err = bufferedFileWriter.Write([]byte("test"))
assert.Nil(t, err)
assert.Equal(t, 4, bufferedFileWriter.buffered())
assert.Equal(t, 4, n)
})
t.Run("If length of input is less than available, it will be buffering without flush call", func(t *testing.T) {
capacity := 10
bufferedFileWriter := newBufferedWriter(os.Stdout, withBufferSize(capacity))
n, err := bufferedFileWriter.Write([]byte("test123"))
assert.Nil(t, err)
assert.Equal(t, 7, bufferedFileWriter.buffered())
assert.Equal(t, 7, n)
})
t.Run("Buffers are gradually accumulated", func(t *testing.T) {
input := []byte("test")
bufferedFileWriter := newBufferedWriter(os.Stdout, withBufferSize(15))
bufferedFileWriter.Write(input)
assert.Equal(t, 4, bufferedFileWriter.buffered())
assert.Equal(t, "test", string(bufferedFileWriter.buffer[0:bufferedFileWriter.buffered()]))
bufferedFileWriter.Write(input)
assert.Equal(t, 8, bufferedFileWriter.buffered())
assert.Equal(t, "testtest", string(bufferedFileWriter.buffer[0:bufferedFileWriter.buffered()]))
bufferedFileWriter.Write(input)
assert.Equal(t, 12, bufferedFileWriter.buffered())
assert.Equal(t, "testtesttest", string(bufferedFileWriter.buffer[0:bufferedFileWriter.buffered()]))
bufferedFileWriter.Write(input)
assert.Equal(t, 4, bufferedFileWriter.buffered())
assert.Equal(t, "test", string(bufferedFileWriter.buffer[0:bufferedFileWriter.buffered()]))
})
}
func TestWriter_Flush(t *testing.T) {
t.Run("returns error if writer has error", func(t *testing.T) {
writer := bufferedWriter{
err: io.ErrShortWrite,
}
err := writer.Flush()
assert.NotNil(t, err)
})
}
func TestWriter_buffered(t *testing.T) {
t.Run("returns zero", func(t *testing.T) {
bufferedFileWriter := newBufferedWriter(os.Stdout)
expected := 0
actual := bufferedFileWriter.buffered()
assert.Equal(t, expected, actual)
})
t.Run("returns length of inputting", func(t *testing.T) {
input := []byte("test")
bufferedFileWriter := newBufferedWriter(os.Stdout)
bufferedFileWriter.Write(input)
expected := 4
actual := bufferedFileWriter.buffered()
assert.Equal(t, expected, actual)
})
t.Run("returns zero after flush", func(t *testing.T) {
input := []byte("test")
bufferedFileWriter := newBufferedWriter(os.Stdout)
bufferedFileWriter.Write(input)
bufferedFileWriter.Flush()
expected := 0
actual := bufferedFileWriter.buffered()
assert.Equal(t, expected, actual)
})
t.Run("returns length of third input after flush automatically", func(t *testing.T) {
input := []byte("test")
bufferedFileWriter := newBufferedWriter(os.Stdout, withBufferSize(10))
bufferedFileWriter.Write(input)
bufferedFileWriter.Write(input)
bufferedFileWriter.Write(input) // flush and input
expected := 4
actual := bufferedFileWriter.buffered()
assert.Equal(t, expected, actual)
})
}