forked from cloudfoundry/go-diodes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
many_to_one_test.go
159 lines (128 loc) · 3.18 KB
/
many_to_one_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
package diodes_test
import (
"code.cloudfoundry.org/go-diodes"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("ManyToOne", func() {
var (
d *diodes.ManyToOne
data []byte
spy *spyAlerter
)
BeforeEach(func() {
spy = newSpyAlerter()
d = diodes.NewManyToOne(5, spy)
data = []byte("some-data")
d.Set(diodes.GenericDataType(&data))
})
Context("multiple data slices", func() {
var (
secondData []byte
)
BeforeEach(func() {
secondData = []byte("some-other-data")
d.Set(diodes.GenericDataType(&secondData))
})
Describe("TryNext()", func() {
It("returns true", func() {
_, ok := d.TryNext()
Expect(ok).To(BeTrue())
})
Context("reads exceed writes", func() {
BeforeEach(func() {
d.TryNext()
d.TryNext()
})
It("returns false", func() {
_, ok := d.TryNext()
Expect(ok).To(BeFalse())
})
})
})
Context("buffer size exceeded", func() {
BeforeEach(func() {
for i := 0; i < 4; i++ {
d.Set(diodes.GenericDataType(&secondData))
}
})
It("wraps", func() {
data, _ := d.TryNext()
Expect(*(*[]byte)(data)).To(Equal(secondData))
})
It("alerts for each dropped point", func() {
d.TryNext()
Expect(spy.AlertInput.Missed).To(Receive(Equal(5)))
})
It("it updates the read index", func() {
d.TryNext()
Expect(spy.AlertInput.Missed).To(Receive(Equal(5)))
for i := 0; i < 6; i++ {
j := i
d.Set(diodes.GenericDataType(&j))
}
data, _ := d.TryNext()
Expect(*(*int)(data)).To(Equal(5))
Expect(spy.AlertInput.Missed).To(Receive(Equal(5)))
})
Context("read catches up with write", func() {
BeforeEach(func() {
d.TryNext()
<-spy.AlertInput.Missed
})
It("does not alert", func() {
d.TryNext()
Expect(spy.AlertInput.Missed).To(Not(Receive()))
})
})
Context("writer laps reader", func() {
BeforeEach(func() {
for i := 0; i < 5; i++ {
d.Set(diodes.GenericDataType(&secondData))
}
d.TryNext()
})
It("sends an alert for each set", func() {
Expect(spy.AlertInput.Missed).To(Receive(Equal(10)))
})
})
Context("writer laps reader with nil alerter", func() {
It("drops the alert", func() {
d = diodes.NewManyToOne(5, nil)
for i := 0; i < 10; i++ {
d.Set(diodes.GenericDataType(&secondData))
}
Expect(func() {
d.TryNext()
}).ToNot(Panic())
})
})
})
})
})
var _ = Describe("reader ahead of writer", func() {
It("must not occur after alerting", func() {
length := 4
spy := newSpyAlerter()
d := diodes.NewManyToOne(length, spy)
data := []byte("some-data")
genData := diodes.GenericDataType(&data)
By("filling up the buffer")
for i := 0; i < length; i++ {
d.Set(genData)
}
By("overwriting the first index")
d.Set(genData)
By("having reader fast forward")
Expect(spy.AlertInput.Missed).To(BeEmpty())
_, ok := d.TryNext()
Expect(ok).To(BeTrue())
Expect(spy.AlertInput.Missed).To(Receive(Equal(4)))
By("failing reads until the writer writes over skipped values")
_, ok = d.TryNext()
Expect(ok).To(BeFalse())
d.Set(genData)
_, ok = d.TryNext()
Expect(ok).To(BeTrue())
})
})