private async Task AbortPushDuringSync(SyncAbortLocation whereToAbort) { ClearStore(); SyncAbortLocation abortLocation = whereToAbort; DateTime now = DateTime.UtcNow; int seed = now.Year * 10000 + now.Month * 100 + now.Day; Random rndGen = new Random(seed); var offlineReadyClient = CreateClient(); var items = Enumerable.Range(0, 10).Select(_ => new OfflineReadyItem(rndGen)).ToArray(); foreach (var item in items) { item.Id = Guid.NewGuid().ToString("D"); } int abortIndex = abortLocation == SyncAbortLocation.Start ? 0 : (abortLocation == SyncAbortLocation.End ? items.Length - 1 : rndGen.Next(1, items.Length - 1)); var idToAbort = items[abortIndex].Id; var localStore = new MobileServiceSQLiteStore(StoreFileName); localStore.DefineTable <OfflineReadyItem>(); var syncHandler = new AbortingSyncHandler(this, id => id == idToAbort); await offlineReadyClient.SyncContext.InitializeAsync(localStore, syncHandler); var localTable = offlineReadyClient.GetSyncTable <OfflineReadyItem>(); var remoteTable = offlineReadyClient.GetTable <OfflineReadyItem>(); try { foreach (var item in items) { await localTable.InsertAsync(item); } await Assert.ThrowsAsync <MobileServicePushFailedException>(() => offlineReadyClient.SyncContext.PushAsync()); var expectedOperationQueueSize = items.Length - abortIndex; Assert.False(expectedOperationQueueSize != offlineReadyClient.SyncContext.PendingOperations); foreach (var allItemsPushed in new bool[] { false, true }) { HashSet <OfflineReadyItem> itemsInServer, itemsNotInServer; if (allItemsPushed) { itemsInServer = new HashSet <OfflineReadyItem>(items.ToArray()); itemsNotInServer = new HashSet <OfflineReadyItem>(Enumerable.Empty <OfflineReadyItem>()); } else { itemsInServer = new HashSet <OfflineReadyItem>(items.Where((item, index) => index < abortIndex)); itemsNotInServer = new HashSet <OfflineReadyItem>(items.Where((item, index) => index >= abortIndex)); } foreach (var item in items) { var itemFromServer = (await remoteTable.Where(i => i.Id == item.Id).Take(1).ToEnumerableAsync()).FirstOrDefault(); Assert.False(itemsInServer.Contains(item) && itemFromServer == null); Assert.False(itemsNotInServer.Contains(item) && itemFromServer != null); } if (!allItemsPushed) { syncHandler.AbortCondition = _ => false; await offlineReadyClient.SyncContext.PushAsync(); } } syncHandler.AbortCondition = _ => false; foreach (var item in items) { await localTable.DeleteAsync(item); } await offlineReadyClient.SyncContext.PushAsync(); } finally { localStore.Dispose(); ClearStore(); } }