public IEnumerable <TestCaseData> GetCases()
        {
            yield return(new TestCaseData(GetCacheConfig(), GetExchange(), GetResponse()).Returns(new CombinedTestResult(ResponseValue))
                         .SetName("not null response should be equal to cached value"));

            var config = GetCacheConfig();

            config[RouteName].Cache.Set(new Message <TestRequest>(MessageLabel.From(RouteName), GetRequest()), GetResponse(), TimeSpan.Zero);
            yield return(new TestCaseData(config, GetExchange(), new TestResponse {
                Value = "NotFromCacheResponse"
            }).Returns(new CombinedTestResult(ResponseValue, "NotFromCacheResponse"))
                         .SetName("cached value should be returned if set"));

            yield return(new TestCaseData(GetCacheConfig(false), GetExchange(), GetResponse()).Returns(new CombinedTestResult(null, ResponseValue))
                         .SetName("cache should be disabled when caching is disabled in configuration"));

            yield return(new TestCaseData(GetCacheConfig(null), GetExchange(), GetResponse()).Returns(new CombinedTestResult(null, ResponseValue))
                         .SetName("cache should be disabled when ttl is not set in configuration"));

            yield return(new TestCaseData(GetCacheConfig(null), new MessageExchange(new Message(MessageLabel.From(RouteName), new TestRequest())), GetResponse()).Returns(new CombinedTestResult(null, ResponseValue))
                         .SetName("cache should not be accessed when emitting non-request message"));

            yield return(new TestCaseData(GetCacheConfig(new AlwaysExpiringCache(), TimeSpan.Zero), GetExchange(), GetResponse()).Returns(new CombinedTestResult(null, ResponseValue))
                         .SetName("value should be requested when not found in cache"));
        }
        public object TestCacheIsSet(IDictionary <string, CacheConfiguration> cacheConfiguration, MessageExchange exchange, TestResponse response)
        {
            // Arrange
            var responseMessage = new Message <TestResponse>(MessageLabel.From(RouteName), response);
            var sendFilter      = new SendingExchangeFilter(
                e =>
            {
                e.In    = responseMessage;
                var tcs = new TaskCompletionSource <bool>();
                tcs.SetResult(true);
                return(tcs.Task);
            });
            var sut = new CachingFilterDecorator(cacheConfiguration);

            // Act
            sut.Process(sendFilter, exchange, new MessageExchangeFilterInvoker(new[] { sendFilter }, new Dictionary <Type, IMessageExchangeFilterDecorator> {
                { sendFilter.GetType(), sut }
            })).Wait();

            // Assert
            var cache  = cacheConfiguration[exchange.Out.Label.Name].Cache;
            var cached = cache.ContainsKey(exchange.Out) ? ((TestResponse)cache[exchange.Out]).Value : null;

            return(new CombinedTestResult(cached, response.Value));
        }
        private static MessageExchange GetExchange(string routeName = null)
        {
            var outMessage = new Message <TestRequest>(MessageLabel.From(!string.IsNullOrEmpty(routeName) ? routeName : RouteName), GetRequest());

            var exchange = new MessageExchange(outMessage, typeof(TestResponse));

            return(exchange);
        }
Exemplo n.º 4
0
            public void should_produce_empty_label()
            {
                MessageLabel label = MessageLabel.From(string.Empty);

                label.IsEmpty.Should().
                BeTrue();

                label = MessageLabel.From(null);
                label.IsEmpty.Should().
                BeTrue();
            }
Exemplo n.º 5
0
 public void should_use_structural_equality()
 {
     "label".ToMessageLabel().
     Should().
     Be(MessageLabel.From("label"));
     "LABEL".ToMessageLabel().
     Should().
     Be(MessageLabel.From("label"));
     "label".ToMessageLabel().
     Should().
     NotBe(MessageLabel.From("labell"));
     "label".ToMessageLabel().
     Should().
     NotBe(MessageLabel.From("labe"));
 }
Exemplo n.º 6
0
        public void TestValueIsNotInCache()
        {
            var hasherMock = new Mock <IHasher>();

            hasherMock.Setup(h => h.GetHash(It.IsAny <IMessage>()))
            .Returns("non-existing-hash");
            var cache = new MemoryCache(hasherMock.Object);
            var key   = new Message <string>(MessageLabel.From("label"), "payload");

            var containsCachedValue = cache.ContainsKey(key);
            var cachedValue         = containsCachedValue ? cache[key] : null;

            Assert.IsFalse(containsCachedValue, "cache should not contain value not cached before");
            Assert.IsNull(cachedValue, "value for not cached key should be null'");

            // teardown because of using MemoryCache.Default
            cache.Set(key, string.Empty, TimeSpan.Zero);
        }
Exemplo n.º 7
0
        public CombinedTestResult TestCacheIsSet(CacheConfiguration config, TestRequest payload)
        {
            // Arrange
            var consumer = new TestConsumer();

            consumer.InitData();
            var message = new Message <TestRequest>(MessageLabel.From("message.label"), payload);
            var context = new TestContext(message);
            var sut     = new CachingConsumerOf <TestRequest>(consumer, config);

            // Act
            sut.Handle(context);

            // Assert
            var cachedValue = config?.Cache.ContainsKey(message) ?? false ? ((TestResponse)config.Cache[message]).Value : null;

            return(new CombinedTestResult(cachedValue, context.Response.Value));
        }
Exemplo n.º 8
0
        public void TestCacheIsSet()
        {
            var hasherMock = new Mock <IHasher>();

            hasherMock.Setup(h => h.GetHash(It.IsAny <IMessage>()))
            .Returns("hash");
            var cache = new MemoryCache(hasherMock.Object);
            var key   = new Message <string>(MessageLabel.From("label"), "payload");
            var value = "value";

            cache.Set(key, value, TimeSpan.FromMinutes(1.0));
            var containsCachedValue = cache.ContainsKey(key);
            var cachedValue         = containsCachedValue ? cache[key] : null;

            Assert.IsTrue(containsCachedValue, "cache should contain cached value");
            Assert.AreEqual(value, cachedValue, $"cached value should be equal to '{value}'");

            // teardown because of using MemoryCache.Default
            cache.Set(key, string.Empty, TimeSpan.Zero);
        }
Exemplo n.º 9
0
 /// <summary>
 /// The on.
 /// </summary>
 /// <param name="label">
 /// The label.
 /// </param>
 /// <returns>
 /// The <see cref="IReceiverConfigurator"/>.
 /// </returns>
 public IReceiverConfigurator On(string label)
 {
     return(this.On(MessageLabel.From(label)));
 }
Exemplo n.º 10
0
 /// <summary>
 /// Преобразует строку в метку сообщения <see cref="MessageLabel"/>.
 /// </summary>
 /// <param name="s">
 /// Текстовое представление метки.
 /// </param>
 /// <returns>
 /// Строго типизированная метка сообщения.
 /// </returns>
 public static MessageLabel ToMessageLabel(this string s)
 {
     return(MessageLabel.From(s));
 }