From a217f15b9055b3a8303a881d7f0f9da9588adc87 Mon Sep 17 00:00:00 2001 From: Giulio Petek Date: Fri, 9 Sep 2016 10:41:05 +0200 Subject: [PATCH 1/2] Ensure that the single is cached --- .../OAuth2AccessTokenManagerSpecs.groovy | 42 ++++++++++++++++++- .../heimdall/OAuth2AccessTokenManager.java | 3 +- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/library/src/androidTest/groovy/de/rheinfabrik/heimdall/OAuth2AccessTokenManagerSpecs.groovy b/library/src/androidTest/groovy/de/rheinfabrik/heimdall/OAuth2AccessTokenManagerSpecs.groovy index 5e86dac..d9c1837 100644 --- a/library/src/androidTest/groovy/de/rheinfabrik/heimdall/OAuth2AccessTokenManagerSpecs.groovy +++ b/library/src/androidTest/groovy/de/rheinfabrik/heimdall/OAuth2AccessTokenManagerSpecs.groovy @@ -149,13 +149,51 @@ class OAuth2AccessTokenManagerGetValidAccessTokenSpecs extends AndroidSpecificat and: "A mock storage emitting that token" OAuth2AccessTokenStorage storage = Mock(OAuth2AccessTokenStorage) - storage.getStoredAccessToken() >> just(accessToken).delay(1, TimeUnit.SECONDS) + storage.getStoredAccessToken() >> just(accessToken) + + and: "An OAuth2AccessTokenManager with that storage" + OAuth2AccessTokenManager tokenManager = new OAuth2AccessTokenManager(storage) + + and: "A mock grant" + OAuth2RefreshAccessTokenGrant grant = Mock(OAuth2RefreshAccessTokenGrant) + def counter = 0 + grant.grantNewAccessToken() >> { it -> + return just(accessToken).doOnSuccess({ x -> counter++ }).delay(1, TimeUnit.SECONDS) + } + + when: "I ask for a valid access token" + tokenManager.getValidAccessToken(grant).subscribe() + + and: "I ask again" + tokenManager.getValidAccessToken(grant).subscribe() + + and: "I wait 2 seconds" + sleep(2000) + + then: "The refresh grant is asked for a new token ONCE" + counter == 1 + } + + def "it should clear the current request once done"() { + + given: "An expired OAuth2AccessToken" + OAuth2AccessToken accessToken = Mock(OAuth2AccessToken) + accessToken.refreshToken = "rt" + accessToken.isExpired() >> true + + and: "A mock storage emitting that token" + OAuth2AccessTokenStorage storage = Mock(OAuth2AccessTokenStorage) + storage.getStoredAccessToken() >> just(accessToken) and: "An OAuth2AccessTokenManager with that storage" OAuth2AccessTokenManager tokenManager = new OAuth2AccessTokenManager(storage) and: "A mock grant" OAuth2RefreshAccessTokenGrant grant = Mock(OAuth2RefreshAccessTokenGrant) + def counter = 0 + grant.grantNewAccessToken() >> { it -> + return just(accessToken).doOnSuccess({ x -> counter++ }).delay(1, TimeUnit.SECONDS) + } when: "I ask for a valid access token" tokenManager.getValidAccessToken(grant).subscribe() @@ -170,7 +208,7 @@ class OAuth2AccessTokenManagerGetValidAccessTokenSpecs extends AndroidSpecificat tokenManager.getValidAccessToken(grant).subscribe() then: "The refresh grant is asked for a new token TWICE" - 2 * grant.grantNewAccessToken() >> just(accessToken) >> just(accessToken) + counter == 2 } def "it should throw an IllegalArgumentException when the refreshAccessTokenGrant parameter is null"() { diff --git a/library/src/main/java/de/rheinfabrik/heimdall/OAuth2AccessTokenManager.java b/library/src/main/java/de/rheinfabrik/heimdall/OAuth2AccessTokenManager.java index 9e16867..b5c852e 100644 --- a/library/src/main/java/de/rheinfabrik/heimdall/OAuth2AccessTokenManager.java +++ b/library/src/main/java/de/rheinfabrik/heimdall/OAuth2AccessTokenManager.java @@ -4,7 +4,6 @@ import de.rheinfabrik.heimdall.grants.OAuth2Grant; import de.rheinfabrik.heimdall.grants.OAuth2RefreshAccessTokenGrant; -import rx.Observable; import rx.Single; import static rx.Single.error; @@ -85,7 +84,7 @@ public Single grantNewAccessToken(OAuth2Grant grant, mStorage.storeAccessToken(accessToken); mTokenSingle = null; - }); + }).toObservable().cache().toSingle(); } return mTokenSingle; From 57cc6e78053390c87b2eee34832baeea6e98f1db Mon Sep 17 00:00:00 2001 From: Giulio Petek Date: Fri, 9 Sep 2016 10:41:49 +0200 Subject: [PATCH 2/2] bump version name --- library/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/build.gradle b/library/build.gradle index cc2cce5..ed5d459 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -38,7 +38,7 @@ android { minSdkVersion 9 targetSdkVersion 23 versionCode 105 - versionName "1.0.5" + versionName "1.0.6" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" }