public async Task Should_honor_the_clear()
        {
            var settings = new TestCacheSettings(100, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(60));

            var cache = new GreenCache <SimpleValue>(settings);

            var index      = cache.AddIndex("id", x => x.Id);
            var valueIndex = cache.AddIndex("value", x => x.Value);

            for (int i = 0; i < 100; i++)
            {
                SimpleValue simpleValue = await index.Get($"key{i}", SimpleValueFactory.Healthy);
            }

            await Task.Delay(100);

            Assert.That(cache.Statistics.Count, Is.EqualTo(100));

            var result = await valueIndex.Get("The key is key27");

            Assert.That(result.Id, Is.EqualTo("key27"));

            cache.Clear();

            Assert.That(async() => await index.Get("key27"), Throws.TypeOf <KeyNotFoundException>());
            Assert.That(async() => await valueIndex.Get("The key is key27"), Throws.TypeOf <KeyNotFoundException>());
        }
Exemple #2
0
        public async Task Should_update_the_second_index_once_removed()
        {
            var settings = new TestCacheSettings(100, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(60));

            var cache = new GreenCache <SimpleValue>(settings);

            IIndex <string, SimpleValue> index      = cache.AddIndex("id", x => x.Id);
            IIndex <string, SimpleValue> valueIndex = cache.AddIndex("value", x => x.Value);

            for (var i = 0; i < 100; i++)
            {
                var simpleValue = await index.Get($"key{i}", SimpleValueFactory.Healthy);
            }

            await Task.Delay(100);

            Assert.That(cache.Statistics.Count, Is.EqualTo(100));

            var result = await valueIndex.Get("The key is key27");

            Assert.That(result.Id, Is.EqualTo("key27"));

            valueIndex.Remove("The key is key29");

            await Task.Delay(300);

            Assert.That(async() => await index.Get("key29"), Throws.TypeOf <KeyNotFoundException>());

            Assert.That(cache.Statistics.Count, Is.EqualTo(99));
            Assert.That(cache.Statistics.Hits, Is.EqualTo(1));
            Assert.That(cache.Statistics.Misses, Is.EqualTo(101));
        }
Exemple #3
0
        public async Task Should_fill_up_the_buckets_with_smart_values_over_time_and_remove_old_entries()
        {
            var settings = new TestCacheSettings(100, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(300));

            var cache = new GreenCache <SmartValue>(settings);

            IIndex <string, SmartValue> index = cache.AddIndex("id", x => x.Id);

            var observer = new SmartAddedCountObserver(200);

            cache.Connect(observer);

            var removed = new NodeRemovedCountObserver <SmartValue>(100);

            cache.Connect(removed);


            for (var i = 0; i < 200; i++)
            {
                var simpleValue = await index.Get($"key{i}", SmartValueFactory.Healthy);

                settings.CurrentTime += TimeSpan.FromSeconds(1);
            }

            await observer.Completed;
            await removed.Completed;

            Assert.That(cache.Statistics.Count, Is.EqualTo(100));
        }
Exemple #4
0
        public async Task Should_fill_them_even_fuller()
        {
            DateTime currentTime = DateTime.UtcNow;

            var cache = new GreenCache <SimpleValue>(100, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(60), () => currentTime);

            var index = cache.AddIndex("id", x => x.Id);

            for (int i = 0; i < 200; i++)
            {
                SimpleValue simpleValue = await index.Get($"key{i}", SimpleValueFactory.Healthy);

                if (i % 2 == 0)
                {
                    currentTime = currentTime.Add(TimeSpan.FromSeconds(1));
                }
            }

            await Task.Delay(100);

            Assert.That(cache.Statistics.Count, Is.EqualTo(101));

            Assert.That(cache.Statistics.ValidityCheckInterval, Is.EqualTo(TimeSpan.FromMilliseconds(250)));
            Assert.That(cache.Statistics.OldestBucketIndex, Is.EqualTo(50));
            Assert.That(cache.Statistics.CurrentBucketIndex, Is.EqualTo(100));


            var values = cache.GetAll().ToArray();

            Assert.That(values.Length, Is.EqualTo(101));
        }
Exemple #5
0
        public MessageProducerCache()
        {
            _cache = new GreenCache <CachedMessageProducer>(10000, TimeSpan.FromMinutes(1), TimeSpan.FromHours(24), () => DateTime.UtcNow);
            _cache.Connect(new CloseAndDisposeOnRemoveObserver());

            _index = _cache.AddIndex("destination", x => x.Destination);
        }
