-
Notifications
You must be signed in to change notification settings - Fork 1
/
writer_test.go
75 lines (58 loc) · 1.96 KB
/
writer_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
package conveyor_test
import (
"bytes"
"testing"
"github.com/fgehrlicher/conveyor"
"github.com/stretchr/testify/assert"
)
func TestWriteChunksNoOrder(t *testing.T) {
var buff bytes.Buffer
assertion := assert.New(t)
writer := conveyor.NewConcurrentWriter(&buff, false)
assertion.NoError(writer.Write(&conveyor.Chunk{Id: 3}, testChunks[3]))
assertion.NoError(writer.Write(&conveyor.Chunk{Id: 1}, testChunks[1]))
assertion.NoError(writer.Write(&conveyor.Chunk{Id: 2}, testChunks[2]))
output := buff.String()
for i := 1; i <= 3; i++ {
chunk := string(testChunks[i])
assertion.Containsf(output, chunk, "Buff doesnt contain chunk %d", i)
}
}
func TestWriteChunksInOrder(t *testing.T) {
var buff bytes.Buffer
assertion := assert.New(t)
writer := conveyor.NewConcurrentWriter(&buff, true)
assertion.NoError(writer.Write(&conveyor.Chunk{Id: 3}, testChunks[3]))
assertion.NoError(writer.Write(&conveyor.Chunk{Id: 2}, testChunks[2]))
assertion.NoError(writer.Write(&conveyor.Chunk{Id: 1}, testChunks[1]))
output := buff.String()
var expectedOutput string
for i := 1; i <= 3; i++ {
expectedOutput += string(testChunks[i])
if i != 3 {
expectedOutput += "\n"
}
}
assertion.Equal(expectedOutput, output, "Chunks buff not in Order")
}
func TestDoesNotWriteEmptyCache(t *testing.T) {
var buff bytes.Buffer
assertion := assert.New(t)
writer := conveyor.NewConcurrentWriter(&buff, false)
assertion.NoError(writer.Write(&conveyor.Chunk{Id: 1}, nil))
assertion.Empty(buff.Bytes())
}
func TestFailingWriterPassesError(t *testing.T) {
assertion := assert.New(t)
writer := conveyor.NewConcurrentWriter(&InvalidWriter{FailAt: 0}, true)
assertion.ErrorIs(
writer.Write(&conveyor.Chunk{Id: 1}, testChunks[1]),
ErrInvalidWrite,
)
writer = conveyor.NewConcurrentWriter(&InvalidWriter{FailAt: 1}, true)
assertion.NoError(writer.Write(&conveyor.Chunk{Id: 1}, testChunks[1]))
assertion.ErrorIs(
writer.Write(&conveyor.Chunk{Id: 2}, testChunks[1]),
ErrInvalidWrite,
)
}