File tree Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -75,6 +75,15 @@ type TContext interface {
75
75
context.Context
76
76
TB
77
77
78
+ // Parallel signals that this test is to be run in parallel with (and
79
+ // only with) other parallel tests. In other words, it needs to be
80
+ // called in each test which is meant to run in parallel.
81
+ //
82
+ // Only supported in Go unit tests. When such a test is run multiple
83
+ // times due to use of -test.count or -test.cpu, multiple instances of
84
+ // a single test never run in parallel with each other.
85
+ Parallel ()
86
+
78
87
// Cancel can be invoked to cancel the context before the test is completed.
79
88
// Tests which use the context to control goroutines and then wait for
80
89
// termination of those goroutines must call Cancel to avoid a deadlock.
@@ -381,6 +390,12 @@ type testingTB struct {
381
390
TB
382
391
}
383
392
393
+ func (tCtx tContext ) Parallel () {
394
+ if tb , ok := tCtx .TB ().(interface { Parallel () }); ok {
395
+ tb .Parallel ()
396
+ }
397
+ }
398
+
384
399
func (tCtx tContext ) Cancel (cause string ) {
385
400
if tCtx .cancel != nil {
386
401
tCtx .cancel (cause )
Original file line number Diff line number Diff line change @@ -86,6 +86,23 @@ func TestCancelCtx(t *testing.T) {
86
86
tCtx .Cancel ("test is complete" )
87
87
}
88
88
89
+ func TestParallel (t * testing.T ) {
90
+ var wg sync.WaitGroup
91
+ wg .Add (3 )
92
+
93
+ tCtx := ktesting .Init (t )
94
+
95
+ // Each sub-test runs in parallel to the others and waits for the other two.
96
+ test := func (tCtx ktesting.TContext ) {
97
+ tCtx .Parallel ()
98
+ wg .Done ()
99
+ wg .Wait ()
100
+ }
101
+ tCtx .Run ("one" , test )
102
+ tCtx .Run ("two" , test )
103
+ tCtx .Run ("three" , test )
104
+ }
105
+
89
106
func TestWithTB (t * testing.T ) {
90
107
tCtx := ktesting .Init (t )
91
108
You can’t perform that action at this time.
0 commit comments