From 03f52d4ac5761ac743acbefb17d595083eba4ec9 Mon Sep 17 00:00:00 2001 From: lwolczynski Date: Mon, 23 Sep 2024 16:35:03 -0500 Subject: [PATCH 1/3] IWF-125: Truncate errorMsg and stacktrace for regular activity --- service/interpreter/activityImpl.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/service/interpreter/activityImpl.go b/service/interpreter/activityImpl.go index bd7a38e7..7a4e9958 100644 --- a/service/interpreter/activityImpl.go +++ b/service/interpreter/activityImpl.go @@ -189,7 +189,20 @@ func composeHttpError( } responseBody = responseBody[:maxL] + "..." errType = "1st-attempt-failure" + } else { + maxL := len(errMsg) + if maxL > 50 { + maxL = 50 + } + errMsg = errMsg[:maxL] + "..." + + maxL = len(responseBody) + if maxL > 500 { + maxL = 500 + } + responseBody = responseBody[:maxL] + "..." } + return provider.NewApplicationError(errType, fmt.Sprintf("statusCode: %v, responseBody: %v, errMsg: %v", statusCode, responseBody, errMsg)) } From 1b5eb346a5afcbfdddf62dc3a7ad09ab018682d6 Mon Sep 17 00:00:00 2001 From: lwolczynski Date: Tue, 24 Sep 2024 13:55:08 -0500 Subject: [PATCH 2/3] IWF-125: Add unit tests --- service/interpreter/activityImpl.go | 8 +- service/interpreter/activityImpl_test.go | 144 +++++ service/interpreter/interfaces_mock.go | 701 +++++++++++++++++++++++ 3 files changed, 849 insertions(+), 4 deletions(-) create mode 100644 service/interpreter/interfaces_mock.go diff --git a/service/interpreter/activityImpl.go b/service/interpreter/activityImpl.go index 7a4e9958..82c7e1f9 100644 --- a/service/interpreter/activityImpl.go +++ b/service/interpreter/activityImpl.go @@ -180,27 +180,27 @@ func composeHttpError( maxL := len(errMsg) if maxL > 5 { maxL = 5 + errMsg = errMsg[:maxL] + "..." } - errMsg = errMsg[:maxL] + "..." maxL = len(responseBody) if maxL > 50 { maxL = 50 + responseBody = responseBody[:maxL] + "..." } - responseBody = responseBody[:maxL] + "..." errType = "1st-attempt-failure" } else { maxL := len(errMsg) if maxL > 50 { maxL = 50 + errMsg = errMsg[:maxL] + "..." } - errMsg = errMsg[:maxL] + "..." maxL = len(responseBody) if maxL > 500 { maxL = 500 + responseBody = responseBody[:maxL] + "..." } - responseBody = responseBody[:maxL] + "..." } return provider.NewApplicationError(errType, diff --git a/service/interpreter/activityImpl_test.go b/service/interpreter/activityImpl_test.go index e0451b04..942e9e18 100644 --- a/service/interpreter/activityImpl_test.go +++ b/service/interpreter/activityImpl_test.go @@ -1,9 +1,15 @@ package interpreter import ( + "errors" + "fmt" + "github.com/golang/mock/gomock" "github.com/indeedeng/iwf/gen/iwfidl" "github.com/indeedeng/iwf/service/common/ptr" "github.com/stretchr/testify/assert" + "io" + "net/http" + "strings" "testing" "time" ) @@ -88,3 +94,141 @@ func createCommands() ([]iwfidl.TimerCommand, []iwfidl.SignalCommand, []iwfidl.I } return validTimerCommands, validSignalCommands, internalCommands } + +func TestComposeHttpError_LocalActivity_LongErrorResponse(t *testing.T) { + ctrl := gomock.NewController(t) + mockActivityProvider := NewMockActivityProvider(ctrl) + + longError := strings.Repeat("a", 1000) + + httpResp := &http.Response{ + StatusCode: 400, + Body: io.NopCloser(strings.NewReader(longError)), + } + errMsg := "original error message" + err := errors.New(errMsg) + + returnedError := errors.New("test error msg") + mockActivityProvider.EXPECT().NewApplicationError("1st-attempt-failure", fmt.Sprintf("statusCode: %v, responseBody: %v, errMsg: %v", httpResp.StatusCode, longError[:50]+"...", errors.New(errMsg[:5]+"..."))).Return(returnedError) + + err = composeHttpError(true, mockActivityProvider, err, httpResp, "test-error-type") + if err != nil { + return + } + + assert.Error(t, err) + assert.Equal(t, returnedError, err) +} + +func TestComposeHttpError_RegularActivity_LongErrorResponse(t *testing.T) { + ctrl := gomock.NewController(t) + mockActivityProvider := NewMockActivityProvider(ctrl) + + longError := strings.Repeat("a", 1000) + + httpResp := &http.Response{ + StatusCode: 400, + Body: io.NopCloser(strings.NewReader(longError)), + } + errMsg := "original error message which is very long like this" + err := errors.New(errMsg) + + returnedError := errors.New("test error msg") + mockActivityProvider.EXPECT().NewApplicationError("test-error-type", fmt.Sprintf("statusCode: %v, responseBody: %v, errMsg: %v", httpResp.StatusCode, longError[:500]+"...", errors.New(errMsg[:50]+"..."))).Return(returnedError) + + err = composeHttpError(false, mockActivityProvider, err, httpResp, "test-error-type") + if err != nil { + return + } + + assert.Error(t, err) + assert.Equal(t, returnedError, err) +} + +func TestComposeHttpError_LocalActivity_ShortErrorResponse(t *testing.T) { + ctrl := gomock.NewController(t) + mockActivityProvider := NewMockActivityProvider(ctrl) + + shortError := strings.Repeat("a", 40) + + httpResp := &http.Response{ + StatusCode: 400, + Body: io.NopCloser(strings.NewReader(shortError)), + } + errMsg := "OK" + err := errors.New(errMsg) + + returnedError := errors.New("test error msg") + mockActivityProvider.EXPECT().NewApplicationError("1st-attempt-failure", fmt.Sprintf("statusCode: %v, responseBody: %v, errMsg: %v", httpResp.StatusCode, shortError, errors.New(errMsg))).Return(returnedError) + + err = composeHttpError(true, mockActivityProvider, err, httpResp, "test-error-type") + if err != nil { + return + } + + assert.Error(t, err) + assert.Equal(t, returnedError, err) +} + +func TestComposeHttpError_RegularActivity_ShortErrorResponse(t *testing.T) { + ctrl := gomock.NewController(t) + mockActivityProvider := NewMockActivityProvider(ctrl) + + shortError := strings.Repeat("a", 40) + + httpResp := &http.Response{ + StatusCode: 400, + Body: io.NopCloser(strings.NewReader(shortError)), + } + errMsg := "OK" + err := errors.New(errMsg) + + returnedError := errors.New("test error msg") + mockActivityProvider.EXPECT().NewApplicationError("test-error-type", fmt.Sprintf("statusCode: %v, responseBody: %v, errMsg: %v", httpResp.StatusCode, shortError, errors.New(errMsg))).Return(returnedError) + + err = composeHttpError(false, mockActivityProvider, err, httpResp, "test-error-type") + if err != nil { + return + } + + assert.Error(t, err) + assert.Equal(t, returnedError, err) +} + +func TestComposeHttpError_LocalActivity_NilResponse(t *testing.T) { + ctrl := gomock.NewController(t) + mockActivityProvider := NewMockActivityProvider(ctrl) + + errMsg := "OK" + err := errors.New(errMsg) + + returnedError := errors.New("test error msg") + mockActivityProvider.EXPECT().NewApplicationError("1st-attempt-failure", fmt.Sprintf("statusCode: %v, responseBody: %v, errMsg: %v", 0, "None", errors.New(errMsg))).Return(returnedError) + + err = composeHttpError(true, mockActivityProvider, err, nil, "test-error-type") + if err != nil { + return + } + + assert.Error(t, err) + assert.Equal(t, returnedError, err) +} + +func TestComposeHttpError_RegularActivity_NilResponse(t *testing.T) { + ctrl := gomock.NewController(t) + mockActivityProvider := NewMockActivityProvider(ctrl) + + errMsg := "OK" + err := errors.New(errMsg) + + returnedError := errors.New("test error msg") + mockActivityProvider.EXPECT().NewApplicationError("test-error-type", fmt.Sprintf("statusCode: %v, responseBody: %v, errMsg: %v", 0, "None", errors.New(errMsg))).Return(returnedError) + + err = composeHttpError(false, mockActivityProvider, err, nil, "test-error-type") + if err != nil { + return + } + + assert.Error(t, err) + assert.Equal(t, returnedError, err) +} diff --git a/service/interpreter/interfaces_mock.go b/service/interpreter/interfaces_mock.go new file mode 100644 index 00000000..3b2799d0 --- /dev/null +++ b/service/interpreter/interfaces_mock.go @@ -0,0 +1,701 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: /Users/lwolczynski/indeedeng/iwf-server/service/interpreter/interfaces.go + +// Package interpreter is a generated GoMock package. +package interpreter + +import ( + context "context" + reflect "reflect" + time "time" + + gomock "github.com/golang/mock/gomock" + iwfidl "github.com/indeedeng/iwf/gen/iwfidl" + service "github.com/indeedeng/iwf/service" +) + +// MockActivityProvider is a mock of ActivityProvider interface. +type MockActivityProvider struct { + ctrl *gomock.Controller + recorder *MockActivityProviderMockRecorder +} + +// MockActivityProviderMockRecorder is the mock recorder for MockActivityProvider. +type MockActivityProviderMockRecorder struct { + mock *MockActivityProvider +} + +// NewMockActivityProvider creates a new mock instance. +func NewMockActivityProvider(ctrl *gomock.Controller) *MockActivityProvider { + mock := &MockActivityProvider{ctrl: ctrl} + mock.recorder = &MockActivityProviderMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockActivityProvider) EXPECT() *MockActivityProviderMockRecorder { + return m.recorder +} + +// GetActivityInfo mocks base method. +func (m *MockActivityProvider) GetActivityInfo(ctx context.Context) ActivityInfo { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetActivityInfo", ctx) + ret0, _ := ret[0].(ActivityInfo) + return ret0 +} + +// GetActivityInfo indicates an expected call of GetActivityInfo. +func (mr *MockActivityProviderMockRecorder) GetActivityInfo(ctx interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetActivityInfo", reflect.TypeOf((*MockActivityProvider)(nil).GetActivityInfo), ctx) +} + +// GetLogger mocks base method. +func (m *MockActivityProvider) GetLogger(ctx context.Context) UnifiedLogger { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetLogger", ctx) + ret0, _ := ret[0].(UnifiedLogger) + return ret0 +} + +// GetLogger indicates an expected call of GetLogger. +func (mr *MockActivityProviderMockRecorder) GetLogger(ctx interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetLogger", reflect.TypeOf((*MockActivityProvider)(nil).GetLogger), ctx) +} + +// NewApplicationError mocks base method. +func (m *MockActivityProvider) NewApplicationError(errType string, details interface{}) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "NewApplicationError", errType, details) + ret0, _ := ret[0].(error) + return ret0 +} + +// NewApplicationError indicates an expected call of NewApplicationError. +func (mr *MockActivityProviderMockRecorder) NewApplicationError(errType, details interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NewApplicationError", reflect.TypeOf((*MockActivityProvider)(nil).NewApplicationError), errType, details) +} + +// MockUnifiedLogger is a mock of UnifiedLogger interface. +type MockUnifiedLogger struct { + ctrl *gomock.Controller + recorder *MockUnifiedLoggerMockRecorder +} + +// MockUnifiedLoggerMockRecorder is the mock recorder for MockUnifiedLogger. +type MockUnifiedLoggerMockRecorder struct { + mock *MockUnifiedLogger +} + +// NewMockUnifiedLogger creates a new mock instance. +func NewMockUnifiedLogger(ctrl *gomock.Controller) *MockUnifiedLogger { + mock := &MockUnifiedLogger{ctrl: ctrl} + mock.recorder = &MockUnifiedLoggerMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockUnifiedLogger) EXPECT() *MockUnifiedLoggerMockRecorder { + return m.recorder +} + +// Debug mocks base method. +func (m *MockUnifiedLogger) Debug(msg string, keyvals ...interface{}) { + m.ctrl.T.Helper() + varargs := []interface{}{msg} + for _, a := range keyvals { + varargs = append(varargs, a) + } + m.ctrl.Call(m, "Debug", varargs...) +} + +// Debug indicates an expected call of Debug. +func (mr *MockUnifiedLoggerMockRecorder) Debug(msg interface{}, keyvals ...interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]interface{}{msg}, keyvals...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Debug", reflect.TypeOf((*MockUnifiedLogger)(nil).Debug), varargs...) +} + +// Error mocks base method. +func (m *MockUnifiedLogger) Error(msg string, keyvals ...interface{}) { + m.ctrl.T.Helper() + varargs := []interface{}{msg} + for _, a := range keyvals { + varargs = append(varargs, a) + } + m.ctrl.Call(m, "Error", varargs...) +} + +// Error indicates an expected call of Error. +func (mr *MockUnifiedLoggerMockRecorder) Error(msg interface{}, keyvals ...interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]interface{}{msg}, keyvals...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Error", reflect.TypeOf((*MockUnifiedLogger)(nil).Error), varargs...) +} + +// Info mocks base method. +func (m *MockUnifiedLogger) Info(msg string, keyvals ...interface{}) { + m.ctrl.T.Helper() + varargs := []interface{}{msg} + for _, a := range keyvals { + varargs = append(varargs, a) + } + m.ctrl.Call(m, "Info", varargs...) +} + +// Info indicates an expected call of Info. +func (mr *MockUnifiedLoggerMockRecorder) Info(msg interface{}, keyvals ...interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]interface{}{msg}, keyvals...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Info", reflect.TypeOf((*MockUnifiedLogger)(nil).Info), varargs...) +} + +// Warn mocks base method. +func (m *MockUnifiedLogger) Warn(msg string, keyvals ...interface{}) { + m.ctrl.T.Helper() + varargs := []interface{}{msg} + for _, a := range keyvals { + varargs = append(varargs, a) + } + m.ctrl.Call(m, "Warn", varargs...) +} + +// Warn indicates an expected call of Warn. +func (mr *MockUnifiedLoggerMockRecorder) Warn(msg interface{}, keyvals ...interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]interface{}{msg}, keyvals...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Warn", reflect.TypeOf((*MockUnifiedLogger)(nil).Warn), varargs...) +} + +// MockUnifiedContext is a mock of UnifiedContext interface. +type MockUnifiedContext struct { + ctrl *gomock.Controller + recorder *MockUnifiedContextMockRecorder +} + +// MockUnifiedContextMockRecorder is the mock recorder for MockUnifiedContext. +type MockUnifiedContextMockRecorder struct { + mock *MockUnifiedContext +} + +// NewMockUnifiedContext creates a new mock instance. +func NewMockUnifiedContext(ctrl *gomock.Controller) *MockUnifiedContext { + mock := &MockUnifiedContext{ctrl: ctrl} + mock.recorder = &MockUnifiedContextMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockUnifiedContext) EXPECT() *MockUnifiedContextMockRecorder { + return m.recorder +} + +// GetContext mocks base method. +func (m *MockUnifiedContext) GetContext() interface{} { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetContext") + ret0, _ := ret[0].(interface{}) + return ret0 +} + +// GetContext indicates an expected call of GetContext. +func (mr *MockUnifiedContextMockRecorder) GetContext() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetContext", reflect.TypeOf((*MockUnifiedContext)(nil).GetContext)) +} + +// MockWorkflowProvider is a mock of WorkflowProvider interface. +type MockWorkflowProvider struct { + ctrl *gomock.Controller + recorder *MockWorkflowProviderMockRecorder +} + +// MockWorkflowProviderMockRecorder is the mock recorder for MockWorkflowProvider. +type MockWorkflowProviderMockRecorder struct { + mock *MockWorkflowProvider +} + +// NewMockWorkflowProvider creates a new mock instance. +func NewMockWorkflowProvider(ctrl *gomock.Controller) *MockWorkflowProvider { + mock := &MockWorkflowProvider{ctrl: ctrl} + mock.recorder = &MockWorkflowProviderMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockWorkflowProvider) EXPECT() *MockWorkflowProviderMockRecorder { + return m.recorder +} + +// Await mocks base method. +func (m *MockWorkflowProvider) Await(ctx UnifiedContext, condition func() bool) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Await", ctx, condition) + ret0, _ := ret[0].(error) + return ret0 +} + +// Await indicates an expected call of Await. +func (mr *MockWorkflowProviderMockRecorder) Await(ctx, condition interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Await", reflect.TypeOf((*MockWorkflowProvider)(nil).Await), ctx, condition) +} + +// ExecuteActivity mocks base method. +func (m *MockWorkflowProvider) ExecuteActivity(valuePtr interface{}, optimizeByLocalActivity bool, ctx UnifiedContext, activity interface{}, args ...interface{}) error { + m.ctrl.T.Helper() + varargs := []interface{}{valuePtr, optimizeByLocalActivity, ctx, activity} + for _, a := range args { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "ExecuteActivity", varargs...) + ret0, _ := ret[0].(error) + return ret0 +} + +// ExecuteActivity indicates an expected call of ExecuteActivity. +func (mr *MockWorkflowProviderMockRecorder) ExecuteActivity(valuePtr, optimizeByLocalActivity, ctx, activity interface{}, args ...interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]interface{}{valuePtr, optimizeByLocalActivity, ctx, activity}, args...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ExecuteActivity", reflect.TypeOf((*MockWorkflowProvider)(nil).ExecuteActivity), varargs...) +} + +// ExtendContextWithValue mocks base method. +func (m *MockWorkflowProvider) ExtendContextWithValue(parent UnifiedContext, key string, val interface{}) UnifiedContext { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ExtendContextWithValue", parent, key, val) + ret0, _ := ret[0].(UnifiedContext) + return ret0 +} + +// ExtendContextWithValue indicates an expected call of ExtendContextWithValue. +func (mr *MockWorkflowProviderMockRecorder) ExtendContextWithValue(parent, key, val interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ExtendContextWithValue", reflect.TypeOf((*MockWorkflowProvider)(nil).ExtendContextWithValue), parent, key, val) +} + +// GetBackendType mocks base method. +func (m *MockWorkflowProvider) GetBackendType() service.BackendType { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetBackendType") + ret0, _ := ret[0].(service.BackendType) + return ret0 +} + +// GetBackendType indicates an expected call of GetBackendType. +func (mr *MockWorkflowProviderMockRecorder) GetBackendType() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetBackendType", reflect.TypeOf((*MockWorkflowProvider)(nil).GetBackendType)) +} + +// GetContextValue mocks base method. +func (m *MockWorkflowProvider) GetContextValue(ctx UnifiedContext, key string) interface{} { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetContextValue", ctx, key) + ret0, _ := ret[0].(interface{}) + return ret0 +} + +// GetContextValue indicates an expected call of GetContextValue. +func (mr *MockWorkflowProviderMockRecorder) GetContextValue(ctx, key interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetContextValue", reflect.TypeOf((*MockWorkflowProvider)(nil).GetContextValue), ctx, key) +} + +// GetLogger mocks base method. +func (m *MockWorkflowProvider) GetLogger(ctx UnifiedContext) UnifiedLogger { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetLogger", ctx) + ret0, _ := ret[0].(UnifiedLogger) + return ret0 +} + +// GetLogger indicates an expected call of GetLogger. +func (mr *MockWorkflowProviderMockRecorder) GetLogger(ctx interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetLogger", reflect.TypeOf((*MockWorkflowProvider)(nil).GetLogger), ctx) +} + +// GetPendingThreadNames mocks base method. +func (m *MockWorkflowProvider) GetPendingThreadNames() map[string]int { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetPendingThreadNames") + ret0, _ := ret[0].(map[string]int) + return ret0 +} + +// GetPendingThreadNames indicates an expected call of GetPendingThreadNames. +func (mr *MockWorkflowProviderMockRecorder) GetPendingThreadNames() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetPendingThreadNames", reflect.TypeOf((*MockWorkflowProvider)(nil).GetPendingThreadNames)) +} + +// GetSearchAttributes mocks base method. +func (m *MockWorkflowProvider) GetSearchAttributes(ctx UnifiedContext, requestedSearchAttributes []iwfidl.SearchAttributeKeyAndType) (map[string]iwfidl.SearchAttribute, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetSearchAttributes", ctx, requestedSearchAttributes) + ret0, _ := ret[0].(map[string]iwfidl.SearchAttribute) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetSearchAttributes indicates an expected call of GetSearchAttributes. +func (mr *MockWorkflowProviderMockRecorder) GetSearchAttributes(ctx, requestedSearchAttributes interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetSearchAttributes", reflect.TypeOf((*MockWorkflowProvider)(nil).GetSearchAttributes), ctx, requestedSearchAttributes) +} + +// GetSignalChannel mocks base method. +func (m *MockWorkflowProvider) GetSignalChannel(ctx UnifiedContext, signalName string) ReceiveChannel { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetSignalChannel", ctx, signalName) + ret0, _ := ret[0].(ReceiveChannel) + return ret0 +} + +// GetSignalChannel indicates an expected call of GetSignalChannel. +func (mr *MockWorkflowProviderMockRecorder) GetSignalChannel(ctx, signalName interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetSignalChannel", reflect.TypeOf((*MockWorkflowProvider)(nil).GetSignalChannel), ctx, signalName) +} + +// GetThreadCount mocks base method. +func (m *MockWorkflowProvider) GetThreadCount() int { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetThreadCount") + ret0, _ := ret[0].(int) + return ret0 +} + +// GetThreadCount indicates an expected call of GetThreadCount. +func (mr *MockWorkflowProviderMockRecorder) GetThreadCount() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetThreadCount", reflect.TypeOf((*MockWorkflowProvider)(nil).GetThreadCount)) +} + +// GetUnhandledSignalNames mocks base method. +func (m *MockWorkflowProvider) GetUnhandledSignalNames(ctx UnifiedContext) []string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetUnhandledSignalNames", ctx) + ret0, _ := ret[0].([]string) + return ret0 +} + +// GetUnhandledSignalNames indicates an expected call of GetUnhandledSignalNames. +func (mr *MockWorkflowProviderMockRecorder) GetUnhandledSignalNames(ctx interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetUnhandledSignalNames", reflect.TypeOf((*MockWorkflowProvider)(nil).GetUnhandledSignalNames), ctx) +} + +// GetVersion mocks base method. +func (m *MockWorkflowProvider) GetVersion(ctx UnifiedContext, changeID string, minSupported, maxSupported int) int { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetVersion", ctx, changeID, minSupported, maxSupported) + ret0, _ := ret[0].(int) + return ret0 +} + +// GetVersion indicates an expected call of GetVersion. +func (mr *MockWorkflowProviderMockRecorder) GetVersion(ctx, changeID, minSupported, maxSupported interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetVersion", reflect.TypeOf((*MockWorkflowProvider)(nil).GetVersion), ctx, changeID, minSupported, maxSupported) +} + +// GetWorkflowInfo mocks base method. +func (m *MockWorkflowProvider) GetWorkflowInfo(ctx UnifiedContext) WorkflowInfo { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetWorkflowInfo", ctx) + ret0, _ := ret[0].(WorkflowInfo) + return ret0 +} + +// GetWorkflowInfo indicates an expected call of GetWorkflowInfo. +func (mr *MockWorkflowProviderMockRecorder) GetWorkflowInfo(ctx interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetWorkflowInfo", reflect.TypeOf((*MockWorkflowProvider)(nil).GetWorkflowInfo), ctx) +} + +// GoNamed mocks base method. +func (m *MockWorkflowProvider) GoNamed(ctx UnifiedContext, name string, f func(UnifiedContext)) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "GoNamed", ctx, name, f) +} + +// GoNamed indicates an expected call of GoNamed. +func (mr *MockWorkflowProviderMockRecorder) GoNamed(ctx, name, f interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GoNamed", reflect.TypeOf((*MockWorkflowProvider)(nil).GoNamed), ctx, name, f) +} + +// IsApplicationError mocks base method. +func (m *MockWorkflowProvider) IsApplicationError(err error) bool { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "IsApplicationError", err) + ret0, _ := ret[0].(bool) + return ret0 +} + +// IsApplicationError indicates an expected call of IsApplicationError. +func (mr *MockWorkflowProviderMockRecorder) IsApplicationError(err interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "IsApplicationError", reflect.TypeOf((*MockWorkflowProvider)(nil).IsApplicationError), err) +} + +// IsReplaying mocks base method. +func (m *MockWorkflowProvider) IsReplaying(ctx UnifiedContext) bool { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "IsReplaying", ctx) + ret0, _ := ret[0].(bool) + return ret0 +} + +// IsReplaying indicates an expected call of IsReplaying. +func (mr *MockWorkflowProviderMockRecorder) IsReplaying(ctx interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "IsReplaying", reflect.TypeOf((*MockWorkflowProvider)(nil).IsReplaying), ctx) +} + +// NewApplicationError mocks base method. +func (m *MockWorkflowProvider) NewApplicationError(errType string, details interface{}) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "NewApplicationError", errType, details) + ret0, _ := ret[0].(error) + return ret0 +} + +// NewApplicationError indicates an expected call of NewApplicationError. +func (mr *MockWorkflowProviderMockRecorder) NewApplicationError(errType, details interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NewApplicationError", reflect.TypeOf((*MockWorkflowProvider)(nil).NewApplicationError), errType, details) +} + +// NewInterpreterContinueAsNewError mocks base method. +func (m *MockWorkflowProvider) NewInterpreterContinueAsNewError(ctx UnifiedContext, input service.InterpreterWorkflowInput) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "NewInterpreterContinueAsNewError", ctx, input) + ret0, _ := ret[0].(error) + return ret0 +} + +// NewInterpreterContinueAsNewError indicates an expected call of NewInterpreterContinueAsNewError. +func (mr *MockWorkflowProviderMockRecorder) NewInterpreterContinueAsNewError(ctx, input interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NewInterpreterContinueAsNewError", reflect.TypeOf((*MockWorkflowProvider)(nil).NewInterpreterContinueAsNewError), ctx, input) +} + +// NewTimer mocks base method. +func (m *MockWorkflowProvider) NewTimer(ctx UnifiedContext, d time.Duration) Future { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "NewTimer", ctx, d) + ret0, _ := ret[0].(Future) + return ret0 +} + +// NewTimer indicates an expected call of NewTimer. +func (mr *MockWorkflowProviderMockRecorder) NewTimer(ctx, d interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NewTimer", reflect.TypeOf((*MockWorkflowProvider)(nil).NewTimer), ctx, d) +} + +// Now mocks base method. +func (m *MockWorkflowProvider) Now(ctx UnifiedContext) time.Time { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Now", ctx) + ret0, _ := ret[0].(time.Time) + return ret0 +} + +// Now indicates an expected call of Now. +func (mr *MockWorkflowProviderMockRecorder) Now(ctx interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Now", reflect.TypeOf((*MockWorkflowProvider)(nil).Now), ctx) +} + +// SetQueryHandler mocks base method. +func (m *MockWorkflowProvider) SetQueryHandler(ctx UnifiedContext, queryType string, handler interface{}) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "SetQueryHandler", ctx, queryType, handler) + ret0, _ := ret[0].(error) + return ret0 +} + +// SetQueryHandler indicates an expected call of SetQueryHandler. +func (mr *MockWorkflowProviderMockRecorder) SetQueryHandler(ctx, queryType, handler interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetQueryHandler", reflect.TypeOf((*MockWorkflowProvider)(nil).SetQueryHandler), ctx, queryType, handler) +} + +// SetRpcUpdateHandler mocks base method. +func (m *MockWorkflowProvider) SetRpcUpdateHandler(ctx UnifiedContext, updateType string, validator UnifiedRpcValidator, handler UnifiedRpcHandler) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "SetRpcUpdateHandler", ctx, updateType, validator, handler) + ret0, _ := ret[0].(error) + return ret0 +} + +// SetRpcUpdateHandler indicates an expected call of SetRpcUpdateHandler. +func (mr *MockWorkflowProviderMockRecorder) SetRpcUpdateHandler(ctx, updateType, validator, handler interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetRpcUpdateHandler", reflect.TypeOf((*MockWorkflowProvider)(nil).SetRpcUpdateHandler), ctx, updateType, validator, handler) +} + +// Sleep mocks base method. +func (m *MockWorkflowProvider) Sleep(ctx UnifiedContext, d time.Duration) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Sleep", ctx, d) + ret0, _ := ret[0].(error) + return ret0 +} + +// Sleep indicates an expected call of Sleep. +func (mr *MockWorkflowProviderMockRecorder) Sleep(ctx, d interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Sleep", reflect.TypeOf((*MockWorkflowProvider)(nil).Sleep), ctx, d) +} + +// UpsertMemo mocks base method. +func (m *MockWorkflowProvider) UpsertMemo(ctx UnifiedContext, memo map[string]iwfidl.EncodedObject) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "UpsertMemo", ctx, memo) + ret0, _ := ret[0].(error) + return ret0 +} + +// UpsertMemo indicates an expected call of UpsertMemo. +func (mr *MockWorkflowProviderMockRecorder) UpsertMemo(ctx, memo interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpsertMemo", reflect.TypeOf((*MockWorkflowProvider)(nil).UpsertMemo), ctx, memo) +} + +// UpsertSearchAttributes mocks base method. +func (m *MockWorkflowProvider) UpsertSearchAttributes(ctx UnifiedContext, attributes map[string]interface{}) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "UpsertSearchAttributes", ctx, attributes) + ret0, _ := ret[0].(error) + return ret0 +} + +// UpsertSearchAttributes indicates an expected call of UpsertSearchAttributes. +func (mr *MockWorkflowProviderMockRecorder) UpsertSearchAttributes(ctx, attributes interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpsertSearchAttributes", reflect.TypeOf((*MockWorkflowProvider)(nil).UpsertSearchAttributes), ctx, attributes) +} + +// WithActivityOptions mocks base method. +func (m *MockWorkflowProvider) WithActivityOptions(ctx UnifiedContext, options ActivityOptions) UnifiedContext { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "WithActivityOptions", ctx, options) + ret0, _ := ret[0].(UnifiedContext) + return ret0 +} + +// WithActivityOptions indicates an expected call of WithActivityOptions. +func (mr *MockWorkflowProviderMockRecorder) WithActivityOptions(ctx, options interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "WithActivityOptions", reflect.TypeOf((*MockWorkflowProvider)(nil).WithActivityOptions), ctx, options) +} + +// MockReceiveChannel is a mock of ReceiveChannel interface. +type MockReceiveChannel struct { + ctrl *gomock.Controller + recorder *MockReceiveChannelMockRecorder +} + +// MockReceiveChannelMockRecorder is the mock recorder for MockReceiveChannel. +type MockReceiveChannelMockRecorder struct { + mock *MockReceiveChannel +} + +// NewMockReceiveChannel creates a new mock instance. +func NewMockReceiveChannel(ctrl *gomock.Controller) *MockReceiveChannel { + mock := &MockReceiveChannel{ctrl: ctrl} + mock.recorder = &MockReceiveChannelMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockReceiveChannel) EXPECT() *MockReceiveChannelMockRecorder { + return m.recorder +} + +// ReceiveAsync mocks base method. +func (m *MockReceiveChannel) ReceiveAsync(valuePtr interface{}) bool { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ReceiveAsync", valuePtr) + ret0, _ := ret[0].(bool) + return ret0 +} + +// ReceiveAsync indicates an expected call of ReceiveAsync. +func (mr *MockReceiveChannelMockRecorder) ReceiveAsync(valuePtr interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ReceiveAsync", reflect.TypeOf((*MockReceiveChannel)(nil).ReceiveAsync), valuePtr) +} + +// ReceiveBlocking mocks base method. +func (m *MockReceiveChannel) ReceiveBlocking(ctx UnifiedContext, valuePtr interface{}) bool { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ReceiveBlocking", ctx, valuePtr) + ret0, _ := ret[0].(bool) + return ret0 +} + +// ReceiveBlocking indicates an expected call of ReceiveBlocking. +func (mr *MockReceiveChannelMockRecorder) ReceiveBlocking(ctx, valuePtr interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ReceiveBlocking", reflect.TypeOf((*MockReceiveChannel)(nil).ReceiveBlocking), ctx, valuePtr) +} + +// MockFuture is a mock of Future interface. +type MockFuture struct { + ctrl *gomock.Controller + recorder *MockFutureMockRecorder +} + +// MockFutureMockRecorder is the mock recorder for MockFuture. +type MockFutureMockRecorder struct { + mock *MockFuture +} + +// NewMockFuture creates a new mock instance. +func NewMockFuture(ctrl *gomock.Controller) *MockFuture { + mock := &MockFuture{ctrl: ctrl} + mock.recorder = &MockFutureMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockFuture) EXPECT() *MockFutureMockRecorder { + return m.recorder +} + +// Get mocks base method. +func (m *MockFuture) Get(ctx UnifiedContext, valuePtr interface{}) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Get", ctx, valuePtr) + ret0, _ := ret[0].(error) + return ret0 +} + +// Get indicates an expected call of Get. +func (mr *MockFutureMockRecorder) Get(ctx, valuePtr interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Get", reflect.TypeOf((*MockFuture)(nil).Get), ctx, valuePtr) +} + +// IsReady mocks base method. +func (m *MockFuture) IsReady() bool { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "IsReady") + ret0, _ := ret[0].(bool) + return ret0 +} + +// IsReady indicates an expected call of IsReady. +func (mr *MockFutureMockRecorder) IsReady() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "IsReady", reflect.TypeOf((*MockFuture)(nil).IsReady)) +} From 634b899ec06925c80c7b244e7b58bb22868a8b7f Mon Sep 17 00:00:00 2001 From: lwolczynski Date: Tue, 24 Sep 2024 15:44:12 -0500 Subject: [PATCH 3/3] IWF-125: Refactor --- service/interpreter/activityImpl.go | 36 +++++-------- service/interpreter/activityImpl_test.go | 69 ++++++++---------------- 2 files changed, 36 insertions(+), 69 deletions(-) diff --git a/service/interpreter/activityImpl.go b/service/interpreter/activityImpl.go index 82c7e1f9..46e73061 100644 --- a/service/interpreter/activityImpl.go +++ b/service/interpreter/activityImpl.go @@ -176,35 +176,25 @@ func composeHttpError( statusCode = httpResp.StatusCode } errMsg := err.Error() + var trimmedResponseBody, trimmedErrMsg string if isLocalActivity { - maxL := len(errMsg) - if maxL > 5 { - maxL = 5 - errMsg = errMsg[:maxL] + "..." - } - - maxL = len(responseBody) - if maxL > 50 { - maxL = 50 - responseBody = responseBody[:maxL] + "..." - } + trimmedErrMsg = trimText(errMsg, 5) + trimmedResponseBody = trimText(responseBody, 50) errType = "1st-attempt-failure" } else { - maxL := len(errMsg) - if maxL > 50 { - maxL = 50 - errMsg = errMsg[:maxL] + "..." - } - - maxL = len(responseBody) - if maxL > 500 { - maxL = 500 - responseBody = responseBody[:maxL] + "..." - } + trimmedErrMsg = trimText(errMsg, 50) + trimmedResponseBody = trimText(responseBody, 500) } return provider.NewApplicationError(errType, - fmt.Sprintf("statusCode: %v, responseBody: %v, errMsg: %v", statusCode, responseBody, errMsg)) + fmt.Sprintf("statusCode: %v, responseBody: %v, errMsg: %v", statusCode, trimmedResponseBody, trimmedErrMsg)) +} + +func trimText(msg string, maxLength int) string { + if len(msg) > maxLength { + return msg[:maxLength] + "..." + } + return msg } func checkCommandRequestFromWaitUntilResponse(resp *iwfidl.WorkflowStateStartResponse) error { diff --git a/service/interpreter/activityImpl_test.go b/service/interpreter/activityImpl_test.go index 942e9e18..4a4e2885 100644 --- a/service/interpreter/activityImpl_test.go +++ b/service/interpreter/activityImpl_test.go @@ -96,17 +96,9 @@ func createCommands() ([]iwfidl.TimerCommand, []iwfidl.SignalCommand, []iwfidl.I } func TestComposeHttpError_LocalActivity_LongErrorResponse(t *testing.T) { - ctrl := gomock.NewController(t) - mockActivityProvider := NewMockActivityProvider(ctrl) - longError := strings.Repeat("a", 1000) - - httpResp := &http.Response{ - StatusCode: 400, - Body: io.NopCloser(strings.NewReader(longError)), - } errMsg := "original error message" - err := errors.New(errMsg) + mockActivityProvider, httpResp, err := createTestComposeHttpErrorInitialState(t, longError, errMsg) returnedError := errors.New("test error msg") mockActivityProvider.EXPECT().NewApplicationError("1st-attempt-failure", fmt.Sprintf("statusCode: %v, responseBody: %v, errMsg: %v", httpResp.StatusCode, longError[:50]+"...", errors.New(errMsg[:5]+"..."))).Return(returnedError) @@ -121,17 +113,9 @@ func TestComposeHttpError_LocalActivity_LongErrorResponse(t *testing.T) { } func TestComposeHttpError_RegularActivity_LongErrorResponse(t *testing.T) { - ctrl := gomock.NewController(t) - mockActivityProvider := NewMockActivityProvider(ctrl) - longError := strings.Repeat("a", 1000) - - httpResp := &http.Response{ - StatusCode: 400, - Body: io.NopCloser(strings.NewReader(longError)), - } errMsg := "original error message which is very long like this" - err := errors.New(errMsg) + mockActivityProvider, httpResp, err := createTestComposeHttpErrorInitialState(t, longError, errMsg) returnedError := errors.New("test error msg") mockActivityProvider.EXPECT().NewApplicationError("test-error-type", fmt.Sprintf("statusCode: %v, responseBody: %v, errMsg: %v", httpResp.StatusCode, longError[:500]+"...", errors.New(errMsg[:50]+"..."))).Return(returnedError) @@ -146,17 +130,9 @@ func TestComposeHttpError_RegularActivity_LongErrorResponse(t *testing.T) { } func TestComposeHttpError_LocalActivity_ShortErrorResponse(t *testing.T) { - ctrl := gomock.NewController(t) - mockActivityProvider := NewMockActivityProvider(ctrl) - shortError := strings.Repeat("a", 40) - - httpResp := &http.Response{ - StatusCode: 400, - Body: io.NopCloser(strings.NewReader(shortError)), - } errMsg := "OK" - err := errors.New(errMsg) + mockActivityProvider, httpResp, err := createTestComposeHttpErrorInitialState(t, shortError, errMsg) returnedError := errors.New("test error msg") mockActivityProvider.EXPECT().NewApplicationError("1st-attempt-failure", fmt.Sprintf("statusCode: %v, responseBody: %v, errMsg: %v", httpResp.StatusCode, shortError, errors.New(errMsg))).Return(returnedError) @@ -171,17 +147,9 @@ func TestComposeHttpError_LocalActivity_ShortErrorResponse(t *testing.T) { } func TestComposeHttpError_RegularActivity_ShortErrorResponse(t *testing.T) { - ctrl := gomock.NewController(t) - mockActivityProvider := NewMockActivityProvider(ctrl) - shortError := strings.Repeat("a", 40) - - httpResp := &http.Response{ - StatusCode: 400, - Body: io.NopCloser(strings.NewReader(shortError)), - } errMsg := "OK" - err := errors.New(errMsg) + mockActivityProvider, httpResp, err := createTestComposeHttpErrorInitialState(t, shortError, errMsg) returnedError := errors.New("test error msg") mockActivityProvider.EXPECT().NewApplicationError("test-error-type", fmt.Sprintf("statusCode: %v, responseBody: %v, errMsg: %v", httpResp.StatusCode, shortError, errors.New(errMsg))).Return(returnedError) @@ -196,16 +164,13 @@ func TestComposeHttpError_RegularActivity_ShortErrorResponse(t *testing.T) { } func TestComposeHttpError_LocalActivity_NilResponse(t *testing.T) { - ctrl := gomock.NewController(t) - mockActivityProvider := NewMockActivityProvider(ctrl) - errMsg := "OK" - err := errors.New(errMsg) + mockActivityProvider, httpResp, err := createTestComposeHttpErrorInitialState(t, "", errMsg) returnedError := errors.New("test error msg") mockActivityProvider.EXPECT().NewApplicationError("1st-attempt-failure", fmt.Sprintf("statusCode: %v, responseBody: %v, errMsg: %v", 0, "None", errors.New(errMsg))).Return(returnedError) - err = composeHttpError(true, mockActivityProvider, err, nil, "test-error-type") + err = composeHttpError(true, mockActivityProvider, err, httpResp, "test-error-type") if err != nil { return } @@ -215,16 +180,13 @@ func TestComposeHttpError_LocalActivity_NilResponse(t *testing.T) { } func TestComposeHttpError_RegularActivity_NilResponse(t *testing.T) { - ctrl := gomock.NewController(t) - mockActivityProvider := NewMockActivityProvider(ctrl) - errMsg := "OK" - err := errors.New(errMsg) + mockActivityProvider, httpResp, err := createTestComposeHttpErrorInitialState(t, "", errMsg) returnedError := errors.New("test error msg") mockActivityProvider.EXPECT().NewApplicationError("test-error-type", fmt.Sprintf("statusCode: %v, responseBody: %v, errMsg: %v", 0, "None", errors.New(errMsg))).Return(returnedError) - err = composeHttpError(false, mockActivityProvider, err, nil, "test-error-type") + err = composeHttpError(false, mockActivityProvider, err, httpResp, "test-error-type") if err != nil { return } @@ -232,3 +194,18 @@ func TestComposeHttpError_RegularActivity_NilResponse(t *testing.T) { assert.Error(t, err) assert.Equal(t, returnedError, err) } + +func createTestComposeHttpErrorInitialState(t *testing.T, httpError string, initialError string) (*MockActivityProvider, *http.Response, error) { + ctrl := gomock.NewController(t) + mockActivityProvider := NewMockActivityProvider(ctrl) + + var httpResp *http.Response = nil + if httpError != "" { + httpResp = &http.Response{ + StatusCode: 400, + Body: io.NopCloser(strings.NewReader(httpError)), + } + } + err := errors.New(initialError) + return mockActivityProvider, httpResp, err +}