Skip to content

Commit

Permalink
fmt, removing globals from tests, scoping test logs (#1142)
Browse files Browse the repository at this point in the history
* Removing some globals from tests
* Use test-scoped loggers everywhere
  • Loading branch information
Groxx committed Oct 12, 2021
1 parent f46c30b commit 0b80b2d
Show file tree
Hide file tree
Showing 34 changed files with 1,278 additions and 1,204 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ IMPORT_ROOT := go.uber.org/cadence
THRIFT_GENDIR := .gen/go
THRIFTRW_SRC := idls/thrift/cadence.thrift idls/thrift/shadower.thrift
# one or more thriftrw-generated file(s), to create / depend on generated code
THRIFTRW_OUT := $(THRIFT_GENDIR)/cadence/idl.go
THRIFTRW_OUT := $(THRIFT_GENDIR)/cadence/cadence.go
TEST_ARG ?= -v -race

# general build-product folder, cleaned as part of `make clean`
Expand Down
2 changes: 1 addition & 1 deletion compatibility/thrift2proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ func NewThrift2ProtoAdapter(
visibility apiv1.VisibilityAPIYARPCClient,
) workflowserviceclient.Interface {
return internal.NewThrift2ProtoAdapter(domain, workflow, worker, visibility)
}
}
31 changes: 22 additions & 9 deletions evictiontest/workflow_cache_eviction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,22 @@ import (
"go.uber.org/cadence/internal/common"
"go.uber.org/cadence/worker"
"go.uber.org/yarpc"
"go.uber.org/zap/zaptest"
"golang.org/x/net/context"
)

// copied from internal/test_helpers_test.go
// this is the mock for yarpcCallOptions, as gomock requires the num of arguments to be the same.
// see getYarpcCallOptions for the default case.
func callOptions() []interface{} {
return []interface{}{
gomock.Any(), // library version
gomock.Any(), // feature version
gomock.Any(), // client name
gomock.Any(), // feature flags
}
}

func testReplayWorkflow(ctx internal.Context) error {
ao := internal.ActivityOptions{
ScheduleToStartTimeout: time.Second,
Expand Down Expand Up @@ -86,9 +99,6 @@ func TestWorkersTestSuite(t *testing.T) {
suite.Run(t, new(CacheEvictionSuite))
}

// this is the mock for yarpcCallOptions, make sure length are the same
var callOptions = []interface{}{gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()}

func createTestEventWorkflowExecutionStarted(eventID int64, attr *m.WorkflowExecutionStartedEventAttributes) *m.HistoryEvent {
return &m.HistoryEvent{
EventId: common.Int64Ptr(eventID),
Expand Down Expand Up @@ -149,25 +159,28 @@ func (s *CacheEvictionSuite) TestResetStickyOnEviction() {
cacheSize := 5
internal.SetStickyWorkflowCacheSize(cacheSize)
// once for workflow worker because we disable activity worker
s.service.EXPECT().DescribeDomain(gomock.Any(), gomock.Any(), callOptions...).Return(nil, nil).Times(1)
s.service.EXPECT().DescribeDomain(gomock.Any(), gomock.Any(), callOptions()...).Return(nil, nil).Times(1)
// feed our worker exactly *cacheSize* "legit" decision tasks
// these are handcrafted decision tasks that are not blatantly obviously mocks
// the goal is to trick our worker into thinking they are real so it
// actually goes along with processing these and puts their execution in the cache.
s.service.EXPECT().PollForDecisionTask(gomock.Any(), gomock.Any(), callOptions...).DoAndReturn(mockPollForDecisionTask).Times(cacheSize)
s.service.EXPECT().PollForDecisionTask(gomock.Any(), gomock.Any(), callOptions()...).DoAndReturn(mockPollForDecisionTask).Times(cacheSize)
// after *cacheSize* "legit" tasks are fed to our worker, start feeding our worker empty responses.
// these will get tossed away immediately after polled, but we still need them so gomock doesn't compain about unexpected calls.
// this is because our worker's poller doesn't stop, it keeps polling on the service client as long
// as Stop() is not called on the worker
s.service.EXPECT().PollForDecisionTask(gomock.Any(), gomock.Any(), callOptions...).Return(&m.PollForDecisionTaskResponse{}, nil).AnyTimes()
s.service.EXPECT().PollForDecisionTask(gomock.Any(), gomock.Any(), callOptions()...).Return(&m.PollForDecisionTaskResponse{}, nil).AnyTimes()
// this gets called after polled decision tasks are processed, any number of times doesn't matter
s.service.EXPECT().RespondDecisionTaskCompleted(gomock.Any(), gomock.Any(), callOptions...).Return(&m.RespondDecisionTaskCompletedResponse{}, nil).AnyTimes()
s.service.EXPECT().RespondDecisionTaskCompleted(gomock.Any(), gomock.Any(), callOptions()...).Return(&m.RespondDecisionTaskCompletedResponse{}, nil).AnyTimes()
// this is the critical point of the test.
// ResetSticky should be called exactly once because our workflow cache evicts when full
// so if our worker puts *cacheSize* entries in the cache, it should evict exactly one
s.service.EXPECT().ResetStickyTaskList(gomock.Any(), gomock.Any(), callOptions...).DoAndReturn(mockResetStickyTaskList).Times(1)
s.service.EXPECT().ResetStickyTaskList(gomock.Any(), gomock.Any(), callOptions()...).DoAndReturn(mockResetStickyTaskList).Times(1)

workflowWorker := internal.NewWorker(s.service, "test-domain", "tasklist", worker.Options{DisableActivityWorker: true})
workflowWorker := internal.NewWorker(s.service, "test-domain", "tasklist", worker.Options{
DisableActivityWorker: true,
Logger: zaptest.NewLogger(s.T()),
})
// this is an arbitrary workflow we use for this test
// NOTE: a simple helloworld that doesn't execute an activity
// won't work because the workflow will simply just complete
Expand Down
48 changes: 21 additions & 27 deletions internal/activity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package internal

import (
"context"
"fmt"
"testing"

"github.com/golang/mock/gomock"
Expand Down Expand Up @@ -54,46 +53,41 @@ func (s *activityTestSuite) TearDownTest() {
s.mockCtrl.Finish() // assert mock’s expectations
}

// this is the mock for yarpcCallOptions, make sure length are the same
var callOptions = []interface{}{gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()}

var featureFlags = FeatureFlags{}

func (s *activityTestSuite) TestActivityHeartbeat() {
ctx, cancel := context.WithCancel(context.Background())
invoker := newServiceInvoker([]byte("task-token"), "identity", s.service, cancel, 1, make(chan struct{}), featureFlags)
invoker := newServiceInvoker([]byte("task-token"), "identity", s.service, cancel, 1, make(chan struct{}), FeatureFlags{})
ctx = context.WithValue(ctx, activityEnvContextKey, &activityEnvironment{serviceInvoker: invoker})

s.service.EXPECT().RecordActivityTaskHeartbeat(gomock.Any(), gomock.Any(), callOptions...).
s.service.EXPECT().RecordActivityTaskHeartbeat(gomock.Any(), gomock.Any(), callOptions()...).
Return(&shared.RecordActivityTaskHeartbeatResponse{}, nil).Times(1)

RecordActivityHeartbeat(ctx, "testDetails")
}

func (s *activityTestSuite) TestActivityHeartbeat_InternalError() {
ctx, cancel := context.WithCancel(context.Background())
invoker := newServiceInvoker([]byte("task-token"), "identity", s.service, cancel, 1, make(chan struct{}), featureFlags)
invoker := newServiceInvoker([]byte("task-token"), "identity", s.service, cancel, 1, make(chan struct{}), FeatureFlags{})
ctx = context.WithValue(ctx, activityEnvContextKey, &activityEnvironment{
serviceInvoker: invoker,
logger: getTestLogger(s.T())})

s.service.EXPECT().RecordActivityTaskHeartbeat(gomock.Any(), gomock.Any(), callOptions...).
s.service.EXPECT().RecordActivityTaskHeartbeat(gomock.Any(), gomock.Any(), callOptions()...).
Return(nil, &shared.InternalServiceError{}).
Do(func(ctx context.Context, request *shared.RecordActivityTaskHeartbeatRequest, opts ...yarpc.CallOption) {
fmt.Println("MOCK RecordActivityTaskHeartbeat executed")
s.T().Log("MOCK RecordActivityTaskHeartbeat executed")
}).AnyTimes()

RecordActivityHeartbeat(ctx, "testDetails")
}

func (s *activityTestSuite) TestActivityHeartbeat_CancelRequested() {
ctx, cancel := context.WithCancel(context.Background())
invoker := newServiceInvoker([]byte("task-token"), "identity", s.service, cancel, 1, make(chan struct{}), featureFlags)
invoker := newServiceInvoker([]byte("task-token"), "identity", s.service, cancel, 1, make(chan struct{}), FeatureFlags{})
ctx = context.WithValue(ctx, activityEnvContextKey, &activityEnvironment{
serviceInvoker: invoker,
logger: getTestLogger(s.T())})

s.service.EXPECT().RecordActivityTaskHeartbeat(gomock.Any(), gomock.Any(), callOptions...).
s.service.EXPECT().RecordActivityTaskHeartbeat(gomock.Any(), gomock.Any(), callOptions()...).
Return(&shared.RecordActivityTaskHeartbeatResponse{CancelRequested: common.BoolPtr(true)}, nil).Times(1)

RecordActivityHeartbeat(ctx, "testDetails")
Expand All @@ -103,12 +97,12 @@ func (s *activityTestSuite) TestActivityHeartbeat_CancelRequested() {

func (s *activityTestSuite) TestActivityHeartbeat_EntityNotExist() {
ctx, cancel := context.WithCancel(context.Background())
invoker := newServiceInvoker([]byte("task-token"), "identity", s.service, cancel, 1, make(chan struct{}), featureFlags)
invoker := newServiceInvoker([]byte("task-token"), "identity", s.service, cancel, 1, make(chan struct{}), FeatureFlags{})
ctx = context.WithValue(ctx, activityEnvContextKey, &activityEnvironment{
serviceInvoker: invoker,
logger: getTestLogger(s.T())})

s.service.EXPECT().RecordActivityTaskHeartbeat(gomock.Any(), gomock.Any(), callOptions...).
s.service.EXPECT().RecordActivityTaskHeartbeat(gomock.Any(), gomock.Any(), callOptions()...).
Return(&shared.RecordActivityTaskHeartbeatResponse{}, &shared.EntityNotExistsError{}).Times(1)

RecordActivityHeartbeat(ctx, "testDetails")
Expand All @@ -118,13 +112,13 @@ func (s *activityTestSuite) TestActivityHeartbeat_EntityNotExist() {

func (s *activityTestSuite) TestActivityHeartbeat_SuppressContinousInvokes() {
ctx, cancel := context.WithCancel(context.Background())
invoker := newServiceInvoker([]byte("task-token"), "identity", s.service, cancel, 2, make(chan struct{}), featureFlags)
invoker := newServiceInvoker([]byte("task-token"), "identity", s.service, cancel, 2, make(chan struct{}), FeatureFlags{})
ctx = context.WithValue(ctx, activityEnvContextKey, &activityEnvironment{
serviceInvoker: invoker,
logger: getTestLogger(s.T())})

// Multiple calls but only one call is made.
s.service.EXPECT().RecordActivityTaskHeartbeat(gomock.Any(), gomock.Any(), callOptions...).
s.service.EXPECT().RecordActivityTaskHeartbeat(gomock.Any(), gomock.Any(), callOptions()...).
Return(&shared.RecordActivityTaskHeartbeatResponse{}, nil).Times(1)
RecordActivityHeartbeat(ctx, "testDetails")
RecordActivityHeartbeat(ctx, "testDetails")
Expand All @@ -133,11 +127,11 @@ func (s *activityTestSuite) TestActivityHeartbeat_SuppressContinousInvokes() {

// No HB timeout configured.
service2 := workflowservicetest.NewMockClient(s.mockCtrl)
invoker2 := newServiceInvoker([]byte("task-token"), "identity", service2, cancel, 0, make(chan struct{}), featureFlags)
invoker2 := newServiceInvoker([]byte("task-token"), "identity", service2, cancel, 0, make(chan struct{}), FeatureFlags{})
ctx = context.WithValue(ctx, activityEnvContextKey, &activityEnvironment{
serviceInvoker: invoker2,
logger: getTestLogger(s.T())})
service2.EXPECT().RecordActivityTaskHeartbeat(gomock.Any(), gomock.Any(), callOptions...).
service2.EXPECT().RecordActivityTaskHeartbeat(gomock.Any(), gomock.Any(), callOptions()...).
Return(&shared.RecordActivityTaskHeartbeatResponse{}, nil).Times(1)
RecordActivityHeartbeat(ctx, "testDetails")
RecordActivityHeartbeat(ctx, "testDetails")
Expand All @@ -146,14 +140,14 @@ func (s *activityTestSuite) TestActivityHeartbeat_SuppressContinousInvokes() {
// simulate batch picks before expiry.
waitCh := make(chan struct{})
service3 := workflowservicetest.NewMockClient(s.mockCtrl)
invoker3 := newServiceInvoker([]byte("task-token"), "identity", service3, cancel, 2, make(chan struct{}), featureFlags)
invoker3 := newServiceInvoker([]byte("task-token"), "identity", service3, cancel, 2, make(chan struct{}), FeatureFlags{})
ctx = context.WithValue(ctx, activityEnvContextKey, &activityEnvironment{
serviceInvoker: invoker3,
logger: getTestLogger(s.T())})
service3.EXPECT().RecordActivityTaskHeartbeat(gomock.Any(), gomock.Any(), callOptions...).
service3.EXPECT().RecordActivityTaskHeartbeat(gomock.Any(), gomock.Any(), callOptions()...).
Return(&shared.RecordActivityTaskHeartbeatResponse{}, nil).Times(1)

service3.EXPECT().RecordActivityTaskHeartbeat(gomock.Any(), gomock.Any(), callOptions...).
service3.EXPECT().RecordActivityTaskHeartbeat(gomock.Any(), gomock.Any(), callOptions()...).
Return(&shared.RecordActivityTaskHeartbeatResponse{}, nil).
Do(func(ctx context.Context, request *shared.RecordActivityTaskHeartbeatRequest, opts ...yarpc.CallOption) {
ev := newEncodedValues(request.Details, nil)
Expand All @@ -176,13 +170,13 @@ func (s *activityTestSuite) TestActivityHeartbeat_SuppressContinousInvokes() {
// simulate batch picks before expiry, with out any progress specified.
waitCh2 := make(chan struct{})
service4 := workflowservicetest.NewMockClient(s.mockCtrl)
invoker4 := newServiceInvoker([]byte("task-token"), "identity", service4, cancel, 2, make(chan struct{}), featureFlags)
invoker4 := newServiceInvoker([]byte("task-token"), "identity", service4, cancel, 2, make(chan struct{}), FeatureFlags{})
ctx = context.WithValue(ctx, activityEnvContextKey, &activityEnvironment{
serviceInvoker: invoker4,
logger: getTestLogger(s.T())})
service4.EXPECT().RecordActivityTaskHeartbeat(gomock.Any(), gomock.Any(), callOptions...).
service4.EXPECT().RecordActivityTaskHeartbeat(gomock.Any(), gomock.Any(), callOptions()...).
Return(&shared.RecordActivityTaskHeartbeatResponse{}, nil).Times(1)
service4.EXPECT().RecordActivityTaskHeartbeat(gomock.Any(), gomock.Any(), callOptions...).
service4.EXPECT().RecordActivityTaskHeartbeat(gomock.Any(), gomock.Any(), callOptions()...).
Return(&shared.RecordActivityTaskHeartbeatResponse{}, nil).
Do(func(ctx context.Context, request *shared.RecordActivityTaskHeartbeatRequest, opts ...yarpc.CallOption) {
require.Nil(s.T(), request.Details)
Expand All @@ -200,14 +194,14 @@ func (s *activityTestSuite) TestActivityHeartbeat_SuppressContinousInvokes() {
func (s *activityTestSuite) TestActivityHeartbeat_WorkerStop() {
ctx, cancel := context.WithCancel(context.Background())
workerStopChannel := make(chan struct{})
invoker := newServiceInvoker([]byte("task-token"), "identity", s.service, cancel, 5, workerStopChannel, featureFlags)
invoker := newServiceInvoker([]byte("task-token"), "identity", s.service, cancel, 5, workerStopChannel, FeatureFlags{})
ctx = context.WithValue(ctx, activityEnvContextKey, &activityEnvironment{serviceInvoker: invoker})

heartBeatDetail := "testDetails"
waitCh := make(chan struct{}, 1)
waitCh <- struct{}{}
waitC2 := make(chan struct{}, 1)
s.service.EXPECT().RecordActivityTaskHeartbeat(gomock.Any(), gomock.Any(), callOptions...).
s.service.EXPECT().RecordActivityTaskHeartbeat(gomock.Any(), gomock.Any(), callOptions()...).
Return(&shared.RecordActivityTaskHeartbeatResponse{}, nil).
Do(func(ctx context.Context, request *shared.RecordActivityTaskHeartbeatRequest, opts ...yarpc.CallOption) {
if _, ok := <-waitCh; ok {
Expand Down
6 changes: 3 additions & 3 deletions internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ type (
Tracer opentracing.Tracer
ContextPropagators []ContextPropagator
FeatureFlags FeatureFlags
Authorization auth.AuthorizationProvider
Authorization auth.AuthorizationProvider
}

// StartWorkflowOptions configuration parameters for starting a workflow execution.
Expand Down Expand Up @@ -544,7 +544,7 @@ func NewClient(service workflowserviceclient.Interface, domain string, options *
} else {
tracer = opentracing.NoopTracer{}
}
if options != nil && options.Authorization != nil{
if options != nil && options.Authorization != nil {
service = auth.NewWorkflowServiceWrapper(service, options.Authorization)
}
service = metrics.NewWorkflowServiceWrapper(service, metricScope)
Expand Down Expand Up @@ -574,7 +574,7 @@ func NewDomainClient(service workflowserviceclient.Interface, options *ClientOpt
metricScope = options.MetricsScope
}
metricScope = tagScope(metricScope, tagDomain, "domain-client", clientImplHeaderName, clientImplHeaderValue)
if options != nil && options.Authorization != nil{
if options != nil && options.Authorization != nil {
service = auth.NewWorkflowServiceWrapper(service, options.Authorization)
}
service = metrics.NewWorkflowServiceWrapper(service, metricScope)
Expand Down
Loading

0 comments on commit 0b80b2d

Please sign in to comment.