public void Stop()
        {
            var sequence = new TestSequence();

            sequence.ExpectConnect(null, StreamInitializationCause.WatchStarting);
            sequence.ProvideResponse(WatchResponseResult.Continue);
            sequence.WaitForCancellation();

            Task task = sequence.RunToStability();

            sequence.WatchStream.Stop(CancellationToken.None);
            task.Wait();
            sequence.Verify();
        }
        public void InitialTokenCancellation_WhileServerHanging()
        {
            var tokenSource = new CancellationTokenSource();
            var sequence    = new TestSequence(tokenSource.Token);

            sequence.ExpectConnect(null, StreamInitializationCause.WatchStarting);
            sequence.ProvideResponse(WatchResponseResult.Continue);
            sequence.WaitForCancellation();

            Task task = sequence.RunToStability();

            tokenSource.Cancel();
            AssertTaskIsCancelledSoon(task);
            sequence.Verify();
        }
        public void InitialTokenCancellation_WhileRunning()
        {
            var tokenSource = new CancellationTokenSource();
            var sequence    = new TestSequence(tokenSource.Token);

            sequence.ExpectConnect(null, StreamInitializationCause.WatchStarting);
            sequence.ProvideResponse(WatchResponseResult.Continue);
            sequence.WaitForCancellation();

            Task task = sequence.RunToStability();

            tokenSource.Cancel();
            // If it doesn't finish quickly, Task.Wait will time out and the assertion will fail.
            Assert.Throws <AggregateException>(() => task.Wait(s_timeout));
            Assert.Equal(TaskStatus.Canceled, task.Status);
        }
        public void StreamComplete()
        {
            ByteString token1   = ByteString.CopyFromUtf8("token1");
            ByteString token2   = ByteString.CopyFromUtf8("token2");
            var        sequence = new TestSequence();

            sequence.ExpectConnect(null, StreamInitializationCause.WatchStarting);
            sequence.ProvideResponse(WatchResponseResult.Continue);
            sequence.ProvideResponse(WatchResponseResult.StreamHealthy, token1);
            sequence.ProvideResponse(WatchResponseResult.StreamHealthy, token2);
            sequence.ProvideResponse(WatchResponseResult.Continue);
            // The stream ended. Restart with the last token.
            sequence.ExpectConnect(token2, StreamInitializationCause.StreamCompleted);
            sequence.ProvideResponse(WatchResponseResult.Continue);
            sequence.WaitForCancellation();

            Task task = sequence.RunToStability();

            sequence.WatchStream.Stop(CancellationToken.None);
            task.Wait();
            sequence.Verify();
        }
        public void StreamReset()
        {
            ByteString token    = ByteString.CopyFromUtf8("token");
            var        sequence = new TestSequence();

            sequence.ExpectConnect(null, StreamInitializationCause.WatchStarting);
            sequence.ProvideResponse(WatchResponseResult.Continue);
            sequence.ProvideResponse(WatchResponseResult.ResetStream);
            // First reset doesn't get a token
            sequence.ExpectConnect(null, StreamInitializationCause.ResetRequested);
            sequence.ProvideResponse(WatchResponseResult.StreamHealthy, token);
            sequence.ProvideResponse(WatchResponseResult.ResetStream);
            // Second reset does
            sequence.ExpectConnect(token, StreamInitializationCause.ResetRequested);
            sequence.WaitForCancellation();

            Task task = sequence.RunToStability();

            sequence.WatchStream.Stop(CancellationToken.None);
            task.Wait();
            sequence.Verify();
        }
        public void RetriableRpcException()
        {
            ByteString token1   = ByteString.CopyFromUtf8("token1");
            ByteString token2   = ByteString.CopyFromUtf8("token2");
            var        sequence = new TestSequence();

            sequence.ExpectConnect(null, StreamInitializationCause.WatchStarting);
            sequence.ProvideResponse(WatchResponseResult.Continue);
            sequence.ProvideResponse(WatchResponseResult.StreamHealthy, token1);
            sequence.ProvideResponse(WatchResponseResult.StreamHealthy, token2);
            sequence.RpcException(new RpcException(new Status(StatusCode.DeadlineExceeded, "This exception is transient")));
            // The server threw a retriable exception. Reinitialize.
            sequence.ExpectConnect(token2, StreamInitializationCause.RpcError);
            sequence.ProvideResponse(WatchResponseResult.Continue);
            sequence.WaitForCancellation();

            Task task = sequence.RunToStability();

            sequence.WatchStream.Stop(CancellationToken.None);
            task.Wait();
            sequence.Verify();
        }