54a8b49c5e
As pointed out by @tgross[1], prior to this change we would have been blocking until all managers waited for first fingerprint rather than timing out as intended. 1: https://github.com/hashicorp/nomad/pull/9590#discussion_r539534906
122 lines
2.6 KiB
Go
122 lines
2.6 KiB
Go
package pluginmanager
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestPluginGroup_RegisterAndRun(t *testing.T) {
|
|
t.Parallel()
|
|
require := require.New(t)
|
|
|
|
var hasRun bool
|
|
var wg sync.WaitGroup
|
|
wg.Add(1)
|
|
manager := &MockPluginManager{RunF: func() {
|
|
hasRun = true
|
|
wg.Done()
|
|
}}
|
|
|
|
group := New(testlog.HCLogger(t))
|
|
require.NoError(group.RegisterAndRun(manager))
|
|
wg.Wait()
|
|
require.True(hasRun)
|
|
}
|
|
|
|
func TestPluginGroup_Shutdown(t *testing.T) {
|
|
t.Parallel()
|
|
require := require.New(t)
|
|
|
|
var stack []int
|
|
var stackMu sync.Mutex
|
|
var runWg sync.WaitGroup
|
|
var shutdownWg sync.WaitGroup
|
|
group := New(testlog.HCLogger(t))
|
|
for i := 1; i < 4; i++ {
|
|
i := i
|
|
runWg.Add(1)
|
|
shutdownWg.Add(1)
|
|
manager := &MockPluginManager{RunF: func() {
|
|
stackMu.Lock()
|
|
defer stackMu.Unlock()
|
|
defer runWg.Done()
|
|
stack = append(stack, i)
|
|
}, ShutdownF: func() {
|
|
stackMu.Lock()
|
|
defer stackMu.Unlock()
|
|
defer shutdownWg.Done()
|
|
idx := len(stack) - 1
|
|
val := stack[idx]
|
|
require.Equal(i, val)
|
|
stack = stack[:idx]
|
|
}}
|
|
require.NoError(group.RegisterAndRun(manager))
|
|
runWg.Wait()
|
|
}
|
|
group.Shutdown()
|
|
shutdownWg.Wait()
|
|
require.Empty(stack)
|
|
|
|
require.Error(group.RegisterAndRun(&MockPluginManager{}))
|
|
}
|
|
|
|
func TestPluginGroup_WaitForFirstFingerprint(t *testing.T) {
|
|
t.Parallel()
|
|
require := require.New(t)
|
|
|
|
managerCh := make(chan struct{})
|
|
manager := &MockPluginManager{
|
|
RunF: func() {},
|
|
WaitForFirstFingerprintCh: managerCh,
|
|
}
|
|
|
|
// close immediately to beat the context timeout
|
|
close(managerCh)
|
|
|
|
group := New(testlog.HCLogger(t))
|
|
require.NoError(group.RegisterAndRun(manager))
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
|
defer cancel()
|
|
|
|
groupCh, err := group.WaitForFirstFingerprint(ctx)
|
|
require.NoError(err)
|
|
|
|
select {
|
|
case <-groupCh:
|
|
case <-time.After(100 * time.Millisecond):
|
|
t.Fatal("expected groupCh to be closed")
|
|
}
|
|
}
|
|
|
|
func TestPluginGroup_WaitForFirstFingerprint_Timeout(t *testing.T) {
|
|
t.Parallel()
|
|
require := require.New(t)
|
|
|
|
managerCh := make(chan struct{})
|
|
manager := &MockPluginManager{
|
|
RunF: func() {},
|
|
WaitForFirstFingerprintCh: managerCh,
|
|
}
|
|
|
|
group := New(testlog.HCLogger(t))
|
|
require.NoError(group.RegisterAndRun(manager))
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond)
|
|
defer cancel()
|
|
|
|
groupCh, err := group.WaitForFirstFingerprint(ctx)
|
|
|
|
select {
|
|
case <-groupCh:
|
|
case <-time.After(100 * time.Millisecond):
|
|
t.Fatal("expected groupCh to be closed due to context timeout")
|
|
}
|
|
require.NoError(err)
|
|
}
|