Exemple #6
0
        public async Task Should_support_access_to_eventual_success()
        {
            var cache = new GreenCache <SimpleValue>(1000, TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(30), () => DateTime.UtcNow);
            var index = cache.AddIndex("id", x => x.Id);

            var helloKey = "Hello";

            var valueTask = index.Get(helloKey, SimpleValueFactory.Faulty);

            var goodValueTask = index.Get(helloKey, SimpleValueFactory.Healthy);

            var readValueTask = index.Get(helloKey);

            Assert.That(async() => await valueTask, Throws.TypeOf <TestException>());

            var value = await goodValueTask;

            Assert.That(value, Is.Not.Null);
            Assert.That(value.Id, Is.EqualTo(helloKey));
            Assert.That(value.Value, Is.EqualTo("The key is Hello"));

            var readValue = await readValueTask;

            Assert.That(readValue, Is.Not.Null);
            Assert.That(readValue.Id, Is.EqualTo(helloKey));
            Assert.That(readValue.Value, Is.EqualTo("The key is Hello"));
        }
Exemple #7
0
        public async Task Should_fill_up_a_bunch_of_buckets()
        {
            var addedObserver   = new NodeAddedCountObserver(100);
            var removedObserver = new NodeRemovedCountObserver <SimpleValue>(40);

            var settings = new TestCacheSettings(100, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(60));

            var cache = new GreenCache <SimpleValue>(settings);

            IIndex <string, SimpleValue> index = cache.AddIndex("id", x => x.Id);

            cache.Connect(addedObserver);
            cache.Connect(removedObserver);

            for (var i = 0; i < 100; i++)
            {
                var simpleValue = await index.Get($"key{i}", SimpleValueFactory.Healthy);

                settings.CurrentTime += TimeSpan.FromSeconds(1);
            }

            await addedObserver.Completed;
            await removedObserver.Completed;

            Assert.That(cache.Statistics.Count, Is.EqualTo(60));

            Task <SimpleValue>[] values = cache.GetAll().ToArray();

            Assert.That(values.Length, Is.EqualTo(60));
        }
        public ServiceBusHost(IServiceBusHostConfiguration hostConfiguration, IServiceBusHostTopology hostTopology)
            : base(hostConfiguration, hostTopology)
        {
            _hostConfiguration = hostConfiguration;
            _hostTopology      = hostTopology;

            RetryPolicy = Retry.CreatePolicy(x =>
            {
                x.Ignore <MessagingEntityNotFoundException>();
                x.Ignore <MessagingEntityAlreadyExistsException>();
                x.Ignore <MessageNotFoundException>();
                x.Ignore <MessageSizeExceededException>();

                x.Handle <ServerBusyException>(exception => exception.IsTransient);
                x.Handle <TimeoutException>();

                x.Interval(5, TimeSpan.FromSeconds(10));
            });

            MessagingFactoryContextSupervisor = new MessagingFactoryContextSupervisor(hostConfiguration);

            NamespaceContextSupervisor = new NamespaceContextSupervisor(hostConfiguration);

            var cacheSettings = new CacheSettings(SendEndpointCacheDefaults.Capacity, SendEndpointCacheDefaults.MinAge, SendEndpointCacheDefaults.MaxAge);

            var cache = new GreenCache <CachedSendTransport>(cacheSettings);

            _index = cache.AddIndex("key", x => x.Address);
        }
Exemple #9
0
        public SendEndpointCache(ISendEndpointProvider sendEndpointProvider)
        {
            _sendEndpointProvider = sendEndpointProvider;

            var cache = new GreenCache <CachedSendEndpoint <Uri> >(10000, TimeSpan.FromMinutes(1), TimeSpan.FromHours(24), () => DateTime.UtcNow);

            _index = cache.AddIndex("address", x => x.Key);
        }
        public SendEndpointCache()
        {
            var cacheSettings = new CacheSettings(1000, TimeSpan.FromMinutes(1), TimeSpan.FromHours(24));

            var cache = new GreenCache <CachedSendEndpoint <TKey> >(cacheSettings);

            _index = cache.AddIndex("key", x => x.Key);
        }
