Skip to content

Commit

Permalink
Update poller
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanTinianov committed Apr 24, 2024
1 parent e05b739 commit daa79f0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
8 changes: 5 additions & 3 deletions common/client/poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Poller[
pollingInterval time.Duration
pollingFunc func(ctx context.Context) (T, error)
pollingTimeout *time.Duration
logger logger.Logger
logger *logger.Logger
channel chan<- T
errCh chan error

Expand All @@ -32,7 +32,7 @@ type Poller[
// NewPoller creates a new Poller instance
func NewPoller[
T any,
](pollingInterval time.Duration, pollingFunc func(ctx context.Context) (T, error), pollingTimeout *time.Duration, channel chan<- T, logger logger.Logger) Poller[T] {
](pollingInterval time.Duration, pollingFunc func(ctx context.Context) (T, error), pollingTimeout *time.Duration, channel chan<- T, logger *logger.Logger) Poller[T] {
return Poller[T]{
pollingInterval: pollingInterval,
pollingFunc: pollingFunc,
Expand Down Expand Up @@ -87,7 +87,9 @@ func (p *Poller[T]) pollingLoop() {
result, err := p.pollingFunc(ctx)
cancel()
if err != nil {
p.logger.Warnw("Polling error", "error", err)
if p.logger != nil {
(*p.logger).Warnw("Polling error", "error", err)
}
select {
case p.errCh <- err:
continue
Expand Down
23 changes: 10 additions & 13 deletions common/client/poller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/smartcontractkit/chainlink-common/pkg/logger"
)

// TODO: Fix race conditions in tests!

func Test_Poller(t *testing.T) {
pollingTimeout := 10 * time.Millisecond
lggr, err := logger.New()
Expand All @@ -34,22 +36,21 @@ func Test_Poller(t *testing.T) {
channel := make(chan Head, 1)

// Create poller and start to receive data
poller := NewPoller[Head](time.Millisecond, pollFunc, &pollingTimeout, channel, lggr)
poller := NewPoller[Head](time.Millisecond, pollFunc, &pollingTimeout, channel, &lggr)
require.NoError(t, poller.Start())
defer poller.Unsubscribe()

done := make(chan struct{})
// Create goroutine to receive updates from the poller
pollCount := 0
pollMax := 50
done := make(chan struct{})
go func() {
pollCount := 0
pollMax := 50
for ; pollCount < pollMax; pollCount++ {
h := <-channel
assert.Equal(t, int64(pollNumber), h.BlockNumber())
}
close(done)
}()

<-done
})

Expand All @@ -63,17 +64,15 @@ func Test_Poller(t *testing.T) {
channel := make(chan Head, 1)

// Create poller and subscribe to receive data
poller := NewPoller[Head](time.Millisecond, pollFunc, &pollingTimeout, channel, lggr)

poller := NewPoller[Head](time.Millisecond, pollFunc, &pollingTimeout, channel, &lggr)
require.NoError(t, poller.Start())
defer poller.Unsubscribe()

done := make(chan struct{})

// Create goroutine to receive updates from the poller
pollCount := 0
pollMax := 50
done := make(chan struct{})
go func() {
pollCount := 0
pollMax := 50
for ; pollCount < pollMax; pollCount++ {
select {
case <-channel:
Expand All @@ -85,14 +84,12 @@ func Test_Poller(t *testing.T) {
}
close(done)
}()

<-done
})
}

func Test_Poller_Unsubscribe(t *testing.T) {
t.Run("Test multiple unsubscribe", func(t *testing.T) {
// TODO: to the p.channel. And one that ensure we can call Unsubscribe twice without panic.
poller := NewPoller[Head](time.Millisecond, nil, nil, nil, nil)
err := poller.Start()
require.NoError(t, err)
Expand Down

0 comments on commit daa79f0

Please sign in to comment.