示例#1
0
		public void StressAsync ()
		{
			string path = null;
			var globalConn = GetConnection (ref path);
			
			globalConn.CreateTableAsync<Customer> ().Wait ();
			
			var threadCount = 0;
			var doneEvent = new AutoResetEvent (false);
			var n = 500;
			var errors = new List<string> ();
			for (var i = 0; i < n; i++) {
				var ii = i;

#if NETFX_CORE
                Task.Run (
#else
				var th = new Thread ((ThreadStart)
#endif
                delegate {
					try {
						var conn = GetConnection ();
						var obj = new Customer {
							FirstName = ii.ToString (),
						};
						conn.InsertAsync (obj).Wait ();
						if (obj.Id == 0) {
							lock (errors) {
								errors.Add ("Bad Id");
							}
						}
						var obj2 = (from c in conn.Table<Customer> () where c.Id == obj.Id select c).ToListAsync ().Result.FirstOrDefault();
						if (obj2 == null) {
							lock (errors) {
								errors.Add ("Failed query");
							}
						}
					}
					catch (Exception ex) {
						lock (errors) {
							errors.Add (ex.Message);
						}
					}
					threadCount++;
					if (threadCount == n) {
						doneEvent.Set();
					}
				});

#if !NETFX_CORE
				th.Start ();
#endif
			}
			doneEvent.WaitOne ();
			
			var count = globalConn.Table<Customer> ().CountAsync ().Result;
			
			Assert.AreEqual (0, errors.Count);
			Assert.AreEqual (n, count);			
		}
示例#2
0
        public void FindAsyncWithExpression()
        {
            // create...
            Customer customer = new Customer();
            customer.FirstName = "foo";
            customer.LastName = "bar";
            customer.Email = Guid.NewGuid().ToString();

            // connect and insert...
            string path = null;
            var conn = GetConnection(ref path);
            conn.CreateTableAsync<Customer>().Wait();
            conn.InsertAsync(customer).Wait();

            // check...
            Assert.AreNotEqual(0, customer.Id);

            // get it back...
            var task = conn.FindAsync<Customer>(x => x.Id == customer.Id);
            task.Wait();
            Customer loaded = task.Result;

            // check...
            Assert.AreEqual(customer.Id, loaded.Id);
        }
示例#3
0
		public void TestRunInTransactionAsync ()
		{
			// connect...
			string path = null;
			var conn = GetConnection (ref path);
			conn.CreateTableAsync<Customer> ().Wait ();
			bool transactionCompleted = false;

			// run...
			Customer customer = new Customer ();
			conn.RunInTransactionAsync ((c) => {
				// insert...
				customer.FirstName = "foo";
				customer.LastName = "bar";
				customer.Email = Guid.NewGuid ().ToString ();
				c.Insert (customer);

				// delete it again...
				c.Execute ("delete from customer where id=?", customer.Id);

				// set completion flag
				transactionCompleted = true;
			}).Wait (10000);

			// check...
			Assert.IsTrue(transactionCompleted);
			using (SQLiteConnection check = new SQLiteConnection (path)) {
				// load it back and check - should be deleted...
				var loaded = check.Table<Customer> ().Where (v => v.Id == customer.Id).ToList ();
				Assert.AreEqual (0, loaded.Count);
			}
		}
示例#4
0
		public void TestInsertAllAsync ()
		{
			// create a bunch of customers...
			List<Customer> customers = new List<Customer> ();
			for (int index = 0; index < 100; index++) {
				Customer customer = new Customer ();
				customer.FirstName = "foo";
				customer.LastName = "bar";
				customer.Email = Guid.NewGuid ().ToString ();
				customers.Add (customer);
			}

			// connect...
			string path = null;
			var conn = GetConnection (ref path);
			conn.CreateTableAsync<Customer> ().Wait ();

			// insert them all...
			conn.InsertAllAsync (customers).Wait ();

			// check...
			using (SQLiteConnection check = new SQLiteConnection (path)) {
				for (int index = 0; index < customers.Count; index++) {
					// load it back and check...
					Customer loaded = check.Get<Customer> (customers[index].Id);
					Assert.AreEqual (loaded.Email, customers[index].Email);
				}
			}
		}
示例#5
0
		public void TestTableAsync ()
		{
			// connect...
			var conn = GetConnection ();
			conn.CreateTableAsync<Customer> ().Wait ();
			conn.ExecuteAsync ("delete from customer").Wait ();

			// insert some...
			List<Customer> customers = new List<Customer> ();
			for (int index = 0; index < 5; index++) {
				Customer customer = new Customer ();
				customer.FirstName = "foo";
				customer.LastName = "bar";
				customer.Email = Guid.NewGuid ().ToString ();

				// insert...
				conn.InsertAsync (customer).Wait ();

				// add...
				customers.Add (customer);
			}

			// run the table operation...
			var query = conn.Table<Customer> ();
			var loaded = query.ToListAsync ().Result;

			// check that we got them all back...
			Assert.AreEqual (5, loaded.Count);
			Assert.IsNotNull (loaded.Where (v => v.Id == customers[0].Id));
			Assert.IsNotNull (loaded.Where (v => v.Id == customers[1].Id));
			Assert.IsNotNull (loaded.Where (v => v.Id == customers[2].Id));
			Assert.IsNotNull (loaded.Where (v => v.Id == customers[3].Id));
			Assert.IsNotNull (loaded.Where (v => v.Id == customers[4].Id));
		}
示例#6
0
		private Customer CreateCustomer ()
		{
			Customer customer = new Customer () {
				FirstName = "foo",
				LastName = "bar",
				Email = Guid.NewGuid ().ToString ()
			};
			return customer;
		}