Skip to content

Commit

Permalink
Add test to check Execute(...) does not panic
Browse files Browse the repository at this point in the history
  • Loading branch information
yvanoers committed Jun 3, 2021
1 parent 248a3a1 commit 147207a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
7 changes: 6 additions & 1 deletion plugin/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,16 @@ func (p *ExecutorPlugin) GRPCClient(ctx context.Context, broker *plugin.GRPCBrok
return &ExecutorClient{client: types.NewExecutorClient(c), broker: broker}, nil
}

type Broker interface {
NextId() uint32
AcceptAndServe(id uint32, s func([]grpc.ServerOption) *grpc.Server)
}

// Here is the gRPC client that GRPCClient talks to.
type ExecutorClient struct {
// This is the real implementation
client types.ExecutorClient
broker *plugin.GRPCBroker
broker Broker
}

func (m *ExecutorClient) Execute(args *types.ExecuteRequest, cb StatusHelper) (*types.ExecuteResponse, error) {
Expand Down
47 changes: 47 additions & 0 deletions plugin/executor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package plugin

import (
"context"
"testing"

"github.com/stretchr/testify/assert"

"github.com/distribworks/dkron/v3/plugin/types"
dktypes "github.com/distribworks/dkron/v3/plugin/types"
grpc "google.golang.org/grpc"
)

type MockedExecutor struct{}

func (m *MockedExecutor) Execute(ctx context.Context, in *types.ExecuteRequest, opts ...grpc.CallOption) (*types.ExecuteResponse, error) {
resp := &dktypes.ExecuteResponse{}
return resp, nil
}

type MockedStatusHelper struct{}

func (m MockedStatusHelper) Update([]byte, bool) (int64, error) {
return 0, nil
}

type MockedBroker struct{}

func (m *MockedBroker) AcceptAndServe(id uint32, s func([]grpc.ServerOption) *grpc.Server) {
}

func (m *MockedBroker) NextId() uint32 {
return 0
}

func TestExecuteDoesNotPanicIfGRPCIsNotInitializedOnTime(t *testing.T) {
var brokerMock MockedBroker
var execMock MockedExecutor
execClient := ExecutorClient{
client: &execMock,
broker: &brokerMock,
}

var requestStub dktypes.ExecuteRequest
var statusHelperMock MockedStatusHelper
assert.NotPanics(t, func() { execClient.Execute(&requestStub, statusHelperMock) })
}

0 comments on commit 147207a

Please sign in to comment.