Skip to content

Commit

Permalink
remove noop, add mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
evlekht committed Oct 17, 2024
1 parent 45f4e46 commit 0f3f171
Show file tree
Hide file tree
Showing 13 changed files with 353 additions and 96 deletions.
8 changes: 4 additions & 4 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
"github.com/chain4travel/camino-messenger-bot/internal/rpc/client"
"github.com/chain4travel/camino-messenger-bot/internal/rpc/server"
"github.com/chain4travel/camino-messenger-bot/internal/tracing"
chequeHandler "github.com/chain4travel/camino-messenger-bot/pkg/cheque_handler"
chequeHandlerStorage "github.com/chain4travel/camino-messenger-bot/pkg/cheque_handler/storage/sqlite"
"github.com/chain4travel/camino-messenger-bot/pkg/chequehandler"
chequeHandlerStorage "github.com/chain4travel/camino-messenger-bot/pkg/chequehandler/storage/sqlite"
cmaccounts "github.com/chain4travel/camino-messenger-bot/pkg/cm_accounts"
"github.com/chain4travel/camino-messenger-bot/pkg/database/sqlite"
"github.com/chain4travel/camino-messenger-bot/pkg/scheduler"
Expand Down Expand Up @@ -116,7 +116,7 @@ func NewApp(ctx context.Context, cfg *config.Config, logger *zap.SugaredLogger)
return nil, err
}

