Skip to content

Commit

Permalink
Updated new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
silaslenihan committed Dec 20, 2024
1 parent f4be417 commit 91ac800
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 42 deletions.
2 changes: 0 additions & 2 deletions pkg/loop/internal/goplugin/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package goplugin
import (
"context"
"errors"
"fmt"
"time"

"google.golang.org/grpc"
Expand Down Expand Up @@ -83,7 +82,6 @@ type ServiceServer struct {
}

func (s *ServiceServer) Close(ctx context.Context, empty *emptypb.Empty) (*emptypb.Empty, error) {
fmt.Println("CLOSING CLIENT SERVICE")
return &emptypb.Empty{}, s.Srv.Close()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,6 @@ func (c *Client) Bind(ctx context.Context, bindings []types.BoundContract) error
}

_, err := c.grpc.Bind(ctx, &pb.BindRequest{Bindings: pbBindings})
fmt.Println("ERROR:", err)
return net.WrapRPCErr(err)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func TestContractReaderInterfaceTests(t *testing.T) {
contractreadertest.WithContractReaderLoopEncoding(version),
),
true,
false,
)
}
})
Expand Down Expand Up @@ -312,10 +313,6 @@ func (it *fakeContractReaderInterfaceTester) Setup(_ *testing.T) {
}
}

func (it *fakeContractReaderInterfaceTester) Init(_ *testing.T) {

}

