Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BCI-3989][common] - CR methods err when service unstarted #705

Draft
wants to merge 38 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
352adfe
check cr service is started
Farber98 Aug 14, 2024
173eca4
small refactor
Farber98 Aug 15, 2024
e16b694
Merge branch 'main' into BCI-3989-cr-methods-error-when-unstarted
Farber98 Aug 15, 2024
0d8a1b0
remove unused error
Farber98 Aug 15, 2024
af1fabf
bind before start
Farber98 Aug 15, 2024
4dcb052
add comments to tests
Farber98 Aug 15, 2024
4d966e6
startChainReader method
Farber98 Aug 15, 2024
5006bec
check methods return error when called without starting service
Farber98 Aug 16, 2024
cc3066b
Revert "check methods return error when called without starting service"
Farber98 Aug 16, 2024
b697ce2
service started checked working for both get latest value methods
Farber98 Aug 16, 2024
855b38c
close chain reader after tests
Farber98 Aug 16, 2024
fe91e30
Merge branch 'main' into BCI-3989-cr-methods-error-when-unstarted
Farber98 Aug 22, 2024
0cf190e
remove duplicate fields
Farber98 Aug 22, 2024
8e1388b
rename interface tester new methods
Farber98 Aug 23, 2024
72d3790
Merge branch 'main' into BCI-3989-cr-methods-error-when-unstarted
Farber98 Aug 23, 2024
215fb70
make start and close safe for concurrent use
Farber98 Aug 23, 2024
585ccc9
use atomic bool for isStarted flag
Farber98 Aug 24, 2024
81aaf5c
Merge branch 'main' into BCI-3989-cr-methods-error-when-unstarted
Farber98 Aug 26, 2024
1830e25
remove start as only closed is needed
Farber98 Aug 26, 2024
dd6363c
Merge branch 'main' into BCI-3989-cr-methods-error-when-unstarted
Farber98 Aug 28, 2024
dd26cd7
Merge branch 'main' into BCI-3989-cr-methods-error-when-unstarted
Farber98 Aug 28, 2024
50c55ef
Merge branch 'main' into BCI-3989-cr-methods-error-when-unstarted
Farber98 Aug 29, 2024
699bdf8
Merge branch 'main' into BCI-3989-cr-methods-error-when-unstarted
Farber98 Aug 30, 2024
fa1ace8
Merge branch 'main' into BCI-3989-cr-methods-error-when-unstarted
Farber98 Sep 3, 2024
bed6fbf
Merge branch 'main' into BCI-3989-cr-methods-error-when-unstarted
Farber98 Sep 4, 2024
208ef02
add start method to the interface. add start and close calls where ne…
Farber98 Sep 4, 2024
a453d2d
Merge branch 'main' into BCI-3989-cr-methods-error-when-unstarted
Farber98 Sep 5, 2024
0d7af20
fix conflicts with chain components pr
Farber98 Sep 5, 2024
647bdde
Merge branch 'main' into BCI-3989-cr-methods-error-when-unstarted
Farber98 Sep 12, 2024
489fd0a
fix pr conflicts
Farber98 Sep 12, 2024
e8ed82b
add flag to control if we return cr started
Farber98 Sep 15, 2024
66ba7cd
fix encodings iface
Farber98 Sep 15, 2024
392195f
Merge branch 'main' into BCI-3989-cr-methods-error-when-unstarted
Farber98 Sep 15, 2024
4e67d8d
refactor codec and chaincomponents ifaces
Farber98 Sep 16, 2024
54d7016
Merge branch 'main' into BCI-3989-cr-methods-error-when-unstarted
Farber98 Sep 16, 2024
e728084
Revert "Merge branch 'main' into BCI-3989-cr-methods-error-when-unsta…
Farber98 Sep 16, 2024
953d786
Reapply "Merge branch 'main' into BCI-3989-cr-methods-error-when-unst…
Farber98 Sep 17, 2024
f94c2c9
Merge branch 'main' into BCI-3989-cr-methods-error-when-unstarted
Farber98 Sep 17, 2024
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
2 changes: 1 addition & 1 deletion pkg/codec/encodings/type_codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func TestCodecFromTypeCodecs(t *testing.T) {

type interfaceTesterBase struct{}

func (*interfaceTesterBase) Setup(_ *testing.T) {}
func (*interfaceTesterBase) Setup(_ *testing.T, _ bool) {}

func (*interfaceTesterBase) GetAccountBytes(i int) []byte {
ib := byte(i)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestCodecClient(t *testing.T) {

es := &errCodec{}
esTester := chaincomponentstest.WrapCodecTesterForLoop(&fakeCodecInterfaceTester{impl: es})
esTester.Setup(t)
esTester.Setup(t, false)
esCodec := esTester.GetCodec(t)

anyObj := &interfacetests.TestStruct{}
Expand Down Expand Up @@ -55,14 +55,14 @@ func TestCodecClient(t *testing.T) {
// make sure that errors come from client directly
es.err = nil
t.Run("Encode returns error if type cannot be encoded in the wire format", func(t *testing.T) {
interfaceTester.Setup(t)
interfaceTester.Setup(t, false)
c := interfaceTester.GetCodec(t)
_, err := c.Encode(tests.Context(t), &cannotEncode{}, "doesnotmatter")
assert.True(t, errors.Is(err, types.ErrInvalidType))
})

t.Run("Decode returns error if type cannot be decoded in the wire format", func(t *testing.T) {
interfaceTester.Setup(t)
interfaceTester.Setup(t, false)
c := interfaceTester.GetCodec(t)
fv := int32(1)
toDecode, err := c.Encode(tests.Context(t), &interfacetests.TestStruct{Field: &fv}, interfacetests.TestItemType)
Expand All @@ -74,7 +74,7 @@ func TestCodecClient(t *testing.T) {
t.Run("Nil esCodec returns unimplemented", func(t *testing.T) {
ctx := tests.Context(t)
nilTester := chaincomponentstest.WrapCodecTesterForLoop(&fakeCodecInterfaceTester{impl: nil})
nilTester.Setup(t)
nilTester.Setup(t, false)
nilCodec := nilTester.GetCodec(t)

item := &interfacetests.TestStruct{}
Expand All @@ -98,7 +98,7 @@ type fakeCodecInterfaceTester struct {
impl types.Codec
}

func (it *fakeCodecInterfaceTester) Setup(_ *testing.T) {}
func (it *fakeCodecInterfaceTester) Setup(_ *testing.T, _ bool) {}

func (it *fakeCodecInterfaceTester) GetCodec(_ *testing.T) types.Codec {
return it.impl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"sort"
"strings"
"sync"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -99,7 +100,7 @@ func TestBind(t *testing.T) {
contractreadertest.WithContractReaderLoopEncoding(version),
)

errTester.Setup(t)
errTester.Setup(t, false)
contractReader := errTester.GetContractReader(t)

for _, errorType := range errorTypes {
Expand Down Expand Up @@ -133,7 +134,7 @@ func TestGetLatestValue(t *testing.T) {
contractreadertest.WithContractReaderLoopEncoding(version),
)

errTester.Setup(t)
errTester.Setup(t, false)
contractReader := errTester.GetContractReader(t)

t.Run("nil reader should return unimplemented", func(t *testing.T) {
Expand All @@ -142,7 +143,7 @@ func TestGetLatestValue(t *testing.T) {
ctx := tests.Context(t)

nilTester := contractreadertest.WrapContractReaderTesterForLoop(&fakeContractReaderInterfaceTester{impl: nil})
nilTester.Setup(t)
nilTester.Setup(t, false)
nilCr := nilTester.GetContractReader(t)

err := nilCr.GetLatestValue(ctx, "method", primitives.Unconfirmed, "anything", "anything")
Expand Down Expand Up @@ -182,7 +183,7 @@ func TestBatchGetLatestValues(t *testing.T) {
contractreadertest.WithContractReaderLoopEncoding(version),
)

errTester.Setup(t)
errTester.Setup(t, false)
contractReader := errTester.GetContractReader(t)

t.Run("nil reader should return unimplemented", func(t *testing.T) {
Expand All @@ -191,7 +192,7 @@ func TestBatchGetLatestValues(t *testing.T) {
ctx := tests.Context(t)

nilTester := contractreadertest.WrapContractReaderTesterForLoop(&fakeContractReaderInterfaceTester{impl: nil})
nilTester.Setup(t)
nilTester.Setup(t, false)
nilCr := nilTester.GetContractReader(t)

_, err := nilCr.BatchGetLatestValues(ctx, types.BatchGetLatestValuesRequest{})
Expand Down Expand Up @@ -235,19 +236,19 @@ func TestQueryKey(t *testing.T) {

impl := &protoConversionTestContractReader{}
crTester := contractreadertest.WrapContractReaderTesterForLoop(&fakeContractReaderInterfaceTester{impl: impl}, contractreadertest.WithContractReaderLoopEncoding(version))
crTester.Setup(t)
crTester.Setup(t, true)
cr := crTester.GetContractReader(t)

es := &errContractReader{}
errTester := contractreadertest.WrapContractReaderTesterForLoop(&fakeContractReaderInterfaceTester{impl: es})
errTester.Setup(t)
errTester.Setup(t, false)
contractReader := errTester.GetContractReader(t)

t.Run("nil reader should return unimplemented", func(t *testing.T) {
ctx := tests.Context(t)

nilTester := contractreadertest.WrapContractReaderTesterForLoop(&fakeContractReaderInterfaceTester{impl: nil})
nilTester.Setup(t)
nilTester.Setup(t, false)
nilCr := nilTester.GetContractReader(t)

_, err := nilCr.QueryKey(ctx, types.BoundContract{}, query.KeyFilter{}, query.LimitAndSort{}, &[]interface{}{nil})
Expand Down Expand Up @@ -291,12 +292,16 @@ type fakeContractReaderInterfaceTester struct {
cw fakeChainWriter
}

func (it *fakeContractReaderInterfaceTester) Setup(_ *testing.T) {
func (it *fakeContractReaderInterfaceTester) Setup(t *testing.T, started bool) {
fake, ok := it.impl.(*fakeContractReader)
if ok {
fake.vals = []valConfidencePair{}
fake.triggers = []eventConfidencePair{}
fake.stored = []TestStruct{}
if started {
require.NoError(t, it.impl.Start(context.Background()))
t.Cleanup(func() { require.NoError(t, it.impl.Close()) })
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use servicetest.Run. Notice it uses assert during cleanup.

}
}
}

Expand Down Expand Up @@ -346,8 +351,11 @@ type fakeContractReader struct {
stored []TestStruct
batchStored BatchCallEntry
lock sync.Mutex
isStarted atomic.Bool
}

var errServiceNotStarted = errors.New("ContractReader service not started")

type fakeChainWriter struct {
types.ChainWriter
cr *fakeContractReader
Expand Down Expand Up @@ -394,9 +402,15 @@ func (f *fakeChainWriter) GetFeeComponents(ctx context.Context) (*types.ChainFee
return &types.ChainFeeComponents{}, nil
}

func (f *fakeContractReader) Start(_ context.Context) error { return nil }
func (f *fakeContractReader) Start(_ context.Context) error {
f.isStarted.Store(true)
return nil
}

func (f *fakeContractReader) Close() error { return nil }
func (f *fakeContractReader) Close() error {
f.isStarted.Store(false)
return nil
}

func (f *fakeContractReader) Ready() error { panic("unimplemented") }

Expand Down Expand Up @@ -434,6 +448,10 @@ func (f *fakeContractReader) SetBatchLatestValues(batchCallEntry BatchCallEntry)
}

func (f *fakeContractReader) GetLatestValue(_ context.Context, readIdentifier string, confidenceLevel primitives.ConfidenceLevel, params, returnVal any) error {
if !f.isStarted.Load() {
return errServiceNotStarted
}

if strings.HasSuffix(readIdentifier, MethodReturningAlterableUint64) {
r := returnVal.(*uint64)
for i := len(f.vals) - 1; i >= 0; i-- {
Expand Down Expand Up @@ -508,6 +526,10 @@ func (f *fakeContractReader) GetLatestValue(_ context.Context, readIdentifier st
}

func (f *fakeContractReader) BatchGetLatestValues(_ context.Context, request types.BatchGetLatestValuesRequest) (types.BatchGetLatestValuesResult, error) {
if !f.isStarted.Load() {
return nil, errServiceNotStarted
}

result := make(types.BatchGetLatestValuesResult)
for requestContract, requestContractBatch := range request {
storedContractBatch := f.batchStored[requestContract]
Expand Down Expand Up @@ -561,6 +583,10 @@ func (f *fakeContractReader) BatchGetLatestValues(_ context.Context, request typ
}

func (f *fakeContractReader) QueryKey(_ context.Context, _ types.BoundContract, filter query.KeyFilter, limitAndSort query.LimitAndSort, _ any) ([]types.Sequence, error) {
if !f.isStarted.Load() {
return nil, errServiceNotStarted
}

if filter.Key == EventName {
f.lock.Lock()
defer f.lock.Unlock()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ type codecReaderLoopTester struct {
lst loopServerTester
}

func (c *codecReaderLoopTester) Setup(t *testing.T) {
c.CodecInterfaceTester.Setup(t)
func (c *codecReaderLoopTester) Setup(t *testing.T, _ bool) {
c.CodecInterfaceTester.Setup(t, false)
codec := c.CodecInterfaceTester.GetCodec(t)
c.lst.registerHook = func(server *grpc.Server) {
if codec != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ type contractReaderLoopTester struct {
encodeWith contractreader.EncodingVersion
}

func (c *contractReaderLoopTester) Setup(t *testing.T) {
c.ChainComponentsInterfaceTester.Setup(t)
func (c *contractReaderLoopTester) Setup(t *testing.T, started bool) {
c.ChainComponentsInterfaceTester.Setup(t, started)
contractReader := c.ChainComponentsInterfaceTester.GetContractReader(t)

c.lst.registerHook = func(server *grpc.Server) {
Expand Down
Loading
Loading