Exemple #11
0
        public SendEndpointCache()
        {
            var cacheSettings = new CacheSettings(SendEndpointCacheDefaults.Capacity, SendEndpointCacheDefaults.MinAge, SendEndpointCacheDefaults.MaxAge);

            var cache = new GreenCache <CachedSendEndpoint <TKey> >(cacheSettings);

            _index = cache.AddIndex("key", x => x.Key);
        }
Exemple #12
0
        public MessageProducerCache()
        {
            var cacheSettings = new CacheSettings(SendEndpointCacheDefaults.Capacity, SendEndpointCacheDefaults.MinAge, SendEndpointCacheDefaults.MaxAge);

            _cache = new GreenCache <CachedMessageProducer>(cacheSettings);
            _cache.Connect(new CloseAndDisposeOnRemoveObserver());

            _index = _cache.AddIndex("destination", x => x.Destination);
        }
Exemple #13
0
        public MessageProducerCache()
        {
            var cacheSettings = new CacheSettings(10000, TimeSpan.FromMinutes(1), TimeSpan.FromHours(24));

            _cache = new GreenCache <CachedMessageProducer>(cacheSettings);
            _cache.Connect(new CloseAndDisposeOnRemoveObserver());

            _index = _cache.AddIndex("destination", x => x.Destination);
        }
Exemple #14
0
        public PublishTransportProvider(IRabbitMqHostControl host, IModelContextSupervisor modelContextSupervisor)
        {
            _host = host;
            _modelContextSupervisor = modelContextSupervisor;

            var cacheSettings = new CacheSettings(SendEndpointCacheDefaults.Capacity, SendEndpointCacheDefaults.MinAge, SendEndpointCacheDefaults.MaxAge);

            var cache = new GreenCache <CachedSendTransport>(cacheSettings);

            _index = cache.AddIndex("key", x => x.Address);
        }
Exemple #15
0
        public InMemoryHost(IInMemoryHostConfiguration hostConfiguration, IInMemoryHostTopology hostTopology)
            : base(hostConfiguration, hostTopology)
        {
            _hostConfiguration = hostConfiguration;

            _messageFabric = new MessageFabric(hostConfiguration.TransportConcurrencyLimit);

            var cache = new GreenCache <InMemorySendTransport>(hostConfiguration.SendTransportCacheSettings);

            _index = cache.AddIndex("exchangeName", x => x.ExchangeName);
        }
Exemple #16
0
        public async Task Should_not_find_a_faulted_value()
        {
            var cache = new GreenCache <SimpleValue>(1000, TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(30), () => DateTime.UtcNow);
            var index = cache.AddIndex("id", x => x.Id);

            var helloKey = "Hello";

            var valueTask = index.Get(helloKey, SimpleValueFactory.Faulty);

            Assert.That(async() => await valueTask, Throws.TypeOf <TestException>());

            Assert.That(async() => await index.Get(helloKey), Throws.TypeOf <KeyNotFoundException>());
        }
        public InMemoryHost(int concurrencyLimit, IHostTopology topology, Uri baseAddress = null)
        {
            Topology = topology;
            _baseUri = baseAddress ?? new Uri("loopback://localhost/");

            _messageFabric = new MessageFabric(concurrencyLimit);

            _receiveEndpoints = new ReceiveEndpointCollection();

            var cache = new GreenCache <InMemorySendTransport>(10000, TimeSpan.FromMinutes(1), TimeSpan.FromHours(24), () => DateTime.UtcNow);

            _index = cache.AddIndex("exchangeName", x => x.ExchangeName);
        }
Exemple #18
0
        public RabbitMqPublishEndpointProvider(IRabbitMqHost host, IMessageSerializer serializer, Uri sourceAddress, IPublishPipe publishPipe)
        {
            _host          = host;
            _serializer    = serializer;
            _sourceAddress = sourceAddress;
            _publishPipe   = publishPipe;

            _publishObservable = new PublishObservable();

            var cache = new GreenCache <CachedSendEndpoint <Type> >(10000, TimeSpan.FromMinutes(1), TimeSpan.FromHours(24), () => DateTime.UtcNow);

            _index = cache.AddIndex("type", x => x.Key);
        }
