예제 #1
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);
    }