-
-
Notifications
You must be signed in to change notification settings - Fork 155
fix(test): schedule one-shot mocks by call index #7098
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
Merged
proggeramlug
merged 4 commits into
PerryTS:main
from
proggeramlug:fix/6767-node-test-mock-once-indices
Jul 31, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| fix(test): schedule one-shot mock implementations by Node-compatible absolute call indices |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
crates/perry-runtime/src/node_submodules/test_once_unit_tests.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| use super::*; | ||
|
|
||
| thread_local! { | ||
| static REENTRANT_MOCK: Cell<f64> = const { Cell::new(f64::from_bits(TAG_UNDEFINED)) }; | ||
| static REENTRANT_ACTIVE: Cell<bool> = const { Cell::new(false) }; | ||
| } | ||
|
|
||
| extern "C" fn reentrant_implementation(_closure: *const ClosureHeader) -> f64 { | ||
| let recurse = REENTRANT_ACTIVE.with(|active| !active.replace(true)); | ||
| if recurse { | ||
| let mock = REENTRANT_MOCK.with(Cell::get); | ||
| js_closure_call0(raw_ptr_from_value(mock) as *const ClosureHeader); | ||
| REENTRANT_ACTIVE.with(|active| active.set(false)); | ||
| } | ||
| 10.0 | ||
| } | ||
|
|
||
| extern "C" fn return_twenty(_closure: *const ClosureHeader) -> f64 { | ||
| 20.0 | ||
| } | ||
|
|
||
| extern "C" fn return_thirty(_closure: *const ClosureHeader) -> f64 { | ||
| 30.0 | ||
| } | ||
|
|
||
| fn mock_state_with_calls(call_count: usize, implementation: f64) -> MockState { | ||
| let mut calls = crate::array::js_array_alloc(call_count as u32); | ||
| for _ in 0..call_count { | ||
| calls = crate::array::js_array_push_f64(calls, undefined_value()); | ||
| } | ||
| MockState { | ||
| id: 1, | ||
| original: implementation, | ||
| implementation, | ||
| once: Vec::new(), | ||
| calls: boxed_ptr(calls), | ||
| context: undefined_value(), | ||
| function: undefined_value(), | ||
| restore: MockRestoreTarget::None, | ||
| } | ||
| } | ||
|
|
||
| fn set_call_count(state: &mut MockState, call_count: usize) { | ||
| let mut calls = crate::array::js_array_alloc(call_count as u32); | ||
| for _ in 0..call_count { | ||
| calls = crate::array::js_array_push_f64(calls, undefined_value()); | ||
| } | ||
| state.calls = boxed_ptr(calls); | ||
| } | ||
|
|
||
| #[test] | ||
| fn default_once_scheduling_overwrites_the_current_call_index() { | ||
| let mut state = mock_state_with_calls(0, 10.0); | ||
| let call = mock_state_call_count(&state); | ||
| schedule_mock_implementation_once(&mut state, call, 20.0); | ||
| schedule_mock_implementation_once(&mut state, call, 30.0); | ||
|
|
||
| assert_eq!(take_mock_implementation(&mut state), 30.0); | ||
| assert_eq!(take_mock_implementation(&mut state), 10.0); | ||
| } | ||
|
|
||
| #[test] | ||
| fn indexed_once_scheduling_uses_absolute_call_indices() { | ||
| let mut state = mock_state_with_calls(1, 10.0); | ||
| schedule_mock_implementation_once(&mut state, 2, 20.0); | ||
| schedule_mock_implementation_once(&mut state, 4, 30.0); | ||
|
|
||
| assert_eq!(take_mock_implementation(&mut state), 10.0); | ||
| set_call_count(&mut state, 2); | ||
| assert_eq!(take_mock_implementation(&mut state), 20.0); | ||
| set_call_count(&mut state, 3); | ||
| assert_eq!(take_mock_implementation(&mut state), 10.0); | ||
| set_call_count(&mut state, 4); | ||
| assert_eq!(take_mock_implementation(&mut state), 30.0); | ||
| set_call_count(&mut state, 5); | ||
| assert_eq!(take_mock_implementation(&mut state), 10.0); | ||
| } | ||
|
|
||
| #[test] | ||
| fn restoring_a_mock_preserves_calls_and_scheduled_implementations() { | ||
| let mut state = mock_state_with_calls(2, 10.0); | ||
| state.implementation = 20.0; | ||
| schedule_mock_implementation_once(&mut state, 4, 30.0); | ||
|
|
||
| let _ = prepare_mock_state_restore(&mut state); | ||
|
|
||
| assert_eq!(state.implementation, 10.0); | ||
| assert_eq!(mock_state_call_count(&state), 2); | ||
| assert_eq!(state.once, vec![(4, 30.0)]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn reentrant_dispatch_uses_completed_call_indices_like_node() { | ||
| MOCK_STATES.with(|states| states.borrow_mut().clear()); | ||
| let outer = closure_value(reentrant_implementation as *const u8, 0); | ||
| let explicit_once = closure_value(return_twenty as *const u8, 0); | ||
| let scheduled_inside = closure_value(return_thirty as *const u8, 0); | ||
| let mock = create_mock_function(outer, outer, MockRestoreTarget::None); | ||
| let mock_ptr = raw_ptr_from_value(mock) as *const ClosureHeader; | ||
| let id = closure_id(mock_ptr); | ||
| REENTRANT_MOCK.with(|slot| slot.set(mock)); | ||
| REENTRANT_ACTIVE.with(|active| active.set(false)); | ||
|
|
||
| // Node records a call only after its implementation completes. The outer | ||
| // and nested dispatches therefore both observe completed-call index 0, so | ||
| // an explicit onCall=1 entry is not consumed by either invocation. | ||
| MOCK_STATES.with(|states| { | ||
| let mut states = states.borrow_mut(); | ||
| let state = states.iter_mut().find(|state| state.id == id).unwrap(); | ||
| schedule_mock_implementation_once(state, 1, explicit_once); | ||
| }); | ||
| assert_eq!(js_closure_call0(mock_ptr), 10.0); | ||
|
|
||
| MOCK_STATES.with(|states| { | ||
| let mut states = states.borrow_mut(); | ||
| let state = states.iter_mut().find(|state| state.id == id).unwrap(); | ||
| assert_eq!(mock_state_call_count(state), 2); | ||
| assert_eq!(state.once.len(), 1); | ||
| assert_eq!(state.once[0].0, 1); | ||
| assert_eq!(state.once[0].1.to_bits(), explicit_once.to_bits()); | ||
|
|
||
| let current = mock_state_call_count(state); | ||
| schedule_mock_implementation_once(state, current, scheduled_inside); | ||
| }); | ||
| assert_eq!(js_closure_call0(mock_ptr), 30.0); | ||
|
|
||
| MOCK_STATES.with(|states| { | ||
| let states = states.borrow(); | ||
| let state = states.iter().find(|state| state.id == id).unwrap(); | ||
| assert_eq!(mock_state_call_count(state), 3); | ||
| assert_eq!(state.once.len(), 1); | ||
| assert_eq!(state.once[0].0, 1); | ||
| assert_eq!(state.once[0].1.to_bits(), explicit_once.to_bits()); | ||
| }); | ||
| MOCK_STATES.with(|states| states.borrow_mut().clear()); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.