Skip to content

Commit

Permalink
Merge pull request #2034 from josephschorr/finalizer-for-testing
Browse files Browse the repository at this point in the history
Only add the finalizer on iterators when CI testing
  • Loading branch information
josephschorr authored Aug 20, 2024
2 parents 7e19001 + c1c65c4 commit d77601b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
5 changes: 2 additions & 3 deletions internal/datastore/common/tuple.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package common

import (
"runtime"

"github.com/authzed/spicedb/pkg/datastore"
"github.com/authzed/spicedb/pkg/datastore/options"
core "github.com/authzed/spicedb/pkg/proto/core/v1"
"github.com/authzed/spicedb/pkg/spiceerrors"
)

// NewSliceRelationshipIterator creates a datastore.TupleIterator instance from a materialized slice of tuples.
func NewSliceRelationshipIterator(tuples []*core.RelationTuple, order options.SortOrder) datastore.RelationshipIterator {
iter := &sliceRelationshipIterator{tuples: tuples, order: order}
runtime.SetFinalizer(iter, MustIteratorBeClosed)
spiceerrors.SetFinalizerForDebugging(iter, MustIteratorBeClosed)
return iter
}

Expand Down
3 changes: 1 addition & 2 deletions internal/datastore/memdb/readonly.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package memdb
import (
"context"
"fmt"
"runtime"
"slices"
"sort"
"strings"
Expand Down Expand Up @@ -524,7 +523,7 @@ func eq(lhsNamespace, lhsObjectID, lhsRelation string, rhs *core.ObjectAndRelati

func newMemdbTupleIterator(it memdb.ResultIterator, limit *uint64, order options.SortOrder) *memdbTupleIterator {
iter := &memdbTupleIterator{it: it, limit: limit, order: order}
runtime.SetFinalizer(iter, mustHaveBeenClosed)
spiceerrors.SetFinalizerForDebugging(iter, mustHaveBeenClosed)
return iter
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/spiceerrors/assert_off.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ package spiceerrors
func DebugAssert(condition func() bool, format string, args ...any) {
// Do nothing on purpose
}

// SetFinalizerForDebugging is a no-op in non-CI builds
func SetFinalizerForDebugging[T any](obj interface{}, finalizer func(obj T)) {
// Do nothing on purpose
}
11 changes: 10 additions & 1 deletion pkg/spiceerrors/assert_on.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@

package spiceerrors

import "fmt"
import (
"fmt"
"runtime"
)

// DebugAssert panics if the condition is false in CI builds.
func DebugAssert(condition func() bool, format string, args ...any) {
if !condition() {
panic(fmt.Sprintf(format, args...))
}
}

// SetFinalizerForDebugging sets a finalizer on the object for debugging purposes
// in CI builds.
func SetFinalizerForDebugging[T any](obj interface{}, finalizer func(obj T)) {
runtime.SetFinalizer(obj, finalizer)
}

0 comments on commit d77601b

Please sign in to comment.