private Customer CreateCustomer()
 {
     var customer = new Customer
                        {
                            FirstName = "foo",
                            LastName = "bar",
                            Email = Guid.NewGuid().ToString()
                        };
     return customer;
 }
        public void StressAsync()
        {
            var globalConn = OrmAsyncTestSession.GetConnection();

            globalConn.CreateTableAsync<Customer>().Wait();

            int threadCount = 0;
            var doneEvent = new AutoResetEvent(false);
            const int n = 500;
            var errors = new List<string>();
            for (int i = 0; i < n; i++)
            {
#if NETFX_CORE
                ThreadPool.RunAsync(
#else
                ThreadPool.QueueUserWorkItem(
#endif
                    delegate
                        {
                            try
                            {
                                var conn = OrmAsyncTestSession.GetConnection(globalConn.ConnectionString);
                                var obj = new Customer { FirstName = i.ToString() };

                                conn.InsertAsync(obj).Wait();
                                if (obj.Id == 0)
                                {
                                    lock (errors)
                                    {
                                        errors.Add("Bad Id");
                                    }
                                }

                                var query = from c in conn.Table<Customer>() where c.Id == obj.Id select c;
                                if (query.ToListAsync().Result.FirstOrDefault() == null)
                                {
                                    lock (errors)
                                    {
                                        errors.Add("Failed query");
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                lock (errors)
                                {
                                    errors.Add(ex.Message);
                                }
                            }

                            threadCount++;
                            if (threadCount == n)
                            {
                                doneEvent.Set();
                            }
                        });
            }
            doneEvent.WaitOne();

            int count = globalConn.Table<Customer>().CountAsync().Result;

            Assert.AreEqual(0, errors.Count);
            Assert.AreEqual(n, count);
        }