Skip to content

Commit

Permalink
Better shutdown of rmn home poller and home poller (#375)
Browse files Browse the repository at this point in the history
* better shutdown of rmn home poller

* add test

* home chain poller close
  • Loading branch information
dimkouv authored Dec 17, 2024
1 parent 4f5dfb0 commit ce8cbe5
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
8 changes: 7 additions & 1 deletion internal/reader/home_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package reader

import (
"context"
"errors"
"fmt"
"sync"
"time"
Expand Down Expand Up @@ -247,11 +248,16 @@ func (r *homeChainPoller) GetOCRConfigs(
}

func (r *homeChainPoller) Close() error {
return r.sync.StopOnce(r.Name(), func() error {
err := r.sync.StopOnce(r.Name(), func() error {
defer r.wg.Wait()
close(r.stopCh)
return nil
})

if errors.Is(err, services.ErrAlreadyStopped) {
return nil
}
return err
}

func (r *homeChainPoller) Ready() error {
Expand Down
9 changes: 8 additions & 1 deletion pkg/reader/rmn_home.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package reader
import (
"context"
"crypto/ed25519"
"errors"
"fmt"
"math/big"
"sync"
Expand Down Expand Up @@ -223,11 +224,17 @@ func (r *rmnHomePoller) GetAllConfigDigests() (
}

func (r *rmnHomePoller) Close() error {
return r.sync.StopOnce(r.Name(), func() error {
err := r.sync.StopOnce(r.Name(), func() error {
defer r.wg.Wait()
close(r.stopCh)
return nil
})

if errors.Is(err, services.ErrAlreadyStopped) {
return nil
}

return err
}

func (r *rmnHomePoller) Ready() error {
Expand Down
39 changes: 39 additions & 0 deletions pkg/reader/rmn_home_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"
"time"

tests "github.com/smartcontractkit/chainlink-common/pkg/utils/tests"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -271,6 +272,44 @@ func Test_RMNHomePollingWorking(t *testing.T) {
}
}

func Test_RMNHomePoller_Close(t *testing.T) {
homeChainReader := readermock.NewMockContractReaderFacade(t)
poller := NewRMNHomePoller(
homeChainReader,
rmnHomeBoundContract,
logger.Test(t),
10*time.Millisecond,
).(*rmnHomePoller)

homeChainReader.On("GetLatestValue",
mock.Anything,
mock.Anything,
mock.Anything,
mock.Anything,
mock.Anything,
).Run(func(args mock.Arguments) {
result := args.Get(4).(*GetAllConfigsResponse)
*result = GetAllConfigsResponse{
ActiveConfig: VersionedConfig{
ConfigDigest: [32]byte{1},
Version: 1,
StaticConfig: StaticConfig{Nodes: []Node{}},
DynamicConfig: DynamicConfig{SourceChains: []SourceChain{}},
},
}
}).Return(nil)

ctx := tests.Context(t)
require.NoError(t, poller.Start(ctx))

err1 := poller.Close()
require.NoError(t, err1)
err2 := poller.Close()
require.NoError(t, err2)
err3 := poller.Close()
require.NoError(t, err3)
}

func TestIsNodeObserver(t *testing.T) {
tests := []struct {
name string
Expand Down

0 comments on commit ce8cbe5

Please sign in to comment.