-
Notifications
You must be signed in to change notification settings - Fork 3.9k
op-e2e/actions: action tests base test util #3588
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
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3023925
op-e2e/actions: action tests base test util
protolambda d3c08e0
actions: extend action testing doc comments
protolambda 884de85
actions: docstring about format and args
protolambda 171f643
Merge branch 'develop' into action-tests-base
mergify[bot] 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,71 @@ | ||
| package actions | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/ethereum-optimism/optimism/op-e2e/e2eutils" | ||
| ) | ||
|
|
||
| // Testing is an interface to Go-like testing, | ||
| // extended with a context getter for the test runner to shut down individual actions without interrupting the test, | ||
| // and a signaling function for when an invalid action is hit. | ||
| // This helps custom test runners navigate slow or invalid actions, e.g. during fuzzing. | ||
| type Testing interface { | ||
| e2eutils.TestingBase | ||
| // Ctx shares a context to execute an action with, the test runner may interrupt the action without stopping the test. | ||
| Ctx() context.Context | ||
| // InvalidAction indicates the failure is due to action incompatibility, does not stop the test. | ||
| InvalidAction(format string, args ...any) | ||
| } | ||
|
|
||
| // Action is a function that may change the state of one or more actors or check their state. | ||
| // Action definitions are meant to be very small building blocks, | ||
| // and then composed into larger patterns to write more elaborate tests. | ||
| type Action func(t Testing) | ||
|
|
||
| // ActionStatus defines the state of an action, to make a basic distinction between InvalidAction() and other calls. | ||
| type ActionStatus uint | ||
|
|
||
| const ( | ||
| // ActionOK indicates the action is valid to apply | ||
| ActionOK ActionStatus = iota | ||
| // ActionInvalid indicates the action is not applicable, and a different next action may taken. | ||
| ActionInvalid | ||
| // More action status types may be used to indicate e.g. required rewinds, | ||
| // simple skips, or special cases for fuzzing. | ||
| ) | ||
|
|
||
| // defaultTesting is a simple implementation of Testing that takes standard Go testing framework, | ||
| // and handles invalid actions as errors, and exposes a Reset function to change the context and action state, | ||
| // to recover after an invalid action or cancelled context. | ||
| type defaultTesting struct { | ||
| e2eutils.TestingBase | ||
| ctx context.Context | ||
| state ActionStatus | ||
| } | ||
|
|
||
| // Ctx shares a context to execute an action with, the test runner may interrupt the action without stopping the test. | ||
| func (st *defaultTesting) Ctx() context.Context { | ||
| return st.ctx | ||
| } | ||
|
|
||
| // InvalidAction indicates the failure is due to action incompatibility, does not stop the test. | ||
| // The format and args behave the same as fmt.Sprintf, testing.T.Errorf, etc. | ||
| func (st *defaultTesting) InvalidAction(format string, args ...any) { | ||
trianglesphere marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| st.TestingBase.Helper() // report the error on the call-site to make debugging clear, not here. | ||
| st.Errorf("invalid action err: "+format, args...) | ||
| st.state = ActionInvalid | ||
| } | ||
|
|
||
| // Reset prepares the testing util for the next action, changing the context and state back to OK. | ||
| func (st *defaultTesting) Reset(actionCtx context.Context) { | ||
| st.state = ActionOK | ||
| st.ctx = actionCtx | ||
| } | ||
|
|
||
| // State shares the current action state. | ||
| func (st *defaultTesting) State() ActionStatus { | ||
| return st.state | ||
| } | ||
|
|
||
| var _ Testing = (*defaultTesting)(nil) | ||
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.