Skip to content
This repository was archived by the owner on Feb 29, 2020. It is now read-only.

Commit 18f1803

Browse files
committed
[client][managed][offline] purge should forget the deltatoken
1 parent 8f8edb8 commit 18f1803

File tree

3 files changed

+59
-9
lines changed

3 files changed

+59
-9
lines changed

sdk/Managed/src/Microsoft.WindowsAzure.MobileServices/Table/Sync/MobileServiceSyncSettingsManager.cs

+8-2
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,15 @@ public virtual async Task<MobileServiceSystemProperties> GetSystemPropertiesAsyn
3737
return (MobileServiceSystemProperties)Int32.Parse(value);
3838
}
3939

40-
public virtual Task ResetDeltaTokenAsync(string tableName, string queryKey)
40+
public virtual async Task ResetDeltaTokenAsync(string tableName, string queryKey)
4141
{
42-
return this.store.DeleteAsync(MobileServiceLocalSystemTables.Config, GetDeltaTokenKey(tableName, queryKey));
42+
string key = GetDeltaTokenKey(tableName, queryKey);
43+
44+
using (await this.cacheLock.Acquire(key, CancellationToken.None))
45+
{
46+
this.cache.Remove(key);
47+
await this.store.DeleteAsync(MobileServiceLocalSystemTables.Config, key);
48+
}
4349
}
4450

4551
public async virtual Task<DateTimeOffset> GetDeltaTokenAsync(string tableName, string queryKey)

sdk/Managed/test/Microsoft.WindowsAzure.MobileServices.SQLiteStore.Test/UnitTests/SyncSettingsManagerTests.Integration.cs

+16
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,22 @@ public class SyncSettingsManagerTests : TestBase
1212
private const string TestQueryKey = "abc";
1313

1414

15+
[AsyncTestMethod]
16+
public async Task ResetDeltaTokenAsync_ResetsTheToken()
17+
{
18+
MobileServiceSyncSettingsManager settings = await GetSettingManager();
19+
20+
DateTimeOffset saved = new DateTime(2014, 7, 24, 3, 4, 5, DateTimeKind.Local);
21+
await settings.SetDeltaTokenAsync(TestTable, TestQueryKey, saved);
22+
23+
DateTimeOffset read = await settings.GetDeltaTokenAsync(TestTable, TestQueryKey);
24+
Assert.AreEqual(read, saved.ToUniversalTime());
25+
26+
await settings.ResetDeltaTokenAsync(TestTable, TestQueryKey);
27+
read = await settings.GetDeltaTokenAsync(TestTable, TestQueryKey);
28+
Assert.AreEqual(read, new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero).ToUniversalTime());
29+
}
30+
1531
[AsyncTestMethod]
1632
public async Task GetDeltaTokenAsync_ReturnsMinValue_WhenTokenDoesNotExist()
1733
{

sdk/Managed/test/Microsoft.WindowsAzure.MobileServices.Test/UnitTests/Table/Sync/MobileServiceSyncTable.Generic.Test.cs

+35-7
Original file line numberDiff line numberDiff line change
@@ -839,19 +839,47 @@ public async Task PurgeAsync_DoesNotTriggerPush_WhenThereIsNoOperationInTable()
839839
[AsyncTestMethod]
840840
public async Task PurgeAsync_ResetsDeltaToken_WhenQueryKeyIsSpecified()
841841
{
842-
var store = new MobileServiceLocalStoreMock();
843-
store.TableMap["stringId_test_table"] = new Dictionary<string, JObject>();
844-
store.TableMap[MobileServiceLocalSystemTables.Config] = new Dictionary<string, JObject>();
845-
store.TableMap[MobileServiceLocalSystemTables.Config]["stringId_test_table_latestNews_deltaToken"] = new JObject();
842+
var hijack = new TestHttpHandler();
843+
hijack.AddResponseContent(@"[{""id"":""abc"",""String"":""Hey"", ""__updatedAt"": ""2001-02-03T00:00:00.0000000+00:00""},
844+
{""id"":""def"",""String"":""How"", ""__updatedAt"": ""2001-02-04T00:00:00.0000000+00:00""}]"); // first page
845+
hijack.AddResponseContent("[]"); // last page of first pull
846+
hijack.AddResponseContent("[]"); // second pull
846847

847-
IMobileServiceClient service = new MobileServiceClient("http://www.test.com", "secret...", new TestHttpHandler());
848+
var store = new MobileServiceLocalStoreMock();
849+
IMobileServiceClient service = new MobileServiceClient("http://www.test.com", "secret...", hijack);
848850
await service.SyncContext.InitializeAsync(store, new MobileServiceSyncHandler());
849851

850852
IMobileServiceSyncTable<ToDoWithStringId> table = service.GetSyncTable<ToDoWithStringId>();
851853

852-
await table.PurgeAsync("latestNews", table.CreateQuery(), CancellationToken.None);
854+
// ensure there is no delta token present already
855+
Assert.IsFalse(store.TableMap.ContainsKey("stringId_test_table"));
856+
857+
// now pull down data
858+
await table.PullAsync("items", table.CreateQuery());
853859

854-
Assert.IsFalse(store.TableMap[MobileServiceLocalSystemTables.Config].ContainsKey("stringId_test_table_latestNews_deltaToken"));
860+
// ensure items were pulled down
861+
Assert.AreEqual(store.TableMap["stringId_test_table"].Count, 2);
862+
Assert.AreEqual(store.TableMap["stringId_test_table"]["abc"].Value<string>("String"), "Hey");
863+
Assert.AreEqual(store.TableMap["stringId_test_table"]["def"].Value<string>("String"), "How");
864+
865+
// ensure delta token was updated
866+
Assert.Equals(store.TableMap[MobileServiceLocalSystemTables.Config]["stringId_test_table_items_deltaToken"]["value"], "2001-02-04T00:00:00.0000000+00:00");
867+
868+
// now purge and forget the delta token
869+
await table.PurgeAsync("items", null, CancellationToken.None);
870+
871+
// make sure data is purged
872+
Assert.AreEqual(store.TableMap["stringId_test_table"].Count, 0);
873+
// make sure delta token is removed
874+
Assert.IsFalse(store.TableMap[MobileServiceLocalSystemTables.Config].ContainsKey("stringId_test_table_items_deltaToken"));
875+
876+
// pull again
877+
await table.PullAsync("items", table.CreateQuery());
878+
879+
// verify request urls
880+
AssertEx.MatchUris(hijack.Requests, "http://www.test.com/tables/stringId_test_table?$filter=(__updatedAt ge datetimeoffset'1970-01-01T00:00:00.0000000%2B00:00')&$orderby=__updatedAt&$skip=0&$top=50&__includeDeleted=true&__systemproperties=__updatedAt%2C__version%2C__deleted",
881+
"http://www.test.com/tables/stringId_test_table?$filter=(__updatedAt ge datetimeoffset'2001-02-04T00:00:00.0000000%2B00:00')&$orderby=__updatedAt&$skip=0&$top=50&__includeDeleted=true&__systemproperties=__updatedAt%2C__version%2C__deleted",
882+
"http://www.test.com/tables/stringId_test_table?$filter=(__updatedAt ge datetimeoffset'1970-01-01T00:00:00.0000000%2B00:00')&$orderby=__updatedAt&$skip=0&$top=50&__includeDeleted=true&__systemproperties=__updatedAt%2C__version%2C__deleted");
855883
}
856884

857885
[AsyncTestMethod]

0 commit comments

Comments
 (0)