From 93a590a81fd379e6a2b6861e7e012f8b8adfb501 Mon Sep 17 00:00:00 2001 From: Abhijeet Mohanty Date: Sat, 1 Feb 2025 07:06:43 -0500 Subject: [PATCH] Fixing unit tests. --- .../routing/LocationCacheTest.java | 20 +++--- .../implementation/RxDocumentClientImpl.java | 6 +- .../implementation/routing/LocationCache.java | 66 ++++++++++--------- 3 files changed, 47 insertions(+), 45 deletions(-) diff --git a/sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/implementation/routing/LocationCacheTest.java b/sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/implementation/routing/LocationCacheTest.java index f54e0747fdbe5..f67f44dfd3523 100644 --- a/sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/implementation/routing/LocationCacheTest.java +++ b/sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/implementation/routing/LocationCacheTest.java @@ -480,9 +480,9 @@ public void validateAsync(boolean useMultipleWriteEndpoints, @Test(groups = "long") public void validateWriteEndpointOrderWithClientSideDisableMultipleWriteLocation() throws Exception { this.initialize(false, true, false); - assertThat(this.cache.getWriteEndpoints().get(0)).isEqualTo(LocationCacheTest.Location1Endpoint); - assertThat(this.cache.getWriteEndpoints().get(1)).isEqualTo(LocationCacheTest.Location2Endpoint); - assertThat(this.cache.getWriteEndpoints().get(2)).isEqualTo(LocationCacheTest.Location3Endpoint); + assertThat(this.cache.getWriteEndpoints().get(0)).isEqualTo(new LocationCache.ConsolidatedRegionalEndpoint(LocationCacheTest.Location1Endpoint, null)); + assertThat(this.cache.getWriteEndpoints().get(1)).isEqualTo(new LocationCache.ConsolidatedRegionalEndpoint(LocationCacheTest.Location2Endpoint, null)); + assertThat(this.cache.getWriteEndpoints().get(2)).isEqualTo(new LocationCache.ConsolidatedRegionalEndpoint(LocationCacheTest.Location3Endpoint, null)); } @Test(groups = "unit", dataProvider = "excludedRegionsTestConfigs") @@ -900,17 +900,17 @@ private void validateRequestEndpointResolution( // ALL write requests flip-flop between current write and alternate write endpoint UnmodifiableList writeEndpoints = this.cache.getWriteEndpoints(); - assertThat(firstAvailableWriteEndpoint).isEqualTo(writeEndpoints.get(0)); - assertThat(secondAvailableWriteEndpoint).isEqualTo(this.resolveEndpointForWriteRequest(ResourceType.Document, true)); - assertThat(firstAvailableWriteEndpoint).isEqualTo(this.resolveEndpointForWriteRequest(ResourceType.Document, false)); + assertThat(new LocationCache.ConsolidatedRegionalEndpoint(firstAvailableWriteEndpoint, null)).isEqualTo(writeEndpoints.get(0)); + assertThat(new LocationCache.ConsolidatedRegionalEndpoint(secondAvailableWriteEndpoint, null)).isEqualTo(this.resolveEndpointForWriteRequest(ResourceType.Document, true)); + assertThat(new LocationCache.ConsolidatedRegionalEndpoint(firstAvailableWriteEndpoint, null)).isEqualTo(this.resolveEndpointForWriteRequest(ResourceType.Document, false)); // Writes to other resource types should be directed to first/second write getEndpoint - assertThat(firstWriteEnpoint).isEqualTo(this.resolveEndpointForWriteRequest(ResourceType.Database, false)); - assertThat(secondWriteEnpoint).isEqualTo(this.resolveEndpointForWriteRequest(ResourceType.Database, true)); + assertThat(new LocationCache.ConsolidatedRegionalEndpoint(firstWriteEnpoint, null)).isEqualTo(this.resolveEndpointForWriteRequest(ResourceType.Database, false)); + assertThat(new LocationCache.ConsolidatedRegionalEndpoint(secondWriteEnpoint, null)).isEqualTo(this.resolveEndpointForWriteRequest(ResourceType.Database, true)); // Reads should be directed to available read endpoints regardless of resource type - assertThat(firstAvailableReadEndpoint).isEqualTo(this.resolveEndpointForReadRequest(true)); - assertThat(firstAvailableReadEndpoint).isEqualTo(this.resolveEndpointForReadRequest(false)); + assertThat(new LocationCache.ConsolidatedRegionalEndpoint(firstAvailableReadEndpoint, null)).isEqualTo(this.resolveEndpointForReadRequest(true)); + assertThat(new LocationCache.ConsolidatedRegionalEndpoint(firstAvailableReadEndpoint, null)).isEqualTo(this.resolveEndpointForReadRequest(false)); } private LocationCache.ConsolidatedRegionalEndpoint resolveEndpointForReadRequest(boolean masterResourceType) { diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/RxDocumentClientImpl.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/RxDocumentClientImpl.java index ca8061a30b65e..e737949c437d1 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/RxDocumentClientImpl.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/RxDocumentClientImpl.java @@ -491,10 +491,8 @@ private RxDocumentClientImpl(URI serviceEndpoint, this.connectionSharingAcrossClientsEnabled = connectionSharingAcrossClientsEnabled; this.configs = configs; this.masterKeyOrResourceToken = masterKeyOrResourceToken; - this.isThinClientEnabled = this.configs.isThinClientEnabled(); - // TODO: we have a default thinclient endpoint in configs, but it is not set to anything - need - // to figure out what default will be - this.serviceEndpoint = this.isThinClientEnabled ? this.configs.getThinclientEndpoint() : serviceEndpoint; + this.isThinClientEnabled = Configs.isThinClientEnabled(); + this.serviceEndpoint = serviceEndpoint; this.credential = credential; this.tokenCredential = tokenCredential; this.contentResponseOnWriteEnabled = contentResponseOnWriteEnabled; diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/routing/LocationCache.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/routing/LocationCache.java index 6dbeab86e8873..aa1ab61f28909 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/routing/LocationCache.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/routing/LocationCache.java @@ -686,50 +686,54 @@ private void addEndpoints( Map regionByEndpoint, List parsedLocations) { - for (DatabaseAccountLocation gatewayDbAccountLocation : gatewayDbAccountLocations) { - if (!Strings.isNullOrEmpty(gatewayDbAccountLocation.getName())) { - try { + if (gatewayDbAccountLocations != null) { + for (DatabaseAccountLocation gatewayDbAccountLocation : gatewayDbAccountLocations) { + if (!Strings.isNullOrEmpty(gatewayDbAccountLocation.getName())) { + try { - String location = gatewayDbAccountLocation.getName().toLowerCase(Locale.ROOT); - URI endpoint = new URI(gatewayDbAccountLocation.getEndpoint().toLowerCase(Locale.ROOT)); + String location = gatewayDbAccountLocation.getName().toLowerCase(Locale.ROOT); + URI endpoint = new URI(gatewayDbAccountLocation.getEndpoint().toLowerCase(Locale.ROOT)); - ConsolidatedRegionalEndpoint consolidatedRegionalEndpoint = new ConsolidatedRegionalEndpoint(endpoint, null); + ConsolidatedRegionalEndpoint consolidatedRegionalEndpoint = new ConsolidatedRegionalEndpoint(endpoint, null); - if (!endpointsByLocation.containsKey(location)) { - endpointsByLocation.put(location, consolidatedRegionalEndpoint); - } + if (!endpointsByLocation.containsKey(location)) { + endpointsByLocation.put(location, consolidatedRegionalEndpoint); + } - if (!regionByEndpoint.containsKey(consolidatedRegionalEndpoint)) { - regionByEndpoint.put(consolidatedRegionalEndpoint, location); - } + if (!regionByEndpoint.containsKey(consolidatedRegionalEndpoint)) { + regionByEndpoint.put(consolidatedRegionalEndpoint, location); + } - parsedLocations.add(gatewayDbAccountLocation.getName()); - } catch (Exception e) { - logger.warn("GetAvailableEndpointsByLocation() - skipping add for location = [{}] as its location name is either empty or endpoint is malformed [{}]", - gatewayDbAccountLocation.getName(), - gatewayDbAccountLocation.getEndpoint()); + parsedLocations.add(gatewayDbAccountLocation.getName()); + } catch (Exception e) { + logger.warn("GetAvailableEndpointsByLocation() - skipping add for location = [{}] as its location name is either empty or endpoint is malformed [{}]", + gatewayDbAccountLocation.getName(), + gatewayDbAccountLocation.getEndpoint()); + } } } } - for (DatabaseAccountLocation thinClientDbAccountLocation: thinClientDbAccountLocations) { - if (!Strings.isNullOrEmpty(thinClientDbAccountLocation.getName())) { - try { - String location = thinClientDbAccountLocation.getName().toLowerCase(Locale.ROOT); - URI endpoint = new URI(thinClientDbAccountLocation.getEndpoint().toLowerCase(Locale.ROOT)); + if (thinClientDbAccountLocations != null) { + for (DatabaseAccountLocation thinClientDbAccountLocation: thinClientDbAccountLocations) { + if (!Strings.isNullOrEmpty(thinClientDbAccountLocation.getName())) { + try { + String location = thinClientDbAccountLocation.getName().toLowerCase(Locale.ROOT); + URI endpoint = new URI(thinClientDbAccountLocation.getEndpoint().toLowerCase(Locale.ROOT)); - ConsolidatedRegionalEndpoint consolidatedRegionalEndpoint = endpointsByLocation.get(location); + ConsolidatedRegionalEndpoint consolidatedRegionalEndpoint = endpointsByLocation.get(location); - if (consolidatedRegionalEndpoint == null) { - throw new IllegalStateException(String.format("Gateway location endpoint doesn't exist while thin client location endpoint exists for location %s", location)); - } + if (consolidatedRegionalEndpoint == null) { + throw new IllegalStateException(String.format("Gateway location endpoint doesn't exist while thin client location endpoint exists for location %s", location)); + } - consolidatedRegionalEndpoint.thinClientLocationEndpoint = endpoint; + consolidatedRegionalEndpoint.thinClientLocationEndpoint = endpoint; - } catch (Exception e) { - logger.warn("GetAvailableEndpointsByLocation() - skipping add for location = [{}] as its location name is either empty or endpoint is malformed [{}]", - thinClientDbAccountLocation.getName(), - thinClientDbAccountLocation.getEndpoint()); + } catch (Exception e) { + logger.warn("GetAvailableEndpointsByLocation() - skipping add for location = [{}] as its location name is either empty or endpoint is malformed [{}]", + thinClientDbAccountLocation.getName(), + thinClientDbAccountLocation.getEndpoint()); + } } } }