public void ContainsKey() { TimeSpan cacheLifeTime = TimeSpan.FromMinutes(5); MemoryCachingService service = new MemoryCachingService(cacheLifeTime); string cachableKey = this.cachableKey; Foobar cachable = GetCachableObject(); service.Cache <Foobar>(cachableKey, cachable); Assert.Single(service);; Assert.True(service.ContainsKey(cachableKey)); }
public void IsValidRequestTest() { MemoryCachingService cachingService = new MemoryCachingService(TimeSpan.FromMinutes(5)); IsValidRequestOverride service = GetService((l, a, c) => new IsValidRequestOverride(l, a, c), cachingService); const string badAppId = "badappId"; const string resource = "/test"; const string method = "GET"; const string content = "some content"; Tuple <bool, HmacIsValidRequestResult> result = service.CheckValidRequest(null, null, null, null, null, null, null); Assert.False(result.Item1); Assert.Equal(HmacIsValidRequestResult.NoValidResouce, result.Item2); result = service.CheckValidRequest(null, resource, null, badAppId, null, null, null); Assert.False(result.Item1); Assert.Equal(HmacIsValidRequestResult.UnableToFindAppId, result.Item2); result = service.CheckValidRequest(null, resource, null, _appId, null, null, null); Assert.False(result.Item1); Assert.Equal(HmacIsValidRequestResult.ReplayRequest, result.Item2); result = service.CheckValidRequest(null, resource, null, _appId, null, "a nonce", null); Assert.False(result.Item1); Assert.Equal(HmacIsValidRequestResult.ReplayRequest, result.Item2); ulong badCurrentTime = DateTime.UtcNow.AddMinutes(-30).UnixTimeStamp(); result = service.CheckValidRequest(null, resource, null, _appId, null, "a nonce", badCurrentTime.ToString()); Assert.False(result.Item1); Assert.Equal(HmacIsValidRequestResult.ReplayRequest, result.Item2); ulong goodCurrentTime = DateTime.UtcNow.UnixTimeStamp(); cachingService.Cache("a nonce", "a nonce"); result = service.CheckValidRequest(null, resource, null, _appId, null, "a nonce", goodCurrentTime.ToString()); Assert.False(result.Item1); Assert.Equal(HmacIsValidRequestResult.ReplayRequest, result.Item2); HmacSignatureGenerator signatureGenerator = new HmacSignatureGenerator(CustomHeaderScheme); string fullSignature = signatureGenerator.GenerateFullHmacSignature(resource, method, _appId, _secretKey, content); string[] signatureParts = service.GetHeaderValues(fullSignature.Split(" ")[1]); result = service.CheckValidRequest(content.ToStream(), resource, method, signatureParts[0], signatureParts[1], signatureParts[2], signatureParts[3]); Assert.True(result.Item1); Assert.Equal(HmacIsValidRequestResult.NoError, result.Item2); fullSignature = signatureGenerator.GenerateFullHmacSignature(resource, method, _appId, _secretKey, content); signatureParts = service.GetHeaderValues(fullSignature.Split(" ")[1]); result = service.CheckValidRequest(content.ToStream(), resource, method, signatureParts[0], _secretKey, signatureParts[2], signatureParts[3]); Assert.False(result.Item1); Assert.Equal(HmacIsValidRequestResult.SignaturesMismatch, result.Item2); }
public void DoubleCache() { TimeSpan cacheLifeTime = TimeSpan.FromMinutes(5); MemoryCachingService service = new MemoryCachingService(cacheLifeTime); Foobar cachable = GetCachableObject(); string cachableKey = this.cachableKey; service.Cache(cachableKey, cachable); ICachedObject <Foobar> existsRetrievedWrapper = service.Retrieve <Foobar>(cachableKey); Foobar cachable2 = GetCachableObject(); service.Cache(cachableKey, cachable2); ICachedObject <Foobar> retrievedWrapper = service.Retrieve <Foobar>(cachableKey); Assert.NotNull(retrievedWrapper); Assert.Equal(cachable2, retrievedWrapper.Value); Assert.NotEqual(existsRetrievedWrapper.Value, retrievedWrapper.Value); Assert.Single(service); }
public void Count() { TimeSpan cacheLifeTime = TimeSpan.FromMinutes(5); MemoryCachingService service = new MemoryCachingService(cacheLifeTime); Assert.Empty(service); Foobar cachable = GetCachableObject(); string cachableKey = this.cachableKey; service.Cache(cachableKey, cachable); Assert.Single(service); }
public void Clear() { TimeSpan cacheLifeTime = TimeSpan.FromMinutes(5); MemoryCachingService service = new MemoryCachingService(cacheLifeTime); string cachableKey = this.cachableKey; Foobar cachable = GetCachableObject(); service.Cache <Foobar>(cachableKey, cachable); Assert.Single(service);; Assert.Equal(cachable, service.Retrieve <Foobar>(cachableKey).Value); service.Clear(); Assert.Empty(service); }
public void Values() { TimeSpan cacheLifeTime = TimeSpan.FromMinutes(5); MemoryCachingService service = new MemoryCachingService(cacheLifeTime); Assert.Empty(service.Values); Foobar cachable = GetCachableObject(); string cachableKey = this.cachableKey; service.Cache(cachableKey, cachable); Assert.Single(service.Values); Assert.NotNull(service.Values.Single()); Assert.Equal(cachable, service.Values.Single().UntypedValue); }
public void TryGetValue() { TimeSpan cacheLifeTime = TimeSpan.FromMinutes(5); MemoryCachingService service = new MemoryCachingService(cacheLifeTime); string cachableKey = this.cachableKey; ICachedObjectBasic result; Assert.False(service.TryGetValue(cachableKey, out result)); Foobar cachable = GetCachableObject(); service.Cache(cachableKey, cachable); bool success = service.TryGetValue(cachableKey, out result); Assert.True(success); Assert.Equal(cachable, result.UntypedValue); }
public void Caching() { TimeSpan cacheLifeTime = TimeSpan.FromMinutes(5); MemoryCachingService service = new MemoryCachingService(cacheLifeTime); Foobar cachable = GetCachableObject(); string cachableKey = this.cachableKey; service.Cache(cachableKey, cachable); Assert.Single(service); Assert.Equal(cacheLifeTime, service.DefaultCacheLifespan); Assert.False(service.IsReadOnly); Assert.Single(service.Keys); Assert.Equal(cachableKey, service.Keys.Single()); Assert.Single(service.Values); Assert.Equal(cachable, service.Values.Single().UntypedValue); }
public void AutomatedExpiration() { TimeSpan cacheLifeTime = TimeSpan.FromMilliseconds(5); MemoryCachingService service = new MemoryCachingService(cacheLifeTime); Foobar cachable = GetCachableObject(); string cachableKey = this.cachableKey; DateTime startedCaching = DateTime.UtcNow; service.Cache(cachableKey, cachable); DateTime endedCaching = DateTime.UtcNow; ICachedObject <Foobar> existsRetrievedWrapper = service.Retrieve <Foobar>(cachableKey); Thread.Sleep(6); ICachedObject <Foobar> retrievedWrapper = service.Retrieve <Foobar>(cachableKey); Assert.NotNull(existsRetrievedWrapper); Assert.Null(retrievedWrapper); }
public void CopyTo() { TimeSpan cacheLifeTime = TimeSpan.FromMinutes(5); MemoryCachingService service = new MemoryCachingService(cacheLifeTime); Foobar cachable = GetCachableObject(); string cachableKey = this.cachableKey; service.Cache(cachableKey, cachable); ICachedObject <Foobar> existsRetrievedWrapper = service.Retrieve <Foobar>(cachableKey); KeyValuePair <string, ICachedObjectBasic>[] cache = new KeyValuePair <string, ICachedObjectBasic> [1]; service.CopyTo(cache, 0); Assert.NotNull(cache); Assert.NotEmpty(cache); Assert.Single(cache); Assert.Equal(existsRetrievedWrapper.Value, cache[0].Value.UntypedValue); Assert.Equal(cachableKey, cache[0].Key); }
public void ManualExpiration() { TimeSpan cacheLifeTime = TimeSpan.FromMinutes(5); MemoryCachingService service = new MemoryCachingService(cacheLifeTime); Foobar cachable = GetCachableObject(); string cachableKey = this.cachableKey; DateTime startedCaching = DateTime.UtcNow; service.Cache(cachableKey, cachable); DateTime endedCaching = DateTime.UtcNow; ICachedObject <Foobar> existsRetrievedWrapper = service.Retrieve <Foobar>(cachableKey); service.Invalidate(cachableKey); ICachedObject <Foobar> retrievedWrapper = service.Retrieve <Foobar>(cachableKey); Assert.Empty(service); Assert.NotNull(existsRetrievedWrapper); Assert.Null(retrievedWrapper); }
public void Retrieval() { TimeSpan cacheLifeTime = TimeSpan.FromMinutes(5); MemoryCachingService service = new MemoryCachingService(cacheLifeTime); Foobar cachable = GetCachableObject(); string cachableKey = this.cachableKey; DateTime startedCaching = DateTime.UtcNow; service.Cache(cachableKey, cachable); DateTime endedCaching = DateTime.UtcNow; ICachedObject <Foobar> retrievedWrapper = service.Retrieve <Foobar>(cachableKey); Assert.NotNull(retrievedWrapper); Assert.Equal(cachable, retrievedWrapper.Value); Assert.True(retrievedWrapper.CachedTime > startedCaching && retrievedWrapper.CachedTime < endedCaching); Assert.True(retrievedWrapper.ExpireTime > startedCaching.Add(cacheLifeTime) && retrievedWrapper.ExpireTime < endedCaching.Add(cacheLifeTime)); }
public void StaticUsage() { TimeSpan cacheLifeTime = TimeSpan.FromMilliseconds(5); MemoryCachingService service = new MemoryCachingService(cacheLifeTime, true); Foobar cachable = GetCachableObject(); string cachableKey = this.cachableKey; DateTime startedCaching = DateTime.UtcNow; service.Cache(cachableKey, cachable); DateTime endedCaching = DateTime.UtcNow; ICachedObject <Foobar> existsRetrievedWrapper = service.Retrieve <Foobar>(cachableKey); MemoryCachingService service2 = new MemoryCachingService(cacheLifeTime, true); ICachedObject <Foobar> existsRetrievedWrapper2 = service2.Retrieve <Foobar>(cachableKey); Assert.Equal(cachable, existsRetrievedWrapper.Value); Assert.Equal(existsRetrievedWrapper.Value, existsRetrievedWrapper2.Value); service2.Clear(); Assert.Null(service.Retrieve <Foobar>(cachableKey)); }
public void TestEnumerators() { TimeSpan cacheLifeTime = TimeSpan.FromMinutes(5); MemoryCachingService service = new MemoryCachingService(cacheLifeTime); Foobar cachable = GetCachableObject(); string cachableKey = this.cachableKey; service.Cache(cachableKey, cachable); IEnumerator <KeyValuePair <string, ICachedObjectBasic> > typedEnumerator = service.GetEnumerator(); IEnumerator enumerator = ((IEnumerable)service).GetEnumerator(); typedEnumerator.MoveNext(); enumerator.MoveNext(); Assert.Equal(cachable, (Foobar)((ICachedObjectBasic)(typedEnumerator.Current).Value).UntypedValue); Assert.Equal(cachable, (Foobar)((ICachedObjectBasic)((KeyValuePair <string, ICachedObjectBasic>) (enumerator.Current)).Value).UntypedValue); }
public void IsReplayRequestTest() { MemoryCachingService cachingService = new MemoryCachingService(TimeSpan.FromMinutes(5)); IsReplayRequestOverride service = GetService((l, a, c) => new IsReplayRequestOverride(l, a, c), cachingService); const string Key = "alreadyAdded"; const string GoodKey = "notAdded"; const string Value = "its here"; cachingService.Cache(Key, Value); Assert.True(service.CheckReplayRequest(Key, null)); ulong badCurrentTime = DateTime.UtcNow.AddMinutes(-30).UnixTimeStamp(); Assert.True(service.CheckReplayRequest(GoodKey, badCurrentTime.ToString())); ulong goodCurrentTime = DateTime.UtcNow.UnixTimeStamp(); Assert.False(service.CheckReplayRequest(GoodKey, goodCurrentTime.ToString())); Assert.Equal(2, cachingService.Count); Assert.Contains(GoodKey, cachingService.Keys); Assert.Equal(goodCurrentTime.FromUnixTimeStamp(), cachingService[GoodKey].CachedTime); Assert.Equal(GoodKey, cachingService[GoodKey].UntypedValue); }