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

Commit

Permalink
[client][managed][offline] purge should forget the deltatoken
Browse files Browse the repository at this point in the history
  • Loading branch information
hasankhan committed Oct 8, 2014
1 parent 8f8edb8 commit 18f1803
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,15 @@ public virtual async Task<MobileServiceSystemProperties> GetSystemPropertiesAsyn
return (MobileServiceSystemProperties)Int32.Parse(value);
}

public virtual Task ResetDeltaTokenAsync(string tableName, string queryKey)
public virtual async Task ResetDeltaTokenAsync(string tableName, string queryKey)
{
return this.store.DeleteAsync(MobileServiceLocalSystemTables.Config, GetDeltaTokenKey(tableName, queryKey));
string key = GetDeltaTokenKey(tableName, queryKey);

using (await this.cacheLock.Acquire(key, CancellationToken.None))
{
this.cache.Remove(key);
await this.store.DeleteAsync(MobileServiceLocalSystemTables.Config, key);
}
}

public async virtual Task<DateTimeOffset> GetDeltaTokenAsync(string tableName, string queryKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@ public class SyncSettingsManagerTests : TestBase
private const string TestQueryKey = "abc";


[AsyncTestMethod]
public async Task ResetDeltaTokenAsync_ResetsTheToken()
{
MobileServiceSyncSettingsManager settings = await GetSettingManager();

DateTimeOffset saved = new DateTime(2014, 7, 24, 3, 4, 5, DateTimeKind.Local);
await settings.SetDeltaTokenAsync(TestTable, TestQueryKey, saved);

DateTimeOffset read = await settings.GetDeltaTokenAsync(TestTable, TestQueryKey);
Assert.AreEqual(read, saved.ToUniversalTime());

await settings.ResetDeltaTokenAsync(TestTable, TestQueryKey);
read = await settings.GetDeltaTokenAsync(TestTable, TestQueryKey);
Assert.AreEqual(read, new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero).ToUniversalTime());
}

[AsyncTestMethod]
public async Task GetDeltaTokenAsync_ReturnsMinValue_WhenTokenDoesNotExist()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -839,19 +839,47 @@ public async Task PurgeAsync_DoesNotTriggerPush_WhenThereIsNoOperationInTable()
[AsyncTestMethod]
public async Task PurgeAsync_ResetsDeltaToken_WhenQueryKeyIsSpecified()
{
var store = new MobileServiceLocalStoreMock();
store.TableMap["stringId_test_table"] = new Dictionary<string, JObject>();
store.TableMap[MobileServiceLocalSystemTables.Config] = new Dictionary<string, JObject>();
store.TableMap[MobileServiceLocalSystemTables.Config]["stringId_test_table_latestNews_deltaToken"] = new JObject();
var hijack = new TestHttpHandler();
hijack.AddResponseContent(@"[{""id"":""abc"",""String"":""Hey"", ""__updatedAt"": ""2001-02-03T00:00:00.0000000+00:00""},
{""id"":""def"",""String"":""How"", ""__updatedAt"": ""2001-02-04T00:00:00.0000000+00:00""}]"); // first page
hijack.AddResponseContent("[]"); // last page of first pull
hijack.AddResponseContent("[]"); // second pull

IMobileServiceClient service = new MobileServiceClient("http://www.test.com", "secret...", new TestHttpHandler());
var store = new MobileServiceLocalStoreMock();
IMobileServiceClient service = new MobileServiceClient("http://www.test.com", "secret...", hijack);
await service.SyncContext.InitializeAsync(store, new MobileServiceSyncHandler());

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

await table.PurgeAsync("latestNews", table.CreateQuery(), CancellationToken.None);
// ensure there is no delta token present already
Assert.IsFalse(store.TableMap.ContainsKey("stringId_test_table"));

// now pull down data
await table.PullAsync("items", table.CreateQuery());

Assert.IsFalse(store.TableMap[MobileServiceLocalSystemTables.Config].ContainsKey("stringId_test_table_latestNews_deltaToken"));
// ensure items were pulled down
Assert.AreEqual(store.TableMap["stringId_test_table"].Count, 2);
Assert.AreEqual(store.TableMap["stringId_test_table"]["abc"].Value<string>("String"), "Hey");
Assert.AreEqual(store.TableMap["stringId_test_table"]["def"].Value<string>("String"), "How");

// ensure delta token was updated
Assert.Equals(store.TableMap[MobileServiceLocalSystemTables.Config]["stringId_test_table_items_deltaToken"]["value"], "2001-02-04T00:00:00.0000000+00:00");

// now purge and forget the delta token
await table.PurgeAsync("items", null, CancellationToken.None);

// make sure data is purged
Assert.AreEqual(store.TableMap["stringId_test_table"].Count, 0);
// make sure delta token is removed
Assert.IsFalse(store.TableMap[MobileServiceLocalSystemTables.Config].ContainsKey("stringId_test_table_items_deltaToken"));

// pull again
await table.PullAsync("items", table.CreateQuery());

// verify request urls
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",
"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",
"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");
}

[AsyncTestMethod]
Expand Down

0 comments on commit 18f1803

Please sign in to comment.