@@ -12,33 +12,54 @@ internal static class GoogleTestGuard
1212
1313 public static void EnsureAvailable ( string apiKey )
1414 {
15- string ? skipReason ;
15+ if ( TryGetCachedSkipReason ( apiKey , out string ? skipReason ) )
16+ {
17+ if ( ! String . IsNullOrWhiteSpace ( skipReason ) )
18+ Assert . Skip ( skipReason ) ;
19+
20+ return ;
21+ }
22+
23+ string ? validatedSkipReason = ValidateCore ( apiKey ) ;
1624
1725 lock ( _sync )
1826 {
19- if ( _validated && String . Equals ( _validatedApiKey , apiKey , StringComparison . Ordinal ) )
27+ if ( ! _validated || ! String . Equals ( _validatedApiKey , apiKey , StringComparison . Ordinal ) )
2028 {
21- skipReason = _skipReason ;
22- }
23- else
24- {
25- skipReason = ValidateCore ( apiKey ) ;
2629 _validatedApiKey = apiKey ;
27- _skipReason = skipReason ;
30+ _skipReason = validatedSkipReason ;
2831 _validated = true ;
2932 }
33+
34+ skipReason = _skipReason ;
3035 }
3136
3237 if ( ! String . IsNullOrWhiteSpace ( skipReason ) )
3338 Assert . Skip ( skipReason ) ;
3439 }
3540
41+ private static bool TryGetCachedSkipReason ( string apiKey , out string ? skipReason )
42+ {
43+ lock ( _sync )
44+ {
45+ if ( _validated && String . Equals ( _validatedApiKey , apiKey , StringComparison . Ordinal ) )
46+ {
47+ skipReason = _skipReason ;
48+ return true ;
49+ }
50+ }
51+
52+ skipReason = null ;
53+ return false ;
54+ }
55+
3656 private static string ? ValidateCore ( string apiKey )
3757 {
3858 try
3959 {
60+ using var cts = new CancellationTokenSource ( TimeSpan . FromSeconds ( 20 ) ) ;
4061 var geocoder = new GoogleGeocoder ( apiKey ) ;
41- _ = geocoder . GeocodeAsync ( "1600 pennsylvania ave nw, washington dc" , CancellationToken . None )
62+ _ = geocoder . GeocodeAsync ( "1600 pennsylvania ave nw, washington dc" , cts . Token )
4263 . GetAwaiter ( )
4364 . GetResult ( )
4465 . FirstOrDefault ( ) ;
@@ -49,6 +70,10 @@ public static void EnsureAvailable(string apiKey)
4970 {
5071 return BuildSkipReason ( ex ) ;
5172 }
73+ catch ( OperationCanceledException )
74+ {
75+ return "Google integration test guard timed out while validating API key availability." ;
76+ }
5277 }
5378
5479 private static string BuildSkipReason ( GoogleGeocodingException ex )
0 commit comments