< Summary

Class:Microsoft.Azure.ServiceBus.Primitives.TimeoutHelper
Assembly:Microsoft.Azure.ServiceBus
File(s):C:\Git\azure-sdk-for-net\sdk\servicebus\Microsoft.Azure.ServiceBus\src\Primitives\TimeoutHelper.cs
Covered lines:20
Uncovered lines:24
Coverable lines:44
Total lines:138
Line coverage:45.4% (20 of 44)
Covered branches:9
Total branches:28
Branch coverage:32.1% (9 of 28)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-100%100%
Add(...)-0%0%
Subtract(...)-0%100%
Divide(...)-0%0%
Min(...)-0%0%
Min(...)-0%0%
ThrowIfNegativeArgument(...)-0%100%
ThrowIfNegativeArgument(...)-66.67%50%
ThrowIfNonPositiveArgument(...)-100%100%
ThrowIfNonPositiveArgument(...)-66.67%50%
RemainingTime()-55.56%50%
ElapsedTime()-0%100%
SetDeadline()-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\servicebus\Microsoft.Azure.ServiceBus\src\Primitives\TimeoutHelper.cs

#LineLine coverage
 1// Copyright (c) Microsoft. All rights reserved.
 2// Licensed under the MIT license. See LICENSE file in the project root for full license information.
 3
 4namespace Microsoft.Azure.ServiceBus.Primitives
 5{
 6    using System;
 7    using System.Diagnostics;
 8
 9    [DebuggerStepThrough]
 10    struct TimeoutHelper
 11    {
 12        DateTime deadline;
 13        bool deadlineSet;
 14        TimeSpan originalTimeout;
 15
 16        public TimeoutHelper(TimeSpan timeout, bool startTimeout)
 17        {
 18            Debug.Assert(timeout >= TimeSpan.Zero, "timeout must be non-negative");
 19
 820            this.originalTimeout = timeout;
 821            this.deadline = DateTime.MaxValue;
 822            this.deadlineSet = (timeout == TimeSpan.MaxValue);
 23
 824            if (startTimeout && !this.deadlineSet)
 25            {
 826                this.SetDeadline();
 27            }
 828        }
 29
 30        public static DateTime Add(DateTime time, TimeSpan timeout)
 31        {
 032            if (timeout >= TimeSpan.Zero && DateTime.MaxValue - time <= timeout)
 33            {
 034                return DateTime.MaxValue;
 35            }
 036            if (timeout <= TimeSpan.Zero && DateTime.MinValue - time >= timeout)
 37            {
 038                return DateTime.MinValue;
 39            }
 040            return time + timeout;
 41        }
 42
 43        public static DateTime Subtract(DateTime time, TimeSpan timeout)
 44        {
 045            return Add(time, TimeSpan.Zero - timeout);
 46        }
 47
 48        public static TimeSpan Divide(TimeSpan timeout, int factor)
 49        {
 050            if (timeout == TimeSpan.MaxValue)
 51            {
 052                return TimeSpan.MaxValue;
 53            }
 54
 055            return Ticks.ToTimeSpan((Ticks.FromTimeSpan(timeout) / factor) + 1);
 56        }
 57
 58        public static TimeSpan Min(TimeSpan time1, TimeSpan time2)
 59        {
 060            if (time1 <= time2)
 61            {
 062                return time1;
 63            }
 64
 065            return time2;
 66        }
 67
 68        public static DateTime Min(DateTime first, DateTime second)
 69        {
 070            if (first <= second)
 71            {
 072                return first;
 73            }
 74
 075            return second;
 76        }
 77
 78        public static void ThrowIfNegativeArgument(TimeSpan timeout)
 79        {
 080            ThrowIfNegativeArgument(timeout, nameof(timeout));
 081        }
 82
 83        public static void ThrowIfNegativeArgument(TimeSpan timeout, string argumentName)
 84        {
 8485            if (timeout < TimeSpan.Zero)
 86            {
 087                throw Fx.Exception.ArgumentOutOfRange(argumentName, timeout, Resources.TimeoutMustBeNonNegative.FormatFo
 88            }
 8489        }
 90
 91        public static void ThrowIfNonPositiveArgument(TimeSpan timeout)
 92        {
 693            ThrowIfNonPositiveArgument(timeout, nameof(timeout));
 694        }
 95
 96        public static void ThrowIfNonPositiveArgument(TimeSpan timeout, string argumentName)
 97        {
 17498            if (timeout <= TimeSpan.Zero)
 99            {
 0100                throw Fx.Exception.ArgumentOutOfRange(argumentName, timeout, Resources.TimeoutMustBePositive.FormatForUs
 101            }
 174102        }
 103
 104        public TimeSpan RemainingTime()
 105        {
 12106            if (!this.deadlineSet)
 107            {
 0108                this.SetDeadline();
 0109                return this.originalTimeout;
 110            }
 111
 12112            if (this.deadline == DateTime.MaxValue)
 113            {
 0114                return TimeSpan.MaxValue;
 115            }
 116
 12117            var remaining = this.deadline - DateTime.UtcNow;
 12118            if (remaining <= TimeSpan.Zero)
 119            {
 0120                return TimeSpan.Zero;
 121            }
 122
 12123            return remaining;
 124        }
 125
 126        public TimeSpan ElapsedTime()
 127        {
 0128            return this.originalTimeout - this.RemainingTime();
 129        }
 130
 131        void SetDeadline()
 132        {
 133            Debug.Assert(!this.deadlineSet, "TimeoutHelper deadline set twice.");
 8134            this.deadline = DateTime.UtcNow + this.originalTimeout;
 8135            this.deadlineSet = true;
 8136        }
 137    }
 138}