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

Refactor async tests #2813

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open

Refactor async tests #2813

wants to merge 26 commits into from

Conversation

fealebenpae
Copy link
Member

@fealebenpae fealebenpae commented Feb 13, 2022

This is a (noisy) refactor of our unit tests that extracts the logic behind TestHelpers.RunAsyncTest and SyncTestHelpers.RunBaasTestAsync into two custom NUnit attributes - [RequiresSynchronizationContext] and [RequiresBaas]. This allows us to switch to using awaitable test methods instead of wrapping async code in TestHelpers.RunAsyncTest() calls.

I originally intended to just replace SyncTestHelpers.RunBaasTestAsync calls with the [RequiresBaas] attribute and remove TestHelpers.RunAsyncTest in favor of making the test methods async themselves, but it turned out that NUnit doesn't automatically install a SynchronizationContext and the only way to tell it to do so is to mark your tests as needing a single-threaded apartment, which also causes NUnit to try to run tests inside a STA thread which .NET only supports on Windows. As a workaround I ended up creating [RequiresSynchronizationContext], made it inheritable, and slapped it on our base fixture class RealmTest, so while it exists, there shouldn't be a need to use it in test code if the test class inherits from RealmTest.

For all intents and purposes now it's enough for a test method to return Task (or another awaitable type) to be considered async, and to decorate any baas integration tests with [RequiresBaas].

Copy link
Member

@nirinchev nirinchev left a comment

Choose a reason for hiding this comment

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

Good stuff - I like this a lot better than the current approach.

Tests/Realm.Tests/Helpers/NUnitAsyncTests.cs Outdated Show resolved Hide resolved
Tests/Realm.Tests/Sync/FlexibleSyncTests.cs Outdated Show resolved Hide resolved
Tests/Realm.Tests/Sync/SyncTestBase.cs Outdated Show resolved Hide resolved
@fealebenpae fealebenpae marked this pull request as ready for review February 15, 2022 00:28
Copy link
Contributor

@papafe papafe left a comment

Choose a reason for hiding this comment

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

Looks quite cool!

* and run the posted work so we don't install a SynchronizationContext for them.
*/

if (SynchronizationContext.Current is SynchronizationContext current)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why don't use the synchronization context that's already there if it exists?

Copy link
Contributor

@DominicFrei DominicFrei left a comment

Choose a reason for hiding this comment

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

Nice one. Big fan of simplifications! 👍

@fealebenpae fealebenpae force-pushed the yg/nunit-refactoring branch 4 times, most recently from 8c4a471 to 9ad2be5 Compare February 15, 2022 20:00
}

var @delegate = new EventHandler<ErrorEventArgs>(ErrorHandler);
Realms.Sync.Session.Error += @delegate;
Copy link
Contributor

@LaPeste LaPeste Feb 16, 2022

Choose a reason for hiding this comment

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

As we discussed already, for this and all other usage we'll need to re-adjust this once the client reset API PR gets merged as the Session.Error is deprecated from there on

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.

Really good work. It's gonna make our tests so much prettier in style and faster to read and write.

@coveralls
Copy link

coveralls commented Feb 22, 2022

Pull Request Test Coverage Report for Build 2016072633

  • 8 of 9 (88.89%) changed or added relevant lines in 1 file are covered.
  • 6 unchanged lines in 3 files lost coverage.
  • Overall coverage increased (+0.2%) to 83.201%

Changes Missing Coverage Covered Lines Changed/Added Lines %
Realm/Realm/Native/SynchronizationContextScheduler.cs 8 9 88.89%
Files with Coverage Reduction New Missed Lines %
Realm/Realm/Extensions/TaskExtensions.cs 1 81.25%
Realm/Realm/Exceptions/RealmException.cs 2 39.45%
Realm/Realm/Handles/SessionHandle.cs 3 85.06%
Totals Coverage Status
Change from base Build 2001531696: 0.2%
Covered Lines: 5564
Relevant Lines: 6572

💛 - Coveralls

nirinchev and others added 9 commits February 28, 2022 16:17
* main:
  Update CHANGELOG.md
  Prepare for vNext (#2828)
  Prepare for 10.10.0 (#2827)
  Revert workflow to use realm-dev
  Make the nuget package target the osx universal runtime (#2825)
  Change UUID encoding (#2784)
  Remove some scheduler workarounds (#2823)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants