-
Notifications
You must be signed in to change notification settings - Fork 42
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
Fix my own bug in postrun Execute #217
Conversation
@guseggert The testing code could probably be less copy-paste, but I think it at least works as expected. Not sure if there's a better way than using a mocked |
bba1701
to
8998fa4
Compare
I went ahead with this. It simplifies the test considerably and should be equally as valid. We're testing the method not the emitter anyway. |
For posterity I checked what commit I was using in my own repo and why it worked where the one submitted in the PR didn't. diff --git a/executor.go b/executor.go
index 77f58d5..5bcfb7a 100644
--- a/executor.go
+++ b/executor.go
@@ -50,24 +50,24 @@ func (x *executor) Execute(req *Request, re ResponseEmitter, env Environment) er
return err
}
}
- maybeStartPostRun := func(postMap PostRunMap) <-chan error {
+ maybeStartPostRun := func(formatters PostRunMap) <-chan error {
var (
postRun func(Response, ResponseEmitter) error
postRunCh = make(chan error)
)
- if cmd.PostRun == nil {
+ if postRun == nil {
close(postRunCh)
return postRunCh
}
- // check if we have a function for this emitter
+ // check if we have a formatter for this emitter type
typer, isTyper := re.(interface {
Type() PostRunType
})
if isTyper &&
- cmd.PostRun[typer.Type()] != nil {
- postRun = cmd.PostRun[typer.Type()]
+ formatters[typer.Type()] != nil {
+ postRun = formatters[typer.Type()]
} else {
close(postRunCh)
return postRunCh This falls in line with my assumption; I meant to check Edit: actually, that's not true. I see what's going on here and will update the commit. |
8998fa4
to
ac182d9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good enough. 👌
ac182d9
to
63c3857
Compare
63c3857
to
b9ee0be
Compare
This is interesting.
I added the commit here but we can separate it out if desired. With the code being correct and CI passing, this should be ready for review for real now. |
ctx := re.req.Context | ||
|
||
if err := ctx.Err(); err != nil { | ||
return err | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The declaration of ctx, and this check should probably be hoisted to the top of the function.
If we're canceled, we probably shouldn't do anything other than return.
The locking, EmitChan, etc. seems pointless if we'll get here anyway.
Need input/opinions on this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather just leave it next to the select
like this, so that we preserve the existing post-cancellation side effects of consuming v if it's a chan, and unblocking Length()
, which might be important somewhere else (don't know off the top of my head).
// Otherwise, relay emitter responses | ||
// from Run to PostRun, and | ||
// from PostRun to the original emitter. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this makes sense, but that's because I wrote it.
If it could be reworded or is confusing, someone let me know.
@@ -316,6 +316,7 @@ func TestCancel(t *testing.T) { | |||
} | |||
|
|||
re, res := NewChanResponsePair(req) | |||
cancel() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cancel
should likely be called before theEmit
goroutine even has the potential to run.
Incorrect. This needs to go back where it was.
We pass either way, but at least when cancel
is called late, there's a 50/50 chance we also test that pending sends will be canceled instead of sent.
Depending on who runs first, Emit
or cancel
.
ctx := re.req.Context | ||
|
||
if err := ctx.Err(); err != nil { | ||
return err | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather just leave it next to the select
like this, so that we preserve the existing post-cancellation side effects of consuming v if it's a chan, and unblocking Length()
, which might be important somewhere else (don't know off the top of my head).
I left in a bad condition in #216 which was supposed to be deleted.
This should amend that and adds tests to make sure postruns actually executes if it should.
The condition was supposed to be checking
formatters
, but was checking the wrong value.We're reading from a map type, even if it's nil we'll just get back the 0 value when indexing it.
In this case that's a nil function pointer, which is checked before being assigned and later used.
formatters[typer.Type()] != nil