Files
notify/stream_lifecycle_test.go
T

65 lines
2.2 KiB
Go
Raw Normal View History

package notify
import (
"context"
"errors"
"io"
"testing"
)
func TestStreamRuntimeAdoptFailureFinalizesCandidate(t *testing.T) {
runtime := newStreamRuntime("cstr")
scope := clientFileScope()
existing := newStreamHandle(context.Background(), runtime, scope, StreamOpenRequest{
StreamID: "duplicate",
DataID: 1,
}, 0, nil, nil, 0, nil, nil, nil, runtime.configSnapshot())
if err := runtime.register(scope, existing); err != nil {
t.Fatalf("register existing stream: %v", err)
}
defer existing.markReset(io.ErrClosedPipe)
candidate := newStreamHandle(context.Background(), runtime, scope, StreamOpenRequest{
StreamID: "duplicate",
DataID: 2,
}, 0, nil, nil, 0, nil, nil, nil, runtime.configSnapshot())
if err := runtime.adopt(scope, candidate); !errors.Is(err, errStreamAlreadyExists) {
t.Fatalf("adopt error = %v, want %v", err, errStreamAlreadyExists)
}
if err := candidate.resetErrSnapshot(); !errors.Is(err, errStreamAlreadyExists) {
t.Fatalf("candidate reset error = %v, want %v", err, errStreamAlreadyExists)
}
select {
case <-candidate.Context().Done():
default:
t.Fatal("failed stream adoption left candidate context active")
}
}
func TestStreamRuntimeStaleFinalizeDoesNotRemoveReplacement(t *testing.T) {
runtime := newStreamRuntime("cstr")
scope := clientFileScope()
old := newStreamHandle(context.Background(), runtime, scope, StreamOpenRequest{
StreamID: "reused",
DataID: 1,
}, 0, nil, nil, 0, nil, nil, nil, runtime.configSnapshot())
if err := runtime.register(scope, old); err != nil {
t.Fatalf("register old stream: %v", err)
}
old.markReset(errors.New("old failed"))
replacement := newStreamHandle(context.Background(), runtime, scope, StreamOpenRequest{
StreamID: "reused",
DataID: 2,
}, 0, nil, nil, 0, nil, nil, nil, runtime.configSnapshot())
if err := runtime.register(scope, replacement); err != nil {
t.Fatalf("register replacement stream: %v", err)
}
defer replacement.markReset(io.ErrClosedPipe)
old.markReset(errors.New("late duplicate reset"))
if got, ok := runtime.lookup(scope, "reused"); !ok || got != replacement {
t.Fatalf("replacement stream after stale finalize = %p/%v, want %p/true", got, ok, replacement)
}
}