public void LogMessage() { MemoryCachingService cachingService = new MemoryCachingService(TimeSpan.FromMinutes(5)); Json serializationService = new Json(); CacheLoggingService loggingService = new CacheLoggingService(cachingService, serializationService, _partSeperator); const string title = "Test Log"; const string message = "A test log"; DateTime startLogging = DateTime.UtcNow; loggingService.LogMessage(title, message, LogLevel.Debug); DateTime endLogging = DateTime.UtcNow; Assert.Single(cachingService); string fullLogName = cachingService.EnumerateDictionary().Single().Key; Assert.NotNull(fullLogName); Log <object> log = (Log <object>)cachingService.EnumerateDictionary().Single().Value.UntypedValue; Assert.NotNull(log); Assert.Null(log.Target); Assert.True(log.TimeStamp >= startLogging.AddMilliseconds(-5) && log.TimeStamp <= endLogging.AddMilliseconds(5)); Assert.Equal(title, log.Title); Assert.Equal(message, log.Message); Assert.Equal(LogLevel.Debug, log.LogLevel); Assert.Null(log.Exception); Assert.Equal("Message Log", log.Description); }
private CacheLoggingService GetLogsService() { Json serializationService = new Json(); MemoryCachingService cachingService = new MemoryCachingService(TimeSpan.FromMinutes(5)); CacheLoggingService loggingService = new CacheLoggingService(cachingService, serializationService); return(loggingService); }
public void LogExceptionWithObject() { MemoryCachingService cachingService = new MemoryCachingService(TimeSpan.FromMinutes(5)); Json serializationService = new Json(); CacheLoggingService loggingService = new CacheLoggingService(cachingService, serializationService, _partSeperator); const string title = "Test Log"; const string message = "A test log"; Foobar original = new Foobar { Foo = 4, Bar = 6 }; InvalidOperationException exception; try { throw new InvalidOperationException(title); } catch (InvalidOperationException ex) { exception = ex; } DateTime startLogging = DateTime.UtcNow; loggingService.LogExceptionWithObject(exception, original, message); DateTime endLogging = DateTime.UtcNow; Assert.Single(cachingService); string fullLogName = cachingService.EnumerateDictionary().Single().Key; Assert.NotNull(fullLogName); Log <Foobar> log = (Log <Foobar>)cachingService.EnumerateDictionary().Single().Value.UntypedValue; string description = "Exception Log - " + title; Assert.NotNull(log); Assert.True(log.TimeStamp >= startLogging.AddMilliseconds(-5) && log.TimeStamp <= endLogging.AddMilliseconds(5)); Assert.Equal(title, log.Title); Assert.Equal(message, log.Message); Assert.Equal(LogLevel.Error, log.LogLevel); Assert.Equal(description, log.Description); Assert.NotNull(log.Target); Assert.Equal(original.Foo, log.Target.Foo); // The memory caching service never serializes or deserializes so the non-serialized values won't change // Assert.Equal(0, log.Target.Bar); Assert.NotNull(log.Exception); Assert.Equal(typeof(SerializableException), log.Exception.GetType()); Assert.Equal(title, log.Exception.Message); }
private static Mock <ConfigurationCacheBase> GenerateCache(bool useStrict = true) { Json jsonSerializer = new Json(); MemoryCachingService cachingService = new MemoryCachingService(TimeSpan.FromDays(1), false); Mock <ConfigurationCacheBase> cacheProxy = new Mock <ConfigurationCacheBase>(useStrict ? MockBehavior.Strict : MockBehavior.Loose, cachingService, jsonSerializer, TimeSpan.FromDays(1)); return(cacheProxy); }
private static DefaultConfigurationService GenerateService() { Json jsonSerializer = new Json(); MemoryCachingService cachingService = new MemoryCachingService(TimeSpan.FromDays(1), false); DefaultConfigurationCache cache = new DefaultConfigurationCache(cachingService, jsonSerializer, TimeSpan.FromDays(1)); DefaultConfigurationService service = new DefaultConfigurationService(cache); return(service); }
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)); }
private static T GetService <T>(Func <ILoggingService, IApiKeyService, ICachingService, T> constructor, MemoryCachingService cachingService = null) where T : AuthenticationService { MemoryCachingService memoryCachingService = cachingService ?? new MemoryCachingService(TimeSpan.FromMinutes(5)); Json serializationService = new Json(); CacheLoggingService loggingService = new CacheLoggingService(memoryCachingService, serializationService); BasicApiKeyService apiKeyService = new BasicApiKeyService(GetBackingKeys()); T service = constructor(loggingService, apiKeyService, memoryCachingService); return(service); }
public void BasicVerification() { TimeSpan cacheLifeTime = TimeSpan.FromMinutes(5); MemoryCachingService service = new MemoryCachingService(cacheLifeTime); Assert.Empty(service); Assert.Equal(cacheLifeTime, service.DefaultCacheLifespan); Assert.False(service.IsReadOnly); Assert.Empty(service.Keys); Assert.Empty(service.Values); }
public void Contains() { TimeSpan cacheLifeTime = TimeSpan.FromMinutes(5); MemoryCachingService service = new MemoryCachingService(cacheLifeTime); Foobar cachable = GetCachableObject(); string cachableKey = this.cachableKey; KeyValuePair <string, ICachedObjectBasic> item = GetCachableKvp(DateTime.UtcNow, cacheLifeTime, cachable, cachableKey); Assert.DoesNotContain(item, service); service.Add(item); Assert.Contains(item, service); }
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 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 AddKvp() { TimeSpan cacheLifeTime = TimeSpan.FromMinutes(5); MemoryCachingService service = new MemoryCachingService(cacheLifeTime); Foobar cachable = GetCachableObject(); string cachableKey = this.cachableKey; KeyValuePair <string, ICachedObjectBasic> item = GetCachableKvp(DateTime.UtcNow, cacheLifeTime, cachable, cachableKey); service.Add(item); ICachedObject <Foobar> result = service.Retrieve <Foobar>(cachableKey); Assert.Equal(cachable, result.Value); }
private MemoryCachingService InitMemoryCachingService() { var services = new ServiceCollection() .AddMemoryCache() .BuildServiceProvider(); var memoryCachingService = new MemoryCachingService( services.GetRequiredService <IMemoryCache>() ); return(memoryCachingService); }
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 Add() { TimeSpan cacheLifeTime = TimeSpan.FromMinutes(5); MemoryCachingService service = new MemoryCachingService(cacheLifeTime); string cachableKey = this.cachableKey; Foobar cachable = GetCachableObject(); DefaultCachedObject <object> dto = new DefaultCachedObject <object> { Value = cachable, CachedTime = DateTime.UtcNow, ExpireTime = DateTime.UtcNow.Add(cacheLifeTime) }; service.Add(cachableKey, dto); Assert.Single(service);; Assert.Equal(dto.Value, service.Retrieve <Foobar>(cachableKey).Value); }
public void LogException() { MemoryCachingService cachingService = new MemoryCachingService(TimeSpan.FromMinutes(5)); Json serializationService = new Json(); CacheLoggingService loggingService = new CacheLoggingService(cachingService, serializationService, _partSeperator); const string title = "Test Log"; const string message = "A test log"; InvalidOperationException exception; try { throw new InvalidOperationException(title); } catch (InvalidOperationException ex) { exception = ex; } DateTime startLogging = DateTime.UtcNow; loggingService.LogException(exception, message); DateTime endLogging = DateTime.UtcNow; Assert.Single(cachingService); string fullLogName = cachingService.EnumerateDictionary().Single().Key; Assert.NotNull(fullLogName); Log <object> log = (Log <object>)cachingService.EnumerateDictionary().Single().Value.UntypedValue; string description = "Exception Log - " + title; Assert.NotNull(log); Assert.Null(log.Target); Assert.True(log.TimeStamp >= startLogging.AddMilliseconds(-5) && log.TimeStamp <= endLogging.AddMilliseconds(5)); Assert.Equal(title, log.Title); Assert.Equal(message, log.Message); Assert.Equal(LogLevel.Error, log.LogLevel); Assert.Equal(description, log.Description); Assert.NotNull(log.Exception); Assert.Equal(typeof(SerializableException), log.Exception.GetType()); Assert.Equal(title, log.Exception.Message); }
public void DoubleRetrieve() { 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); ICachedObject <Foobar> retrievedWrapper = service.Retrieve <Foobar>(cachableKey); Assert.NotNull(existsRetrievedWrapper); Assert.NotNull(retrievedWrapper); Assert.Equal(cachable, existsRetrievedWrapper.Value); Assert.Equal(retrievedWrapper.Value, existsRetrievedWrapper.Value); Assert.Single(service); }
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 RemoveBranching() { TimeSpan cacheLifeTime = TimeSpan.FromMinutes(5); MemoryCachingService service = new MemoryCachingService(cacheLifeTime); Foobar cachable = GetCachableObject(); string cachableKey = this.cachableKey; KeyValuePair <string, ICachedObjectBasic> originalItem = GetCachableKvp(DateTime.UtcNow, cacheLifeTime, cachable, cachableKey); KeyValuePair <string, ICachedObjectBasic> item; DateTime originalTime = originalItem.Value.CachedTime; Assert.False(service.Remove(originalItem));; service.Clear(); Assert.Empty(service); item = GetCachableKvp(originalTime, cacheLifeTime, null, cachableKey); service.Add(item); Assert.False(service.Remove(originalItem)); service.Clear(); Assert.Empty(service); item = GetCachableKvp(originalTime, TimeSpan.FromMinutes(-5), cachable, cachableKey); service.Add(item); Assert.False(service.Remove(originalItem)); Assert.Empty(service); service.Clear(); Assert.Empty(service); item = GetCachableKvp(originalTime.AddMilliseconds(10), cacheLifeTime, cachable, cachableKey); service.Add(item); Assert.False(service.Remove(originalItem)); service.Clear(); Assert.Empty(service); item = GetCachableKvp(originalTime, TimeSpan.FromMinutes(4), cachable, cachableKey); service.Add(item); Assert.False(service.Remove(originalItem)); service.Clear(); Assert.Empty(service); item = GetCachableKvp(originalTime, cacheLifeTime, GetCachableObject(), cachableKey); service.Add(item); Assert.False(service.Remove(originalItem)); }
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 LogMessageWithObjectWithoutMessage() { MemoryCachingService cachingService = new MemoryCachingService(TimeSpan.FromMinutes(5)); Json serializationService = new Json(); CacheLoggingService loggingService = new CacheLoggingService(cachingService, serializationService, _partSeperator); const string title = "Test Log"; Foobar original = new Foobar { Foo = 4, Bar = 6 }; DateTime startLogging = DateTime.UtcNow; loggingService.LogMessage(title, original, LogLevel.Debug); DateTime endLogging = DateTime.UtcNow; Assert.Single(cachingService); string fullLogName = cachingService.EnumerateDictionary().Single().Key; Assert.NotNull(fullLogName); Log <Foobar> log = (Log <Foobar>)cachingService.EnumerateDictionary().Single().Value.UntypedValue; string description = "Message Log with object - " + typeof(Foobar).FullName; Assert.NotNull(log); Assert.True(log.TimeStamp >= startLogging.AddMilliseconds(-5) && log.TimeStamp <= endLogging.AddMilliseconds(5)); Assert.Equal(title, log.Title); Assert.Equal(description, log.Message); Assert.Equal(LogLevel.Debug, log.LogLevel); Assert.Null(log.Exception); Assert.Equal(description, log.Description); Assert.NotNull(log.Target); Assert.Equal(original.Foo, log.Target.Foo); // The memory caching service never serializes or deserializes so the non-serialized values won't change // Assert.Equal(0, log.Target.Bar); }
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 Indexing() { TimeSpan cacheLifeTime = TimeSpan.FromMinutes(5); MemoryCachingService service = new MemoryCachingService(cacheLifeTime); string cachableKey = this.cachableKey; // this shouldn't throw Assert.Null(service[cachableKey]); Foobar cachable = GetCachableObject(); DefaultCachedObject <object> dto = new DefaultCachedObject <object> { Value = cachable, CachedTime = DateTime.UtcNow, ExpireTime = DateTime.UtcNow.Add(cacheLifeTime) }; service[cachableKey] = dto; Assert.Single(service); ICachedObjectBasic retrieved = service[cachableKey]; Assert.Equal(dto.Value, retrieved.UntypedValue); }
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 LogTest() { MemoryCachingService cachingService = new MemoryCachingService(TimeSpan.FromMinutes(5)); Json serializationService = new Json(); CacheLoggingService loggingService = new CacheLoggingService(cachingService, serializationService, _partSeperator); DateTime startLogging = DateTime.UtcNow; loggingService.LogMessage("Test Log", "A test log", LogLevel.Debug); DateTime endLogging = DateTime.UtcNow; Assert.Single(cachingService); string fullLogName = cachingService.EnumerateDictionary().Single().Key; Assert.StartsWith(LogLevel.Debug.ToString() + _partSeperator, fullLogName); string timestampString = fullLogName.Split('_')[1]; DateTime timeStamp = DateTime.FromFileTimeUtc(long.Parse(timestampString)); Assert.True(timeStamp >= startLogging.AddMilliseconds(-5) && timeStamp <= endLogging.AddMilliseconds(5)); }