improved delivery tests
This commit is contained in:
@@ -12,18 +12,18 @@ func TestSequentialDelivery(t *testing.T) {
|
||||
feedback := make([]int, 0, 3)
|
||||
|
||||
subscribers := []Subscriber[int]{
|
||||
func(i int) bool {
|
||||
assert.Equalf(t, testMsg, i, "expected %d but got %d", testMsg, i)
|
||||
func(msg int) bool {
|
||||
assert.Equalf(t, testMsg, msg, "expected %d but got %d", testMsg, msg)
|
||||
feedback = append(feedback, 1)
|
||||
return true
|
||||
},
|
||||
func(i int) bool {
|
||||
assert.Equalf(t, testMsg, i, "expected %d but got %d", testMsg, i)
|
||||
feedback = append(feedback, 2)
|
||||
return false
|
||||
},
|
||||
func(i int) bool {
|
||||
assert.Equalf(t, testMsg, i, "expected %d but got %d", testMsg, i)
|
||||
func(msg int) bool {
|
||||
assert.Equalf(t, testMsg, msg, "expected %d but got %d", testMsg, msg)
|
||||
feedback = append(feedback, 2)
|
||||
return true
|
||||
},
|
||||
func(msg int) bool {
|
||||
assert.Equalf(t, testMsg, msg, "expected %d but got %d", testMsg, msg)
|
||||
feedback = append(feedback, 3)
|
||||
return true
|
||||
},
|
||||
@@ -38,4 +38,25 @@ func TestSequentialDelivery(t *testing.T) {
|
||||
|
||||
assert.Len(t, finalSubscribers, len(nextSubscribers), "expected to have the same subscribers")
|
||||
assert.Len(t, feedback, 5, "one or more subscriber was not called")
|
||||
|
||||
assertContainsExactlyN(t, 1, 1, feedback)
|
||||
assertContainsExactlyN(t, 2, 2, feedback)
|
||||
assertContainsExactlyN(t, 3, 2, feedback)
|
||||
}
|
||||
|
||||
func assertContainsExactlyN[T comparable](t testing.TB, exp T, n int, slice []T) {
|
||||
t.Helper()
|
||||
|
||||
var found int
|
||||
for _, v := range slice {
|
||||
if exp == v {
|
||||
found++
|
||||
}
|
||||
}
|
||||
|
||||
if n > found {
|
||||
t.Errorf("contains too few '%v': expected %d but found %d", exp, n, found)
|
||||
} else if n < found {
|
||||
t.Errorf("contains too many '%v': expected %d but found %d", exp, n, found)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user