improved delivery tests

This commit is contained in:
2024-08-22 15:47:14 +01:00
parent 7fd9c030c0
commit af2003fa5e
5 changed files with 37 additions and 26 deletions

View File

@@ -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)
}
}