event: add ResubscribeErr (#22191)
This adds a way to get the error of the failing subscription for logging/debugging purposes. Co-authored-by: Felix Lange <fjl@twurst.com>
This commit is contained in:
@ -19,6 +19,8 @@ package event
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
@ -118,3 +120,37 @@ func TestResubscribeAbort(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResubscribeWithErrorHandler(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var i int
|
||||
nfails := 6
|
||||
subErrs := make([]string, 0)
|
||||
sub := ResubscribeErr(100*time.Millisecond, func(ctx context.Context, lastErr error) (Subscription, error) {
|
||||
i++
|
||||
var lastErrVal string
|
||||
if lastErr != nil {
|
||||
lastErrVal = lastErr.Error()
|
||||
}
|
||||
subErrs = append(subErrs, lastErrVal)
|
||||
sub := NewSubscription(func(unsubscribed <-chan struct{}) error {
|
||||
if i < nfails {
|
||||
return fmt.Errorf("err-%v", i)
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
})
|
||||
return sub, nil
|
||||
})
|
||||
|
||||
<-sub.Err()
|
||||
if i != nfails {
|
||||
t.Fatalf("resubscribe function called %d times, want %d times", i, nfails)
|
||||
}
|
||||
|
||||
expectedSubErrs := []string{"", "err-1", "err-2", "err-3", "err-4", "err-5"}
|
||||
if !reflect.DeepEqual(subErrs, expectedSubErrs) {
|
||||
t.Fatalf("unexpected subscription errors %v, want %v", subErrs, expectedSubErrs)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user