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

[move testing] allow introspection on emitted events in tests #17699

Merged
merged 1 commit into from
May 22, 2024

Conversation

sblackshear
Copy link
Collaborator

  • Extend the event module with test-only native functions for grabbing emitted events by type and getting the total event count. This will make it possible to write tests that assert things about emitted events.
  • Add tests to show that the new functions and test_scenario behave as expected. Fortunately, test_scenario dumps the object runtime state (including events) between txes, so it works without any mods--calling get_events_by_type during a tx only shows events from the current transaction, not from previous ones.

Copy link

vercel bot commented May 13, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
sui-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 18, 2024 2:48pm

Copy link

vercel bot commented May 13, 2024

@sblackshear is attempting to deploy a commit to the Mysten Labs Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Contributor

@tnowacki tnowacki left a comment

Choose a reason for hiding this comment

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

Thanks! Tagging @tzakian and @damirka for naming concerns.

@tzakian thoughts on the private generics/native function issue? We had discussed usually staggering these by a release--where we add the Rust implementations one release and then add the native functions the next release. Do you think it matters for test code? I think it will but just wanted to be sure


#[test_only]
/// Get the total number of events emitted during execution so far
public native fun num_events(): u32;
Copy link
Contributor

Choose a reason for hiding this comment

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

num_user_events exists in test_scenario. Do we need this one here too?

#[test_only]
/// Get all events of type `T` emitted during execution.
/// Can only be used in testing,
public native fun get_events_by_type<T: copy + drop>(): vector<T>;
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably just events_by_type. But I'm wondering if we should add this to test scenario for discoverability. Not sure

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think it probably makes sense here--the event module is where I would be inclined to look for a function related to testing events, and this is definitely useful outside of code that uses test_scenario (e.g., unit tests that call complex third-party code that emits events + want to check that they look as expected).

On naming: agreed, shorter is always better!

@@ -160,8 +161,12 @@ fn verify_private_event_emit(
type_arguments: &[SignatureToken],
) -> Result<(), String> {
let fident = view.identifier_at(fhandle.name);
if fident == GET_EVENTS_TEST_FUNCTION {
Copy link
Contributor

Choose a reason for hiding this comment

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

Might be worthwhile to have something like PUBLIC_EVENT_FUNCTIONS to match the other private generics code, but this is probably fine as is

assert_eq(event::get_events_by_type<S1>()[1], e3);
assert_eq(event::num_events(), 4);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

nit

Suggested change
}
}

@tzakian
Copy link
Contributor

tzakian commented May 14, 2024

houghts on the private generics/native function issue? We had discussed usually staggering these by a release--where we add the Rust implementations one release and then add the native functions the next release. Do you think it matters for test code? I think it will but just wanted to be sure

I believe you are correct, and we might want to stagger the release of the natives and framework for this. In this case, the issue will arise where if you are running the current CLI (which I believe is released along with mainnet) and then if you try to run tests with your Sui framework set to devnet/testnet as this is rolling out then it will fail to run since the framework will be looking for the natives that aren't there.

@sblackshear
Copy link
Collaborator Author

I believe you are correct, and we might want to stagger the release of the natives and framework for this. In this case, the issue will arise where if you are running the current CLI (which I believe is released along with mainnet) and then if you try to run tests with your Sui framework set to devnet/testnet as this is rolling out then it will fail to run since the framework will be looking for the natives that aren't there.

Gotcha--so I should modify this to contain the sui-execution/latest changes only?

@tzakian
Copy link
Contributor

tzakian commented May 15, 2024

Yea, I think that's the right thing to do :)

Then once those changes make their way to mainnet, at that point the framework part of this PR can be landed, and everything shouldn't break (and/or if it does there's an easy fix of "update your CLI")

@@ -1018,4 +1020,32 @@ module sui::test_scenario_tests {
scenario.next_tx(sender);
abort 42
}

public struct E1(u64) has copy, drop;
Copy link
Contributor

Choose a reason for hiding this comment

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

🎉

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for this feature, enjoyed not defining a dummy field :)

@sblackshear
Copy link
Collaborator Author

Yea, I think that's the right thing to do :)

Then once those changes make their way to mainnet, at that point the framework part of this PR can be landed, and everything shouldn't break (and/or if it does there's an easy fix of "update your CLI")

Roger! Made that update. Left the rest of the code in comments so it's easy to bring back

- Extend the `event` module with test-only native functions for grabbing emitted events by type and getting the total event count. This will make it possible to write tests that assert things about emitted events.
- Add tests to show that the new functions and `test_scenario` behave as expected. Fortunately, `test_scenario` dumps the object runtime state (including events) between txes, so it works without any mods--calling `get_events_by_type` during a tx only shows events from the current transaction, not from previous ones.
Copy link
Contributor

@tzakian tzakian left a comment

Choose a reason for hiding this comment

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

Changes look good to me!

@sblackshear sblackshear merged commit 9c9ffd0 into MystenLabs:main May 22, 2024
42 of 45 checks passed
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.

None yet

3 participants