Esempio n. 1
0
        public async Task LargeDeadline_DeadlineExceededWithReschedule()
        {
            // Arrange
            var testSink   = new TestSink();
            var testLogger = new TestLogger(string.Empty, testSink, true);

            var testSystemClock = new TestSystemClock(DateTime.UtcNow);
            var timeout         = TimeSpan.FromMilliseconds(100);
            var httpContext     = new DefaultHttpContext();

            httpContext.Request.Headers[GrpcProtocolConstants.TimeoutHeader] = "100m";
            var context = CreateServerCallContext(httpContext, testLogger);

            // Act
            var manager = new ServerCallDeadlineManager(context, testSystemClock, timeout, maxTimerDueTime: 5);

            // Assert
            var assertTask = TestHelpers.AssertIsTrueRetryAsync(
                () => context.Status.StatusCode == StatusCode.DeadlineExceeded,
                "StatusCode not set to DeadlineExceeded.");

            await Task.Delay(timeout);

            testSystemClock.UtcNow = testSystemClock.UtcNow.Add(timeout);

            await assertTask.DefaultTimeout();

            var write = testSink.Writes.Single(w => w.EventId.Name == "DeadlineExceeded");

            Assert.AreEqual("Request with timeout of 00:00:00.1000000 has exceeded its deadline.", write.Message);

            Assert.IsTrue(testSink.Writes.Any(w => w.EventId.Name == "DeadlineTimerRescheduled"));
        }
Esempio n. 2
0
        public async Task CancellationToken_ThrowExceptionInRegister()
        {
            // Arrange
            var testSink   = new TestSink();
            var testLogger = new TestLogger(string.Empty, testSink, true);

            var testSystemClock = new TestSystemClock(DateTime.UtcNow);
            var timeout         = TimeSpan.FromMilliseconds(100);
            var httpContext     = new DefaultHttpContext();

            httpContext.Request.Headers[GrpcProtocolConstants.TimeoutHeader] = "100m";
            var context   = CreateServerCallContext(httpContext, testLogger);
            var exception = new InvalidOperationException("Test");

            // Act
            var manager = new ServerCallDeadlineManager(context, SystemClock.Instance, timeout);

            // Assert
            manager.CancellationToken.Register(() =>
            {
                throw exception;
            });

            await TestHelpers.AssertIsTrueRetryAsync(
                () => context.Status.StatusCode == StatusCode.DeadlineExceeded,
                "StatusCode not set to DeadlineExceeded.").DefaultTimeout();

            await manager.WaitDeadlineCompleteAsync().DefaultTimeout();

            var deadlineExceededWrite = testSink.Writes.Single(w => w.EventId.Name == "DeadlineExceeded");

            Assert.AreEqual("Request with timeout of 00:00:00.1000000 has exceeded its deadline.", deadlineExceededWrite.Message);

            var deadlineCancellationErrorWrite = testSink.Writes.Single(w => w.EventId.Name == "DeadlineCancellationError");

            Assert.AreEqual("Error occurred while trying to cancel the request due to deadline exceeded.", deadlineCancellationErrorWrite.Message);
            Assert.AreEqual(exception, deadlineCancellationErrorWrite.Exception.InnerException);
        }