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

Clear test state flag #5689

Merged
merged 10 commits into from
Apr 5, 2023
Merged

Clear test state flag #5689

merged 10 commits into from
Apr 5, 2023

Conversation

kraenhansen
Copy link
Member

@kraenhansen kraenhansen commented Apr 3, 2023

What, How & Why?

Instead of introducing a memory leak when running in production, this PR introduce a flag set from the tests to signal the SDK that it's allowed to store WeakRef instances into Sets without worrying about the memory leak, which would have happened if clearTestState was never called.

This also removes the IterableWeakRefs, as we no longer need something mimicking a WeakSet.

This also introduce the reset of SyncSession shared pointers and canceling unresolved async open tasks when clearing test state.

@kraenhansen kraenhansen force-pushed the kh/clean-test-state-mode branch from 49124e2 to a42c967 Compare April 3, 2023 17:27
@kraenhansen kraenhansen changed the title Clean test state mode Clean test state flag Apr 3, 2023
Copy link
Contributor

@elle-j elle-j left a comment

Choose a reason for hiding this comment

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

Nice addition!

Comment on lines 25 to 26
// We need this to call Realm.cleanTestState()
flags.CLEAN_TEST_STATE = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

Typo? CLEAR_ would align with the method name (rather than CLEAN_) 👍

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh my. Thanks.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've updated this and hopefully all other occurrences 🤞

Comment on lines 69 to 72
const promise = promiseRef.deref();
if (promise) {
promise.cancel();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Alternative

Suggested change
const promise = promiseRef.deref();
if (promise) {
promise.cancel();
}
promiseRef.deref()?.cancel();

@@ -262,6 +263,23 @@ const CONNECTION_LISTENERS = new Listeners<ConnectionNotificationCallback, Liste
});

export class SyncSession {
private static instances = new Set<binding.WeakRef<SyncSession>>();
Copy link
Contributor

Choose a reason for hiding this comment

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

We have the @internal on the instances on ProgressRealmPromise. Did we decide which one we wanted to converge on?

Copy link
Contributor

Choose a reason for hiding this comment

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

FWIW, there is less (but still not 0) benefit from keeping the private static members in the .d.ts vs the private instance members.

Comment on lines 275 to 278
const session = sessionRef.deref();
if (session) {
session.resetInternal();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Alternative

Suggested change
const session = sessionRef.deref();
if (session) {
session.resetInternal();
}
sessionRef.deref()?.resetInternal();

@@ -26,4 +26,9 @@ export const flags = {
* Helps finding places where the app is depending on the now deprecated way of using the package.
*/
THROW_ON_GLOBAL_REALM: false,
/**
* When enabled, calling `Realm.cleanTestState` will be callable.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* When enabled, calling `Realm.cleanTestState` will be callable.
* When enabled, calling `Realm.clearTestState` will be callable.

@@ -22,6 +22,8 @@ import { flags } from "realm";

// TODO: Refactor tests to disable this
flags.ALLOW_VALUES_ARRAYS = true;
// We need this to call Realm.cleanTestState()
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// We need this to call Realm.cleanTestState()
// We need this to call Realm.clearTestState()

kraenhansen added a commit that referenced this pull request Apr 3, 2023
quashed commit of the following:

commit 30c6c82
Author: Kræn Hansen <kraen.hansen@mongodb.com>
Date:   Mon Apr 3 19:33:08 2023 +0200

    Fixed missing WeakRef wrapping

commit a42c967
Author: Kræn Hansen <kraen.hansen@mongodb.com>
Date:   Mon Apr 3 19:27:35 2023 +0200

    Canceling async tasks on clear test state

commit 1022fba
Author: Kræn Hansen <kraen.hansen@mongodb.com>
Date:   Mon Apr 3 19:21:46 2023 +0200

    Capturing and cleaning WeakRef of SyncSessions

commit 8befc51
Author: Kræn Hansen <kraen.hansen@mongodb.com>
Date:   Mon Apr 3 18:47:15 2023 +0200

    Deleting the IterableWeakRefs

commit 4645004
Author: Kræn Hansen <kraen.hansen@mongodb.com>
Date:   Mon Apr 3 18:45:55 2023 +0200

    Introduce a CLEAN_TEST_STATE flag
kraenhansen added a commit that referenced this pull request Apr 3, 2023
quashed commit of the following:

commit 30c6c82
Author: Kræn Hansen <kraen.hansen@mongodb.com>
Date:   Mon Apr 3 19:33:08 2023 +0200

    Fixed missing WeakRef wrapping

commit a42c967
Author: Kræn Hansen <kraen.hansen@mongodb.com>
Date:   Mon Apr 3 19:27:35 2023 +0200

    Canceling async tasks on clear test state

commit 1022fba
Author: Kræn Hansen <kraen.hansen@mongodb.com>
Date:   Mon Apr 3 19:21:46 2023 +0200

    Capturing and cleaning WeakRef of SyncSessions

commit 8befc51
Author: Kræn Hansen <kraen.hansen@mongodb.com>
Date:   Mon Apr 3 18:47:15 2023 +0200

    Deleting the IterableWeakRefs

commit 4645004
Author: Kræn Hansen <kraen.hansen@mongodb.com>
Date:   Mon Apr 3 18:45:55 2023 +0200

    Introduce a CLEAN_TEST_STATE flag
@kraenhansen kraenhansen changed the title Clean test state flag Clear test state flag Apr 4, 2023
// We need this to call Realm.cleanTestState()
flags.CLEAN_TEST_STATE = true;
// We need this to call Realm.clearTestState()
flags.CLEAR_TEST_STATE = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
flags.CLEAR_TEST_STATE = true;
flags.ENABLE_CLEAR_TEST_STATE = true;

Or ALLOW. The point is that setting the flag doesn't by itself clear the test state, it mearly enables (or allows) the test state to be cleared.

Also, reading this now, I think clearTestState() is slightly misnamed. Really it should be something like clearStateAfterTest() because it doesn't clear the test state, it tries to reset the general state of the world. But I don't suggest changing that right now.

Copy link
Member Author

@kraenhansen kraenhansen Apr 5, 2023

Choose a reason for hiding this comment

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

I opted for ALLOW_CLEAR_TEST_STATE to align with the other existing flag.

And I agree, the function name is confusing. It's internal and can easily be renamed in another PR 👍

* Cancels all unresolved `ProgressRealmPromise` instances.
* @internal
*/
public static cancelAll() {
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems weird to have a method in the middle of your instance variable declarations. Move this down?

Copy link
Member Author

Choose a reason for hiding this comment

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

I consider it "normal" to have all static members and methods before any instance methods and properties.

Copy link
Contributor

Choose a reason for hiding this comment

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

Fair enough. Could you at least separate the sections with a blank line then?

@@ -262,6 +263,23 @@ const CONNECTION_LISTENERS = new Listeners<ConnectionNotificationCallback, Liste
});

export class SyncSession {
private static instances = new Set<binding.WeakRef<SyncSession>>();
Copy link
Contributor

Choose a reason for hiding this comment

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

FWIW, there is less (but still not 0) benefit from keeping the private static members in the .d.ts vs the private instance members.

@@ -26,4 +26,9 @@ export const flags = {
* Helps finding places where the app is depending on the now deprecated way of using the package.
*/
THROW_ON_GLOBAL_REALM: false,
/**
* When enabled, calling `Realm.clearTestState` will be callable.
* This is disabled by default, mainly because the data-structures needed to support this, introduce minor memory leaks and are not intended for production use.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* This is disabled by default, mainly because the data-structures needed to support this, introduce minor memory leaks and are not intended for production use.
* This is disabled by default, mainly because the data-structures needed to support this, introduce minor memory leaks if clearTestState() is not called regularly and are not intended for production use.

Copy link
Member Author

Choose a reason for hiding this comment

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

I added this.

@@ -26,4 +26,9 @@ export const flags = {
* Helps finding places where the app is depending on the now deprecated way of using the package.
*/
THROW_ON_GLOBAL_REALM: false,
/**
* When enabled, calling `Realm.clearTestState` will be callable.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* When enabled, calling `Realm.clearTestState` will be callable.
* Enables calling internal, test-only functions like `Realm.clearTestState`.
  • better to use active rather than passive voice where possible.
  • the effects of this flag are already not limited to that function, so we might as well not lie.

Copy link
Member Author

Choose a reason for hiding this comment

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

I added this.

@kraenhansen kraenhansen merged commit 45dd275 into main Apr 5, 2023
@kraenhansen kraenhansen deleted the kh/clean-test-state-mode branch April 5, 2023 15:35
papafe added a commit that referenced this pull request Apr 13, 2023
* main: (41 commits)
  allow useQuery to filter or sort a collection by using a callback (#5513)
  Fix `realm.create` types and 'requiredProperties' on Realm.Object constructor (#5697)
  Fix return type of `App.allUsers` (#5708)
  Removed renamed file
  Remove console.log
  Fix README instructions and bump version
  Bump template versions
  Update Templates (#5702)
  Moved "submodules" wireit task to "postinstall"
  Set up typedoc for realm-react, realm-web (#5709)
  Prepare for vNext (#5711)
  Prepare for 12.0.0-alpha.2 (#5707)
  Build iOS prebuilds in release by default (#5710)
  Use the event.sender as assignee when preparing release
  Updated prepare release workflow to print PR url in summary
  Fixing lint error
  fix the template app links (#5701)
  Expose `Sync` as named export (#5705)
  Enable all tests after bad rebase (#5595)
  Clear test state flag (#5689)
  ...

# Conflicts:
#	packages/realm/src/Realm.ts
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants