Skip to content

Conversation

@lpahlavi
Copy link
Contributor

This PR refactors the MockHttpRuntime into the more generic PocketItRuntime, namely:

  1. Allow PocketIcRuntime to be used with or without HTTP outcall mocks.
  2. Allow PocketIcRuntime to be used in live mode.
  3. Introduce a trait for mocked HTTP outcalls to allow for more flexibility.

@lpahlavi lpahlavi changed the base branch from main to lpahlavi/add-readme-and-examples November 10, 2025 09:56
@lpahlavi lpahlavi force-pushed the lpahlavi/add-live-mode-runtime branch 4 times, most recently from 4527c68 to b494162 Compare November 10, 2025 10:19
@lpahlavi lpahlavi force-pushed the lpahlavi/add-live-mode-runtime branch from b494162 to 6195d06 Compare November 10, 2025 10:47
@lpahlavi lpahlavi marked this pull request as ready for review November 10, 2025 11:49
@lpahlavi lpahlavi requested a review from a team as a code owner November 10, 2025 11:49
Base automatically changed from lpahlavi/add-readme-and-examples to main November 11, 2025 13:42
Copy link
Collaborator

@gregorydemay gregorydemay left a comment

Choose a reason for hiding this comment

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

Some understanding questions

/// Add a new mocked HTTP outcall.
pub fn push(&mut self, mock: MockHttpOutcall) {
self.0.push(mock);
pub fn push(&self, mock: MockHttpOutcall) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

understanding question: why is this no longer &mut self?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good question. I moved the Mutex directly to the MockHttpOutcalls struct so that I could implement the new ExecuteHttpOutcallMocks trait directly on it (i.e. we previously had a Mutex<MockHttpOutcalls(Vec<MockHttpOutcall>)>, now we have a MockHttpOutcalls(Mutex<Vec<MockHttpOutcall>>)).

This means all the operations on MockHttpOutcalls now use interior mutability and so don't need mut anymore. This is not ideal, but is a hack so that we can mutate mocks inside Runtime::update_call which takes an &self.

self
}

/// Use Pocket IC in [live mode](https://github.com/dfinity/ic/blob/f0c82237ae16745ac54dd3838b3f91ce32a6bc52/packages/pocket-ic/HOWTO.md?plain=1#L43).
Copy link
Collaborator

Choose a reason for hiding this comment

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

This might be a bit misleading because it doesn't change the PocketIC instance. Maybe ask Martin if there is a way to know whether a given PocketIc instance has live mode activated?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, good point. I had a look and couldn't find any method that tells whether a PocketIC instance is in live more... Martin is currently away but I'll have another look and worst case add a big fat warning.

#[async_trait]
pub trait ExecuteHttpOutcallMocks: Send + Sync {
/// Execute HTTP outcall mocks.
async fn execute_http_outcall_mocks(&self, runtime: &PocketIc) -> ();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Unless I'm missing something, this will force anyone using that crate to have an async runtime when mocking HTTPs outcalls, even if their tests is actually not async. I think that's something we should ideally avoid (async should be opt-in).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm, do you mean if someone wants to use the mocks without actually using Runtime? Runtime is an async trait (and has to be) so to me it's not a problem. Or did I not understand correctly?

Copy link
Contributor Author

@lpahlavi lpahlavi left a comment

Choose a reason for hiding this comment

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

Thank you for having a look @gregorydemay! I've added some answers to your questions.

/// Add a new mocked HTTP outcall.
pub fn push(&mut self, mock: MockHttpOutcall) {
self.0.push(mock);
pub fn push(&self, mock: MockHttpOutcall) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good question. I moved the Mutex directly to the MockHttpOutcalls struct so that I could implement the new ExecuteHttpOutcallMocks trait directly on it (i.e. we previously had a Mutex<MockHttpOutcalls(Vec<MockHttpOutcall>)>, now we have a MockHttpOutcalls(Mutex<Vec<MockHttpOutcall>>)).

This means all the operations on MockHttpOutcalls now use interior mutability and so don't need mut anymore. This is not ideal, but is a hack so that we can mutate mocks inside Runtime::update_call which takes an &self.

self
}

/// Use Pocket IC in [live mode](https://github.com/dfinity/ic/blob/f0c82237ae16745ac54dd3838b3f91ce32a6bc52/packages/pocket-ic/HOWTO.md?plain=1#L43).
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, good point. I had a look and couldn't find any method that tells whether a PocketIC instance is in live more... Martin is currently away but I'll have another look and worst case add a big fat warning.

#[async_trait]
pub trait ExecuteHttpOutcallMocks: Send + Sync {
/// Execute HTTP outcall mocks.
async fn execute_http_outcall_mocks(&self, runtime: &PocketIc) -> ();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm, do you mean if someone wants to use the mocks without actually using Runtime? Runtime is an async trait (and has to be) so to me it's not a problem. Or did I not understand correctly?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants