diff options
author | Karthik Nayak <knayak@gitlab.com> | 2023-12-13 21:23:23 +0300 |
---|---|---|
committer | Karthik Nayak <knayak@gitlab.com> | 2023-12-19 11:50:56 +0300 |
commit | 46a35cfe7ef5bb9c6e8eb80c308b3885327a379a (patch) | |
tree | a71366d7a76c79fe4fa67c9b653d1ec50ab76383 | |
parent | 311f6768dc9ec588d75fb4564436b1138742cb88 (diff) |
testhelper: Add goroutine safe `ProtoEqualAssert`
We currently have `ProtoEqual`, which compares two proto structs. This
function uses the `require` package. The require package though is not
goroutine safe since it uses `t.FailNow()`.
So introduce a similar `ProtoEqualAssert()` which uses the `assert`
package. This is safe to use in goroutines.
-rw-r--r-- | internal/testhelper/grpc.go | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/internal/testhelper/grpc.go b/internal/testhelper/grpc.go index 7a89eb571..24e375250 100644 --- a/internal/testhelper/grpc.go +++ b/internal/testhelper/grpc.go @@ -44,10 +44,20 @@ func (mockServerTransportStream) SetTrailer(md metadata.MD) error { return nil } func ProtoEqual(tb testing.TB, expected, actual interface{}, opts ...cmp.Option) { tb.Helper() + ProtoEqualAssert(tb, expected, actual, opts...) + if tb.Failed() { + tb.FailNow() + } +} + +// ProtoEqualAssert is similar to ProtoEqual but safe to use in goroutines. +func ProtoEqualAssert(tb testing.TB, expected, actual interface{}, opts ...cmp.Option) { + tb.Helper() + opts = append(opts, protocmp.Transform(), cmpopts.EquateErrors()) diff := cmp.Diff(expected, actual, opts...) if len(diff) > 0 { - require.Fail(tb, fmt.Sprintf("Protobufs not equal\nDiff: %v", diff)) + assert.Fail(tb, fmt.Sprintf("Protobufs not equal\nDiff: %v", diff)) } } |