@@ -1164,61 +1164,50 @@ public void testFallbackIntegration_doesNotSwitchWhenThresholdNotMet() throws Ex
11641164 OpenTelemetrySdk openTelemetry =
11651165 OpenTelemetrySdk .builder ().setMeterProvider (meterProvider ).build ();
11661166
1167- SpannerOptions .useEnvironment (
1168- new SpannerOptions .SpannerEnvironment () {
1169- @ Override
1170- public boolean isEnableGcpFallback () {
1171- return true ;
1172- }
1173- });
1167+ SpannerOptions .Builder builder =
1168+ SpannerOptions .newBuilder ()
1169+ .setProjectId ("test-project" )
1170+ .setEnableDirectAccess (true )
1171+ .setHost ("http://localhost:1" ) // Closed port
1172+ .setCredentials (NoCredentials .getInstance ())
1173+ .setOpenTelemetry (openTelemetry );
1174+ // Make sure the ExecuteBatchDml RPC fails quickly to keep the test fast.
1175+ // Note that the timeout is actually not used. It is the fact that it does not retry that
1176+ // makes it fail fast.
1177+ builder
1178+ .getSpannerStubSettingsBuilder ()
1179+ .executeBatchDmlSettings ()
1180+ .setSimpleTimeoutNoRetriesDuration (Duration .ofSeconds (10 ));
1181+ // Setup Options with invalid host to force error
1182+ SpannerOptions options = builder .build ();
1183+
1184+ TestableGapicSpannerRpc rpc = new TestableGapicSpannerRpc (options );
11741185 try {
1175- SpannerOptions .Builder builder =
1176- SpannerOptions .newBuilder ()
1177- .setProjectId ("test-project" )
1178- .setEnableDirectAccess (true )
1179- .setHost ("http://localhost:1" ) // Closed port
1180- .setCredentials (NoCredentials .getInstance ())
1181- .setOpenTelemetry (openTelemetry );
1182- // Make sure the ExecuteBatchDml RPC fails quickly to keep the test fast.
1183- // Note that the timeout is actually not used. It is the fact that it does not retry that
1184- // makes it fail fast.
1185- builder
1186- .getSpannerStubSettingsBuilder ()
1187- .executeBatchDmlSettings ()
1188- .setSimpleTimeoutNoRetriesDuration (Duration .ofSeconds (10 ));
1189- // Setup Options with invalid host to force error
1190- SpannerOptions options = builder .build ();
1191-
1192- TestableGapicSpannerRpc rpc = new TestableGapicSpannerRpc (options );
1193- try {
1194- // Make a call that is expected to fail
1195- SpannerException exception =
1196- assertThrows (
1197- SpannerException .class ,
1198- () ->
1199- rpc .executeBatchDml (
1200- com .google .spanner .v1 .ExecuteBatchDmlRequest .newBuilder ()
1201- .setSession ("projects/p/instances/i/databases/d/sessions/s" )
1202- .build (),
1203- null ));
1204- assertEquals (ErrorCode .UNAVAILABLE , exception .getErrorCode ());
1205-
1206- // Wait briefly for the 10ms period to trigger the fallback check
1207- Thread .sleep (10 );
1208-
1209- // Verify Fallback via Metrics
1210- Collection <MetricData > metrics = metricReader .collectAllMetrics ();
1211- boolean fallbackOccurred =
1212- metrics .stream ()
1213- .anyMatch (md -> md .getName ().contains ("fallback_count" ) && hasValue (md ));
1214-
1215- assertFalse ("Fallback metric should not be present" , fallbackOccurred );
1216-
1217- } finally {
1218- rpc .shutdown ();
1219- }
1186+ // Make a call that is expected to fail
1187+ SpannerException exception =
1188+ assertThrows (
1189+ SpannerException .class ,
1190+ () ->
1191+ rpc .executeBatchDml (
1192+ com .google .spanner .v1 .ExecuteBatchDmlRequest .newBuilder ()
1193+ .setSession ("projects/p/instances/i/databases/d/sessions/s" )
1194+ .build (),
1195+ null ));
1196+ assertEquals (ErrorCode .UNAVAILABLE , exception .getErrorCode ());
1197+
1198+ // Wait briefly for the 10ms period to trigger the fallback check
1199+ Thread .sleep (10 );
1200+
1201+ // Verify Fallback via Metrics
1202+ Collection <MetricData > metrics = metricReader .collectAllMetrics ();
1203+ boolean fallbackOccurred =
1204+ metrics .stream ()
1205+ .anyMatch (md -> md .getName ().contains ("fallback_count" ) && hasValue (md ));
1206+
1207+ assertFalse ("Fallback metric should not be present" , fallbackOccurred );
1208+
12201209 } finally {
1221- SpannerOptions . useDefaultEnvironment ();
1210+ rpc . shutdown ();
12221211 }
12231212 }
12241213
@@ -1255,64 +1244,53 @@ public void testFallbackIntegration_switchesToFallbackOnFailure() throws Excepti
12551244 OpenTelemetrySdk openTelemetry =
12561245 OpenTelemetrySdk .builder ().setMeterProvider (meterProvider ).build ();
12571246
1258- SpannerOptions .useEnvironment (
1259- new SpannerOptions .SpannerEnvironment () {
1260- @ Override
1261- public boolean isEnableGcpFallback () {
1262- return true ;
1263- }
1264- });
1247+ SpannerOptions .Builder builder =
1248+ SpannerOptions .newBuilder ()
1249+ .setProjectId ("test-project" )
1250+ .setEnableDirectAccess (true )
1251+ .setHost ("http://localhost:1" ) // Closed port
1252+ .setCredentials (NoCredentials .getInstance ())
1253+ .setOpenTelemetry (openTelemetry );
1254+ // Make sure the ExecuteBatchDml RPC fails quickly to keep the test fast.
1255+ // Note that the timeout is actually not used. It is the fact that it does not retry that
1256+ // makes it fail fast.
1257+ builder
1258+ .getSpannerStubSettingsBuilder ()
1259+ .executeBatchDmlSettings ()
1260+ .setSimpleTimeoutNoRetriesDuration (Duration .ofSeconds (10 ));
1261+ // Setup Options with invalid host to force error
1262+ SpannerOptions options = builder .build ();
1263+
1264+ TestableGapicSpannerRpcWithLowerMinFailedCalls rpc =
1265+ new TestableGapicSpannerRpcWithLowerMinFailedCalls (options );
12651266 try {
1266- SpannerOptions .Builder builder =
1267- SpannerOptions .newBuilder ()
1268- .setProjectId ("test-project" )
1269- .setEnableDirectAccess (true )
1270- .setHost ("http://localhost:1" ) // Closed port
1271- .setCredentials (NoCredentials .getInstance ())
1272- .setOpenTelemetry (openTelemetry );
1273- // Make sure the ExecuteBatchDml RPC fails quickly to keep the test fast.
1274- // Note that the timeout is actually not used. It is the fact that it does not retry that
1275- // makes it fail fast.
1276- builder
1277- .getSpannerStubSettingsBuilder ()
1278- .executeBatchDmlSettings ()
1279- .setSimpleTimeoutNoRetriesDuration (Duration .ofSeconds (10 ));
1280- // Setup Options with invalid host to force error
1281- SpannerOptions options = builder .build ();
1282-
1283- TestableGapicSpannerRpcWithLowerMinFailedCalls rpc =
1284- new TestableGapicSpannerRpcWithLowerMinFailedCalls (options );
1285- try {
1286- // Make a call that is expected to fail
1287- SpannerException exception =
1288- assertThrows (
1289- SpannerException .class ,
1290- () ->
1291- rpc .executeBatchDml (
1292- com .google .spanner .v1 .ExecuteBatchDmlRequest .newBuilder ()
1293- .setSession ("projects/p/instances/i/databases/d/sessions/s" )
1294- .build (),
1295- null ));
1296- assertEquals (ErrorCode .UNAVAILABLE , exception .getErrorCode ());
1297-
1298- // Wait briefly for the 10ms period to trigger the fallback check
1299- Thread .sleep (10 );
1300-
1301- // Verify Fallback via Metrics
1302- Collection <MetricData > metrics = metricReader .collectAllMetrics ();
1303- boolean fallbackOccurred =
1304- metrics .stream ()
1305- .anyMatch (md -> md .getName ().contains ("fallback_count" ) && hasValue (md ));
1306-
1307- assertTrue (
1308- "Fallback metric should be present, indicating GcpFallbackChannel is active" ,
1309- fallbackOccurred );
1310-
1311- } finally {
1312- rpc .shutdown ();
1313- }
1267+ // Make a call that is expected to fail
1268+ SpannerException exception =
1269+ assertThrows (
1270+ SpannerException .class ,
1271+ () ->
1272+ rpc .executeBatchDml (
1273+ com .google .spanner .v1 .ExecuteBatchDmlRequest .newBuilder ()
1274+ .setSession ("projects/p/instances/i/databases/d/sessions/s" )
1275+ .build (),
1276+ null ));
1277+ assertEquals (ErrorCode .UNAVAILABLE , exception .getErrorCode ());
1278+
1279+ // Wait briefly for the 10ms period to trigger the fallback check
1280+ Thread .sleep (10 );
1281+
1282+ // Verify Fallback via Metrics
1283+ Collection <MetricData > metrics = metricReader .collectAllMetrics ();
1284+ boolean fallbackOccurred =
1285+ metrics .stream ()
1286+ .anyMatch (md -> md .getName ().contains ("fallback_count" ) && hasValue (md ));
1287+
1288+ assertTrue (
1289+ "Fallback metric should be present, indicating GcpFallbackChannel is active" ,
1290+ fallbackOccurred );
1291+
13141292 } finally {
1315- SpannerOptions . useDefaultEnvironment ();
1293+ rpc . shutdown ();
13161294 }
13171295 }
13181296
0 commit comments