Skip to content

Commit

Permalink
Merge pull request #41 from trivago/feature/really_fix_raice_conditions
Browse files Browse the repository at this point in the history
Feature/really fix raice conditions
  • Loading branch information
Gi-lo committed Sep 9, 2016
2 parents 6246ede + 57cc6e7 commit 47f8ae9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
2 changes: 1 addition & 1 deletion library/build.gradle
Expand Up @@ -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"
}

Expand Down
Expand Up @@ -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<OAuth2AccessToken>(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<OAuth2AccessToken>(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()
Expand All @@ -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"() {
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -85,7 +84,7 @@ public Single<TAccessToken> grantNewAccessToken(OAuth2Grant<TAccessToken> grant,
mStorage.storeAccessToken(accessToken);

mTokenSingle = null;
});
}).toObservable().cache().toSingle();
}

return mTokenSingle;
Expand Down

0 comments on commit 47f8ae9

Please sign in to comment.