|
| 1 | +package session_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/floatdrop/moq-go/pkg/moqt/message" |
| 7 | + "github.com/floatdrop/moq-go/pkg/moqt/session" |
| 8 | + "github.com/floatdrop/moq-go/pkg/moqt/wire" |
| 9 | +) |
| 10 | + |
| 11 | +// TestAcceptFetchRepliesFetchOK checks AcceptFetch writes FETCH_OK with the |
| 12 | +// supplied fields and that the responder's OpenFetchStream binds this fetch's |
| 13 | +// Request ID onto the FETCH_HEADER uni-stream automatically. |
| 14 | +// |
| 15 | +// The server side runs in a goroutine and the client reads on the main |
| 16 | +// goroutine in send order (FETCH_OK, then the FETCH_HEADER stream): the |
| 17 | +// sessiontest streams are synchronous pipes, so a writer needs a concurrent |
| 18 | +// reader. |
| 19 | +func TestAcceptFetchRepliesFetchOK(t *testing.T) { |
| 20 | + t.Parallel() |
| 21 | + client, server := openPair(t) |
| 22 | + ctx := t.Context() |
| 23 | + |
| 24 | + fetchMsg := &message.Fetch{ |
| 25 | + RequestID: client.AllocRequestID(), |
| 26 | + FetchType: message.FetchTypeStandalone, |
| 27 | + Standalone: &message.StandaloneFetch{ |
| 28 | + Namespace: wire.Namespace("ns"), |
| 29 | + Name: []byte("clip"), |
| 30 | + }, |
| 31 | + } |
| 32 | + |
| 33 | + srvErr := make(chan error, 1) |
| 34 | + go func() { |
| 35 | + srvErr <- func() error { |
| 36 | + req, err := server.AcceptRequest(ctx) |
| 37 | + if err != nil { |
| 38 | + return err |
| 39 | + } |
| 40 | + resp, err := req.AcceptFetch(&message.FetchOK{ |
| 41 | + EndOfTrack: true, |
| 42 | + EndLocation: message.Location{Group: 5, Object: 9}, |
| 43 | + }) |
| 44 | + if err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + fs, err := resp.OpenFetchStream() // Request ID bound automatically |
| 48 | + if err != nil { |
| 49 | + return err |
| 50 | + } |
| 51 | + return fs.Close() |
| 52 | + }() |
| 53 | + }() |
| 54 | + |
| 55 | + fr, err := client.Fetch(ctx, fetchMsg) |
| 56 | + if err != nil { |
| 57 | + t.Fatalf("client Fetch: %v", err) |
| 58 | + } |
| 59 | + if !fr.OK.EndOfTrack || fr.OK.EndLocation != (message.Location{Group: 5, Object: 9}) { |
| 60 | + t.Errorf("FETCH_OK = %+v, want EndOfTrack and EndLocation {5,9}", fr.OK) |
| 61 | + } |
| 62 | + |
| 63 | + ds, err := client.AcceptDataStream(ctx) |
| 64 | + if err != nil { |
| 65 | + t.Fatalf("AcceptDataStream: %v", err) |
| 66 | + } |
| 67 | + fin, ok := ds.(*session.IncomingFetchStream) |
| 68 | + if !ok { |
| 69 | + t.Fatalf("got %T, want *IncomingFetchStream", ds) |
| 70 | + } |
| 71 | + if fin.Header.RequestID != fetchMsg.RequestID { |
| 72 | + t.Errorf("FETCH_HEADER RequestID = %d, want %d (bound by the responder)", |
| 73 | + fin.Header.RequestID, fetchMsg.RequestID) |
| 74 | + } |
| 75 | + if err := <-srvErr; err != nil { |
| 76 | + t.Fatalf("server: %v", err) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// TestAcceptTrackStatusRepliesOK checks AcceptTrackStatus replies TRACK_STATUS_OK |
| 81 | +// (a REQUEST_OK) carrying the supplied Track Properties. |
| 82 | +func TestAcceptTrackStatusRepliesOK(t *testing.T) { |
| 83 | + t.Parallel() |
| 84 | + client, server := openPair(t) |
| 85 | + |
| 86 | + ts := &message.TrackStatus{ |
| 87 | + RequestID: client.AllocRequestID(), |
| 88 | + Namespace: wire.Namespace("ns"), |
| 89 | + Name: []byte("t"), |
| 90 | + } |
| 91 | + _, resp := runRequestRoundTrip(t, client, server, ts, func(r *session.Request) error { |
| 92 | + return r.AcceptTrackStatus(&message.TrackStatusOK{}) |
| 93 | + }) |
| 94 | + if _, ok := resp.(*message.RequestOK); !ok { |
| 95 | + t.Fatalf("client got %s, want REQUEST_OK (TRACK_STATUS_OK)", resp.Type()) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +// TestAcceptNamespaceRequestsReplyOK checks the three namespace-request accept |
| 100 | +// helpers each reply REQUEST_OK and return a handle. |
| 101 | +func TestAcceptNamespaceRequestsReplyOK(t *testing.T) { |
| 102 | + t.Parallel() |
| 103 | + |
| 104 | + t.Run("PublishNamespace", func(t *testing.T) { |
| 105 | + t.Parallel() |
| 106 | + client, server := openPair(t) |
| 107 | + m := &message.PublishNamespace{RequestID: client.AllocRequestID(), Namespace: wire.Namespace("ns")} |
| 108 | + _, resp := runRequestRoundTrip(t, client, server, m, func(r *session.Request) error { |
| 109 | + _, err := r.AcceptPublishNamespace() |
| 110 | + return err |
| 111 | + }) |
| 112 | + if _, ok := resp.(*message.RequestOK); !ok { |
| 113 | + t.Fatalf("got %s, want REQUEST_OK", resp.Type()) |
| 114 | + } |
| 115 | + }) |
| 116 | + |
| 117 | + t.Run("SubscribeNamespace", func(t *testing.T) { |
| 118 | + t.Parallel() |
| 119 | + client, server := openPair(t) |
| 120 | + m := &message.SubscribeNamespace{RequestID: client.AllocRequestID(), TrackNamespacePrefix: wire.Namespace("ns")} |
| 121 | + _, resp := runRequestRoundTrip(t, client, server, m, func(r *session.Request) error { |
| 122 | + _, err := r.AcceptSubscribeNamespace() |
| 123 | + return err |
| 124 | + }) |
| 125 | + if _, ok := resp.(*message.RequestOK); !ok { |
| 126 | + t.Fatalf("got %s, want REQUEST_OK", resp.Type()) |
| 127 | + } |
| 128 | + }) |
| 129 | + |
| 130 | + t.Run("SubscribeTracks", func(t *testing.T) { |
| 131 | + t.Parallel() |
| 132 | + client, server := openPair(t) |
| 133 | + m := &message.SubscribeTracks{RequestID: client.AllocRequestID(), TrackNamespacePrefix: wire.Namespace("ns")} |
| 134 | + _, resp := runRequestRoundTrip(t, client, server, m, func(r *session.Request) error { |
| 135 | + _, err := r.AcceptSubscribeTracks() |
| 136 | + return err |
| 137 | + }) |
| 138 | + if _, ok := resp.(*message.RequestOK); !ok { |
| 139 | + t.Fatalf("got %s, want REQUEST_OK", resp.Type()) |
| 140 | + } |
| 141 | + }) |
| 142 | +} |
| 143 | + |
| 144 | +// TestAcceptSubscribeTracksWritePublishBlocked checks the publisher-side |
| 145 | +// WritePublishBlocked round-trips to the subscriber's ReadPublishBlocked. The |
| 146 | +// server side runs in a goroutine so its REQUEST_OK and PUBLISH_BLOCKED writes |
| 147 | +// have the client's concurrent reads (SubscribeTracks, ReadPublishBlocked) to |
| 148 | +// drain the synchronous sessiontest pipes. |
| 149 | +func TestAcceptSubscribeTracksWritePublishBlocked(t *testing.T) { |
| 150 | + t.Parallel() |
| 151 | + client, server := openPair(t) |
| 152 | + ctx := t.Context() |
| 153 | + |
| 154 | + srvErr := make(chan error, 1) |
| 155 | + go func() { |
| 156 | + srvErr <- func() error { |
| 157 | + req, err := server.AcceptRequest(ctx) |
| 158 | + if err != nil { |
| 159 | + return err |
| 160 | + } |
| 161 | + pub, err := req.AcceptSubscribeTracks() |
| 162 | + if err != nil { |
| 163 | + return err |
| 164 | + } |
| 165 | + return pub.WritePublishBlocked(&message.PublishBlocked{ |
| 166 | + TrackNamespaceSuffix: wire.Namespace("ns"), |
| 167 | + TrackName: []byte("blocked-track"), |
| 168 | + }) |
| 169 | + }() |
| 170 | + }() |
| 171 | + |
| 172 | + ts, err := client.SubscribeTracks(ctx, &message.SubscribeTracks{ |
| 173 | + TrackNamespacePrefix: wire.Namespace("ns"), |
| 174 | + }) |
| 175 | + if err != nil { |
| 176 | + t.Fatalf("client SubscribeTracks: %v", err) |
| 177 | + } |
| 178 | + defer ts.Close() |
| 179 | + |
| 180 | + pb, err := ts.ReadPublishBlocked() |
| 181 | + if err != nil { |
| 182 | + t.Fatalf("ReadPublishBlocked: %v", err) |
| 183 | + } |
| 184 | + if string(pb.TrackName) != "blocked-track" { |
| 185 | + t.Errorf("PUBLISH_BLOCKED TrackName = %q, want %q", pb.TrackName, "blocked-track") |
| 186 | + } |
| 187 | + if err := <-srvErr; err != nil { |
| 188 | + t.Fatalf("server: %v", err) |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +// TestAcceptPublishReturnsHandle checks AcceptPublish replies REQUEST_OK and |
| 193 | +// returns a handle reporting the publisher-assigned Track Alias. |
| 194 | +func TestAcceptPublishReturnsHandle(t *testing.T) { |
| 195 | + t.Parallel() |
| 196 | + client, server := openPair(t) |
| 197 | + ctx := t.Context() |
| 198 | + |
| 199 | + type result struct { |
| 200 | + pub *session.Publication |
| 201 | + err error |
| 202 | + } |
| 203 | + done := make(chan result, 1) |
| 204 | + go func() { |
| 205 | + pub, err := client.Publish(ctx, &message.Publish{ |
| 206 | + Namespace: wire.Namespace("ns"), |
| 207 | + Name: []byte("track"), |
| 208 | + TrackAlias: 11, |
| 209 | + }) |
| 210 | + done <- result{pub, err} |
| 211 | + }() |
| 212 | + |
| 213 | + req, err := server.AcceptRequest(ctx) |
| 214 | + if err != nil { |
| 215 | + t.Fatalf("AcceptRequest: %v", err) |
| 216 | + } |
| 217 | + recv, err := req.AcceptPublish() |
| 218 | + if err != nil { |
| 219 | + t.Fatalf("AcceptPublish: %v", err) |
| 220 | + } |
| 221 | + if recv.TrackAlias() != 11 { |
| 222 | + t.Errorf("IncomingPublication.TrackAlias = %d, want 11", recv.TrackAlias()) |
| 223 | + } |
| 224 | + |
| 225 | + got := <-done |
| 226 | + if got.err != nil { |
| 227 | + t.Fatalf("client Publish: %v", got.err) |
| 228 | + } |
| 229 | + _ = got.pub |
| 230 | +} |
0 commit comments