public async Task NewUserConnectsAndStreamsData()
        {
            Mock <IHubCallerClients <ISpectatorClient> > mockClients = new Mock <IHubCallerClients <ISpectatorClient> >();
            Mock <ISpectatorClient> mockReceiver = new Mock <ISpectatorClient>();

            mockClients.Setup(clients => clients.All).Returns(mockReceiver.Object);
            mockClients.Setup(clients => clients.Group(SpectatorHub.GetGroupId(streamer_id))).Returns(mockReceiver.Object);

            Mock <HubCallerContext> mockContext = new Mock <HubCallerContext>();

            mockContext.Setup(context => context.UserIdentifier).Returns(streamer_id.ToString());
            hub.Context = mockContext.Object;
            hub.Clients = mockClients.Object;

            await hub.BeginPlaySession(new SpectatorState(88));

            // check all other users were informed that streaming began
            mockClients.Verify(clients => clients.All, Times.Once);
            mockReceiver.Verify(clients => clients.UserBeganPlaying(streamer_id, It.Is <SpectatorState>(m => m.Equals(state))), Times.Once());

            // check state data was added
            var cacheState = await cache.GetStringAsync(SpectatorHub.GetStateId(streamer_id));

            Assert.Equal(state, JsonConvert.DeserializeObject <SpectatorState>(cacheState));

            var data = new FrameDataBundle(new[] { new LegacyReplayFrame(1234, 0, 0, ReplayButtonState.None) });

            // check streaming data is propagating to watchers
            await hub.SendFrameData(data);

            mockReceiver.Verify(clients => clients.UserSentFrames(streamer_id, data));
        }
        public async Task NewUserBeginsWatchingStream(bool ongoing)
        {
            string connectionId = Guid.NewGuid().ToString();

            Mock <IHubCallerClients <ISpectatorClient> > mockClients = new Mock <IHubCallerClients <ISpectatorClient> >();
            Mock <ISpectatorClient> mockCaller = new Mock <ISpectatorClient>();

            mockClients.Setup(clients => clients.Caller).Returns(mockCaller.Object);

            Mock <IGroupManager> mockGroups = new Mock <IGroupManager>();

            Mock <HubCallerContext> mockContext = new Mock <HubCallerContext>();

            mockContext.Setup(context => context.UserIdentifier).Returns(watcher_id);
            mockContext.Setup(context => context.ConnectionId).Returns(connectionId);

            hub.Context = mockContext.Object;
            hub.Clients = mockClients.Object;
            hub.Groups  = mockGroups.Object;

            if (ongoing)
            {
                await cache.SetStringAsync(SpectatorHub.GetStateId(streamer_id), JsonConvert.SerializeObject(state));
            }

            await hub.StartWatchingUser(streamer_id);

            mockGroups.Verify(groups => groups.AddToGroupAsync(connectionId, SpectatorHub.GetGroupId(streamer_id), default));

            mockCaller.Verify(clients => clients.UserBeganPlaying(streamer_id, It.Is <SpectatorState>(m => m.Equals(state))), ongoing ? Times.Once : Times.Never);
        }
Example #3
0
        public async Task NewUserBeginsWatchingStream(bool ongoing)
        {
            string connectionId = Guid.NewGuid().ToString();

            Mock <IHubCallerClients <ISpectatorClient> > mockClients = new Mock <IHubCallerClients <ISpectatorClient> >();
            Mock <ISpectatorClient> mockCaller = new Mock <ISpectatorClient>();

            mockClients.Setup(clients => clients.Caller).Returns(mockCaller.Object);
            mockClients.Setup(clients => clients.All).Returns(mockCaller.Object);

            Mock <IGroupManager> mockGroups = new Mock <IGroupManager>();

            Mock <HubCallerContext> streamerContext = new Mock <HubCallerContext>();

            streamerContext.Setup(context => context.UserIdentifier).Returns(streamer_id.ToString());

            Mock <HubCallerContext> watcherContext = new Mock <HubCallerContext>();

            watcherContext.Setup(context => context.UserIdentifier).Returns(watcher_id.ToString());
            watcherContext.Setup(context => context.ConnectionId).Returns(connectionId);

            hub.Clients = mockClients.Object;
            hub.Groups  = mockGroups.Object;

            if (ongoing)
            {
                hub.Context = streamerContext.Object;
                await hub.BeginPlaySession(state);

                mockCaller.Verify(clients => clients.UserBeganPlaying(streamer_id, It.Is <SpectatorState>(m => m.Equals(state))), Times.Once);
            }

            hub.Context = watcherContext.Object;

            await hub.StartWatchingUser(streamer_id);

            mockGroups.Verify(groups => groups.AddToGroupAsync(connectionId, SpectatorHub.GetGroupId(streamer_id), default));

            mockCaller.Verify(clients => clients.UserBeganPlaying(streamer_id, It.Is <SpectatorState>(m => m.Equals(state))), Times.Exactly(ongoing ? 2 : 0));
        }