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

Ittest improve #425

Merged
merged 4 commits into from
Jul 8, 2024
Merged

Ittest improve #425

merged 4 commits into from
Jul 8, 2024

Conversation

stonedu1011
Copy link
Contributor

Description

ittest has being a very useful tool to test components that communicate with external servers via HTTP. However, ittest was designed to use with bootstrap and apptest and cannot be used as a standalone test utility.

The purpose of this PR is to improve ittest for standalone usage. Projects that doesn't use bootstrap, apptest or uber/fx can still leverage utilities provided by ittest package.

Type of Change

  • Bug Fix
  • New Feature
  • Breaking Change
  • Refactor
  • Documentation
  • Other (please describe)

Checklist

  • I have read the contributing guidelines
  • Existing issues have been referenced (where applicable)
  • I have verified this change is not present in other open pull requests
  • Functionality is documented
  • All code style checks pass
  • New code contribution is covered by automated tests
  • All new and existing tests pass

Copy link

github-actions bot commented Jun 26, 2024

PR Coverage Summary
Changed Statements 107
Covered Statements 97
Test Coverage 90.7%

PR Verification Succeeded: Coverage >= 70%

@TimShi TimShi self-requested a review June 28, 2024 14:06
@@ -132,18 +146,25 @@ func DefaultValueSanitizer() ValueSanitizer {
// InteractionIndexAwareHook inject interaction index into stored header:
// httpvcr store interaction's ID but doesn't expose it to cassette.MatchFunc,
Copy link
Contributor

Choose a reason for hiding this comment

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

Not from this PR, but this should say MatcherFunc instead of MatchFunc

for i := range names {
for j := range opt.Hooks {
if names[i] == opt.Hooks[j].Name() {
opt.Hooks[j] = opt.Hooks[len(opt.Hooks)-1]
Copy link
Contributor

Choose a reason for hiding this comment

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

This changed the order of the hooks, is that ok? If we sort the hooks somewhere else, is it better to sort it in this method instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hooks are sorted when creating the recorder based on OrderedFirstCompare rule, see NewHttpRecorder()

// When enforced, HTTP interactions have to happen in the recorded order.
// Otherwise, HTTP interactions can happen in any order, but each matched record can only replay once
// By default, record ordering is enabled
func HttpRecordOrdering(enforced bool) HTTPVCROptions {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this a breaking change? I see opensearch test package is changed because of this. But I think if someone was using WithHttpPlackback this is the default behaviour.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a new option. We only had DisableHttpRecordOrdering before. The reason opensearchtest need to update is due to the default behaviour change of NewHttpRecorder()

// HttpRecorder wrapper of recorder.Recorder, used to hold some value that normally inaccessible via wrapped recorder.Recorder.
// Note: Internal Use Only! this type is for other test utilities to re-configure recorder.Recorder
// HttpRecorder wrapper of recorder.RawRecorder, used to hold some value that normally inaccessible via wrapped recorder.RawRecorder.
// Note: This type is for other test utilities to re-configure recorder.RawRecorder
type HttpRecorder struct {
*recorder.Recorder
RawOptions *recorder.Options
Matcher cassette.MatcherFunc
Copy link
Contributor

Choose a reason for hiding this comment

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

If this is called OrigMatcher or something like that it would make it clear that this is not the effective matcher after AdditionalMatcherOptions is applied.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed it to InitMatcher

FixedHttpRecordDuration(DefaultHTTPDuration),
FixedHttpRecordDuration(0),
FixedHttpRecordDuration(DefaultHTTPDuration),
HttpTransport(http.DefaultTransport),
Copy link
Contributor

@TimShi TimShi Jun 28, 2024

Choose a reason for hiding this comment

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

developer doesn't need to comment out this line when going from recording mode to playback mode, right?

also, I think the recorder package already sets it to this value, so this line is optional?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

developers do need to comment out HttpRecordingMode() to switch from recording mode to playback mode. The default is playback.

FixedHttpRecordDuration() is a "recording" specific option. It takes no effect in playback mode. The playback counterpart options is ApplyHttpLatency()

I added an comment on the option method to clarify that.

}

// StopRecorder stops the recorder extracted from the given context.
func StopRecorder(ctx context.Context) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably should add a comment on these methods that they are only required to be used in tests without using the test bootstrap.

@TimShi
Copy link
Contributor

TimShi commented Jun 28, 2024

We should probably add a readme on what features we added on top of recorder and cassette, and how it can be used independent of the test bootstrap.

@stonedu1011 stonedu1011 merged commit b3910c3 into main Jul 8, 2024
1 check passed
@stonedu1011 stonedu1011 deleted the ittest-improve branch July 8, 2024 18:40
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