|
|
|
@@ -511,6 +511,57 @@ func TestSourceClearsLastErrorAfterSuccessfulReconnect(t *testing.T) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func TestNewWithoutClientUsesStreamingSafeHTTPClient(t *testing.T) { |
|
|
|
src := New("ice-test", "http://example", nil, ReconnectConfig{}) |
|
|
|
if src.client == nil { |
|
|
|
t.Fatal("expected default http client") |
|
|
|
} |
|
|
|
if src.client.Timeout != 0 { |
|
|
|
t.Fatalf("client timeout=%v want 0 for streaming", src.client.Timeout) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func TestSourceReconnectsAfterDeadlineExceededError(t *testing.T) { |
|
|
|
var requests atomic.Int64 |
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
|
|
|
requests.Add(1) |
|
|
|
w.Header().Set("Content-Type", "audio/mpeg") |
|
|
|
_, _ = w.Write([]byte("test-stream")) |
|
|
|
})) |
|
|
|
defer srv.Close() |
|
|
|
|
|
|
|
dec := &scriptedLoopDecoder{ |
|
|
|
actions: []decodeAction{ |
|
|
|
{err: context.DeadlineExceeded}, // first attempt fails transiently |
|
|
|
{blockUntilStop: true}, // second attempt recovers and stays running |
|
|
|
}, |
|
|
|
} |
|
|
|
reg := decoder.NewRegistry() |
|
|
|
reg.Register("mp3", func() decoder.Decoder { return dec }) |
|
|
|
reg.Register("ffmpeg", func() decoder.Decoder { return &testDecoder{name: "ffmpeg"} }) |
|
|
|
|
|
|
|
src := New("ice-test", srv.URL, srv.Client(), ReconnectConfig{ |
|
|
|
Enabled: true, |
|
|
|
InitialBackoffMs: 1, |
|
|
|
MaxBackoffMs: 1, |
|
|
|
}, WithDecoderRegistry(reg), WithDecoderPreference("auto")) |
|
|
|
|
|
|
|
if err := src.Start(context.Background()); err != nil { |
|
|
|
t.Fatalf("start: %v", err) |
|
|
|
} |
|
|
|
defer src.Stop() |
|
|
|
|
|
|
|
waitForCondition(t, func() bool { return dec.callCount() >= 2 }, "second decode call after deadline exceeded") |
|
|
|
|
|
|
|
stats := src.Stats() |
|
|
|
if stats.Reconnects < 1 { |
|
|
|
t.Fatalf("reconnects=%d want >=1", stats.Reconnects) |
|
|
|
} |
|
|
|
if got := requests.Load(); got < 2 { |
|
|
|
t.Fatalf("requests=%d want >=2", got) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func waitForCondition(t *testing.T, cond func() bool, label string) { |
|
|
|
t.Helper() |
|
|
|
deadline := time.Now().Add(2 * time.Second) |
|
|
|
|