Skip to content

Commit

Permalink
Merge pull request GoogleCloudPlatform#1184 from justinsb/fix_shutdow…
Browse files Browse the repository at this point in the history
…n_for_real_hopefully

tests: Fix shutdown ordering (again)
  • Loading branch information
google-oss-prow[bot] authored Feb 2, 2024
2 parents 7ba31fd + bd34dad commit 77167cd
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 29 deletions.
32 changes: 5 additions & 27 deletions config/tests/samples/create/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,6 @@ type Harness struct {
kccConfig kccmanager.Config
}

// ForSubtest returns a harness scoped to a subtest (created by t.Run).
func (h *Harness) ForSubtest(t *testing.T) *Harness {
ctx, cancel := context.WithCancel(h.Ctx)
t.Cleanup(func() {
cancel()
})

subHarness := *h
subHarness.T = t
subHarness.Ctx = ctx
return &subHarness
}

type httpRoundTripperKeyType int

// httpRoundTripperKey is the key value for http.RoundTripper in a context.Context
Expand All @@ -106,9 +93,9 @@ func NewHarnessWithManager(t *testing.T, ctx context.Context, mgr manager.Manage
}

func NewHarness(t *testing.T, ctx context.Context) *Harness {
ctx, cancel := context.WithCancel(ctx)
ctx, ctxCancel := context.WithCancel(ctx)
t.Cleanup(func() {
cancel()
ctxCancel()
})

log := log.FromContext(ctx)
Expand Down Expand Up @@ -339,9 +326,9 @@ func NewHarness(t *testing.T, ctx context.Context) *Harness {
// Create a context specifically for this, and register the test cleanup function
// after the envtest cleanup function (these run last-in, first-out).
// See https://github.com/kubernetes-sigs/controller-runtime/issues/1571#issuecomment-945535598
mgrContext, cancel := context.WithCancel(ctx)
mgrContext, mgrContextCancel := context.WithCancel(ctx)
t.Cleanup(func() {
cancel()
mgrContextCancel()
})
mgr, err := kccmanager.New(mgrContext, h.restConfig, kccConfig)
if err != nil {
Expand All @@ -360,22 +347,13 @@ func NewHarness(t *testing.T, ctx context.Context) *Harness {
t.Fatalf("error adding registration controller for deletion defender controllers: %v", err)
}
// Start the manager, Start(...) is a blocking operation so it needs to be done asynchronously.
errors := make(chan error)
go func() {
err := mgr.Start(ctx)
err := mgr.Start(mgrContext)
if err != nil {
t.Errorf("error from mgr.Start: %v", err)
}
errors <- err
}()

t.Cleanup(func() {
cancel() // because cleanups run last-in-first-out, we need to cancel again
if err := <-errors; err != nil {
t.Errorf("error from mgr.Start: %v", err)
}
})

return h
}

Expand Down
3 changes: 1 addition & 2 deletions tests/e2e/unified_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,14 @@ import (

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
)

func TestAllInSeries(t *testing.T) {
if os.Getenv("RUN_E2E") == "" {
t.Skip("RUN_E2E not set; skipping")
}

ctx := signals.SetupSignalHandler()
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
t.Cleanup(func() {
cancel()
Expand Down

0 comments on commit 77167cd

Please sign in to comment.