Exemple #19
0
        public async Task Should_honor_the_clear_and_still_allow_adding_nodes()
        {
            DateTime currentTime = DateTime.UtcNow;

            var cache = new GreenCache <SimpleValue>(100, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(60), () => currentTime);

            var index      = cache.AddIndex("id", x => x.Id);
            var valueIndex = cache.AddIndex("value", x => x.Value);

            for (int i = 0; i < 100; i++)
            {
                SimpleValue simpleValue = await index.Get($"key{i}", SimpleValueFactory.Healthy);
            }

            await Task.Delay(100);

            Assert.That(cache.Statistics.Count, Is.EqualTo(100));
            Assert.That(cache.Statistics.Misses, Is.EqualTo(100));

            var result = await valueIndex.Get("The key is key27");

            Assert.That(result.Id, Is.EqualTo("key27"));

            Assert.That(cache.Statistics.Hits, Is.EqualTo(1));

            cache.Clear();

            Assert.That(async() => await index.Get("key27"), Throws.TypeOf <KeyNotFoundException>());
            Assert.That(async() => await valueIndex.Get("The key is key27"), Throws.TypeOf <KeyNotFoundException>());

            for (int i = 0; i < 100; i++)
            {
                SimpleValue simpleValue = await index.Get($"key{i}", SimpleValueFactory.Healthy);
            }

            await Task.Delay(100);

            Assert.That(cache.Statistics.Count, Is.EqualTo(100));
        }
Exemple #20
0
        public InMemoryHost(IInMemoryHostConfiguration hostConfiguration, int concurrencyLimit)
            : base(hostConfiguration)
        {
            _hostConfiguration = hostConfiguration;

            _messageFabric = new MessageFabric(concurrencyLimit);

            var cacheSettings = new CacheSettings(10000, TimeSpan.FromMinutes(1), TimeSpan.FromHours(24));

            var cache = new GreenCache <InMemorySendTransport>(cacheSettings);

            _index = cache.AddIndex("exchangeName", x => x.ExchangeName);
        }
Exemple #21
0
        public async Task Should_support_a_simple_addition()
        {
            var cache = new GreenCache <SimpleValue>(1000, TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(30), () => DateTime.UtcNow);
            var index = cache.AddIndex("id", x => x.Id);

            var helloKey = "Hello";

            var value = await index.Get(helloKey, SimpleValueFactory.Healthy);

            Assert.That(value, Is.Not.Null);
            Assert.That(value.Id, Is.EqualTo(helloKey));
            Assert.That(value.Value, Is.EqualTo("The key is Hello"));
        }
        public async Task Should_update_the_second_index()
        {
            var settings = new TestCacheSettings(100, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(60));

            var cache = new GreenCache <SimpleValue>(settings);

            var index      = cache.AddIndex("id", x => x.Id);
            var valueIndex = cache.AddIndex("value", x => x.Value);

            for (int i = 0; i < 100; i++)
            {
                SimpleValue simpleValue = await index.Get($"key{i}", SimpleValueFactory.Healthy);
            }

            await Task.Delay(100);

            Assert.That(cache.Statistics.Count, Is.EqualTo(100));

            var result = await valueIndex.Get("The key is key27");

            Assert.That(result.Id, Is.EqualTo("key27"));
        }
Exemple #23
0
        public InMemoryPublishEndpointProvider(ISendTransportProvider transportProvider, IPublishPipe publishPipe, IInMemoryPublishTopology publishTopology,
                                               IMessageSerializer serializer, Uri sourceAddress)
        {
            _publishPipe       = publishPipe;
            _publishTopology   = publishTopology;
            _serializer        = serializer;
            _sourceAddress     = sourceAddress;
            _host              = transportProvider as InMemoryHost;
            _publishObservable = new PublishObservable();

            var cache = new GreenCache <CachedSendEndpoint <TypeKey> >(10000, TimeSpan.FromMinutes(1), TimeSpan.FromHours(24), () => DateTime.UtcNow);

            _index = cache.AddIndex("type", x => x.Key);
        }
Exemple #24
0
        public InMemoryHost(IInMemoryHostConfiguration hostConfiguration, int concurrencyLimit, IHostTopology topology, Uri baseAddress = null)
        {
            _hostConfiguration = hostConfiguration;
            Topology           = topology;
            Address            = baseAddress ?? new Uri("loopback://localhost/");

            _messageFabric = new MessageFabric(concurrencyLimit);

            _receiveEndpoints = new ReceiveEndpointCollection();
            Add(_receiveEndpoints);

            var cacheSettings = new CacheSettings(10000, TimeSpan.FromMinutes(1), TimeSpan.FromHours(24));

            var cache = new GreenCache <InMemorySendTransport>(cacheSettings);

            _index = cache.AddIndex("exchangeName", x => x.ExchangeName);
        }
