< Summary

Class:Microsoft.Azure.ServiceBus.Amqp.ActiveClientLinkManager
Assembly:Microsoft.Azure.ServiceBus
File(s):C:\Git\azure-sdk-for-net\sdk\servicebus\Microsoft.Azure.ServiceBus\src\Amqp\ActiveClientLinkManager.cs
Covered lines:7
Uncovered lines:67
Coverable lines:74
Total lines:155
Line coverage:9.4% (7 of 74)
Covered branches:1
Total branches:20
Branch coverage:5% (1 of 20)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.cctor()-0%100%
.ctor(...)-100%50%
Close()-0%100%
SetActiveSendReceiveLink(...)-0%0%
OnSendReceiveLinkClosed(...)-0%100%
SetActiveRequestResponseLink(...)-0%0%
OnRenewSendReceiveCbsToken()-0%100%
OnRenewRequestResponseCbsToken()-0%100%
RenewCbsTokenAsync()-0%0%
<RenewCbsTokenAsync()-0%100%
OnRequestResponseLinkClosed(...)-0%100%
SetRenewCbsTokenTimer(...)-0%0%
ChangeRenewTimer(...)-0%0%

File(s)

C:\Git\azure-sdk-for-net\sdk\servicebus\Microsoft.Azure.ServiceBus\src\Amqp\ActiveClientLinkManager.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.Amqp
 5{
 6    using Azure.Amqp;
 7    using Primitives;
 8    using System;
 9    using System.Threading;
 10    using System.Threading.Tasks;
 11
 12    sealed class ActiveClientLinkManager
 13    {
 014        static readonly TimeSpan SendTokenTimeout = TimeSpan.FromMinutes(1);
 015        static readonly TimeSpan TokenRefreshBuffer = TimeSpan.FromSeconds(10);
 016        static readonly TimeSpan MaxTokenRefreshTime = TimeSpan.FromDays(30);
 17
 18        readonly string clientId;
 19        readonly RetryPolicy retryPolicy;
 20        readonly ICbsTokenProvider cbsTokenProvider;
 21        Timer sendReceiveLinkCbsTokenRenewalTimer;
 22        Timer requestResponseLinkCbsTokenRenewalTimer;
 23
 24        ActiveSendReceiveClientLink activeSendReceiveClientLink;
 25        ActiveRequestResponseLink activeRequestResponseClientLink;
 26
 1227        public ActiveClientLinkManager(ClientEntity client, ICbsTokenProvider tokenProvider)
 28        {
 1229            this.clientId = client.ClientId;
 1230            this.retryPolicy = client.RetryPolicy ?? RetryPolicy.Default;
 1231            this.cbsTokenProvider = tokenProvider;
 1232            this.sendReceiveLinkCbsTokenRenewalTimer = new Timer(OnRenewSendReceiveCbsToken, this, Timeout.Infinite, Tim
 1233            this.requestResponseLinkCbsTokenRenewalTimer = new Timer(OnRenewRequestResponseCbsToken, this, Timeout.Infin
 1234        }
 35
 36        public void Close()
 37        {
 038            this.sendReceiveLinkCbsTokenRenewalTimer.Dispose();
 039            this.sendReceiveLinkCbsTokenRenewalTimer = null;
 040            this.requestResponseLinkCbsTokenRenewalTimer.Dispose();
 041            this.requestResponseLinkCbsTokenRenewalTimer = null;
 042        }
 43
 44        public void SetActiveSendReceiveLink(ActiveSendReceiveClientLink sendReceiveClientLink)
 45        {
 046            this.activeSendReceiveClientLink = sendReceiveClientLink;
 047            this.activeSendReceiveClientLink.Link.Closed += this.OnSendReceiveLinkClosed;
 048            if (this.activeSendReceiveClientLink.Link.State == AmqpObjectState.Opened)
 49            {
 050                this.SetRenewCbsTokenTimer(sendReceiveClientLink);
 51            }
 052        }
 53
 54        void OnSendReceiveLinkClosed(object sender, EventArgs e)
 55        {
 056            this.ChangeRenewTimer(this.activeSendReceiveClientLink, Timeout.InfiniteTimeSpan);
 057        }
 58
 59        public void SetActiveRequestResponseLink(ActiveRequestResponseLink requestResponseLink)
 60        {
 061            this.activeRequestResponseClientLink = requestResponseLink;
 062            this.activeRequestResponseClientLink.Link.Closed += this.OnRequestResponseLinkClosed;
 063            if (this.activeRequestResponseClientLink.Link.State == AmqpObjectState.Opened)
 64            {
 065                this.SetRenewCbsTokenTimer(requestResponseLink);
 66            }
 067        }
 68
 69        static async void OnRenewSendReceiveCbsToken(object state)
 70        {
 071            var activeClientLinkManager = (ActiveClientLinkManager)state;
 072            await activeClientLinkManager.RenewCbsTokenAsync(activeClientLinkManager.activeSendReceiveClientLink).Config
 073        }
 74
 75        static async void OnRenewRequestResponseCbsToken(object state)
 76        {
 077            var activeClientLinkManager = (ActiveClientLinkManager)state;
 078            await activeClientLinkManager.RenewCbsTokenAsync(activeClientLinkManager.activeRequestResponseClientLink).Co
 079        }
 80
 81        async Task RenewCbsTokenAsync(ActiveClientLinkObject activeClientLinkObject)
 82        {
 83            try
 84            {
 085                var cbsLink = activeClientLinkObject.Connection.Extensions.Find<AmqpCbsLink>() ?? new AmqpCbsLink(active
 086                DateTime cbsTokenExpiresAtUtc = DateTime.MaxValue;
 87
 088                foreach (var resource in activeClientLinkObject.Audience)
 89                {
 090                    MessagingEventSource.Log.AmqpSendAuthenticationTokenStart(activeClientLinkObject.EndpointUri, resour
 91
 092                    await this.retryPolicy.RunOperation(
 093                        async () =>
 094                        {
 095                            cbsTokenExpiresAtUtc = TimeoutHelper.Min(
 096                                cbsTokenExpiresAtUtc,
 097                                await cbsLink.SendTokenAsync(
 098                                    this.cbsTokenProvider,
 099                                    activeClientLinkObject.EndpointUri,
 0100                                    resource,
 0101                                    resource,
 0102                                    activeClientLinkObject.RequiredClaims,
 0103                                    ActiveClientLinkManager.SendTokenTimeout).ConfigureAwait(false));
 0104                        }, ActiveClientLinkManager.SendTokenTimeout).ConfigureAwait(false);
 105
 0106                    MessagingEventSource.Log.AmqpSendAuthenticationTokenStop();
 107                }
 108
 0109                activeClientLinkObject.AuthorizationValidUntilUtc = cbsTokenExpiresAtUtc;
 0110                this.SetRenewCbsTokenTimer(activeClientLinkObject);
 0111            }
 0112            catch (Exception e)
 113            {
 114                // failed to refresh token, no need to do anything since the server will shut the link itself
 0115                MessagingEventSource.Log.AmqpSendAuthenticationTokenException(this.clientId, e);
 116
 0117                this.ChangeRenewTimer(activeClientLinkObject, Timeout.InfiniteTimeSpan);
 0118            }
 0119        }
 120
 121        void OnRequestResponseLinkClosed(object sender, EventArgs e)
 122        {
 0123            this.ChangeRenewTimer(this.activeRequestResponseClientLink, Timeout.InfiniteTimeSpan);
 0124        }
 125
 126        void SetRenewCbsTokenTimer(ActiveClientLinkObject activeClientLinkObject)
 127        {
 0128            var utcNow = DateTime.UtcNow;
 0129            if (activeClientLinkObject.AuthorizationValidUntilUtc < utcNow)
 130            {
 0131                return;
 132            }
 133
 0134            var interval = activeClientLinkObject.AuthorizationValidUntilUtc.Subtract(utcNow) - ActiveClientLinkManager.
 0135            if (interval < ActiveClientLinkManager.TokenRefreshBuffer)
 0136                interval = TimeSpan.Zero;
 137
 0138            interval = TimeoutHelper.Min(interval, ActiveClientLinkManager.MaxTokenRefreshTime);
 139
 0140            this.ChangeRenewTimer(activeClientLinkObject, interval);
 0141        }
 142
 143        void ChangeRenewTimer(ActiveClientLinkObject activeClientLinkObject, TimeSpan dueTime)
 144        {
 0145            if (activeClientLinkObject is ActiveSendReceiveClientLink)
 146            {
 0147                this.sendReceiveLinkCbsTokenRenewalTimer?.Change(dueTime, Timeout.InfiniteTimeSpan);
 148            }
 149            else
 150            {
 0151                this.requestResponseLinkCbsTokenRenewalTimer?.Change(dueTime, Timeout.InfiniteTimeSpan);
 152            }
 0153        }
 154    }
 155}