Skip to content

Commit 2159229

Browse files
committed
test: fix flaky concurrent test in cab token generator
The refreshCredentialsIfRequired_asyncMultiThread test was failing because the mock clock was returning a fixed stale time, causing multiple threads to trigger redundant refreshes if they didn't overlap perfectly. This change modifies the mock clock to return a stale time on the first call (to trigger the async refresh) and a fresh time on subsequent calls (to skip redundant refreshes). This fix is isolated to only the ASYNC test case to avoid breaking BLOCKING tests.
1 parent acab557 commit 2159229

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

google-auth-library-java/cab-token-generator/javatests/com/google/auth/credentialaccessboundary/ClientSideCredentialAccessBoundaryFactoryTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,22 +614,30 @@ private Clock createMockClock(RefreshType refreshType, GoogleCredentials sourceC
614614
// Set mocked time so that the token is fresh and no refresh is needed (before the refresh
615615
// margin).
616616
mockedTimeInMillis = expirationTimeInMillis - refreshMarginInMillis - 60000;
617+
when(mockClock.currentTimeMillis()).thenReturn(mockedTimeInMillis);
617618
break;
618619
case ASYNC:
619620
// Set mocked time so that the token is nearing expiry and an async refresh is triggered
620621
// (within the refresh margin).
621622
mockedTimeInMillis = expirationTimeInMillis - refreshMarginInMillis + 60000;
623+
when(mockClock.currentTimeMillis())
624+
.thenReturn(
625+
mockedTimeInMillis, // First call: Stale (triggers the async refresh)
626+
currentTimeInMillis // Subsequent calls: Fresh (skips redundant refreshes)
627+
);
622628
break;
623629
case BLOCKING:
624630
// Set mocked time so that the token requires immediate refresh (just after the minimum
625631
// token lifetime).
626632
mockedTimeInMillis = expirationTimeInMillis - minimumTokenLifetimeMillis + 60000;
633+
when(mockClock.currentTimeMillis()).thenReturn(mockedTimeInMillis);
627634
break;
628635
default:
629636
throw new IllegalArgumentException("Unexpected RefreshType: " + refreshType);
630637
}
631638

632-
when(mockClock.currentTimeMillis()).thenReturn(mockedTimeInMillis);
639+
640+
633641
return mockClock;
634642
}
635643

0 commit comments

Comments
 (0)