-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwriter_test.go
More file actions
159 lines (122 loc) · 4.14 KB
/
writer_test.go
File metadata and controls
159 lines (122 loc) · 4.14 KB
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
package mongo
import (
"context"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/bson"
driver "go.mongodb.org/mongo-driver/mongo"
)
func TestWriter(t *testing.T) {
count := func(coll *driver.Collection) (res int) {
count, err := coll.CountDocuments(context.Background(), bson.M{})
assert.Nil(t, err)
return int(count)
}
mg, coll, teardown := MakeTestConnection(t)
defer teardown()
var wr BufferedWriter = NewBufferedWriter(mg, "test", coll.Name(), 3)
assert.Nil(t, wr.Write(bson.M{"key1": "val1"}), "write rec #1")
assert.Nil(t, wr.Write(bson.M{"key2": "val2"}), "write rec #2")
assert.Equal(t, 0, count(coll), "nothing yet")
assert.Nil(t, wr.Write(bson.M{"key3": "val3"}), "write rec #3")
assert.Equal(t, 3, count(coll), "all 3 records in")
assert.Nil(t, wr.Write(bson.M{"key4": "val4"}), "write rec #4")
assert.Equal(t, 3, count(coll), "still 3 records")
assert.Nil(t, wr.Flush())
assert.Equal(t, 4, count(coll), "all 4 records")
assert.Nil(t, wr.Flush())
assert.Equal(t, 4, count(coll), "still 4 records, nothing left to flush")
assert.Nil(t, wr.Close())
}
func TestWriter_WithCollection(t *testing.T) {
mg, coll, teardown := MakeTestConnection(t)
defer func() {
_ = coll.Drop(context.Background())
teardown()
}()
wr := NewBufferedWriter(mg, "test", coll.Name(), 3).WithCollection("coll1")
for i := 0; i < 100; i++ {
require.NoError(t, wr.Write(bson.M{"key1": 1, "key2": 2}))
}
require.NoError(t, wr.Flush())
coll = mg.Database("test").Collection("coll1")
count, err := coll.CountDocuments(context.Background(), bson.M{})
assert.Nil(t, err)
assert.Equal(t, 100, int(count))
}
func TestWriter_Parallel(t *testing.T) {
mg, coll, teardown := MakeTestConnection(t)
defer teardown()
var wg sync.WaitGroup
wr := NewBufferedWriter(mg, "test", coll.Name(), 75)
writeMany := func() {
for i := 0; i < 100; i++ {
require.NoError(t, wr.Write(bson.M{"key1": 1, "key2": 2}))
}
require.NoError(t, wr.Flush())
wg.Done()
}
for i := 0; i < 16; i++ {
wg.Add(1)
go writeMany()
}
wg.Wait()
count, err := coll.CountDocuments(context.Background(), bson.M{})
assert.Nil(t, err)
assert.Equal(t, 100*16, int(count))
assert.Nil(t, wr.Close())
}
func TestWriter_WithAutoFlush(t *testing.T) {
mg, coll, teardown := MakeTestConnection(t)
defer teardown()
var wr BufferedWriter = NewBufferedWriter(mg, "test", coll.Name(), 3).WithAutoFlush(300 * time.Millisecond)
count := func() (res int) {
count, err := coll.CountDocuments(context.Background(), bson.M{})
assert.Nil(t, err)
return int(count)
}
assert.Nil(t, wr.Write(bson.M{"key1": "val1"}), "write rec #1")
assert.Nil(t, wr.Write(bson.M{"key2": "val2"}), "write rec #2")
assert.Equal(t, 0, count(), "nothing yet")
time.Sleep(600 * time.Millisecond)
assert.Equal(t, 2, count(), "2 records flushed")
assert.Nil(t, wr.Write(bson.M{"key3": "val3"}), "write rec #3")
assert.Nil(t, wr.Write(bson.M{"key4": "val4"}), "write rec #4")
assert.Nil(t, wr.Write(bson.M{"key5": "val5"}), "write rec #5")
assert.Equal(t, 5, count(), "5 records, flushed by size, not duration")
assert.Nil(t, wr.Write(bson.M{"key6": "val6"}), "write rec #6")
assert.Nil(t, wr.Write(bson.M{"key7": "val7"}), "write rec #7")
assert.Equal(t, 5, count(), "still 5 records")
assert.Nil(t, wr.Flush())
assert.Equal(t, 7, count(), "all 7 records")
assert.Nil(t, wr.Flush())
assert.Equal(t, 7, count(), "still 7 records, nothing left to flush")
assert.Nil(t, wr.Close())
}
func TestWriter_ParallelWithAutoFlush(t *testing.T) {
mg, coll, teardown := MakeTestConnection(t)
defer teardown()
var wg sync.WaitGroup
wr := NewBufferedWriter(mg, "test", coll.Name(), 75).WithAutoFlush(time.Millisecond)
writeMany := func() {
for i := 0; i < 100; i++ {
require.NoError(t, wr.Write(bson.M{"key1": 1, "key2": 2}))
time.Sleep(time.Millisecond * 3)
}
err := wr.Flush()
require.NoError(t, err)
wg.Done()
}
for i := 0; i < 16; i++ {
wg.Add(1)
go writeMany()
}
wg.Wait()
count, err := coll.CountDocuments(context.Background(), bson.M{})
assert.Nil(t, err)
assert.Equal(t, 100*16, int(count))
assert.Nil(t, wr.Close())
}