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 a goroutine is created, you should have some idea of when and how it gets to run and when it stops running, and how other goroutines wait for it to do its job.

You could do this for example by controlling the lifetime of the goroutine with a sync.WaitGroup. Pass in a WaitGroup and call Done() on it when the goroutine returns (probably as a deferred call), then call Wait() in some other goroutine (for example in your main function or in the TestFoo function) so there's some sort of ordering enforced.

In general, you'll need some way of signalling "I'm done copying stuff from zipReader to zipWriter" in the goroutine, and you'll need to arrange for some other, longer lived, goroutine to wait for that signal.


On 10.09.20 09:34, Yvonne Zhang wrote:
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 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 functionB a method on a type that implements the
    Ber
    interface, the same for the real functionB. In unit tests, pass in an
    instance of the type with the mock method, in real life, pass in an
    instance of the type with the real method.

    Note that the types could be as simple as

    type mockB struct{}
    func (m mockB) functionB(...)

    On 10.09.20 09: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(pipeWriter)
     >
     >    go func(){
     >       // err := functionB(zipReader, zipWriter)call another
    function to
     > prepare files and write to zipwriter
     >    If err != nil{
     >        zipWriter.Close()
     >         _ = pipeWriter.CloseWithError(err)
     >         return
     >    }
     >       zipWriter.Close()
     >        pipeWriter.Close()
     >    }()
     >    return
     > }
     >
     > My question is about unit test about this functionA. I have mock for
     > functionB. How do I write unit test for functionA with the mock
     > functionB. It seems my unit test does not work well when it goes
    into
     > the goroutine.
     > Many thanks!
     >
     > --
     > You received this message because you are subscribed to the Google
     > Groups "golang-nuts" group.
     > To unsubscribe from this group and stop receiving emails from it,
    send
     > an email to golang-nuts...@googlegroups.com
     > <mailto:golang-nuts...@googlegroups.com>.
     > To view this discussion on the web visit
     >
    
https://groups.google.com/d/msgid/golang-nuts/4ca0817a-f1f7-44a9-be3d-3584bcb61b8an%40googlegroups.com

     >
    
<https://groups.google.com/d/msgid/golang-nuts/4ca0817a-f1f7-44a9-be3d-3584bcb61b8an%40googlegroups.com?utm_medium=email&utm_source=footer>.


-- Gregor Best
    be...@pferdewetten.de

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com <mailto:golang-nuts+unsubscr...@googlegroups.com>. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/33648e9a-4011-4d02-a07b-01b506cf5cd1n%40googlegroups.com <https://groups.google.com/d/msgid/golang-nuts/33648e9a-4011-4d02-a07b-01b506cf5cd1n%40googlegroups.com?utm_medium=email&utm_source=footer>.

--
Gregor Best
  b...@pferdewetten.de

--
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/746e54f8-cefc-9fee-c4e0-8724833e38e8%40pferdewetten.de.

Reply via email to