-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocus_test.go
169 lines (130 loc) · 4.13 KB
/
locus_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
166
167
168
169
package goutube
import (
"fmt"
"io/ioutil"
"os"
"testing"
"time"
streaming_api "github.com/Brijeshlakkad/goutube/api/streaming/v1"
"github.com/Brijeshlakkad/goutube/pointcron"
"github.com/stretchr/testify/require"
)
var (
testWrite = []byte("hello world")
)
func TestLocus(t *testing.T) {
for scenario, fn := range map[string]func(
t *testing.T, locus *Locus,
){
"create five pointers": testCreatePointers,
"append and read a record succeeds": testPointAppendRead,
"point should found": testPointShouldFound,
"point not found": testPointNotFoundErr,
"append on non-existing point": testNotExistingPointAppend,
"remove pointer": testRemovePointer,
"close unnecessary pointer": testPointCloseAfter,
"keep pointer open if a recent access": testPointKeepOpen,
} {
t.Run(scenario, func(t *testing.T) {
parentDir, err := ioutil.TempDir("", "locus-test")
require.NoError(t, err)
defer os.RemoveAll(parentDir)
c := Config{}
c.Distributed.MaxChunkSize = uint64(len(testWrite))
pointcronConfig := pointcron.Config{}
pointcronConfig.CloseTimeout = 1 * time.Second
pointcronConfig.TickTime = time.Second
c.Point.pointScheduler = pointcron.NewPointScheduler(pointcronConfig)
c.Point.pointScheduler.StartAsync()
log, err := NewLocus(parentDir, c)
require.NoError(t, err)
fn(t, log)
})
}
}
func testCreatePointers(t *testing.T, locus *Locus) {
pointCount := 5
for i := 0; i < pointCount; i++ {
newPointId := fmt.Sprintf("locus-test-file-%d", i)
_, err := locus.addPoint(newPointId)
require.NoError(t, err)
}
require.Equal(t, pointCount, len(locus.GetPoints()))
}
func testPointAppendRead(t *testing.T, locus *Locus) {
newPointId := "locus-test-file-0"
point, err := locus.addPoint(newPointId)
require.NoError(t, err)
defer locus.Remove(point.pointId)
_, pos, err := locus.Append(point.pointId, testWrite)
require.Equal(t, uint64(0), pos)
require.NoError(t, err)
_, b, err := locus.Read(point.pointId, 0, 0, 0)
require.Equal(t, b, testWrite)
}
func testPointShouldFound(t *testing.T, locus *Locus) {
newPointId := "locus-test-file-0"
pId, err := locus.addPoint(newPointId)
require.NoError(t, err)
defer locus.Remove(pId.pointId)
_, err = locus.get(pId.pointId)
require.NoError(t, err)
}
func testPointNotFoundErr(t *testing.T, locus *Locus) {
pId := "locus-test-file-0"
_, err := locus.get(pId)
apiErr := err.(streaming_api.PointNotFound)
require.Equal(t, pId, apiErr.PointId)
}
func testNotExistingPointAppend(t *testing.T, locus *Locus) {
pId := "locus-test-file-0"
if err := locus.Remove(pId); err != nil {
require.Error(t, err)
}
got := PanicValue(func() {
_, _, _ = locus.Append(pId, testWrite)
})
_, ok := got.(error)
if ok {
t.Error("Expected No Error")
}
}
func testRemovePointer(t *testing.T, locus *Locus) {
pId := "locus-test-file-0"
_, err := locus.addPoint(pId)
require.NoError(t, err)
err = locus.Remove(pId)
_, _, err = locus.Read(pId, 0, 0, 0)
apiErr := err.(streaming_api.PointNotFound)
require.Equal(t, pId, apiErr.PointId)
}
func testPointCloseAfter(t *testing.T, locus *Locus) {
pId := "locus-test-file-0"
point, err := locus.addPoint(pId)
require.NoError(t, err)
_, _, err = locus.Append(point.pointId, write)
require.NoError(t, err)
require.Eventually(t, func() bool {
return point.closed.Load().(bool)
}, 3*time.Second, 1*time.Second)
_, _, err = locus.Append(point.pointId, write)
require.NoError(t, err)
require.Equal(t, point.closed.Load().(bool), false)
require.Eventually(t, func() bool {
return point.closed.Load().(bool)
}, 3*time.Second, 1*time.Second)
}
func testPointKeepOpen(t *testing.T, locus *Locus) {
pId := "locus-test-file-0"
point, err := locus.addPoint(pId)
require.NoError(t, err)
_, _, err = locus.Append(point.pointId, write)
require.NoError(t, err)
time.Sleep(2 * time.Second)
_, _, err = locus.Append(point.pointId, write)
require.NoError(t, err)
require.Equal(t, point.closed.Load().(bool), false)
require.Eventually(t, func() bool {
return point.closed.Load().(bool)
}, 3*time.Second, 1*time.Second)
}