Beispiel #1
0
        public void TestAddMessage()
        {
            var storage  = new InMemoryStorage();
            var context1 = new MessageContext()
            {
                Id = Guid.NewGuid(), Type = "request"
            };
            var context2 = new MessageContext()
            {
                Id = Guid.NewGuid(), Type = "request"
            };

            storage.Persist(CreateBeginRequestMessage(context1));
            storage.Persist(CreateBeginRequestMessage(context2));
            storage.Persist(CreateEndRequestMessage(context2));
            storage.Persist(CreateEndRequestMessage(context1));

            Guid[] idsToCheck = { context1.Id, context2.Id };

            foreach (Guid requestId in idsToCheck)
            {
                var messages = storage.GetMessagesByRequestId(requestId);
                Assert.Equal(2, messages.Count());
                foreach (IMessage m in messages)
                {
                    Assert.Equal(requestId, m.Context.Id);
                }
            }

            // TODO - test store's other query mechanisms
        }
Beispiel #2
0
        public async Task TestMessageCleanup()
        {
            int maxRequests       = 500;
            int numThreads        = 25;
            int totalRequests     = 1000;
            int requestsPerThread = totalRequests / numThreads;

            var storage      = new InMemoryStorage(maxRequests);
            var tasks        = new List <Task>();
            var requestGuids = new List <Guid>();

            for (int i = 0; i < numThreads; i++)
            {
                // ThreadPool.QueueUserWorkItem(callback);
                var task = Task.Factory.StartNew(() =>
                {
                    for (int j = 0; j < requestsPerThread; j++)
                    {
                        MessageContext context = new MessageContext()
                        {
                            Id = Guid.NewGuid(), Type = "request"
                        };
                        storage.Persist(CreateBeginRequestMessage(context));
                        storage.Persist(CreateEndRequestMessage(context));
                        lock (requestGuids)
                        {
                            requestGuids.Add(context.Id);
                        }
                    }
                });

                tasks.Add(task);
            }


            await Task.WhenAll(tasks).ContinueWith((task) =>
            {
                Assert.InRange(storage.GetRequestCount(), maxRequests - (maxRequests / 10), maxRequests + 1);

                Assert.Equal(totalRequests, requestGuids.Count());

                foreach (Guid g in requestGuids)
                {
                    Assert.True(!storage.GetMessagesByRequestId(g).Any() || storage.GetMessagesByRequestId(g).Count() == 2);
                }

                Assert.True(storage.CheckConsistency());
            });
        }