Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions libs/capability/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/smartcontractkit/chainlink-common/pkg/capabilities/pb"
"github.com/smartcontractkit/chainlink-common/pkg/logger"
"github.com/smartcontractkit/chainlink-common/pkg/loop"
"github.com/smartcontractkit/chainlink-common/pkg/settings/cresettings"
"github.com/smartcontractkit/chainlink-common/pkg/types/core"
)
Expand Down Expand Up @@ -42,7 +43,7 @@ func TestFollowHostSettingsAppliesUpdates(t *testing.T) {
require.NoError(t, err)

host := newFakeBroadcaster()
go followHostSettings(t.Context(), logger.Test(t), host, s)
host.follow(t, s)

host.send(t, core.SettingsUpdate{Settings: "[global]\nTriggerRegistrationStatusUpdateTimeout = \"15s\"\n", Hash: "a"})

Expand All @@ -60,7 +61,7 @@ func TestFollowHostSettingsKeepsThePreviousSettingsOnFailure(t *testing.T) {
require.NoError(t, err)

host := newFakeBroadcaster()
go followHostSettings(t.Context(), logger.Test(t), host, s)
host.follow(t, s)

host.send(t, core.SettingsUpdate{Settings: "[global]\nTriggerRegistrationStatusUpdateTimeout = \"15s\"\n", Hash: "a"})
host.send(t, core.SettingsUpdate{Settings: "not a settings payload", Hash: "b"})
Expand All @@ -79,7 +80,7 @@ func TestFollowHostSettingsResubscribes(t *testing.T) {
host := newFakeBroadcaster()
host.failFirst.Store(true)

go followHostSettings(t.Context(), logger.Test(t), host, s)
host.follow(t, s)

host.send(t, core.SettingsUpdate{Settings: "[global]\n", Hash: "a"})
host.closeStream()
Expand All @@ -100,8 +101,36 @@ func newFakeBroadcaster() *fakeBroadcaster {
return &fakeBroadcaster{streams: make(chan chan core.SettingsUpdate, 1)}
}

func (f *fakeBroadcaster) Subscribe(context.Context) (<-chan core.SettingsUpdate, error) {
// follow runs followHostSettings against f until the test ends, then waits for it to return, so
// nothing it logs can land after the test has completed.
func (f *fakeBroadcaster) follow(t *testing.T, settings *loop.AtomicSettings) {
ctx, cancel := context.WithCancel(context.Background())
done := make(chan struct{})
go func() {
defer close(done)
followHostSettings(ctx, logger.Test(t), f, settings)
}()
t.Cleanup(func() {
cancel()
if f.current == nil {
// Wait for the follower's current subscription, if any, so it can be ended.
select {
case f.current = <-f.streams:
case <-done:
}
}
if f.current != nil {
f.closeStream()
}
<-done
})
}

func (f *fakeBroadcaster) Subscribe(ctx context.Context) (<-chan core.SettingsUpdate, error) {
f.subscriptions.Add(1)
if err := ctx.Err(); err != nil {
return nil, err
}
if f.failFirst.CompareAndSwap(true, false) {
return nil, errors.New("host unavailable")
}
Expand Down
Loading