Re: [go-nuts] unit test for function which has goroutine

2020-09-11 Thread roger peppe
On Thu, 10 Sep 2020 at 08:15, Yvonne Zhang wrote: > Hi, > I have a function streaming a zipreader to browser. It is like this. > func functionA()(body io.ReadCloser, err error){ > >// some logic to get a zipreader > >body, pipeWriter := io.Pipe() >zipWriter := zip.NewWriter(pipeWr

Re: [go-nuts] unit test for function which has goroutine

2020-09-10 Thread Yvonne Zhang
Thank you Gregor. As this is just to make unit test work, if I add waitgroup in the code, will this affect the real streaming logic? Is that OK, I check if is it a mock function functionB called, then I tell it to sleep for a sec before return. On Thursday, September 10, 2020 at 5:42:21 PM UTC+

Re: [go-nuts] unit test for function which has goroutine

2020-09-10 Thread Gregor Best
Right, I wanted to write something about the goroutine and forgot about it. Sorry about that. The problem here could be that in the code you posted there's nothing waiting on any result from the goroutine, and if the test exits quickly enough, it might not get scheduled. In general, whenever

Re: [go-nuts] unit test for function which has goroutine

2020-09-10 Thread Yvonne Zhang
Thank you Gregor for your quick reply! Anything special I need to think about this go routine logic in my unit test. It might be still running after test returns..? On Thursday, September 10, 2020 at 5:26:13 PM UTC+10 be...@pferdewetten.de wrote: > If I were you, I'd pass in an interface value

Re: [go-nuts] unit test for function which has goroutine

2020-09-10 Thread Gregor Best
If I were you, I'd pass in an interface value to functionA with the signature type Ber interface{ functionB(io.Reader, io.Writer) } (assuming that zipReader is an io.Reader, zipWriter is an io.Writer, you may have to adjust this a bit). Then make your mock fun