-
Notifications
You must be signed in to change notification settings - Fork 584
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
Clear test state flag #5689
Conversation
49124e2
to
a42c967
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice addition!
integration-tests/tests/src/index.ts
Outdated
// We need this to call Realm.cleanTestState() | ||
flags.CLEAN_TEST_STATE = true; |
There was a problem hiding this comment.
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_
) 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh my. Thanks.
There was a problem hiding this comment.
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 🤞
const promise = promiseRef.deref(); | ||
if (promise) { | ||
promise.cancel(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternative
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>>(); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
const session = sessionRef.deref(); | ||
if (session) { | ||
session.resetInternal(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternative
const session = sessionRef.deref(); | |
if (session) { | |
session.resetInternal(); | |
} | |
sessionRef.deref()?.resetInternal(); |
packages/realm/src/flags.ts
Outdated
@@ -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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* When enabled, calling `Realm.cleanTestState` will be callable. | |
* When enabled, calling `Realm.clearTestState` will be callable. |
integration-tests/tests/src/index.ts
Outdated
@@ -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() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// We need this to call Realm.cleanTestState() | |
// We need this to call Realm.clearTestState() |
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
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
integration-tests/tests/src/index.ts
Outdated
// We need this to call Realm.cleanTestState() | ||
flags.CLEAN_TEST_STATE = true; | ||
// We need this to call Realm.clearTestState() | ||
flags.CLEAR_TEST_STATE = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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>>(); |
There was a problem hiding this comment.
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.
packages/realm/src/flags.ts
Outdated
@@ -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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added this.
packages/realm/src/flags.ts
Outdated
@@ -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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added this.
* 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
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 intoSet
s without worrying about the memory leak, which would have happened ifclearTestState
was never called.This also removes the
IterableWeakRefs
, as we no longer need something mimicking aWeakSet
.This also introduce the reset of SyncSession shared pointers and canceling unresolved async open tasks when clearing test state.