-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cappl 391 wire in workflow registry (#15460)
* don notifier added * wip * wip * wire up of wf syncer * test udpate * srvcs fix * review comments * lint * attempt to fix ci tests
- Loading branch information
Showing
11 changed files
with
332 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package capabilities | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
"github.com/smartcontractkit/chainlink-common/pkg/capabilities" | ||
) | ||
|
||
type DonNotifier struct { | ||
mu sync.Mutex | ||
don capabilities.DON | ||
notified bool | ||
ch chan struct{} | ||
} | ||
|
||
func NewDonNotifier() *DonNotifier { | ||
return &DonNotifier{ | ||
ch: make(chan struct{}), | ||
} | ||
} | ||
|
||
func (n *DonNotifier) NotifyDonSet(don capabilities.DON) { | ||
n.mu.Lock() | ||
defer n.mu.Unlock() | ||
if !n.notified { | ||
n.don = don | ||
n.notified = true | ||
close(n.ch) | ||
} | ||
} | ||
|
||
func (n *DonNotifier) WaitForDon(ctx context.Context) (capabilities.DON, error) { | ||
select { | ||
case <-ctx.Done(): | ||
return capabilities.DON{}, ctx.Err() | ||
case <-n.ch: | ||
} | ||
<-n.ch | ||
n.mu.Lock() | ||
defer n.mu.Unlock() | ||
return n.don, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package capabilities_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
commoncap "github.com/smartcontractkit/chainlink-common/pkg/capabilities" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/smartcontractkit/chainlink/v2/core/capabilities" | ||
) | ||
|
||
func TestDonNotifier_WaitForDon(t *testing.T) { | ||
notifier := capabilities.NewDonNotifier() | ||
don := commoncap.DON{ | ||
ID: 1, | ||
} | ||
|
||
go func() { | ||
time.Sleep(100 * time.Millisecond) | ||
notifier.NotifyDonSet(don) | ||
}() | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) | ||
defer cancel() | ||
|
||
result, err := notifier.WaitForDon(ctx) | ||
require.NoError(t, err) | ||
assert.Equal(t, don, result) | ||
|
||
result, err = notifier.WaitForDon(ctx) | ||
require.NoError(t, err) | ||
assert.Equal(t, don, result) | ||
} | ||
|
||
func TestDonNotifier_WaitForDon_ContextTimeout(t *testing.T) { | ||
notifier := capabilities.NewDonNotifier() | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond) | ||
defer cancel() | ||
|
||
_, err := notifier.WaitForDon(ctx) | ||
require.Error(t, err) | ||
assert.Equal(t, context.DeadlineExceeded, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.