コード例 #1
0
        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);
        }
コード例 #2
0
        private CacheLoggingService GetLogsService()
        {
            Json serializationService           = new Json();
            MemoryCachingService cachingService = new MemoryCachingService(TimeSpan.FromMinutes(5));
            CacheLoggingService  loggingService = new CacheLoggingService(cachingService, serializationService);

            return(loggingService);
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        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);
        }
コード例 #5
0
        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);
        }
コード例 #6
0
        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));
        }
コード例 #7
0
        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);
        }
コード例 #8
0
        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);
        }
コード例 #9
0
        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);
        }
コード例 #10
0
        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);
        }
コード例 #11
0
        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);
        }
コード例 #12
0
        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);
        }
コード例 #13
0
        private MemoryCachingService InitMemoryCachingService()
        {
            var services =
                new ServiceCollection()
                .AddMemoryCache()
                .BuildServiceProvider();

            var memoryCachingService = new MemoryCachingService(
                services.GetRequiredService <IMemoryCache>()
                );

            return(memoryCachingService);
        }
コード例 #14
0
        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);
        }
コード例 #15
0
        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);
        }
コード例 #16
0
        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);
        }
コード例 #17
0
        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);
        }
コード例 #18
0
        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);
        }
コード例 #19
0
        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);
        }
コード例 #20
0
        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);
        }
コード例 #21
0
        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);
        }
コード例 #22
0
        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));
        }
コード例 #23
0
        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);
        }
コード例 #24
0
        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);
        }
コード例 #25
0
        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));
        }
コード例 #26
0
        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);
        }
コード例 #27
0
        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));
        }
コード例 #28
0
        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);
        }
コード例 #29
0
        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);
        }
コード例 #30
0
        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));
        }