Ejemplo n.º 1
0
    public async Task AccessTokenCacheManager_AddOrGet_NotEmptyCache_ShouldRenewExpiredItem()
    {
        var tokenResponse1 = await TokenResponseObjects.GetValidTokenResponseAsync("access_token_1", 1);

        var tokenResponse2 = await TokenResponseObjects.GetValidTokenResponseAsync("access_token_2", 1);

        var accessTokenCacheManager = new TokenResponseCacheManager(
            new MemoryCache(
                Options.Create(new MemoryCacheOptions())
                )
            );

        var cacheKey = "CacheKey_" + Guid.NewGuid();

        //should cache it and returne tokenResponse1
        var tokenResponseFromCache = await accessTokenCacheManager.AddOrGetExistingAsync(
            cacheKey,
            () => { return(Task.FromResult(tokenResponse1)); }
            );

        Assert.AreEqual(tokenResponse1, tokenResponseFromCache);

        //sleep for cache expiration
        Thread.Sleep(1000);

        //should cache it and return tokenResponse2
        tokenResponseFromCache = await accessTokenCacheManager.AddOrGetExistingAsync(
            cacheKey,
            () => { return(Task.FromResult(tokenResponse2)); }//now it is 2
            );

        Assert.AreEqual(tokenResponse2, tokenResponseFromCache);
    }
Ejemplo n.º 2
0
    public async Task AccessTokenCacheManager_AddOrGet_NotEmptyCache_RemoveItem_ShouldRenewCache()
    {
        var tokenResponse1 = await TokenResponseObjects.GetValidTokenResponseAsync("access_token_1", 5);

        var tokenResponse2 = await TokenResponseObjects.GetValidTokenResponseAsync("access_token_2", 5);

        var accessTokenCacheManager = new TokenResponseCacheManager(
            new MemoryCache(
                Options.Create(new MemoryCacheOptions())
                )
            );

        var cacheKey = "CacheKey_" + Guid.NewGuid();

        //should cache it and returne tokenResponse1
        var tokenResponseFromCache = await accessTokenCacheManager.AddOrGetExistingAsync(
            cacheKey,
            () => { return(Task.FromResult(tokenResponse1)); }
            );

        Assert.AreEqual(tokenResponse1, tokenResponseFromCache);

        //remove
        accessTokenCacheManager.Remove(cacheKey);

        //tokenResponse1 should be removed
        tokenResponseFromCache = await accessTokenCacheManager.AddOrGetExistingAsync(
            cacheKey,
            () => { return(Task.FromResult(tokenResponse2)); }//now it is tokenResponse2
            );

        Assert.AreEqual(tokenResponse2, tokenResponseFromCache);
    }
Ejemplo n.º 3
0
    public async Task AccessTokenCacheManager_AddOrGet_EmptyCache_ShouldRetrieveNew()
    {
        var tokenResponse = await TokenResponseObjects.GetValidTokenResponseAsync("access_token", 5);

        var accessTokenCacheManager = new TokenResponseCacheManager(
            new MemoryCache(
                Options.Create(new MemoryCacheOptions())
                )
            );

        var tokenResponseFromCache = await accessTokenCacheManager.AddOrGetExistingAsync(
            "CacheKey_" + Guid.NewGuid(),
            () => { return(Task.FromResult(tokenResponse)); }
            );

        Assert.AreEqual(tokenResponse, tokenResponseFromCache);
    }