Beispiel #1
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());
            });
        }