2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2022-09-26 18:58:15 +00:00
|
|
|
package hcp
|
|
|
|
|
|
|
|
import (
|
2022-11-10 16:26:01 +00:00
|
|
|
"io"
|
2022-09-26 18:58:15 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestManager_Run(t *testing.T) {
|
|
|
|
client := NewMockClient(t)
|
|
|
|
statusF := func(ctx context.Context) (ServerStatus, error) {
|
|
|
|
return ServerStatus{ID: t.Name()}, nil
|
|
|
|
}
|
|
|
|
updateCh := make(chan struct{}, 1)
|
|
|
|
client.EXPECT().PushServerStatus(mock.Anything, &ServerStatus{ID: t.Name()}).Return(nil).Once()
|
|
|
|
mgr := NewManager(ManagerConfig{
|
|
|
|
Client: client,
|
2022-11-10 16:26:01 +00:00
|
|
|
Logger: hclog.New(&hclog.LoggerOptions{Output: io.Discard}),
|
2022-09-26 18:58:15 +00:00
|
|
|
StatusFn: statusF,
|
|
|
|
})
|
|
|
|
mgr.testUpdateSent = updateCh
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
t.Cleanup(cancel)
|
|
|
|
|
|
|
|
go mgr.Run(ctx)
|
|
|
|
select {
|
|
|
|
case <-updateCh:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
require.Fail(t, "manager did not send update in expected time")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure after manager has stopped no more statuses are pushed.
|
|
|
|
cancel()
|
|
|
|
mgr.SendUpdate()
|
|
|
|
client.AssertExpectations(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestManager_SendUpdate(t *testing.T) {
|
|
|
|
client := NewMockClient(t)
|
|
|
|
statusF := func(ctx context.Context) (ServerStatus, error) {
|
|
|
|
return ServerStatus{ID: t.Name()}, nil
|
|
|
|
}
|
|
|
|
updateCh := make(chan struct{}, 1)
|
|
|
|
|
|
|
|
// Expect two calls, once during run startup and again when SendUpdate is called
|
|
|
|
client.EXPECT().PushServerStatus(mock.Anything, &ServerStatus{ID: t.Name()}).Return(nil).Twice()
|
|
|
|
mgr := NewManager(ManagerConfig{
|
|
|
|
Client: client,
|
2022-11-10 16:26:01 +00:00
|
|
|
Logger: hclog.New(&hclog.LoggerOptions{Output: io.Discard}),
|
2022-09-26 18:58:15 +00:00
|
|
|
StatusFn: statusF,
|
|
|
|
})
|
|
|
|
mgr.testUpdateSent = updateCh
|
|
|
|
go mgr.Run(context.Background())
|
|
|
|
select {
|
|
|
|
case <-updateCh:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
require.Fail(t, "manager did not send update in expected time")
|
|
|
|
}
|
|
|
|
mgr.SendUpdate()
|
|
|
|
select {
|
|
|
|
case <-updateCh:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
require.Fail(t, "manager did not send update in expected time")
|
|
|
|
}
|
|
|
|
client.AssertExpectations(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestManager_SendUpdate_Periodic(t *testing.T) {
|
|
|
|
client := NewMockClient(t)
|
|
|
|
statusF := func(ctx context.Context) (ServerStatus, error) {
|
|
|
|
return ServerStatus{ID: t.Name()}, nil
|
|
|
|
}
|
|
|
|
updateCh := make(chan struct{}, 1)
|
|
|
|
|
|
|
|
// Expect two calls, once during run startup and again when SendUpdate is called
|
|
|
|
client.EXPECT().PushServerStatus(mock.Anything, &ServerStatus{ID: t.Name()}).Return(nil).Twice()
|
|
|
|
mgr := NewManager(ManagerConfig{
|
|
|
|
Client: client,
|
2022-11-10 16:26:01 +00:00
|
|
|
Logger: hclog.New(&hclog.LoggerOptions{Output: io.Discard}),
|
2022-09-26 18:58:15 +00:00
|
|
|
StatusFn: statusF,
|
|
|
|
MaxInterval: time.Second,
|
|
|
|
MinInterval: 100 * time.Millisecond,
|
|
|
|
})
|
|
|
|
mgr.testUpdateSent = updateCh
|
|
|
|
go mgr.Run(context.Background())
|
|
|
|
select {
|
|
|
|
case <-updateCh:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
require.Fail(t, "manager did not send update in expected time")
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-updateCh:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
require.Fail(t, "manager did not send update in expected time")
|
|
|
|
}
|
|
|
|
client.AssertExpectations(t)
|
|
|
|
}
|