示例#1
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));
        }
示例#2
0
        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);
        }
        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>());
        }
示例#4
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);
        }
示例#5
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));
        }
示例#6
0
        public async Task Should_support_access_to_eventual_success()
        {
            IIndex <string, SimpleValue> index = new GreenCache <SimpleValue>().AddIndex("id", x => x.Id);

            var helloKey = "Hello";

            Task <SimpleValue> valueTask = index.Get(helloKey, SimpleValueFactory.Faulty);

            Task <SimpleValue> goodValueTask = index.Get(helloKey, SimpleValueFactory.Healthy);

            Task <SimpleValue> 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"));
        }
示例#7
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"));
        }
示例#8
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));
        }
示例#9
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));
        }
示例#10
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);
        }
示例#11
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);
        }
示例#12
0
        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);
        }
示例#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);
        }
示例#14
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);
        }
示例#15
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);
        }
示例#16
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);
        }
示例#17
0
        public async Task Should_support_a_simple_addition()
        {
            IIndex <string, SimpleValue> index = new GreenCache <SimpleValue>().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"));
        }
示例#18
0
        public async Task Should_not_find_a_faulted_value()
        {
            IIndex <string, SimpleValue> index = new GreenCache <SimpleValue>().AddIndex("id", x => x.Id);

            var helloKey = "Hello";

            Task <SimpleValue> valueTask = index.Get(helloKey, SimpleValueFactory.Faulty);

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

            Assert.That(async() => await index.Get(helloKey), Throws.TypeOf <KeyNotFoundException>());
        }
示例#19
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);
        }
示例#20
0
        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);
        }
示例#21
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);
        }
示例#22
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"));
        }
示例#23
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>());
        }
示例#24
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);
        }
示例#25
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);
        }
示例#26
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));
        }
示例#27
0
文件: Tests.cs 项目: zzms/MassTransit
        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
            }));
        }
示例#28
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
            }));
        }
示例#29
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));
        }
示例#30
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);
        }