func (it *fakeContractReaderInterfaceTester) GetContractReader(_ *testing.T) types.ContractReader {
return it.impl
}
Expand Down Expand Up @@ -1166,7 +1163,7 @@ func runContractReaderByIDQueryKey(t *testing.T) {
func(t *testing.T) {
t.Parallel()
fake := &fakeContractReader{}
fakeCW := &fakeContractWriter{}
fakeCW := &fakeContractWriter{cr: fake}
tester := &fakeContractReaderInterfaceTester{impl: fake}
tester.Setup(t)

Expand Down Expand Up @@ -1210,7 +1207,7 @@ func runContractReaderByIDQueryKey(t *testing.T) {
func(t *testing.T) {
t.Parallel()
fake := &fakeContractReader{}
fakeCW := &fakeContractWriter{}
fakeCW := &fakeContractWriter{cr: fake}

tester := &fakeContractReaderInterfaceTester{impl: fake}
tester.Setup(t)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func WithContractReaderLoopEncoding(version contractreader.EncodingVersion) Loop
type contractReaderLoopTester struct {
ChainComponentsInterfaceTester[*testing.T]
lst loopServerTester
conn *grpc.ClientConn
encodeWith contractreader.EncodingVersion
}

Expand All @@ -71,10 +72,11 @@ func (c *contractReaderLoopTester) Setup(t *testing.T) {
}

c.lst.Setup(t)
c.conn = c.lst.GetConn(t)
}

func (c *contractReaderLoopTester) GetContractReader(t *testing.T) types.ContractReader {
return contractreader.NewClient(nil, c.lst.GetConn(t), contractreader.WithClientEncoding(c.encodeWith))
return contractreader.NewClient(nil, c.conn, contractreader.WithClientEncoding(c.encodeWith))
}

func (c *contractReaderLoopTester) Name() string {
Expand Down
84 changes: 53 additions & 31 deletions pkg/types/interfacetests/chain_components_interface_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@ var AnySliceToReadWithoutAnArgument = []uint64{3, 4}

const AnyExtraValue = 3

func RunContractReaderInterfaceTests[T TestingT[T]](t T, tester ChainComponentsInterfaceTester[T], mockRun bool) {
func RunContractReaderInterfaceTests[T TestingT[T]](t T, tester ChainComponentsInterfaceTester[T], mockRun bool, parallel bool) {
t.Run(tester.Name(), func(t T) {
t.Run("GetLatestValue", func(t T) { runContractReaderGetLatestValueInterfaceTests(t, tester, mockRun) })
t.Run("BatchGetLatestValues", func(t T) { runContractReaderBatchGetLatestValuesInterfaceTests(t, tester, mockRun) })
t.Run("QueryKey", func(t T) { runQueryKeyInterfaceTests(t, tester) })
t.Run("QueryKeys", func(t T) { runQueryKeysInterfaceTests(t, tester) })
t.Run("GetLatestValue", func(t T) { runContractReaderGetLatestValueInterfaceTests(t, tester, mockRun, parallel) })
t.Run("BatchGetLatestValues", func(t T) { runContractReaderBatchGetLatestValuesInterfaceTests(t, tester, mockRun, parallel) })
t.Run("QueryKey", func(t T) { runQueryKeyInterfaceTests(t, tester, parallel) })
t.Run("QueryKeys", func(t T) { runQueryKeysInterfaceTests(t, tester, parallel) })
})
}

Expand All @@ -129,19 +129,21 @@ type sequenceWithKey struct {
Key string
}

func runQueryKeysInterfaceTests[T TestingT[T]](t T, tester ChainComponentsInterfaceTester[T]) {
func runQueryKeysInterfaceTests[T TestingT[T]](t T, tester ChainComponentsInterfaceTester[T], parallel bool) {
tests := []Testcase[T]{
{
Name: ContractReaderQueryKeysReturnsDataTwoEventTypes,
Test: func(t T) {
ctx := tests.Context(t)
cr := tester.GetContractReader(t)
cw := tester.GetContractWriter(t)

bindings := tester.GetBindings(t)

require.NoError(t, cr.Bind(ctx, bindings))
boundContract := BindingsByName(bindings, AnyContractName)[0]

expectedSequenceData := createMixedEventTypeSequence(t, tester, boundContract)
expectedSequenceData := createMixedEventTypeSequence(t, tester, cw, boundContract)

ts := &TestStruct{}
require.Eventually(t, func() bool {
Expand Down Expand Up @@ -181,7 +183,7 @@ func runQueryKeysInterfaceTests[T TestingT[T]](t T, tester ChainComponentsInterf
bindings := tester.GetBindings(t)
bound := BindingsByName(bindings, AnyContractName)[0]

require.NoError(t, cr.Bind(ctx, tester.GetBindings(t)))
require.NoError(t, cr.Bind(ctx, bindings))

contractFilter := types.ContractKeyFilter{
Contract: bound,
Expand All @@ -204,12 +206,13 @@ func runQueryKeysInterfaceTests[T TestingT[T]](t T, tester ChainComponentsInterf
Test: func(t T) {
ctx := tests.Context(t)
cr := tester.GetContractReader(t)
cw := tester.GetContractWriter(t)
bindings := tester.GetBindings(t)

require.NoError(t, cr.Bind(ctx, bindings))
bound := BindingsByName(bindings, AnyContractName)[0]

expectedSequenceData := createMixedEventTypeSequence(t, tester, bound)
expectedSequenceData := createMixedEventTypeSequence(t, tester, cw, bound)

var value values.Value
require.Eventually(t, func() bool {
Expand Down Expand Up @@ -268,12 +271,15 @@ func runQueryKeysInterfaceTests[T TestingT[T]](t T, tester ChainComponentsInterf
Test: func(t T) {
ctx := tests.Context(t)
cr := tester.GetContractReader(t)
cw := tester.GetContractWriter(t)

bindings := tester.GetBindings(t)

require.NoError(t, cr.Bind(ctx, bindings))
boundContract := BindingsByName(bindings, AnyContractName)[0]

expectedSequenceData := createMixedEventTypeSequence(t, tester, boundContract)
expectedSequenceData := createMixedEventTypeSequence(t, tester, cw, boundContract)
fmt.Println("expectedSequenceData", expectedSequenceData)

ts := &TestStruct{}
require.Eventually(t, func() bool {
Expand Down Expand Up @@ -321,6 +327,7 @@ func runQueryKeysInterfaceTests[T TestingT[T]](t T, tester ChainComponentsInterf
Test: func(t T) {
ctx := tests.Context(t)
cr := tester.GetContractReader(t)
cw := tester.GetContractWriter(t)
bindings := tester.GetBindings(t)

require.NoError(t, cr.Bind(ctx, bindings))
Expand All @@ -330,26 +337,26 @@ func runQueryKeysInterfaceTests[T TestingT[T]](t T, tester ChainComponentsInterf

ts1 := CreateTestStruct[T](0, tester)
expectedSequenceData = append(expectedSequenceData, &ts1)
_ = SubmitTransactionToCW(t, tester, MethodTriggeringEvent, ts1, boundContract, types.Unconfirmed)
_ = SubmitTransactionToCW(t, tester, cw, MethodTriggeringEvent, ts1, boundContract, types.Unconfirmed)
ts2 := CreateTestStruct[T](1, tester)
expectedSequenceData = append(expectedSequenceData, &ts2)
_ = SubmitTransactionToCW(t, tester, MethodTriggeringEvent, ts2, boundContract, types.Unconfirmed)
_ = SubmitTransactionToCW(t, tester, cw, MethodTriggeringEvent, ts2, boundContract, types.Unconfirmed)

ds1 := SomeDynamicTopicEvent{Field: "1"}
expectedSequenceData = append(expectedSequenceData, &ds1)
_ = SubmitTransactionToCW(t, tester, MethodTriggeringEventWithDynamicTopic, ds1, boundContract, types.Unconfirmed)
_ = SubmitTransactionToCW(t, tester, cw, MethodTriggeringEventWithDynamicTopic, ds1, boundContract, types.Unconfirmed)

ts3 := CreateTestStruct[T](2, tester)
expectedSequenceData = append(expectedSequenceData, &ts3)
_ = SubmitTransactionToCW(t, tester, MethodTriggeringEvent, ts3, boundContract, types.Unconfirmed)
_ = SubmitTransactionToCW(t, tester, cw, MethodTriggeringEvent, ts3, boundContract, types.Unconfirmed)

ds2 := SomeDynamicTopicEvent{Field: "2"}
expectedSequenceData = append(expectedSequenceData, &ds2)
_ = SubmitTransactionToCW(t, tester, MethodTriggeringEventWithDynamicTopic, ds2, boundContract, types.Unconfirmed)
_ = SubmitTransactionToCW(t, tester, cw, MethodTriggeringEventWithDynamicTopic, ds2, boundContract, types.Unconfirmed)

ts4 := CreateTestStruct[T](3, tester)
expectedSequenceData = append(expectedSequenceData, &ts4)
_ = SubmitTransactionToCW(t, tester, MethodTriggeringEvent, ts4, boundContract, types.Finalized)
_ = SubmitTransactionToCW(t, tester, cw, MethodTriggeringEvent, ts4, boundContract, types.Finalized)

require.Eventually(t, func() bool {
var allSequences []sequenceWithKey
Expand Down Expand Up @@ -399,35 +406,38 @@ func runQueryKeysInterfaceTests[T TestingT[T]](t T, tester ChainComponentsInterf
},
}

RunTests(t, tester, tests)
if parallel {
RunTestsInParallel(t, tester, tests)
} else {
RunTests(t, tester, tests)
}
}

func createMixedEventTypeSequence[T TestingT[T]](t T, tester ChainComponentsInterfaceTester[T], boundContract types.BoundContract) []any {
func createMixedEventTypeSequence[T TestingT[T]](t T, tester ChainComponentsInterfaceTester[T], cw types.ContractWriter, boundContract types.BoundContract) []any {
var expectedSequenceData []any

ts1 := CreateTestStruct[T](0, tester)
expectedSequenceData = append(expectedSequenceData, &ts1)
_ = SubmitTransactionToCW(t, tester, MethodTriggeringEvent, ts1, boundContract, types.Unconfirmed)
_ = SubmitTransactionToCW(t, tester, cw, MethodTriggeringEvent, ts1, boundContract, types.Unconfirmed)
ts2 := CreateTestStruct[T](1, tester)
expectedSequenceData = append(expectedSequenceData, &ts2)
_ = SubmitTransactionToCW(t, tester, MethodTriggeringEvent, ts2, boundContract, types.Unconfirmed)
_ = SubmitTransactionToCW(t, tester, cw, MethodTriggeringEvent, ts2, boundContract, types.Unconfirmed)

ds1 := SomeDynamicTopicEvent{Field: "1"}
expectedSequenceData = append(expectedSequenceData, &ds1)
_ = SubmitTransactionToCW(t, tester, MethodTriggeringEventWithDynamicTopic, ds1, boundContract, types.Unconfirmed)
_ = SubmitTransactionToCW(t, tester, cw, MethodTriggeringEventWithDynamicTopic, ds1, boundContract, types.Unconfirmed)

ts3 := CreateTestStruct[T](2, tester)
expectedSequenceData = append(expectedSequenceData, &ts3)
_ = SubmitTransactionToCW(t, tester, MethodTriggeringEvent, ts3, boundContract, types.Unconfirmed)
_ = SubmitTransactionToCW(t, tester, cw, MethodTriggeringEvent, ts3, boundContract, types.Unconfirmed)

ds2 := SomeDynamicTopicEvent{Field: "2"}
expectedSequenceData = append(expectedSequenceData, &ds2)
_ = SubmitTransactionToCW(t, tester, MethodTriggeringEventWithDynamicTopic, ds2, boundContract, types.Unconfirmed)
_ = SubmitTransactionToCW(t, tester, cw, MethodTriggeringEventWithDynamicTopic, ds2, boundContract, types.Unconfirmed)

ts4 := CreateTestStruct[T](3, tester)
expectedSequenceData = append(expectedSequenceData, &ts4)
_ = SubmitTransactionToCW(t, tester, MethodTriggeringEvent, ts4, boundContract, types.Unconfirmed)

_ = SubmitTransactionToCW(t, tester, cw, MethodTriggeringEvent, ts4, boundContract, types.Unconfirmed)
return expectedSequenceData
}

Expand All @@ -445,7 +455,7 @@ func sequenceDataEqual(expectedSequenceData []any, sequences []sequenceWithKey)
return true
}

func runContractReaderGetLatestValueInterfaceTests[T TestingT[T]](t T, tester ChainComponentsInterfaceTester[T], mockRun bool) {
func runContractReaderGetLatestValueInterfaceTests[T TestingT[T]](t T, tester ChainComponentsInterfaceTester[T], mockRun bool, parallel bool) {
tests := []Testcase[T]{
{
Name: ContractReaderGetLatestValueAsValuesDotValue,
Expand Down Expand Up @@ -776,10 +786,14 @@ func runContractReaderGetLatestValueInterfaceTests[T TestingT[T]](t T, tester Ch
},
},
}
RunTests(t, tester, tests)
if parallel {
RunTestsInParallel(t, tester, tests)
} else {
RunTests(t, tester, tests)
}
}

func runContractReaderBatchGetLatestValuesInterfaceTests[T TestingT[T]](t T, tester ChainComponentsInterfaceTester[T], mockRun bool) {
func runContractReaderBatchGetLatestValuesInterfaceTests[T TestingT[T]](t T, tester ChainComponentsInterfaceTester[T], mockRun bool, parallel bool) {
testCases := []Testcase[T]{
{
Name: ContractReaderBatchGetLatestValue,
Expand Down Expand Up @@ -1058,10 +1072,14 @@ func runContractReaderBatchGetLatestValuesInterfaceTests[T TestingT[T]](t T, tes
},
},
}
RunTests(t, tester, testCases)
if parallel {
RunTestsInParallel(t, tester, testCases)
} else {
RunTests(t, tester, testCases)
}
}

func runQueryKeyInterfaceTests[T TestingT[T]](t T, tester ChainComponentsInterfaceTester[T]) {
func runQueryKeyInterfaceTests[T TestingT[T]](t T, tester ChainComponentsInterfaceTester[T], parallel bool) {
tests := []Testcase[T]{
{
Name: ContractReaderQueryKeyNotFound,
Expand Down Expand Up @@ -1235,7 +1253,11 @@ func runQueryKeyInterfaceTests[T TestingT[T]](t T, tester ChainComponentsInterfa
},
}

RunTests(t, tester, tests)
if parallel {
RunTestsInParallel(t, tester, tests)
} else {
RunTests(t, tester, tests)
}
}

func BindingsByName(bindings []types.BoundContract, name string) []types.BoundContract {
Expand Down
15 changes: 14 additions & 1 deletion pkg/types/interfacetests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,20 @@ type TestingT[T any] interface {
// Basic Tester configuration.
func RunTests[T TestingT[T]](t T, tester BasicTester[T], tests []Testcase[T]) {
t.Run(tester.Name(), func(t T) {
tester.Setup(t)
for _, test := range tests {
if !tester.IsDisabled(test.Name) {
t.Run(test.Name, func(t T) {
tester.Setup(t)
test.Test(t)
})
}
}
})
}

func RunTestsInParallel[T TestingT[T]](t T, tester BasicTester[T], tests []Testcase[T]) {
// Assumes Setup() called on tester initialization to avoid race conditions on tester setup
t.Run(tester.Name(), func(t T) {
for _, test := range tests {
if !tester.IsDisabled(test.Name) {
t.Run(test.Name, func(t T) {
Expand Down

0 comments on commit 91ac800

Please sign in to comment.