public void MyTestInitialize() { DropCollection(typeof(TestMessage).Name); _factory = null; _factory = new monQue.MonQueFactory(); Thread.Sleep(500); // I dont know if drop collection is sync call and if DB is not still doing stuff after call return...This will make tests timing more accurate }
public void WorkQueue_SimpleSendReceiveTest() { int MESSAGES_COUNT = 10000; int MAX_TEST_TIME_MS = 15 * 1000; int count = 0; DateTime startTime = DateTime.UtcNow; // Start producing jobs (async) Task producer = Task.Factory.StartNew(() => { var sender_factory = new MonQueFactory(); var sendQueue = sender_factory.GetMessagesPublisher <TestMessage>(); Enumerable.Range(1, MESSAGES_COUNT).Select(i => new TestMessage(i, i.ToString())).ToList() .ForEach(m => sendQueue.Send(m, QueueMode.WorkQueue)); }); // start the job worker var receiver_factory = new MonQueFactory(); receiver_factory.RegisterWorker <TestMessage>(msg => { Interlocked.Increment(ref count); }); // wait till all items are done or timeout while (count < MESSAGES_COUNT && DateTime.UtcNow - startTime < TimeSpan.FromMilliseconds(MAX_TEST_TIME_MS)) { Thread.Sleep(500); } Assert.AreEqual(MESSAGES_COUNT, count); }
public void WorkQueueTest_OneProducer_N_consumers_should_shareTheLoad() { int MESSAGES_COUNT = 500; int MAX_TEST_TIME_MS = 30 * 1000; int CONSUMERS_COUNT = 100; var sender_factory = new MonQueFactory(); var producer = sender_factory.GetMessagesPublisher <TestMessage>(); int globalCounter = 0; for (int i = 0; i < CONSUMERS_COUNT; i++) { var receiver_factory = new MonQueFactory(); // each factory represents a different process communicating with mongo. Assumption is that no 2 workers on same message type on the same process int local = i; Action <TestMessage> workerAction = msg => { Interlocked.Increment(ref globalCounter); Console.WriteLine("Consumer:{0} Msg:{1}", local, msg.IntVal); Thread.Sleep(100); }; receiver_factory.RegisterWorker <TestMessage>(workerAction); } // start shooting some jobs... DateTime startTime = DateTime.UtcNow; Enumerable.Range(1, MESSAGES_COUNT).Select(i => new TestMessage(i, i.ToString())).ToList() .ForEach(m => producer.Send(m)); while (globalCounter < MESSAGES_COUNT && DateTime.UtcNow - startTime < TimeSpan.FromMilliseconds(MAX_TEST_TIME_MS)) { Thread.Sleep(500); } Thread.Sleep(1000); // makes this test stronger - in case there are still workers doing stuff and taking the same jobs. We should consider removing the while and limit this test only by time Assert.AreEqual(MESSAGES_COUNT, globalCounter); }