-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Cancel as an example of an injector
- Loading branch information
Showing
4 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package ntest | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
// This file contains example injectors that may be useful | ||
|
||
// Cancel is the injected type for the function type that will cancel | ||
// a Context that has been augmented with AutoCancel. | ||
type Cancel func() | ||
|
||
// AutoCancel adjusts context.Context so that it will be cancelled | ||
// when the test finishes. It can be cancelled early by calling | ||
// the returned Cancel function. | ||
func AutoCancel(ctx context.Context, t T) (context.Context, Cancel) { | ||
ctx, cancel := context.WithCancel(ctx) | ||
onlyOnce := onceFunc(cancel) | ||
t.Cleanup(onlyOnce) | ||
return ctx, onlyOnce | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package ntest_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/memsql/ntest" | ||
) | ||
|
||
func TestCancel(t *testing.T) { | ||
t.Parallel() | ||
ntest.RunTest(t, context.Background, ntest.AutoCancel, func( | ||
ctx context.Context, | ||
cancel ntest.Cancel, | ||
) { | ||
select { | ||
case <-ctx.Done(): | ||
t.Fatal("context is cancelled before cancel()") | ||
default: | ||
} | ||
cancel() | ||
select { | ||
case <-ctx.Done(): | ||
t.Log("context is cancelled") | ||
default: | ||
t.Fatal("context is not cancelled after cancel()") | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
//go:build go1.21 | ||
|
||
package ntest | ||
|
||
import "sync" | ||
|
||
var onceFunc = sync.OnceFunc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
//go:build !go1.21 | ||
|
||
package ntest | ||
|
||
import "sync" | ||
|
||
func onceFunc(f func()) func() { | ||
var once sync.Once | ||
return func() { | ||
once.Do(f) | ||
} | ||
} |