MCPcopy
hub / github.com/livekit/livekit / TestSubscribeStatusChanged

Function TestSubscribeStatusChanged

pkg/rtc/subscriptionmanager_test.go:257–316  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

255}
256
257func TestSubscribeStatusChanged(t *testing.T) {
258 sm := newTestSubscriptionManager()
259 defer sm.Close(false)
260 resolver := newTestResolver(true, true, "pub", "pubID")
261 sm.params.TrackResolver = resolver.Resolve
262 numParticipantSubscribed := atomic.Int32{}
263 numParticipantUnsubscribed := atomic.Int32{}
264 sm.OnSubscribeStatusChanged(func(pubID livekit.ParticipantID, subscribed bool) {
265 if subscribed {
266 numParticipantSubscribed.Add(1)
267 } else {
268 numParticipantUnsubscribed.Add(1)
269 }
270 })
271
272 sm.SubscribeToTrack("track1", false)
273 sm.SubscribeToTrack("track2", false)
274 s1 := sm.subscriptions["track1"]
275 s2 := sm.subscriptions["track2"]
276 require.Eventually(t, func() bool {
277 return !s1.needsSubscribe() && !s2.needsSubscribe()
278 }, subSettleTimeout, subCheckInterval, "track1 and track2 should be subscribed")
279 st1 := s1.getSubscribedTrack()
280 st1.OnClose(func(isExpectedToResume bool) {
281 sm.handleSubscribedTrackClose(s1, isExpectedToResume)
282 })
283 st2 := s2.getSubscribedTrack()
284 st2.OnClose(func(isExpectedToResume bool) {
285 sm.handleSubscribedTrackClose(s2, isExpectedToResume)
286 })
287 st1.MediaTrack().(*typesfakes.FakeMediaTrack).RemoveSubscriberCalls(func(pID livekit.ParticipantID, isExpectedToResume bool) {
288 setTestSubscribedTrackClosed(t, st1, isExpectedToResume)
289 })
290 st2.MediaTrack().(*typesfakes.FakeMediaTrack).RemoveSubscriberCalls(func(pID livekit.ParticipantID, isExpectedToResume bool) {
291 setTestSubscribedTrackClosed(t, st2, isExpectedToResume)
292 })
293
294 require.Eventually(t, func() bool {
295 return numParticipantSubscribed.Load() == 1
296 }, subSettleTimeout, subCheckInterval, "should be subscribed to publisher")
297 require.Equal(t, int32(0), numParticipantUnsubscribed.Load())
298 require.True(t, sm.IsSubscribedTo("pubID"))
299
300 // now unsubscribe track2, no event should be fired
301 sm.UnsubscribeFromTrack("track2")
302 require.Eventually(t, func() bool {
303 return !s2.needsUnsubscribe()
304 }, subSettleTimeout, subCheckInterval, "track2 should be unsubscribed")
305 require.Equal(t, int32(0), numParticipantUnsubscribed.Load())
306
307 // unsubscribe track1, expect event
308 sm.UnsubscribeFromTrack("track1")
309 require.Eventually(t, func() bool {
310 return !s1.needsUnsubscribe()
311 }, subSettleTimeout, subCheckInterval, "track1 should be unsubscribed")
312 require.Eventually(t, func() bool {
313 return numParticipantUnsubscribed.Load() == 1
314 }, subSettleTimeout, subCheckInterval, "should be subscribed to publisher")

Callers

nothing calls this directly

Calls 15

newTestResolverFunction · 0.85
CloseMethod · 0.65
SubscribeToTrackMethod · 0.65
OnCloseMethod · 0.65
MediaTrackMethod · 0.65
LoadMethod · 0.65
IsSubscribedToMethod · 0.65
UnsubscribeFromTrackMethod · 0.65

Tested by

no test coverage detected