Пример #1
0
        public async Task <CosmosAccountSettings> InitializeReaderAsync()
        {
            CosmosAccountSettings databaseAccount = await GlobalEndpointManager.GetDatabaseAccountFromAnyLocationsAsync(
                this.serviceEndpoint, this.connectionPolicy.PreferredLocations, this.GetDatabaseAccountAsync);

            return(databaseAccount);
        }
Пример #2
0
        public async Task ValidateCancellationTokenLogicForGetDatabaseAccountFromAnyLocationAsync()
        {
            Uri defaultEndpoint = new Uri("https://testfailover.documents-test.windows-int.net/");

            using CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
            cancellationTokenSource.Cancel();

            try
            {
                await GlobalEndpointManager.GetDatabaseAccountFromAnyLocationsAsync(
                    defaultEndpoint,
                    locations : new List <string>()
                {
                    "westus",
                    "southeastasia",
                    "northcentralus"
                },
                    getDatabaseAccountFn : (uri) => throw new Exception("The operation should be canceled and never make the network call."),
                    cancellationTokenSource.Token);

                Assert.Fail("Previous call should have failed");
            }
            catch (OperationCanceledException op)
            {
                Assert.IsTrue(op.Message.Contains("GlobalEndpointManager"));
            }
        }
Пример #3
0
        public async Task GetDatabaseAccountFromAnyLocationsMockNegativeTestAsync()
        {
            Uri defaultEndpoint = new Uri("https://testfailover.documents-test.windows-int.net/");

            int count = 0;

            try
            {
                await GlobalEndpointManager.GetDatabaseAccountFromAnyLocationsAsync(
                    defaultEndpoint : defaultEndpoint,
                    locations : new List <string>()
                {
                    "westus",
                    "southeastasia",
                    "northcentralus"
                },
                    getDatabaseAccountFn : (uri) =>
                {
                    count++;
                    if (uri == defaultEndpoint)
                    {
                        throw new Microsoft.Azure.Documents.UnauthorizedException("Mock failed exception");
                    }

                    throw new Exception("This should never be hit since it should stop after the global endpoint hit the nonretriable exception");
                });

                Assert.Fail("Should throw the UnauthorizedException");
            }
            catch (Microsoft.Azure.Documents.UnauthorizedException)
            {
                Assert.AreEqual(1, count, "Only request should be made");
            }

            int countDelayRequests = 0;

            count = 0;
            try
            {
                await GlobalEndpointManager.GetDatabaseAccountFromAnyLocationsAsync(
                    defaultEndpoint : defaultEndpoint,
                    locations : new List <string>()
                {
                    "westus",
                    "southeastasia",
                    "northcentralus"
                },
                    getDatabaseAccountFn : async(uri) =>
                {
                    count++;
                    if (uri == defaultEndpoint)
                    {
                        countDelayRequests++;
                        await Task.Delay(TimeSpan.FromMinutes(1));
                    }

                    throw new Microsoft.Azure.Documents.UnauthorizedException("Mock failed exception");
                });

                Assert.Fail("Should throw the UnauthorizedException");
            }
            catch (Microsoft.Azure.Documents.UnauthorizedException)
            {
                Assert.IsTrue(count <= 3, "Global endpoint is 1, 2 tasks going to regions parallel");
                Assert.AreEqual(2, count, "Only request should be made");
            }
        }
