-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Brian Mendoza
committed
Apr 24, 2024
1 parent
ea505af
commit 3341a63
Showing
9 changed files
with
155 additions
and
56 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
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
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
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
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
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
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
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
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,92 @@ | ||
package mocktest | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/onsi/ginkgo/v2" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/linode/cluster-api-provider-linode/mock" | ||
) | ||
|
||
func TestSuitesNoClients(t *testing.T) { | ||
t.Parallel() | ||
|
||
assert.Panics(t, func() { NewSuite(t) }) | ||
assert.Panics(t, func() { NewControllerSuite(ginkgo.GinkgoT()) }) | ||
} | ||
|
||
func TestSuite(t *testing.T) { | ||
t.Parallel() | ||
|
||
//nolint:paralleltest // these tests should run prior to their nested t.Run | ||
for _, testCase := range []struct { | ||
name string | ||
beforeEach, afterEach bool | ||
expectedCount int | ||
}{ | ||
{ | ||
name: "beforeEach", | ||
beforeEach: true, | ||
expectedCount: 6, | ||
}, | ||
{ | ||
name: "afterEach", | ||
afterEach: true, | ||
expectedCount: 6, | ||
}, | ||
{ | ||
name: "both", | ||
beforeEach: true, | ||
afterEach: true, | ||
expectedCount: 8, | ||
}, | ||
} { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
// Create a counter with the expected number of calls. | ||
// As each call runs, the counter will decrement to 0. | ||
var counter sync.WaitGroup | ||
counter.Add(testCase.expectedCount) | ||
|
||
suite := NewSuite(t, mock.MockK8sClient{}) | ||
if testCase.beforeEach { | ||
suite.BeforeEach(func(_ context.Context, _ Mock) { counter.Done() }) | ||
} | ||
if testCase.afterEach { | ||
suite.AfterEach(func(_ context.Context, _ Mock) { counter.Done() }) | ||
} | ||
|
||
suite.Run(Paths( | ||
Either( | ||
Call("", func(_ context.Context, _ Mock) { counter.Done() }), | ||
Call("", func(_ context.Context, _ Mock) { counter.Done() }), | ||
), | ||
Result("", func(_ context.Context, _ Mock) { counter.Done() }), | ||
)) | ||
|
||
// Wait until the counter reaches 0, or time out. | ||
// This runs in a goroutine so the nested t.Runs are scheduled. | ||
go func() { | ||
select { | ||
case <-waitCh(&counter): | ||
return | ||
case <-time.NewTimer(time.Second * 5).C: | ||
assert.Error(t, errors.New(testCase.name)) | ||
} | ||
}() | ||
}) | ||
} | ||
} | ||
|
||
func waitCh(counter *sync.WaitGroup) <-chan struct{} { | ||
out := make(chan struct{}) | ||
go func() { | ||
counter.Wait() | ||
out <- struct{}{} | ||
}() | ||
return out | ||
} |