private async Task LongTaskAsync(WebSocketService.WebSocketService webSocketService,
                                         CancellationToken cancellationToken, string taskId)
        {
            for (int i = 1; i < 5; ++i)
            {
                await Task.Delay(20);

                cancellationToken.ThrowIfCancellationRequested();
                await Task.Delay(20);
            }
        }
        public WebSocketServiceTests()
        {
            mapper = new Mock <IViewModelSerializationMapper>();

            context    = new Mock <IDotvvmRequestContext>();
            serializer = new Mock <WebSocketViewModelSerializer>(mapper.Object);
            socket     = new Mock <WebSocket>();
            manager    = new Mock <WebSocketManager>();
            connection = new Mock <Connection>();
            service    = new WebSocketService.WebSocketService(manager.Object, serializer.Object, context.Object)
            {
                Path = "/test"
            };
        }
        public void CreateTaskTest()
        {
            Assert.Empty(manager.Object.TaskList.Values);

            service = new WebSocketService.WebSocketService(manager.Object, serializer.Object, context.Object)
            {
                Path         = "/test",
                ConnectionId = guid
            };
            manager.Object.Connections.TryAdd(guid, connection.Object);


            var id = service.CreateTask <WebSocketService.WebSocketService>(LongTaskAsync);

            Assert.NotEmpty(manager.Object.TaskList.Values);
            Assert.NotNull(manager.Object.TaskList.SelectMany(s => s.Value).First(s => s.TaskId == id));
        }
        public void SaveStateTest()
        {
            context.SetupGet(c => c.ViewModel).Returns(new { a = 2, b = 3 });
            context.SetupGet(c => c.CsrfToken).Returns("csrfstring");
            service = new WebSocketService.WebSocketService(manager.Object, serializer.Object, context.Object)
            {
                Path         = "/test",
                ConnectionId = guid
            };

            connection.Object.ViewModelState.ChangedViewModelJson = JObject.Parse(@"{'CPU': 'Intel'}");

            manager.Object.Connections.TryAdd(guid, connection.Object);
            serializer.Setup(s => s.BuildViewModel(connection.Object.ViewModelState));

            service.SaveCurrentState();


            serializer.Verify(s => s.BuildViewModel(connection.Object.ViewModelState));
            Assert.Equal(context.Object.CsrfToken, connection.Object.ViewModelState.CsrfToken);
            Assert.Equal(context.Object.ViewModel, connection.Object.ViewModelState.LastSentViewModel);
            Assert.Equal(connection.Object.ViewModelState.LastSentViewModelJson,
                         connection.Object.ViewModelState.ChangedViewModelJson);
        }