public async Task WhenCannotAcquireToken_ThrowsInvalidOperationException()
            {
                // Arrange.
                var tokenCache = new MockAccessTokenCache(throwOnAcquireToken: true);

                // Act.
                await Assert.ThrowsAsync <InvalidOperationException>(async() => {
                    await tokenCache.GetAsync(
                        MockConnectionString,
                        MockAccessTokenCache.DefaultCertificateData);
                });

                // Assert.
                Assert.Equal(1, tokenCache.AcquireTokenCount);
            }
            public async Task WhenNoTokenInCache_AcquiresNew()
            {
                // Arrange.
                var token1     = MockAccessTokenCache.CreateValidAccessToken();
                var tokenCache = new MockAccessTokenCache(mockTokens: token1);

                // Act.
                var result = await tokenCache.GetAsync(
                    MockConnectionString,
                    MockAccessTokenCache.DefaultCertificateData);

                // Assert.
                Assert.Equal(1, tokenCache.AcquireTokenCount);
                Assert.Equal("valid", result.AccessToken);
            }
            public async Task WhenExpiredTokenInCache_AcquiresNew()
            {
                // Arrange.
                var token0     = MockAccessTokenCache.CreateMockAccessToken("expired", DateTimeOffset.Now);
                var token1     = MockAccessTokenCache.CreateValidAccessToken();
                var tokenCache = new MockAccessTokenCache(initialValue: token0, mockTokens: token1);

                // Act.
                var result = await tokenCache.GetAsync(
                    MockConnectionString,
                    MockAccessTokenCache.DefaultCertificateData);

                // Assert.
                Assert.Equal(1, tokenCache.AcquireTokenCount);
                Assert.Equal("valid", result.AccessToken);
            }
            public async Task WhenValidTokenInCache_ReturnsExisting()
            {
                // Arrange.
                var token0     = MockAccessTokenCache.CreateValidAccessToken();
                var token1     = token0;
                var tokenCache = new MockAccessTokenCache(initialValue: token0, mockTokens: token1);

                // Act.
                var result = await tokenCache.GetAsync(
                    MockConnectionString,
                    MockAccessTokenCache.DefaultCertificateData);

                // Assert.
                Assert.Equal(0, tokenCache.AcquireTokenCount);
                Assert.Equal("valid", result.AccessToken);
            }
            public async Task WhenNearExpiredTokenInCache_AcquiresNewInBackground()
            {
                // Arrange.
                var token0     = MockAccessTokenCache.CreateMockAccessToken("nearExpired", DateTimeOffset.Now + TimeSpan.FromMinutes(10));
                var token1     = MockAccessTokenCache.CreateValidAccessToken();
                var tokenCache = new MockAccessTokenCache(initialValue: token0, mockTokens: token1);

                // Act.
                var result = await tokenCache.GetAsync(
                    MockConnectionString,
                    MockAccessTokenCache.DefaultCertificateData);

                // Assert.
                Assert.Equal("nearExpired", result.AccessToken);

                await Task.Delay(500);

                Assert.Equal(1, tokenCache.AcquireTokenCount);
            }