|
| 1 | +package session_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "slices" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/floatdrop/moq-go/pkg/moqt" |
| 9 | + "github.com/floatdrop/moq-go/pkg/moqt/message" |
| 10 | + "github.com/floatdrop/moq-go/pkg/moqt/session" |
| 11 | + "github.com/floatdrop/moq-go/pkg/moqt/wire" |
| 12 | +) |
| 13 | + |
| 14 | +// TestRequestMuxRoutesByType checks that RequestMux dispatches requests by their |
| 15 | +// first message's Type, routes an unregistered type to OnUnknown, and honours a |
| 16 | +// handler registered after Run has already started. |
| 17 | +func TestRequestMuxRoutesByType(t *testing.T) { |
| 18 | + t.Parallel() |
| 19 | + client, server := openPair(t) |
| 20 | + |
| 21 | + events := make(chan message.Type, 8) |
| 22 | + firstSeen := make(chan struct{}, 1) |
| 23 | + record := func(r *session.Request) { |
| 24 | + events <- r.First.Type() |
| 25 | + _ = r.Stream.Close() |
| 26 | + } |
| 27 | + |
| 28 | + mux := session.NewRequestMux() |
| 29 | + mux.Handle(message.TypeSubscribe, func(r *session.Request) { |
| 30 | + record(r) |
| 31 | + firstSeen <- struct{}{} |
| 32 | + }) |
| 33 | + mux.Handle(message.TypePublishNamespace, record) |
| 34 | + mux.OnUnknown(record) |
| 35 | + |
| 36 | + go func() { _ = mux.Run(t.Context(), server) }() |
| 37 | + |
| 38 | + // Client sends even Request IDs (§10.1), strictly increasing. |
| 39 | + |
| 40 | + // 1. SUBSCRIBE on a registered type. |
| 41 | + openRequest(t, client, &message.Subscribe{ |
| 42 | + RequestID: 0, |
| 43 | + Namespace: wire.Namespace("ns"), |
| 44 | + Name: []byte("a"), |
| 45 | + Parameters: message.Parameters{message.LargestObjectFilter()}, |
| 46 | + }) |
| 47 | + <-firstSeen // ensure Run is live before late registration |
| 48 | + |
| 49 | + // 2. Register TRACK_STATUS *after* Run started, then send one. |
| 50 | + mux.Handle(message.TypeTrackStatus, record) |
| 51 | + openRequest(t, client, &message.TrackStatus{ |
| 52 | + RequestID: 2, |
| 53 | + Namespace: wire.Namespace("ns"), |
| 54 | + Name: []byte("a"), |
| 55 | + }) |
| 56 | + |
| 57 | + // 3. PUBLISH_NAMESPACE on a registered type. |
| 58 | + openRequest(t, client, &message.PublishNamespace{ |
| 59 | + RequestID: 4, |
| 60 | + Namespace: wire.Namespace("ns"), |
| 61 | + }) |
| 62 | + |
| 63 | + // 4. SUBSCRIBE_NAMESPACE — unregistered → OnUnknown. |
| 64 | + openRequest(t, client, &message.SubscribeNamespace{ |
| 65 | + RequestID: 6, |
| 66 | + TrackNamespacePrefix: wire.Namespace("ns"), |
| 67 | + }) |
| 68 | + |
| 69 | + got := collectEvents(t, events, 4) |
| 70 | + for _, want := range []message.Type{ |
| 71 | + message.TypeSubscribe, |
| 72 | + message.TypeTrackStatus, |
| 73 | + message.TypePublishNamespace, |
| 74 | + message.TypeSubscribeNamespace, |
| 75 | + } { |
| 76 | + if !slices.Contains(got, want) { |
| 77 | + t.Errorf("missing dispatched type %s; got %v", want, got) |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +// TestRequestMuxHandleType checks that HandleType derives the message.Type key |
| 83 | +// from T and hands the handler the already-asserted typed message. |
| 84 | +func TestRequestMuxHandleType(t *testing.T) { |
| 85 | + t.Parallel() |
| 86 | + client, server := openPair(t) |
| 87 | + |
| 88 | + got := make(chan string, 1) |
| 89 | + mux := session.NewRequestMux() |
| 90 | + session.HandleType(mux, func(_ *session.Request, msg *message.Subscribe) { |
| 91 | + got <- string(msg.Name) // typed access without a manual assertion |
| 92 | + }) |
| 93 | + go func() { _ = mux.Run(t.Context(), server) }() |
| 94 | + |
| 95 | + openRequest(t, client, &message.Subscribe{ |
| 96 | + RequestID: 0, |
| 97 | + Namespace: wire.Namespace("ns"), |
| 98 | + Name: []byte("typed-track"), |
| 99 | + Parameters: message.Parameters{message.LargestObjectFilter()}, |
| 100 | + }) |
| 101 | + |
| 102 | + select { |
| 103 | + case name := <-got: |
| 104 | + if name != "typed-track" { |
| 105 | + t.Errorf("handler saw Name = %q, want %q", name, "typed-track") |
| 106 | + } |
| 107 | + case <-time.After(2 * time.Second): |
| 108 | + t.Fatal("typed handler was not invoked") |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +// TestRequestMuxDefaultRejectsUnhandled verifies that with no OnUnknown set, an |
| 113 | +// unhandled request type is rejected with REQUEST_ERROR NOT_SUPPORTED and its |
| 114 | +// stream FIN'd, so the requester learns the server cannot serve it. |
| 115 | +func TestRequestMuxDefaultRejectsUnhandled(t *testing.T) { |
| 116 | + t.Parallel() |
| 117 | + client, server := openPair(t) |
| 118 | + |
| 119 | + mux := session.NewRequestMux() // no handlers, no OnUnknown |
| 120 | + go func() { _ = mux.Run(t.Context(), server) }() |
| 121 | + |
| 122 | + stream, err := client.OpenRequest(&message.TrackStatus{ |
| 123 | + RequestID: 0, |
| 124 | + Namespace: wire.Namespace("ns"), |
| 125 | + Name: []byte("x"), |
| 126 | + }) |
| 127 | + if err != nil { |
| 128 | + t.Fatalf("OpenRequest: %v", err) |
| 129 | + } |
| 130 | + |
| 131 | + resp, err := message.Parse(stream) |
| 132 | + if err != nil { |
| 133 | + t.Fatalf("read response: %v", err) |
| 134 | + } |
| 135 | + rerr, ok := resp.(*message.RequestError) |
| 136 | + if !ok { |
| 137 | + t.Fatalf("got %s, want REQUEST_ERROR", resp.Type()) |
| 138 | + } |
| 139 | + if rerr.ErrorCode != moqt.RequestNotSupported { |
| 140 | + t.Errorf("ErrorCode = %#x, want NOT_SUPPORTED (%#x)", rerr.ErrorCode, moqt.RequestNotSupported) |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +// openRequest opens a bidi request stream carrying first as its initial message |
| 145 | +// and leaves it open (the mux handler or session cleanup tears it down). |
| 146 | +func openRequest(t *testing.T, s *session.Session, first message.Message) { |
| 147 | + t.Helper() |
| 148 | + if _, err := s.OpenRequest(first); err != nil { |
| 149 | + t.Fatalf("OpenRequest(%s): %v", first.Type(), err) |
| 150 | + } |
| 151 | +} |
0 commit comments