2018年8月31日星期五

Ginkgo and goroutine

Ginkgo is a fantastic Behavior Driven Development (BDD) framework for golang and I've been using it for a while. The other day I hit an unusual error:

panic:
Your test failed.
Ginkgo panics to prevent subsequent assertions from running.
Normally Ginkgo rescues this panic so you shouldn't see it.

But, if you make an assertion in a goroutine, Ginkgo can't capture the panic.
To circumvent this, you should call

        defer GinkgoRecover()

at the top of the goroutine that caused this panic.

followed by a bunch of stack traces.

This is not good, obviously. By checking the documentation you'd get an example which basically does what had been told in the above error message - putting defer GinkgoRecover() at the beginning of the goroutine.

This was important, not because it eliminated the panic - it didn't do that, but because it let Ginkgo rescue the panic and tell you where it actually went wrong - missing a mock function in my case.

Unexpected call to *mock_storage.MockBackendNew.ChaincodeDeploymentUpdate([mychannel  unique_mycc_id mycc {        done  0001-01-01 00:00:00 +0000 UTC}]) at /Users/tony/workspace/go/src/github.com/arxanchain/baymax/vendor/github.com/arxanchain/chain-mgmt/core/storage/mock/mock_storage/mock_storage.go:336 because: there are no expected calls of the method "ChaincodeDeploymentUpdate" for that receiver

After fixing whatever the root cause is, you could of course remove the GinkgoRecover function call from your goroutine, because you've already got a successful test suite and don't need the diagnosis.

没有评论:

发表评论