You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm working my way through the chapters, and I was curious about how to possibly hit the case in the goroutine with the comment in the snippet below. I tried a handful of things, but whenever the context is cancelled, only the other case for <-ctx.Done() is ever hit. (I changed the log to fmt for simplistic debugging)
I removed the case in the goroutine, and the desired behavior remains the same, so I'm not sure the purpose of it's inclusion if it's not possible to hit it?
Is it protection to prevent the goroutines from continuing to work? If so, then why is it never hit?
Pulled from context/v3/testdoubles.go
// Fetch returns response after a short delay.func (s*SpyStore) Fetch(ctx context.Context) (string, error) {
data:=make(chanstring, 1)
gofunc() {
varresultstringfor_, c:=ranges.response {
select {
case<-ctx.Done():
//How to hit this case?log.Println("spy store got cancelled")
returndefault:
result+=string(c)
}
}
data<-result
}()
select {
case<-ctx.Done():
//Only ever hits this casereturn"", ctx.Err()
caseres:=<-data:
returnres, nil
}
}
Thank you for the great guide! It's been very helpful for someone who is pretty green on TDD and Go!
The text was updated successfully, but these errors were encountered:
I was going to that chapter as well and asked the same thing. It's like the select at the bottom of Fetch function listens the context's done channel first, terminating the goroutine as this no longer runs.
I coded this alernative, not sure if it's ok, but it's working:
I'm working my way through the chapters, and I was curious about how to possibly hit the case in the goroutine with the comment in the snippet below. I tried a handful of things, but whenever the context is cancelled, only the other case for
<-ctx.Done()
is ever hit. (I changed thelog
tofmt
for simplistic debugging)I removed the case in the goroutine, and the desired behavior remains the same, so I'm not sure the purpose of it's inclusion if it's not possible to hit it?
Is it protection to prevent the goroutines from continuing to work? If so, then why is it never hit?
Pulled from
context/v3/testdoubles.go
Thank you for the great guide! It's been very helpful for someone who is pretty green on TDD and Go!
The text was updated successfully, but these errors were encountered: