github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/pkg/testutil/require.go (about) 1 // Package testutil implements various utilities to reduce boilerplate in unit 2 // tests a la testify. 3 package testutil 4 5 import ( 6 "testing" 7 "time" 8 9 v0 "github.com/authzed/authzed-go/proto/authzed/api/v0" 10 11 "github.com/google/go-cmp/cmp" 12 "github.com/google/go-cmp/cmp/cmpopts" 13 "github.com/stretchr/testify/require" 14 15 core "github.com/authzed/spicedb/pkg/proto/core/v1" 16 ) 17 18 // RequireEqualEmptyNil is a version of require.Equal, but considers nil 19 // slices/maps to be equal to empty slices/maps. 20 func RequireEqualEmptyNil(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{}) { 21 opts := []cmp.Option{ 22 cmpopts.IgnoreUnexported( 23 v0.RelationTuple{}, 24 v0.ObjectAndRelation{}, 25 v0.RelationReference{}, 26 v0.User_Userset{}, 27 v0.User{}, 28 v0.EditCheckResult{}, 29 v0.EditCheckResultValidationError{}, 30 v0.DeveloperError{}, 31 core.RelationTuple{}, 32 core.ObjectAndRelation{}, 33 core.RelationReference{}, 34 core.Relation{}, 35 core.RelationTupleTreeNode_IntermediateNode{}, 36 core.RelationTupleTreeNode{}, 37 core.RelationTupleTreeNode_LeafNode{}, 38 core.DirectSubjects{}, 39 core.SetOperationUserset{}), 40 cmpopts.EquateEmpty(), 41 } 42 43 msgAndArgs = append(msgAndArgs, cmp.Diff(expected, actual, opts...)) 44 require.Truef(t, cmp.Equal(expected, actual, opts...), "Should be equal", msgAndArgs...) 45 } 46 47 // RequireWithin requires that the runner complete its execution within the specified duration. 48 func RequireWithin(t *testing.T, runner func(t *testing.T), timeout time.Duration) { 49 t.Helper() 50 51 ch := make(chan bool, 1) 52 timer := time.NewTimer(timeout) 53 defer timer.Stop() 54 55 go (func() { 56 t.Helper() 57 runner(t) 58 ch <- true 59 })() 60 61 select { 62 case <-timer.C: 63 require.Failf(t, "timed out waiting for runner", "expected to complete in %v", timeout) 64 65 case <-ch: 66 timer.Stop() 67 return 68 } 69 }