Skip to content

Commit

Permalink
(#907) test updates to ensure pendingOperations is updated correctly (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianhall committed Mar 18, 2024
1 parent f3b2be6 commit 102c88b
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 30 deletions.
Expand Up @@ -32,10 +32,10 @@ public SyncContext_Tests()
client = GetMockClient();
store = new MockOfflineStore();

client.SynchronizationProgress += (sender, args) =>
client.SynchronizationProgress += (_, args) =>
{
// Some tests are in parallel, so we have to be careful not to modify concurrently
lock(eventLock) { events.Add(args); }
lock (eventLock) { events.Add(args); }
};

testObject = new IdEntity { Id = Guid.NewGuid().ToString("N"), StringValue = "testValue" };
Expand Down Expand Up @@ -126,9 +126,9 @@ private void AssertEventsRecorded(int count, int qlen)
{
// push started, push finished, plus 2 events (before/after) for each count.
Assert.Equal((count * 2) + 2, events.Count);
Assert.Equal(SynchronizationEventType.PushStarted, events.First().EventType);
Assert.Equal(SynchronizationEventType.PushStarted, events[0].EventType);
Assert.Equal(SynchronizationEventType.PushFinished, events.Last().EventType);
Assert.Equal(qlen, events.First().QueueLength);
Assert.Equal(qlen, events[0].QueueLength);
}

private static void AssertItemWillBePushed(SynchronizationEventArgs args, string table, string id)
Expand Down Expand Up @@ -639,8 +639,8 @@ public async Task PullItemsAsync_ProducesCorrectQuery_WithoutUpdatedAt()

// Events were sent properly
Assert.Equal(22, events.Count); // 2 for each event, plus start and finish.
Assert.Equal(SynchronizationEventType.PullStarted, events.First().EventType);
Assert.Equal("movies", events.First().TableName);
Assert.Equal(SynchronizationEventType.PullStarted, events[0].EventType);
Assert.Equal("movies", events[0].TableName);
Assert.Equal(SynchronizationEventType.PullFinished, events.Last().EventType);
Assert.Equal("movies", events.Last().TableName);

Expand Down Expand Up @@ -749,10 +749,7 @@ public async Task PullItemsAsync_CallsPush_WhenDirty()
// We don't "consume" the operation, so PullItemsAsync() will throw an invalid operation because the table is dirty the second time.
await Assert.ThrowsAsync<DatasyncInvalidOperationException>(() => context.PullItemsAsync("movies", "", new PullOptions()));

Received.InOrder(async () =>
{
await pushContext.Received(1).PushItemsAsync(Arg.Is<string[]>(t => t.Length == 1 && t[0] == "movies"), default);
});
Received.InOrder(async () => await pushContext.Received(1).PushItemsAsync(Arg.Is<string[]>(t => t.Length == 1 && t[0] == "movies"), default));
}

[Fact]
Expand All @@ -774,10 +771,7 @@ public async Task PullItemsAsync_CallsPush_WhenDirty_WithPushOtherTables()
var options = new PullOptions { PushOtherTables = true };
await Assert.ThrowsAsync<DatasyncInvalidOperationException>(() => context.PullItemsAsync("movies", "", options));

Received.InOrder(async () =>
{
await pushContext.Received(1).PushItemsAsync(default, default);
});
Received.InOrder(async () => await pushContext.Received(1).PushItemsAsync(default, default));
}

[Fact]
Expand Down Expand Up @@ -1294,7 +1288,7 @@ public async Task PushItemsAsync_SingleTable_HandlesDeleteOperation_MultiOperati
{
var context = await GetSyncContext();
var options = new PushOptions { ParallelOperations = nThreads };
int nItems = 10;
const int nItems = 10;

// Create a list of ten movies
List<ClientMovie> movies = new();
Expand All @@ -1320,9 +1314,9 @@ public async Task PushItemsAsync_SingleTable_HandlesDeleteOperation_MultiOperati
Assert.Equal(nItems, MockHandler.Requests.Count);
// PushStarted, PushFinished, plus nItems delete operations
Assert.Equal((nItems * 2) + 2, events.Count);
Assert.Equal(SynchronizationEventType.PushStarted, events.First().EventType);
Assert.Equal(SynchronizationEventType.PushStarted, events[0].EventType);
Assert.Equal(SynchronizationEventType.PushFinished, events.Last().EventType);
Assert.Equal(nItems, events.First().QueueLength);
Assert.Equal(nItems, events[0].QueueLength);

foreach (var movie in movies)
{
Expand Down
Expand Up @@ -1143,16 +1143,16 @@ public async Task ToAsyncEnumerable_Linq_where_100()
);
}