chequeHandler, err := chequeHandler.NewChequeHandler(
chequeHandler, err := chequehandler.NewChequeHandler(
logger,
evmClient,
cfg.BotKey,
Expand Down Expand Up @@ -198,7 +198,7 @@ type App struct {
logger *zap.SugaredLogger
tracer tracing.Tracer
scheduler scheduler.Scheduler
chequeHandler chequeHandler.ChequeHandler
chequeHandler chequehandler.ChequeHandler
rpcClient *client.RPCClient
rpcServer server.Server
messageProcessor messaging.Processor
Expand Down
6 changes: 3 additions & 3 deletions internal/messaging/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/chain4travel/camino-messenger-bot/internal/messaging/types"
"github.com/chain4travel/camino-messenger-bot/internal/metadata"
"github.com/chain4travel/camino-messenger-bot/internal/rpc"
chequeHandler "github.com/chain4travel/camino-messenger-bot/pkg/cheque_handler"
"github.com/chain4travel/camino-messenger-bot/pkg/chequehandler"
"github.com/chain4travel/camino-messenger-bot/pkg/cheques"
cmaccounts "github.com/chain4travel/camino-messenger-bot/pkg/cm_accounts"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -66,7 +66,7 @@ func NewProcessor(
networkFeeRecipientCMAccountAddress common.Address,
registry ServiceRegistry,
responseHandler ResponseHandler,
chequeHandler chequeHandler.ChequeHandler,
chequeHandler chequehandler.ChequeHandler,
compressor compression.Compressor[*types.Message, [][]byte],
cmAccounts cmaccounts.Service,
) Processor {
Expand Down Expand Up @@ -106,7 +106,7 @@ type processor struct {
responseChannels map[string]chan *types.Message
serviceRegistry ServiceRegistry
responseHandler ResponseHandler
chequeHandler chequeHandler.ChequeHandler
chequeHandler chequehandler.ChequeHandler
compressor compression.Compressor[*types.Message, [][]byte]
cmAccounts cmaccounts.Service
}
Expand Down
44 changes: 26 additions & 18 deletions internal/messaging/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/chain4travel/camino-messenger-bot/internal/metadata"
"github.com/chain4travel/camino-messenger-bot/internal/rpc"
"github.com/chain4travel/camino-messenger-bot/internal/rpc/generated"
chequehandler "github.com/chain4travel/camino-messenger-bot/pkg/cheque_handler"
"github.com/chain4travel/camino-messenger-bot/pkg/chequehandler"
"github.com/chain4travel/camino-messenger-bot/pkg/cheques"
cmaccounts "github.com/chain4travel/camino-messenger-bot/pkg/cm_accounts"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -68,6 +68,7 @@ func TestProcessInbound(t *testing.T) {
mockService := rpc.NewMockService(mockCtrl)
mockMessenger := NewMockMessenger(mockCtrl)
mockCMAccounts := cmaccounts.NewMockService(mockCtrl)
mockChequeHandler := chequehandler.NewMockChequeHandler(mockCtrl)

type fields struct {
messenger Messenger
Expand Down Expand Up @@ -123,7 +124,7 @@ func TestProcessInbound(t *testing.T) {
fields: fields{
serviceRegistry: mockServiceRegistry,
responseHandler: NoopResponseHandler{},
chequeHandler: chequehandler.NoopChequeHandler{},
chequeHandler: mockChequeHandler,
messenger: mockMessenger,
compressor: &noopCompressor{},
cmAccounts: mockCMAccounts,
Expand All @@ -133,6 +134,7 @@ func TestProcessInbound(t *testing.T) {
mockService.EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, generated.PingServiceV1Response, nil)
mockServiceRegistry.EXPECT().GetService(gomock.Any()).Return(mockService, true)
mockMessenger.EXPECT().SendAsync(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(errSomeError)
mockChequeHandler.EXPECT().VerifyCheque(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
mockCMAccounts.EXPECT().GetServiceFee(gomock.Any(), gomock.Any(), gomock.Any()).Return(big.NewInt(1), nil)
},
args: args{
Expand All @@ -150,7 +152,7 @@ func TestProcessInbound(t *testing.T) {
fields: fields{
serviceRegistry: mockServiceRegistry,
responseHandler: NoopResponseHandler{},
chequeHandler: chequehandler.NoopChequeHandler{},
chequeHandler: mockChequeHandler,
messenger: mockMessenger,
compressor: &noopCompressor{},
cmAccounts: mockCMAccounts,
Expand All @@ -160,6 +162,7 @@ func TestProcessInbound(t *testing.T) {
mockService.EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, generated.PingServiceV1Response, nil)
mockServiceRegistry.EXPECT().GetService(gomock.Any()).Return(mockService, true)
mockMessenger.EXPECT().SendAsync(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
mockChequeHandler.EXPECT().VerifyCheque(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
mockCMAccounts.EXPECT().GetServiceFee(gomock.Any(), gomock.Any(), gomock.Any()).Return(big.NewInt(1), nil)
},
args: args{
Expand All @@ -176,7 +179,7 @@ func TestProcessInbound(t *testing.T) {
fields: fields{
serviceRegistry: mockServiceRegistry,
responseHandler: NoopResponseHandler{},
chequeHandler: chequehandler.NoopChequeHandler{},
chequeHandler: mockChequeHandler,
messenger: mockMessenger,
compressor: &noopCompressor{},
cmAccounts: mockCMAccounts,
Expand Down Expand Up @@ -232,6 +235,7 @@ func TestProcessOutbound(t *testing.T) {
mockServiceRegistry := NewMockServiceRegistry(mockCtrl)
mockMessenger := NewMockMessenger(mockCtrl)
mockCMAccounts := cmaccounts.NewMockService(mockCtrl)
mockChequeHandler := chequehandler.NewMockChequeHandler(mockCtrl)

type fields struct {
responseTimeout time.Duration
Expand All @@ -257,7 +261,7 @@ func TestProcessOutbound(t *testing.T) {
fields: fields{
serviceRegistry: mockServiceRegistry,
responseHandler: NoopResponseHandler{},
chequeHandler: chequehandler.NoopChequeHandler{},
chequeHandler: mockChequeHandler,
messenger: mockMessenger,
compressor: &noopCompressor{},
cmAccounts: mockCMAccounts,
Expand All @@ -271,7 +275,7 @@ func TestProcessOutbound(t *testing.T) {
fields: fields{
serviceRegistry: mockServiceRegistry,
responseHandler: NoopResponseHandler{},
chequeHandler: chequehandler.NoopChequeHandler{},
chequeHandler: mockChequeHandler,
messenger: mockMessenger,
compressor: &noopCompressor{},
cmAccounts: mockCMAccounts,
Expand All @@ -286,7 +290,7 @@ func TestProcessOutbound(t *testing.T) {
responseTimeout: 10 * time.Millisecond, // 10ms
serviceRegistry: mockServiceRegistry,
responseHandler: NoopResponseHandler{},
chequeHandler: chequehandler.NoopChequeHandler{},
chequeHandler: mockChequeHandler,
messenger: mockMessenger,
compressor: &noopCompressor{},
cmAccounts: mockCMAccounts,
Expand All @@ -301,6 +305,7 @@ func TestProcessOutbound(t *testing.T) {
mockCMAccounts.EXPECT().GetChequeOperators(gomock.Any(), gomock.Any()).Return([]common.Address{{}}, nil)
mockCMAccounts.EXPECT().GetServiceFee(gomock.Any(), gomock.Any(), gomock.Any()).Return(big.NewInt(1), nil)
mockCMAccounts.EXPECT().IsBotAllowed(gomock.Any(), gomock.Any(), gomock.Any()).Return(true, nil)
mockChequeHandler.EXPECT().IssueCheque(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Times(2).Return(&cheques.SignedCheque{}, nil)
mockMessenger.EXPECT().SendAsync(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
},
err: ErrExceededResponseTimeout,
Expand All @@ -310,7 +315,7 @@ func TestProcessOutbound(t *testing.T) {
responseTimeout: 100 * time.Millisecond, // 100ms
serviceRegistry: mockServiceRegistry,
responseHandler: NoopResponseHandler{},
chequeHandler: chequehandler.NoopChequeHandler{},
chequeHandler: mockChequeHandler,
messenger: mockMessenger,
compressor: &noopCompressor{},
cmAccounts: mockCMAccounts,
Expand All @@ -327,6 +332,7 @@ func TestProcessOutbound(t *testing.T) {
mockCMAccounts.EXPECT().GetServiceFee(gomock.Any(), gomock.Any(), gomock.Any()).
Return(big.NewInt(1), nil)
mockCMAccounts.EXPECT().IsBotAllowed(gomock.Any(), gomock.Any(), gomock.Any()).Return(true, nil)
mockChequeHandler.EXPECT().IssueCheque(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Times(2).Return(&cheques.SignedCheque{}, nil)
mockMessenger.EXPECT().SendAsync(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(errSomeError)
},
Expand All @@ -337,7 +343,7 @@ func TestProcessOutbound(t *testing.T) {
responseTimeout: 500 * time.Millisecond, // long enough timeout for response to be received
serviceRegistry: mockServiceRegistry,
responseHandler: NoopResponseHandler{},
chequeHandler: chequehandler.NoopChequeHandler{},
chequeHandler: mockChequeHandler,
messenger: mockMessenger,
compressor: &noopCompressor{},
cmAccounts: mockCMAccounts,
Expand All @@ -349,13 +355,11 @@ func TestProcessOutbound(t *testing.T) {
},
},
prepare: func() {
mockCMAccounts.EXPECT().GetChequeOperators(gomock.Any(), gomock.Any()).
Return([]common.Address{{}}, nil)
mockCMAccounts.EXPECT().GetServiceFee(gomock.Any(), gomock.Any(), gomock.Any()).
Return(big.NewInt(1), nil)
mockCMAccounts.EXPECT().GetChequeOperators(gomock.Any(), gomock.Any()).Return([]common.Address{{}}, nil)
mockCMAccounts.EXPECT().GetServiceFee(gomock.Any(), gomock.Any(), gomock.Any()).Return(big.NewInt(1), nil)
mockCMAccounts.EXPECT().IsBotAllowed(gomock.Any(), gomock.Any(), gomock.Any()).Return(true, nil)
mockMessenger.EXPECT().SendAsync(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(nil)
mockMessenger.EXPECT().SendAsync(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
mockChequeHandler.EXPECT().IssueCheque(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Times(2).Return(&cheques.SignedCheque{}, nil)
},
writeResponseToChannel: func(p *processor) {
done := func() bool {
Expand Down Expand Up @@ -423,14 +427,18 @@ func (d dummyService) Name() string {
func TestStart(t *testing.T) {
mockCtrl := gomock.NewController(t)

mockServiceRegistry := NewMockServiceRegistry(mockCtrl)
mockMessenger := NewMockMessenger(mockCtrl)
mockServiceRegistry.EXPECT().GetService(gomock.Any()).AnyTimes().Return(dummyService{}, true)
mockMessenger.EXPECT().SendAsync(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Times(2).Return(nil)

mockServiceRegistry := NewMockServiceRegistry(mockCtrl)
mockServiceRegistry.EXPECT().GetService(gomock.Any()).AnyTimes().Return(dummyService{}, true)

mockCMAccounts := cmaccounts.NewMockService(mockCtrl)
mockCMAccounts.EXPECT().GetServiceFee(gomock.Any(), gomock.Any(), gomock.Any()).Times(2).Return(big.NewInt(1), nil)

mockChequeHandler := chequehandler.NewMockChequeHandler(mockCtrl)
mockChequeHandler.EXPECT().VerifyCheque(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Times(2).Return(nil)

t.Run("start processor and accept messages", func(*testing.T) {
ch := make(chan types.Message, 5)
// incoming messages
Expand Down Expand Up @@ -466,7 +474,7 @@ func TestStart(t *testing.T) {
common.Address{},
mockServiceRegistry,
NoopResponseHandler{},
chequehandler.NoopChequeHandler{},
mockChequeHandler,
&noopCompressor{},
mockCMAccounts,
)
Expand Down
38 changes: 0 additions & 38 deletions pkg/cheque_handler/noop_cheque_handler.go

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
100 changes: 100 additions & 0 deletions pkg/chequehandler/mock_cheque_handler.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0f3f171

Please sign in to comment.