open-nomad/helper/broker/notify_test.go
James Rasell 181b247384
core: allow pausing and un-pausing of leader broker routine (#13045)
* core: allow pause/un-pause of eval broker on region leader.

* agent: add ability to pause eval broker via scheduler config.

* cli: add operator scheduler commands to interact with config.

* api: add ability to pause eval broker via scheduler config

* e2e: add operator scheduler test for eval broker pause.

* docs: include new opertor scheduler CLI and pause eval API info.
2022-07-06 16:13:48 +02:00

56 lines
1.2 KiB
Go

package broker
import (
"sync"
"testing"
"time"
"github.com/hashicorp/nomad/ci"
"github.com/stretchr/testify/require"
)
func TestGenericNotifier(t *testing.T) {
ci.Parallel(t)
// Create the new notifier.
stopChan := make(chan struct{})
defer close(stopChan)
notifier := NewGenericNotifier()
go notifier.Run(stopChan)
// Ensure we have buffered channels.
require.Equal(t, 1, cap(notifier.publishCh))
require.Equal(t, 1, cap(notifier.subscribeCh))
require.Equal(t, 1, cap(notifier.unsubscribeCh))
// Test that the timeout works.
var timeoutWG sync.WaitGroup
for i := 0; i < 6; i++ {
go func(wg *sync.WaitGroup) {
wg.Add(1)
msg := notifier.WaitForChange(100 * time.Millisecond)
require.Equal(t, "wait timed out after 100ms", msg)
wg.Done()
}(&timeoutWG)
}
timeoutWG.Wait()
// Test that all subscribers recieve an update when a single notification
// is sent.
var notifiedWG sync.WaitGroup
for i := 0; i < 6; i++ {
go func(wg *sync.WaitGroup) {
wg.Add(1)
msg := notifier.WaitForChange(3 * time.Second)
require.Equal(t, "we got an update and not a timeout", msg)
wg.Done()
}(&notifiedWG)
}
notifier.Notify("we got an update and not a timeout")
notifiedWG.Wait()
}