petergtz / pegomock

Pegomock is a powerful, yet simple mocking framework for the Go programming language
Apache License 2.0
252 stars 28 forks source link

Different number of return values #108

Closed fatekiller closed 1 year ago

fatekiller commented 3 years ago

Some time some func panic with err Different number of return values, the mock which panic and whether it will panic is randomly, a plot is that I use a go routine in my business func. Is anyone know what is the cause?

petergtz commented 3 years ago

Hi @fatekiller, thanks for reporting this issue. Could you please provide some example code and the actual output you're getting when running your test, and what you would expect instead?

fatekiller commented 3 years ago

thanks for your reply, I can not share the real code, but the output is, I tried recreate this error, but failed....

panic: Different number of return values [recovered]
        panic: Different number of return values

goroutine 129 [running]:
testing.tRunner.func1(0xc0000ffc00)
        /Users/xxx/go@1.13/libexec/src/testing/testing.go:874 +0x3a3
panic(0x1a48580, 0xc0002f7570)
        /Users/xxx/go@1.13/libexec/src/runtime/panic.go:679 +0x1b2
github.com/petergtz/pegomock/internal/verify.Argument(...)
        /Users/xxx/go@1.13/pkg/mod/github.com/petergtz/pegomock@v2.8.0+incompatible/internal/verify/verify.go:7
github.com/petergtz/pegomock.checkAssignabilityOf(0xc000695650, 0x3, 0x3, 0xc0002c0830, 0x1, 0x1)
        /Users/xxx/go@1.13/pkg/mod/github.com/petergtz/pegomock@v2.8.0+incompatible/dsl.go:491 +0x38c
github.com/petergtz/pegomock.(*ongoingStubbing).ThenReturn(0xc00047b2c0, 0xc000695650, 0x3, 0x3, 0x10)
        /Users/xxxx/go@1.13/pkg/mod/github.com/petergtz/pegomock@v2.8.0+incompatible/dsl.go:485 +0x63
fatekiller commented 3 years ago

when I use pegomock generate XXInterface --output-dir=mock to generate mock file, I always got panic: Loading input failed: extra data in buffer, so I used pegomock generate XXInterface --use-experimental-model-gen -m --output-dir=mock, this information maybe helpful

fatekiller commented 3 years ago

It was caused by multi go routine, the code use a global variable lastInvocation to judge whether the return values of last invocation have same length of the given params in pegomock.When(xx).ThenReturn(xx), the lastInvocation can not be modify safely in concurrent environment.

petergtz commented 3 years ago

Hey @fatekiller, would you be able to reproduce the problem with a simplified version of your code that you can actually share (rename variables, remove unneeded logic etc.)? Otherwise this will be nearly impossible to debug.

fatekiller commented 3 years ago

thanks for you help, I have already solved this problem.

petergtz commented 3 years ago

Glad you could solve it. Is there anything we can improve in the library or in the documentation, so you could have solved your issue more quickly?

fatekiller commented 3 years ago

It would be better if you told users that this tool is not thread safely in the manual

petergtz commented 3 years ago

When you refer to multiple threads, do you mean calling When from different goroutines? That's indeed something one shouldn't be doing and you're right, this is not documented.

Note that your mocks can be invoked from multiple goroutines. In that sense pegomock is thread-safe.

Could you clarify?

petergtz commented 1 year ago

Closing due to inactivity.