diff --git a/context.go b/context.go index 7901c90..867b748 100644 --- a/context.go +++ b/context.go @@ -1,26 +1,19 @@ // Copyright (c) The OpenTofu Authors // SPDX-License-Identifier: MPL-2.0 +// Copyright (c) 2023 HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 package tofutestutils import ( "context" - "testing" - "time" - "github.com/opentofu/tofutestutils/testcontext" + "testing" ) -const minCleanupSafety = time.Second * 30 -const maxCleanupSafety = time.Minute * 5 - -// Context returns a context configured for the test deadline. This function configures a context with 25% safety for -// any cleanup tasks to finish. For a more flexible function see testcontext.Context. +// Context creates a context with a deadline that allows for enough time to clean up a test before the testing framework +// unceremoniously kills the process. Starting with Go 1.24, this function is an alias for t.Context(). In Go 1.23 and +// lower this function reserves 25% of the test timeout, minimum 1 minute, maximum 5 minutes for cleanup. func Context(t *testing.T) context.Context { - return testcontext.Context( - t, - 4, - minCleanupSafety, - maxCleanupSafety, - ) + return testcontext.Context(t) } diff --git a/testcontext/context_1.24.go b/testcontext/context_1.24.go new file mode 100644 index 0000000..e33bbb4 --- /dev/null +++ b/testcontext/context_1.24.go @@ -0,0 +1,15 @@ +// Copyright (c) The OpenTofu Authors +// SPDX-License-Identifier: MPL-2.0 +// Copyright (c) 2023 HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +//go:build go1.24 + +package testcontext + +// Context creates a context with a deadline that allows for enough time to clean up a test before the testing framework +// unceremoniously kills the process. Starting with Go 1.24, this function is an alias for t.Context(). In Go 1.23 and +// lower this function reserves 25% of the test timeout, minimum 1 minute, maximum 5 minutes for cleanup. +func Context(t *testing.T) context.Context { + return t.Context() +} diff --git a/testcontext/context.go b/testcontext/context_before_1.24.go similarity index 50% rename from testcontext/context.go rename to testcontext/context_before_1.24.go index 48eda01..4fa0fe0 100644 --- a/testcontext/context.go +++ b/testcontext/context_before_1.24.go @@ -3,6 +3,8 @@ // Copyright (c) 2023 HashiCorp, Inc. // SPDX-License-Identifier: MPL-2.0 +//go:build !go1.24 + package testcontext import ( @@ -11,12 +13,23 @@ import ( "time" ) +const defaultCleanupTimeFraction = 4 +const defaultMinimumCleanupTime = time.Minute +const defaultMaximumCleanupTime = 5 * time.Minute + // Context creates a context with a deadline that allows for enough time to clean up a test before the testing framework -// unceremoniously kills the process. The desired time is expressed as a fraction of the time remaining until the hard -// timeout, such as 4 for 25%. The minimumCleanupTime and maximumCleanupTime clamp the remaining time. -// -// For a simpler to use version of this function call tofutestutils.Context. -func Context(t *testing.T, cleanupTimeFraction int64, minimumCleanupTime time.Duration, maximumCleanupTime time.Duration) context.Context { +// unceremoniously kills the process. Starting with Go 1.24, this function is an alias for t.Context(). In Go 1.23 and +// lower this function reserves 25% of the test timeout, minimum 1 minute, maximum 5 minutes for cleanup. +func Context(t *testing.T) context.Context { + return contextWithParameters(t, defaultCleanupTimeFraction, defaultMinimumCleanupTime, defaultMaximumCleanupTime) +} + +func contextWithParameters( + t *testing.T, + cleanupTimeFraction uint, + minimumCleanupTime time.Duration, + maximumCleanupTime time.Duration, +) context.Context { ctx := context.Background() if deadline, ok := t.Deadline(); ok { var cancel func() diff --git a/testcontext/context_test.go b/testcontext/context_before_1.24_test.go similarity index 88% rename from testcontext/context_test.go rename to testcontext/context_before_1.24_test.go index 25851a1..9839243 100644 --- a/testcontext/context_test.go +++ b/testcontext/context_before_1.24_test.go @@ -3,18 +3,18 @@ // Copyright (c) 2023 HashiCorp, Inc. // SPDX-License-Identifier: MPL-2.0 -package testcontext_test +//go:build !go1.24 + +package testcontext import ( "testing" "time" - - "github.com/opentofu/tofutestutils/testcontext" ) func TestContext(t *testing.T) { const checkTime = 20 * time.Second - ctx := testcontext.Context( + ctx := contextWithParameters( t, 4, 30*time.Second,