private async Task NoOptimisticConcurrencyTest() { // If a table does not have a version column, then offline will still // work, but there will be no conflicts DateTime now = DateTime.UtcNow; int seed = now.Year * 10000 + now.Month * 100 + now.Day; Log("Using random seed: {0}", seed); Random rndGen = new Random(seed); var offlineReadyClient = CreateClient(); var localStore = new MobileServiceSQLiteStore(StoreFileName); Log("Defined the table on the local store"); localStore.DefineTable <OfflineReadyItemNoVersion>(); await offlineReadyClient.SyncContext.InitializeAsync(localStore); Log("Initialized the store and sync context"); var localTable = offlineReadyClient.GetSyncTable <OfflineReadyItemNoVersion>(); var remoteTable = offlineReadyClient.GetTable <OfflineReadyItemNoVersion>(); var item = new OfflineReadyItemNoVersion(rndGen); try { offlineReadyClient.CurrentUser = await Utilities.GetDummyUser(offlineReadyClient); await localTable.InsertAsync(item); Log("Inserted the item to the local store:", item); await offlineReadyClient.SyncContext.PushAsync(); Log("Pushed the changes to the server"); var serverItem = await remoteTable.LookupAsync(item.Id); serverItem.Name = "changed name"; serverItem.Age = 0; await remoteTable.UpdateAsync(serverItem); Log("Server item updated (changes will be overwritten later"); item.Age = item.Age + 1; item.Name = item.Name + " - modified"; await localTable.UpdateAsync(item); Log("Updated item locally, will now push changes to the server: {0}", item); await offlineReadyClient.SyncContext.PushAsync(); serverItem = await remoteTable.LookupAsync(item.Id); Log("Retrieved the item from the server: {0}", serverItem); if (serverItem.Equals(item)) { Log("Items are the same"); } else { Assert.Fail(string.Format("Items are different. Local: {0}; remote: {1}", item, serverItem)); } Log("Cleaning up"); localTable.DeleteAsync(item).Wait(); Log("Local table cleaned up. Now sync'ing once more"); await offlineReadyClient.SyncContext.PushAsync(); } catch (MobileServicePushFailedException ex) { Log("PushResult status: " + ex.PushResult.Status); throw; } finally { localStore.Dispose(); ClearStore(); } await offlineReadyClient.LogoutAsync(); }
private async Task AuthenticatedTableSyncTest() { bool isUserLoggedIn = false; DateTime now = DateTime.UtcNow; int seed = now.Year * 10000 + now.Month * 100 + now.Day; Log("Using random seed: {0}", seed); Random rndGen = new Random(seed); var offlineReadyClient = CreateClient(); var localStore = new MobileServiceSQLiteStore(StoreFileName); Log("Defined the table on the local store"); localStore.DefineTable <OfflineReadyItemNoVersion>(); await offlineReadyClient.SyncContext.InitializeAsync(localStore); Log("Initialized the store and sync context"); try { var localTable = offlineReadyClient.GetSyncTable <OfflineReadyItemNoVersion>(); var remoteTable = offlineReadyClient.GetTable <OfflineReadyItemNoVersion>(); var item = new OfflineReadyItemNoVersion(rndGen); await localTable.InsertAsync(item); Log("Inserted the item to the local store:", item); try { await offlineReadyClient.SyncContext.PushAsync(); Log("Pushed the changes to the server"); if (isUserLoggedIn) { Log("As expected, push succeeded"); } else { Assert.Fail("Error, table should only work with authenticated access, but user is not logged in"); } } catch (MobileServicePushFailedException ex) { if (isUserLoggedIn) { Assert.Fail(string.Format("Error, user is logged in but push operation failed: {0}", ex)); } Log("Got expected exception: {0}: {1}", ex.GetType().FullName, ex.Message); Exception inner = ex.InnerException; while (inner != null) { Log(" {0}: {1}", inner.GetType().FullName, inner.Message); inner = inner.InnerException; } } if (!isUserLoggedIn) { Log("Push should have failed, so now will try to log in to complete the push operation"); MobileServiceUser user = await Utilities.GetDummyUser(offlineReadyClient); offlineReadyClient.CurrentUser = user; Log("Logged in as {0}", offlineReadyClient.CurrentUser.UserId); await offlineReadyClient.SyncContext.PushAsync(); Log("Push succeeded"); } await localTable.PurgeAsync(); Log("Purged the local table"); await localTable.PullAsync(null, localTable.Where(i => i.Id == item.Id)); Log("Pulled the data into the local table"); List <OfflineReadyItemNoVersion> serverItems = await localTable.ToListAsync(); Log("Retrieved items from the local table"); Log("Removing item from the remote table"); await remoteTable.DeleteAsync(item); if (!isUserLoggedIn) { await offlineReadyClient.LogoutAsync(); Log("Logged out again"); } var firstServerItem = serverItems.FirstOrDefault(); if (item.Equals(firstServerItem)) { Log("Data round-tripped successfully"); } else { Assert.Fail(string.Format("Error, data did not round-trip successfully. Expected: {0}, actual: {1}", item, firstServerItem)); } Log("Cleaning up"); await localTable.PurgeAsync(); Log("Done"); } finally { localStore.Dispose(); ClearStore(); } await offlineReadyClient.LogoutAsync(); }