improved code coverage

This commit is contained in:
2024-08-22 17:07:42 +01:00
parent 4093f1893a
commit f4321c6261
2 changed files with 27 additions and 16 deletions

View File

@@ -12,27 +12,28 @@ func TestSequentialDelivery(t *testing.T) {
feedback := make([]int, 0, 3) feedback := make([]int, 0, 3)
subscribers := []Subscriber[int]{ subscribers := []Subscriber[int]{
func(msg int) bool { Once(func(msg int) {
assert.Equalf(t, testMsg, msg, "expected %d but got %d", testMsg, msg) assert.Equalf(t, testMsg, msg, "expected %d but got %d", testMsg, msg)
feedback = append(feedback, 1) feedback = append(feedback, 1)
return false }),
}, Once(func(msg int) {
func(msg int) bool {
assert.Equalf(t, testMsg, msg, "expected %d but got %d", testMsg, msg) assert.Equalf(t, testMsg, msg, "expected %d but got %d", testMsg, msg)
feedback = append(feedback, 2) feedback = append(feedback, 2)
return true }),
}, Once(func(msg int) {
func(msg int) bool {
assert.Equalf(t, testMsg, msg, "expected %d but got %d", testMsg, msg) assert.Equalf(t, testMsg, msg, "expected %d but got %d", testMsg, msg)
feedback = append(feedback, 3) feedback = append(feedback, 3)
return true }),
}, Forever(func(msg int) {
assert.Equalf(t, testMsg, msg, "expected %d but got %d", testMsg, msg)
feedback = append(feedback, 4)
}),
} }
nextSubscribers := sequentialDelivery(testMsg, subscribers) nextSubscribers := sequentialDelivery(testMsg, subscribers)
assert.Len(t, nextSubscribers, len(subscribers)-1, "expected to have one less subscriber") assert.Len(t, nextSubscribers, 1, "expected to have 1 subscriber")
assert.Len(t, feedback, 3, "one or more subscriber was not called") assert.Len(t, feedback, 4, "one or more subscriber was not called")
finalSubscribers := sequentialDelivery(testMsg, nextSubscribers) finalSubscribers := sequentialDelivery(testMsg, nextSubscribers)
@@ -40,8 +41,9 @@ func TestSequentialDelivery(t *testing.T) {
assert.Len(t, feedback, 5, "one or more subscriber was not called") assert.Len(t, feedback, 5, "one or more subscriber was not called")
assertContainsExactlyN(t, 1, 1, feedback) assertContainsExactlyN(t, 1, 1, feedback)
assertContainsExactlyN(t, 2, 2, feedback) assertContainsExactlyN(t, 2, 1, feedback)
assertContainsExactlyN(t, 3, 2, feedback) assertContainsExactlyN(t, 3, 1, feedback)
assertContainsExactlyN(t, 4, 2, feedback)
} }
func assertContainsExactlyN[T comparable](t testing.TB, exp T, n int, slice []T) { func assertContainsExactlyN[T comparable](t testing.TB, exp T, n int, slice []T) {

View File

@@ -1,8 +1,7 @@
package gubgub package gubgub
// Forever wrapper makes it more explicit that a subscriber will never stop consuming messages. // Forever wrapper makes it more explicit that a subscriber will never stop consuming messages.
// This helps avoiding subscribers that always return true which, depending on their size, might // This helps avoiding subscribers that always return TRUE.
// not be immediately clear.
func Forever[T any](fn func(T)) Subscriber[T] { func Forever[T any](fn func(T)) Subscriber[T] {
return func(msg T) bool { return func(msg T) bool {
fn(msg) fn(msg)
@@ -10,7 +9,17 @@ func Forever[T any](fn func(T)) Subscriber[T] {
} }
} }
// NoOp creates a subscriber that does absolutely nothing forever. This is mostly useful for testing. // Once returns a subscriber that will consume only one message.
// This helps avoiding subscribers that always return FALSE.
func Once[T any](fn func(T)) Subscriber[T] {
return func(t T) bool {
fn(t)
return false
}
}
// NoOp creates a subscriber that does absolutely nothing forever.
// This is mostly useful for testing.
func NoOp[T any]() Subscriber[T] { func NoOp[T any]() Subscriber[T] {
return func(_ T) bool { return true } return func(_ T) bool { return true }
} }