public void TestStoreNestedEntry()
        {
            var CouchConveyor = new OrderedPooledCouchConveyor <TestEntry>(HOSTNAME, DATABASE, 2);

            CouchConveyor.StartAll();

            ManualResetEvent wait_event = new ManualResetEvent(false);

            CouchConveyor.Convey("TestStoreNestedEntry", CreateTestEntry("a-test-entry", 1), new TestWaitEventHandler(wait_event));
            wait_event.WaitOne(30 * 1000);

            CouchConveyor.StopAll();
        }
        public void TestMassiveStoreEntries()
        {
            var iteration = 1000;
            var entries   = new List <TestEntry>(3000);

            for (int i = 0; i < iteration; ++i)
            {
                for (int n = 0; n < 10; ++n)
                {
                    var entry = CreateTestEntry("massive-test-entry", i);
                    entry.StringValue += " :" + n;
                    entries.Add(entry);
                }
            }
            entries.Sort(delegate(TestEntry x, TestEntry y) { return(x.IntValue.CompareTo(y.IntValue)); });            // shuffle randomly

            var CouchConveyor = new OrderedPooledCouchConveyor <TestEntry>(HOSTNAME, DATABASE, 1024);

            CouchConveyor.StartAll();

            var monitor = new TestMonitor <TestEntry>();

            foreach (var entry in entries)
            {
                CouchConveyor.Convey(entry.Id, entry, monitor.StartOne());
            }
            ;

            monitor.WaitToFinish();

            Trace.WriteLine(string.Format("Completed to store all test entries. Start checking it now"));
            var mycouch_pool      = new ConcurrentBag <MyCouchStore>(Enumerable.Range(0, 100).Select((i) => new MyCouchStore(HOSTNAME, DATABASE)));
            var entries_to_remove = new ConcurrentBag <TestEntry>();

            // Async tasks are suppressing Assert exceptions
            Parallel.ForEach(entries, new ParallelOptions {
                MaxDegreeOfParallelism = 100
            }, (entry) =>
            {
                MyCouchStore couchdb;
                if (false == mycouch_pool.TryTake(out couchdb))                 // Actually, this condition is not required. Just to avoid CS0165
                {
                    SpinWait.SpinUntil(() => mycouch_pool.TryTake(out couchdb));
                }
                var task    = couchdb.GetByIdAsync <TestEntry>(entry.Id);
                var dbentry = task.Result;
                mycouch_pool.Add(couchdb);
                couchdb = null;

                Assert.IsNotNull(dbentry);

                var last_entry = entries.Where((e) => e.Id == entry.Id).Last();
                if (last_entry.IntValue != dbentry.IntValue)
                {
                    var es = entries.Where((e) => e.Id == entry.Id);
                    Trace.WriteLine(string.Format("Entry '{0}' has stored incorrectly: {1} for {2}", entry.Id, dbentry.IntValue, string.Join(",", es.Select((e) => e.IntValue))));
                }
                Assert.AreEqual(entry.Id, dbentry.Id);
                Assert.AreEqual(last_entry.IntValue, dbentry.IntValue);
                Assert.AreEqual(last_entry.IntNullValue, dbentry.IntNullValue);
                Assert.AreEqual(last_entry.StringValue, dbentry.StringValue);
                Assert.AreEqual(last_entry.StringNullValue, dbentry.StringNullValue);
                Assert.AreEqual(last_entry.CustomValue, dbentry.CustomValue);
                Assert.AreEqual(last_entry.CustomNullValue, dbentry.CustomNullValue);
                Assert.AreEqual(last_entry.DateTimeValue.ToString(), dbentry.DateTimeValue.ToString());
                CollectionAssert.AreEqual(last_entry.ListValue, dbentry.ListValue);

                if (last_entry.IntValue == entry.IntValue)
                {
                    entries_to_remove.Add(entry);
                }
            });

            var delete_monitor = new TestMonitorForDelete <TestEntry>();

            Parallel.ForEach(entries_to_remove, (entry) => CouchConveyor.Convey(entry.Id, null, delete_monitor.StartOne()));
            delete_monitor.WaitToFinish();
            CouchConveyor.StopAll();

            Parallel.ForEach(entries, new ParallelOptions {
                MaxDegreeOfParallelism = 100
            }, (entry) =>
            {
                MyCouchStore couchdb;
                if (false == mycouch_pool.TryTake(out couchdb))
                {
                    SpinWait.SpinUntil(() => mycouch_pool.TryTake(out couchdb));
                }
                var task    = couchdb.GetByIdAsync <TestEntry>(entry.Id);
                var dbentry = task.Result;
                mycouch_pool.Add(couchdb);
                couchdb = null;

                Assert.IsNull(dbentry);
            });

            foreach (var c in mycouch_pool)
            {
                c.Dispose();
            }
        }