Exemple #25
0
        public async Task Should_fill_up_the_buckets()
        {
            DateTime currentTime = DateTime.UtcNow;

            var cache = new GreenCache <SimpleValue>(100, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(60), () => currentTime);

            var index = cache.AddIndex("id", x => x.Id);

            for (int i = 0; i < 100; i++)
            {
                SimpleValue simpleValue = await index.Get($"key{i}", SimpleValueFactory.Healthy);
            }

            await Task.Delay(100);

            Assert.That(cache.Statistics.Count, Is.EqualTo(100));
        }
Exemple #26
0
        public async Task Test1()
        {
            var cache = new GreenCache <Endpoint>(100, TimeSpan.FromSeconds(60), TimeSpan.FromMinutes(30),
                                                  () => DateTime.UtcNow);

            var addressIndex = cache.AddIndex("address", x => x.Address);

            var address = new Uri("rabbitmq://localhost/vhost/input-queue");

            cache.Add(new Endpoint {
                Address = address
            });


            var endpoint = await addressIndex.Get(address, key => Task.FromResult(new Endpoint {
                Address = key
            }));
        }
Exemple #27
0
        public async Task Test1()
        {
            var settings = new CacheSettings(100, TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(30));
            var cache    = new GreenCache <Endpoint>(settings);

            IIndex <Uri, Endpoint> addressIndex = cache.AddIndex("address", x => x.Address);

            var address = new Uri("rabbitmq://localhost/vhost/input-queue");

            cache.Add(new Endpoint {
                Address = address
            });


            var endpoint = await addressIndex.Get(address, key => Task.FromResult(new Endpoint {
                Address = key
            }));
        }
Exemple #28
0
        public async Task Should_fill_up_the_buckets()
        {
            var settings = new TestCacheSettings(100, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(60));

            var cache = new GreenCache <SimpleValue>(settings);

            IIndex <string, SimpleValue> index = cache.AddIndex("id", x => x.Id);

            var observer = new NodeAddedCountObserver(100);

            cache.Connect(observer);

            for (var i = 0; i < 100; i++)
            {
                var simpleValue = await index.Get($"key{i}", SimpleValueFactory.Healthy);
            }

            await observer.Completed;

            Assert.That(cache.Statistics.Count, Is.EqualTo(100));
        }
Exemple #29
0
        public RabbitMqHost(IRabbitMqHostConfiguration hostConfiguration, IRabbitMqHostTopology hostTopology)
            : base(hostConfiguration, hostTopology)
        {
            _hostConfiguration = hostConfiguration;
            _hostTopology      = hostTopology;

            ConnectionRetryPolicy = Retry.CreatePolicy(x =>
            {
                x.Handle <RabbitMqConnectionException>();

                x.Exponential(1000, TimeSpan.FromSeconds(3), TimeSpan.FromSeconds(30), TimeSpan.FromSeconds(3));
            });

            ConnectionContextSupervisor = new RabbitMqConnectionContextSupervisor(hostConfiguration, hostTopology);

            var cacheSettings = new CacheSettings(SendEndpointCacheDefaults.Capacity, SendEndpointCacheDefaults.MinAge, SendEndpointCacheDefaults.MaxAge);

            var cache = new GreenCache <CachedSendTransport>(cacheSettings);

            _index = cache.AddIndex("key", x => x.Address);
        }
Exemple #30
0
        public async Task Should_fill_them_even_fuller()
        {
            var settings = new TestCacheSettings(100, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(60));

            var cache = new GreenCache <SimpleValue>(settings);

            var index = cache.AddIndex("id", x => x.Id);


            var addedObserver = new NodeAddedCountObserver(200);

            cache.Connect(addedObserver);

            var removedObserver = new NodeRemovedCountObserver <SimpleValue>(99);

            cache.Connect(removedObserver);

            for (int i = 0; i < 200; i++)
            {
                SimpleValue simpleValue = await index.Get($"key{i}", SimpleValueFactory.Healthy);

                if (i % 2 == 0)
                {
                    settings.CurrentTime += TimeSpan.FromSeconds(1);
                }
            }

            await addedObserver.Completed;
            await removedObserver.Completed;

            Assert.That(cache.Statistics.Count, Is.EqualTo(101));

            var values = cache.GetAll().ToArray();

            Assert.That(values.Length, Is.EqualTo(101));
        }