Skip to content

Commit

Permalink
Adding support for Go 1.24 test contexts
Browse files Browse the repository at this point in the history
Signed-off-by: AbstractionFactory <[email protected]>
  • Loading branch information
abstractionfactory committed Jan 22, 2025
1 parent 63248f3 commit 2e11b56
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 23 deletions.
21 changes: 7 additions & 14 deletions context.go
Original file line number Diff line number Diff line change
@@ -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)
}
15 changes: 15 additions & 0 deletions testcontext/context_1.24.go
Original file line number Diff line number Diff line change
@@ -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()
}
23 changes: 18 additions & 5 deletions testcontext/context.go → testcontext/context_before_1.24.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// Copyright (c) 2023 HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

//go:build !go1.24

package testcontext

import (
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 2e11b56

Please sign in to comment.