[Fact(Skip = "Needs OData parser updates in SqliteStore to work")]
public async Task ToAsyncEnumerable_Linq_where_101()
{
string[] ratings = new string[] { "G", "PG" };
await RunLinqTest(
m => m.Where(x => ratings.Contains(x.Rating)),
50,
new[] { "id-010", "id-015", "id-024", "id-026", "id-027" }
);
}
//[Fact]
//public async Task ToAsyncEnumerable_Linq_where_101()
//{
// string[] ratings = new string[] { "G", "PG" };
// await RunLinqTest(
// m => m.Where(x => ratings.Contains(x.Rating)),
// 50,
// new[] { "id-010", "id-015", "id-024", "id-026", "id-027" }
// );
//}

[Fact]
public async Task ToAsyncEnumerable_Linq_where_120()
Expand Down
Expand Up @@ -26,6 +26,7 @@ public new async Task InitializeAsync(bool pullItems = true)

public new TestServer MovieServer { get => base.MovieServer; }

[SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "Cannot override a non-static with a static")]
public new int MovieCount { get => BaseTest.MovieCount; }
}

Expand Down
Expand Up @@ -15,7 +15,9 @@ public abstract class BaseOperationTest : BaseTest, IDisposable
protected readonly OfflineSQLiteStore store;
protected readonly DatasyncClient client;
protected IOfflineTable<KitchenSinkDto>? offlineTable;
protected IOfflineTable<ClientMovie>? offlineMovieTable;
protected IRemoteTable<KitchenSinkDto> remoteTable;
protected IRemoteTable<ClientMovie>? remoteMovieTable;
protected ILogger<OfflineSQLiteStore> storeLoggerMock;

protected BaseOperationTest(ITestOutputHelper logger, bool useFile = true)
Expand All @@ -35,19 +37,23 @@ protected BaseOperationTest(ITestOutputHelper logger, bool useFile = true)
storeLoggerMock.Log(Arg.Any<LogLevel>(), Arg.Any<EventId>(), Arg.Any<object>(), Arg.Any<Exception>(), Arg.Any<Func<object, Exception?, string>>());
store = new OfflineSQLiteStore(connectionString, storeLoggerMock);
store.DefineTable<KitchenSinkDto>("kitchensink");
store.DefineTable<ClientMovie>("movies");
client = GetMovieClient(store: store);
remoteTable = client.GetRemoteTable<KitchenSinkDto>("kitchensink");
remoteMovieTable = client.GetRemoteTable<ClientMovie>("movies");
}

protected async Task InitializeAsync(bool pullItems = true)
{
await client.InitializeOfflineStoreAsync();

offlineTable = client.GetOfflineTable<KitchenSinkDto>("kitchensink");
offlineMovieTable = client.GetOfflineTable<ClientMovie>("movies");

if (pullItems)
{
await offlineTable.PullItemsAsync();
await offlineMovieTable.PullItemsAsync();
}
}

Expand Down
@@ -1,8 +1,6 @@
// Copyright (c) Microsoft Corporation. All Rights Reserved.
// Licensed under the MIT License.

using Microsoft.Extensions.Logging;
using NSubstitute;
using Movies = Datasync.Common.Test.TestData.Movies;

namespace Microsoft.Datasync.Integration.Test.KitchenSink;
Expand All @@ -15,7 +13,7 @@ namespace Microsoft.Datasync.Integration.Test.KitchenSink;
[ExcludeFromCodeCoverage]
public class KitchenSink_Tests : BaseOperationTest
{
public KitchenSink_Tests(ITestOutputHelper logger) : base(logger)
public KitchenSink_Tests(ITestOutputHelper logger) : base(logger, useFile: true)
{
}

Expand Down Expand Up @@ -170,4 +168,41 @@ public async Task KS6_SpecialCharacterOfflineWhere(string searchTarget)
Assert.Single(items);
Assert.Equal(searchTarget, items[0].StringValue);
}

[Fact]
public async Task KS7_EndToEndSyncTest()
{
const int insertedItems = 5;

await InitializeAsync(true);

Assert.Equal(0, client.PendingOperations);

// Add a couple of new items, and ensure pending operations are updated.
for (int i = 0; i < insertedItems; i++)
{
var insertion = new KitchenSinkDto { StringValue = $"Item {i}" };
await offlineTable!.InsertItemAsync(insertion);
Assert.Equal(i + 1, client.PendingOperations);
}

// Also add a movie, in case there is a multi-table issue
var movie = GetSampleMovie<ClientMovie>();
await offlineMovieTable!.InsertItemAsync(movie);
Assert.Equal(insertedItems + 1, client.PendingOperations);

await offlineTable!.PushItemsAsync();
Assert.Equal(1, client.PendingOperations);
await offlineTable!.PullItemsAsync();
Assert.Equal(1, client.PendingOperations);

await offlineMovieTable!.PushItemsAsync();
Assert.Equal(0, client.PendingOperations);
await offlineMovieTable!.PullItemsAsync();
Assert.Equal(0, client.PendingOperations);

Assert.Equal(0, client.PendingOperations);
var nItems = await offlineTable!.CountItemsAsync();
Assert.Equal(insertedItems, nItems);
}
}

0 comments on commit 102c88b

Please sign in to comment.