Пример #4
0
        public async Task GetDatabaseAccountFromAnyLocationsMockTestAsync()
        {
            AccountProperties databaseAccount = new AccountProperties
            {
                ReadLocationsInternal = new Collection <AccountRegion>()
                {
                    new AccountRegion
                    {
                        Name     = "Location1",
                        Endpoint = "https://testfailover-westus.documents-test.windows-int.net/"
                    },
                    new AccountRegion
                    {
                        Name     = "Location2",
                        Endpoint = "https://testfailover-southeastasia.documents-test.windows-int.net/"
                    },
                    new AccountRegion
                    {
                        Name     = "Location3",
                        Endpoint = "https://testfailover-northcentralus.documents-test.windows-int.net/"
                    },
                }
            };

            Uri defaultEndpoint = new Uri("https://testfailover.documents-test.windows-int.net/");

            GetAccountRequestInjector slowPrimaryRegionHelper = new GetAccountRequestInjector()
            {
                ShouldFailRequest  = (uri) => false,
                ShouldDelayRequest = (uri) => false,
                SuccessResponse    = databaseAccount,
            };

            // Happy path where global succeeds
            AccountProperties globalEndpointResult = await GlobalEndpointManager.GetDatabaseAccountFromAnyLocationsAsync(
                defaultEndpoint : defaultEndpoint,
                locations : new List <string>()
            {
                "westus",
                "southeastasia",
                "northcentralus"
            },
                getDatabaseAccountFn : (uri) => slowPrimaryRegionHelper.RequestHelper(uri));

            Assert.AreEqual(globalEndpointResult, databaseAccount);
            Assert.AreEqual(0, slowPrimaryRegionHelper.FailedEndpointCount);
            Assert.AreEqual(0, slowPrimaryRegionHelper.SlowEndpointCount);
            Assert.IsTrue(slowPrimaryRegionHelper.ReturnedSuccess);

            // global and primary slow and fail
            {
                slowPrimaryRegionHelper.Reset();
                slowPrimaryRegionHelper.ShouldDelayRequest = (uri) => uri == defaultEndpoint || uri == new Uri(databaseAccount.ReadLocationsInternal.First().Endpoint);
                slowPrimaryRegionHelper.ShouldFailRequest  = slowPrimaryRegionHelper.ShouldDelayRequest;

                Stopwatch stopwatch = Stopwatch.StartNew();
                globalEndpointResult = await GlobalEndpointManager.GetDatabaseAccountFromAnyLocationsAsync(
                    defaultEndpoint : defaultEndpoint,
                    locations : new List <string>()
                {
                    "westus",
                    "southeastasia",
                    "northcentralus"
                },
                    getDatabaseAccountFn : (uri) => slowPrimaryRegionHelper.RequestHelper(uri));

                stopwatch.Stop();

                Assert.AreEqual(globalEndpointResult, databaseAccount);
                Assert.AreEqual(2, slowPrimaryRegionHelper.SlowEndpointCount);
                Assert.IsTrue(slowPrimaryRegionHelper.ReturnedSuccess);
                Assert.IsTrue(stopwatch.Elapsed > TimeSpan.FromSeconds(5));
                Assert.IsTrue(stopwatch.Elapsed < TimeSpan.FromSeconds(10));
            }

            // All but the last URI succeeds
            {
                slowPrimaryRegionHelper.Reset();
                slowPrimaryRegionHelper.ShouldDelayRequest = (uri) => false;
                slowPrimaryRegionHelper.ShouldFailRequest  = (uri) => uri != new Uri(databaseAccount.ReadLocationsInternal.Last().Endpoint);

                globalEndpointResult = await GlobalEndpointManager.GetDatabaseAccountFromAnyLocationsAsync(
                    defaultEndpoint : defaultEndpoint,
                    locations : new List <string>()
                {
                    "westus",
                    "southeastasia",
                    "northcentralus"
                },
                    getDatabaseAccountFn : (uri) => slowPrimaryRegionHelper.RequestHelper(uri));

                Assert.AreEqual(globalEndpointResult, databaseAccount);
                Assert.AreEqual(3, slowPrimaryRegionHelper.FailedEndpointCount);
                Assert.IsTrue(slowPrimaryRegionHelper.ReturnedSuccess);
            }

            // All request but middle is delayed
            {
                slowPrimaryRegionHelper.Reset();
                slowPrimaryRegionHelper.ShouldDelayRequest = (uri) => uri != new Uri(databaseAccount.ReadLocationsInternal[1].Endpoint);
                slowPrimaryRegionHelper.ShouldFailRequest  = (uri) => false;

                globalEndpointResult = await GlobalEndpointManager.GetDatabaseAccountFromAnyLocationsAsync(
                    defaultEndpoint : defaultEndpoint,
                    locations : new List <string>()
                {
                    "westus",
                    "southeastasia",
                    "northcentralus"
                },
                    getDatabaseAccountFn : (uri) => slowPrimaryRegionHelper.RequestHelper(uri));

                Assert.AreEqual(globalEndpointResult, databaseAccount);
                Assert.AreEqual(0, slowPrimaryRegionHelper.FailedEndpointCount);
                Assert.IsTrue(slowPrimaryRegionHelper.ReturnedSuccess);
            }

            // Delay global and primary region, then only last region should succeed.
            {
                slowPrimaryRegionHelper.Reset();
                slowPrimaryRegionHelper.ShouldFailRequest  = (uri) => !uri.ToString().Contains("westus7");
                slowPrimaryRegionHelper.ShouldDelayRequest = (uri) => uri == defaultEndpoint || uri == new Uri(databaseAccount.ReadLocationsInternal.First().Endpoint);

                globalEndpointResult = await GlobalEndpointManager.GetDatabaseAccountFromAnyLocationsAsync(
                    defaultEndpoint : defaultEndpoint,
                    locations : new List <string>()
                {
                    "westus",
                    "westus2",
                    "westus3",
                    "westus4",
                    "westus5",
                    "westus6",
                    "westus7",
                },
                    getDatabaseAccountFn : (uri) => slowPrimaryRegionHelper.RequestHelper(uri));

                Assert.AreEqual(globalEndpointResult, databaseAccount);
                Assert.AreEqual(5, slowPrimaryRegionHelper.FailedEndpointCount);
                Assert.IsTrue(slowPrimaryRegionHelper.ReturnedSuccess);
            }
        }