notify/signal_reliable_test.go

58 lines
1.4 KiB
Go
Raw Permalink Normal View History

package notify
import (
"errors"
"testing"
"time"
)
func TestRetryReliableSignalSendRetriesUntilAck(t *testing.T) {
cfg := signalReliabilityConfig{
AckTimeout: 10 * time.Millisecond,
SendRetry: 3,
ReceiveCacheLimit: 4,
}
pool := newSignalAckPool()
attempts := 0
err := retryReliableSignalSend(cfg, func(cfg signalReliabilityConfig) error {
attempts++
return sendSignalWithAck("client", 2001, cfg.AckTimeout, pool, func() error {
if attempts == 2 {
go func() {
time.Sleep(time.Millisecond)
pool.deliver("client", 2001)
}()
}
return nil
})
})
if err != nil {
t.Fatalf("retryReliableSignalSend should succeed after ack: %v", err)
}
if got, want := attempts, 2; got != want {
t.Fatalf("retry attempts mismatch: got %d want %d", got, want)
}
}
func TestRetryReliableSignalSendReturnsLastError(t *testing.T) {
wantErr := errors.New("send failed")
cfg := signalReliabilityConfig{
AckTimeout: 5 * time.Millisecond,
SendRetry: 2,
ReceiveCacheLimit: 4,
}
attempts := 0
err := retryReliableSignalSend(cfg, func(cfg signalReliabilityConfig) error {
attempts++
return wantErr
})
if !errors.Is(err, wantErr) {
t.Fatalf("retryReliableSignalSend should return last error: %v", err)
}
if got, want := attempts, 2; got != want {
t.Fatalf("retry attempts mismatch: got %d want %d", got, want)
}
}