Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix failing sync tests #3096

Merged
merged 32 commits into from
Jan 17, 2023
Merged

Fix failing sync tests #3096

merged 32 commits into from
Jan 17, 2023

Conversation

nirinchev
Copy link
Member

@nirinchev nirinchev commented Nov 16, 2022

Description

Try and diagnose the reason for tests timing out.

Fixes #3063

TODO

  • Investigate WriteCopy_CanSynchronizeData - it appears like there's conflict resolution happening there when it shouldn't. Run in isolation and inspect server logs.

@nirinchev nirinchev changed the title Run tests for iOS and Linux; increase timeout Investigate failing sync tests Nov 17, 2022
@nirinchev nirinchev added the no-changelog Used to skip the changelog check label Nov 23, 2022
if (SharedRealmHandle.OwnsNativeRealm)
{
_state.RemoveRealm(this);
}
_state.RemoveRealm(this, closeOnEmpty: SharedRealmHandle.OwnsNativeRealm);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes the behavior to always remove the Realm from the state, even when it's unowned. The reason is that otherwise we ended up not closing the Realm in certain situations. Example:

var config = new RealmConfiguration
{
    PopulateInitialData = (r) =>
    {
		// (1)
    }
}
using var realm = Realm.GetInstance(config);

// (2)

At point (1), we create an unowned Realm instance - r. When created, it will create a RealmState and set it to the native instance. At point (2), we get a regular Realm instance, that points to the same native Realm as r. This means that the state now references two Realms - r and realm. When we dispose realm, we remove it from the state, but we don't close the native realm because we see that the _weakRealms collection is not empty.

With this change, we remove unowned realms from the state, but don't explicitly call close on the native Realm, because we don't own it (yet). When an owned instance is created for the native Realm, we're free to close it, because there won't be unowned managed instances that keep it around.

@@ -146,11 +146,25 @@ protected Realm GetRealm(string path)
return result;
}

protected async Task<Realm> GetRealmAsync(RealmConfigurationBase config, CancellationToken cancellationToken = default)
protected async Task<Realm> GetRealmAsync(RealmConfigurationBase config, int timeout = 10000, CancellationToken? cancellationToken = default)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is aimed at making sure we do proper cleanup on timeout. Previously we were leaking sessions for Realms we couldn't open due to a timeout. E.g.:

var r = await GetRealmAsync(config).Timeout(1000);

If we timeout here, we wouldn't get a Realm reference that we could enqueue for cleanup, so when the test fails, we won't close the Realm/terminate the session. With this change, we're using the built-in cancellation mechanics to stop the download.

@@ -102,6 +102,8 @@ void HandleSessionError(object _, ErrorEventArgs errorArgs)
{
TestHelpers.RunAsyncTest(testFunc, timeout);
}

Task.Delay(1000).Wait();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is working around realm/realm-core#6052

# Conflicts:
#	Tests/Realm.Tests/Generated/Realm.SourceGenerator/Realms.SourceGenerator.RealmGenerator/ObjectWithPartitionValue_generated.cs
#	Tests/Realm.Tests/Sync/SyncTestBase.cs
@nirinchev nirinchev marked this pull request as ready for review January 12, 2023 14:56
@nirinchev nirinchev requested review from LaPeste and papafe January 12, 2023 14:56
Copy link
Contributor

@LaPeste LaPeste left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just have a few questions here and there to get some clarifications. Otherwise, all seems good.
It must have taken a lot of time to figure out all those, more or less, sporadic issues. Thank you for having taken the time.

As a side note, the comments that you left behind were very useful to quickly have context and understand the changes.

@@ -269,6 +269,10 @@ public async Task<SubscriptionSetState> WaitForStateChangeAsync()

return await tcs.Task;
}
catch (Exception ex) when (ex.Message == "Active SubscriptionSet without a SubscriptionStore")
{
throw new TaskCanceledException("The SubscriptionSet was closed before the wait could complete. This is likely because the Realm it belongs to was disposed.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would make sense to move this message to core?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is somewhat c#-specific message, so I don't think the Core team would like to adopt it.


Logger.Default = Logger.Function(sw.WriteLine);
var logger = new Logger.InMemoryLogger();
Logger.Default = logger;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to reset the original logger?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is done automatically in RealmTest.TearDown

@nirinchev nirinchev changed the title Investigate failing sync tests Fix failing sync tests Jan 17, 2023
@nirinchev nirinchev merged commit 6157936 into main Jan 17, 2023
@nirinchev nirinchev deleted the ni/sync-tests branch January 17, 2023 18:38
papafe added a commit that referenced this pull request Jan 27, 2023
* main:
  Survey ad (#3201)
  Workaround Mono yield bug (#3200)
  Bump docker layer caching action to use node16 (#3193)
  Keypath filtering (#3179)
  Move realm files created by tests to current directory on CI (#2991)
  Fix failing sync tests (#3096)
  Updated example project to MAUI and Source Generators (#3168)
  Fix uwp workflow when running in debug (#3177)
  Deprecate push client (#3176)
  Don't use reflection in DynamicRealmObjectHelper.TryGetPrimaryKeyValue (#3175)
  Keep track of the active transaction and close it on realm close (#3164)
  Add more detailed unsupported type error (#3163)
  Build Xamarin.Android tests in all ABIs (#3165)

# Conflicts:
#	Tests/Realm.Tests/Generated/Realm.SourceGenerator/Realms.SourceGenerator.RealmGenerator/ObjectWithPartitionValue_generated.cs
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla: yes no-changelog Used to skip the changelog check
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CI tasks
3 participants