From 28f1beff646498c1977bea397d6d4d0c5e024fd8 Mon Sep 17 00:00:00 2001 From: Ekaterina Pavlova Date: Mon, 18 Dec 2023 22:36:53 +0300 Subject: [PATCH] rpcclient: export NewWaiter function Change first argument of NewWaiter to be able to directly accept RPC Client and export for external usage. Refs #3244. Signed-off-by: Ekaterina Pavlova --- pkg/rpcclient/actor/actor.go | 2 +- pkg/rpcclient/actor/waiter.go | 13 ++++++++----- pkg/rpcclient/actor/waiter_test.go | 10 +++++----- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/pkg/rpcclient/actor/actor.go b/pkg/rpcclient/actor/actor.go index 35ae9346a..bf3ec334e 100644 --- a/pkg/rpcclient/actor/actor.go +++ b/pkg/rpcclient/actor/actor.go @@ -126,7 +126,7 @@ func New(ra RPCActor, signers []SignerAccount) (*Actor, error) { } return &Actor{ Invoker: *inv, - Waiter: newWaiter(ra, version), + Waiter: NewWaiter(ra, version), client: ra, opts: NewDefaultOptions(), signers: signers, diff --git a/pkg/rpcclient/actor/waiter.go b/pkg/rpcclient/actor/waiter.go index d37b4383b..183660846 100644 --- a/pkg/rpcclient/actor/waiter.go +++ b/pkg/rpcclient/actor/waiter.go @@ -100,10 +100,13 @@ func errIsAlreadyExists(err error) bool { return strings.Contains(strings.ToLower(err.Error()), "already exists") } -// newWaiter creates Waiter instance. It can be either websocket-based or -// polling-base, otherwise Waiter stub is returned. -func newWaiter(ra RPCActor, v *result.Version) Waiter { - if eventW, ok := ra.(RPCEventWaiter); ok { +// NewWaiter creates Waiter instance. It can be either websocket-based or +// polling-base, otherwise Waiter stub is returned. As a first argument +// it accepts RPCEventWaiter implementation, RPCPollingWaiter implementation +// or not an implementation of these two interfaces. It returns websocket-based +// waiter, polling-based waiter or a stub correspondingly. +func NewWaiter(base any, v *result.Version) Waiter { + if eventW, ok := base.(RPCEventWaiter); ok { return &EventWaiter{ ws: eventW, polling: &PollingWaiter{ @@ -112,7 +115,7 @@ func newWaiter(ra RPCActor, v *result.Version) Waiter { }, } } - if pollW, ok := ra.(RPCPollingWaiter); ok { + if pollW, ok := base.(RPCPollingWaiter); ok { return &PollingWaiter{ polling: pollW, version: v, diff --git a/pkg/rpcclient/actor/waiter_test.go b/pkg/rpcclient/actor/waiter_test.go index f6b06fb00..495e62815 100644 --- a/pkg/rpcclient/actor/waiter_test.go +++ b/pkg/rpcclient/actor/waiter_test.go @@ -38,15 +38,15 @@ func (c *AwaitableRPCClient) ReceiveExecutions(flt *neorpc.ExecutionFilter, rcvr func (c *AwaitableRPCClient) Unsubscribe(id string) error { return nil } func TestNewWaiter(t *testing.T) { - w := newWaiter((RPCActor)(nil), nil) + w := NewWaiter((RPCActor)(nil), nil) _, ok := w.(NullWaiter) require.True(t, ok) - w = newWaiter(&RPCClient{}, &result.Version{}) + w = NewWaiter(&RPCClient{}, &result.Version{}) _, ok = w.(*PollingWaiter) require.True(t, ok) - w = newWaiter(&AwaitableRPCClient{RPCClient: RPCClient{}}, &result.Version{}) + w = NewWaiter(&AwaitableRPCClient{RPCClient: RPCClient{}}, &result.Version{}) _, ok = w.(*EventWaiter) require.True(t, ok) } @@ -58,7 +58,7 @@ func TestPollingWaiter_Wait(t *testing.T) { expected := &state.AppExecResult{Container: h, Execution: state.Execution{}} c := &RPCClient{appLog: appLog} c.bCount.Store(bCount) - w := newWaiter(c, &result.Version{Protocol: result.Protocol{MillisecondsPerBlock: 1}}) // reduce testing time. + w := NewWaiter(c, &result.Version{Protocol: result.Protocol{MillisecondsPerBlock: 1}}) // reduce testing time. _, ok := w.(*PollingWaiter) require.True(t, ok) @@ -123,7 +123,7 @@ func TestWSWaiter_Wait(t *testing.T) { expected := &state.AppExecResult{Container: h, Execution: state.Execution{}} c := &AwaitableRPCClient{RPCClient: RPCClient{appLog: appLog}} c.bCount.Store(bCount) - w := newWaiter(c, &result.Version{Protocol: result.Protocol{MillisecondsPerBlock: 1}}) // reduce testing time. + w := NewWaiter(c, &result.Version{Protocol: result.Protocol{MillisecondsPerBlock: 1}}) // reduce testing time. _, ok := w.(*EventWaiter) require.True(t, ok)