Commit a99230af authored by Pâris MEULEMAN's avatar Pâris MEULEMAN Committed by Commit Bot

Fix flakiness in OAuth2TokenServiceTest.java

This fixes at least part of the "Flakiness": CL modifying the code
introduced a real bug in the test, as it should not fail automatically
within |onGetTokenFailure| and it should wait for the execution to
complete.

I ran the tests several times, both locally and on try bots without any failure.

Bug: 962976
Change-Id: Id4b88380cfdaaa31445c8ae0ed74956f54ea17d1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1611993
Commit-Queue: Pâris Meuleman <pmeuleman@chromium.org>
Reviewed-by: default avatarBoris Sazonov <bsazonov@chromium.org>
Cr-Commit-Position: refs/heads/master@{#662092}
parent a0c0018f
......@@ -15,7 +15,6 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.test.util.AdvancedMockContext;
import org.chromium.base.test.util.DisabledTest;
import org.chromium.base.test.util.Feature;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.components.signin.AccountManagerFacade;
......@@ -25,6 +24,7 @@ import org.chromium.components.signin.test.util.FakeAccountManagerDelegate;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
import java.util.Arrays;
import java.util.concurrent.CountDownLatch;
/** Tests for OAuth2TokenService. */
@RunWith(ChromeJUnit4ClassRunner.class)
......@@ -32,6 +32,40 @@ public class OAuth2TokenServiceTest {
private AdvancedMockContext mContext;
private FakeAccountManagerDelegate mAccountManager;
/**
* Class handling GetAccessToken callbacks and providing a blocking {@link
* getToken}.
*/
class GetAccessTokenCallbackForTest implements OAuth2TokenService.GetAccessTokenCallback {
private String mToken;
final CountDownLatch mTokenRetrievedCountDown = new CountDownLatch(1);
/**
* Blocks until the callback is called once and returns the token.
* See {@link CountDownLatch#await}
*/
public String getToken() {
try {
mTokenRetrievedCountDown.await();
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted or timed-out while waiting for updates", e);
}
return mToken;
}
@Override
public void onGetTokenSuccess(String token) {
mToken = token;
mTokenRetrievedCountDown.countDown();
}
@Override
public void onGetTokenFailure(boolean isTransientError) {
mToken = null;
mTokenRetrievedCountDown.countDown();
}
}
@Before
public void setUp() throws Exception {
mContext = new AdvancedMockContext(InstrumentationRegistry.getTargetContext());
......@@ -94,7 +128,6 @@ public class OAuth2TokenServiceTest {
@Test
@SmallTest
@Feature({"Sync"})
@DisabledTest(message = "https://crbug.com/962976")
public void testGetOAuth2AccessTokenWithTimeoutOnSuccess() {
String authToken = "someToken";
// Auth token should be successfully received.
......@@ -104,7 +137,6 @@ public class OAuth2TokenServiceTest {
@Test
@SmallTest
@Feature({"Sync"})
@DisabledTest(message = "https://crbug.com/962976")
public void testGetOAuth2AccessTokenWithTimeoutOnError() {
String authToken = null;
// Should not crash when auth token is null.
......@@ -112,30 +144,18 @@ public class OAuth2TokenServiceTest {
}
private void runTestOfGetOAuth2AccessTokenWithTimeout(String expectedToken) {
String scope = "http://example.com/scope";
String scope = "oauth2:http://example.com/scope";
Account account = AccountManagerFacade.createAccountFromName("test@gmail.com");
String oauth2Scope = "oauth2:" + scope;
// Add an account with given auth token for the given scope, already accepted auth popup.
AccountHolder accountHolder = AccountHolder.builder(account)
.hasBeenAccepted(oauth2Scope, true)
.authToken(oauth2Scope, expectedToken)
.hasBeenAccepted(scope, true)
.authToken(scope, expectedToken)
.build();
mAccountManager.addAccountHolderBlocking(accountHolder);
TestThreadUtils.runOnUiThreadBlocking(() -> {
OAuth2TokenService.getAccessToken(
account, scope, new OAuth2TokenService.GetAccessTokenCallback() {
@Override
public void onGetTokenSuccess(String token) {
Assert.assertEquals(expectedToken, token);
}
@Override
public void onGetTokenFailure(boolean isTransientError) {
Assert.fail();
}
});
});
GetAccessTokenCallbackForTest callback = new GetAccessTokenCallbackForTest();
TestThreadUtils.runOnUiThreadBlocking(
() -> { OAuth2TokenService.getAccessToken(account, scope, callback); });
Assert.assertEquals(expectedToken, callback.getToken());
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment