From 27199a71cf7f4ca5a1e257f4ad8dcabfbc500884 Mon Sep 17 00:00:00 2001 From: sankari gopalakrishnan Date: Fri, 24 May 2024 09:38:06 +0200 Subject: [PATCH 1/4] Add unit tests for mutable_state_builder_methods_signal.go --- ...ble_state_builder_methods_activity_test.go | 2 + ...table_state_builder_methods_signal_test.go | 181 ++++++++++++++++++ 2 files changed, 183 insertions(+) create mode 100644 service/history/execution/mutable_state_builder_methods_signal_test.go diff --git a/service/history/execution/mutable_state_builder_methods_activity_test.go b/service/history/execution/mutable_state_builder_methods_activity_test.go index 32f534d4488..2381ce2f239 100644 --- a/service/history/execution/mutable_state_builder_methods_activity_test.go +++ b/service/history/execution/mutable_state_builder_methods_activity_test.go @@ -24,6 +24,7 @@ package execution import ( "context" + "github.com/uber/cadence/common/cache" "testing" "time" @@ -60,6 +61,7 @@ func testMutableStateBuilder(t *testing.T) *mutableStateBuilder { logger := log.NewNoop() mockShard.Resource.DomainCache.EXPECT().GetDomainID(constants.TestDomainName).Return(constants.TestDomainID, nil).AnyTimes() + mockShard.Resource.DomainCache.EXPECT().GetDomainByID(constants.TestDomainID).Return(&cache.DomainCacheEntry{}, nil).AnyTimes() return newMutableStateBuilder(mockShard, logger, constants.TestLocalDomainEntry) } diff --git a/service/history/execution/mutable_state_builder_methods_signal_test.go b/service/history/execution/mutable_state_builder_methods_signal_test.go new file mode 100644 index 00000000000..a3e011120d6 --- /dev/null +++ b/service/history/execution/mutable_state_builder_methods_signal_test.go @@ -0,0 +1,181 @@ +package execution + +import ( + "github.com/stretchr/testify/assert" + "github.com/uber/cadence/common/persistence" + "github.com/uber/cadence/common/types" + "github.com/uber/cadence/service/history/constants" + "testing" +) + +func Test__IsSignalRequested(t *testing.T) { + mb := testMutableStateBuilder(t) + requestID := "101" + t.Run("signal not found", func(t *testing.T) { + result := mb.IsSignalRequested(requestID) + assert.False(t, result) + }) + t.Run("signal found", func(t *testing.T) { + mb.pendingSignalRequestedIDs[requestID] = struct{}{} + result := mb.IsSignalRequested(requestID) + assert.True(t, result) + }) +} + +func Test__GetSignalInfo(t *testing.T) { + mb := testMutableStateBuilder(t) + initiatedEventID := int64(1) + info := &persistence.SignalInfo{ + InitiatedID: 1, + SignalRequestID: "101", + } + t.Run("signal not found", func(t *testing.T) { + _, ok := mb.GetSignalInfo(initiatedEventID) + assert.False(t, ok) + }) + t.Run("signal found", func(t *testing.T) { + mb.pendingSignalInfoIDs[initiatedEventID] = info + result, ok := mb.GetSignalInfo(initiatedEventID) + assert.True(t, ok) + assert.Equal(t, info, result) + }) +} + +func Test__ReplicateExternalWorkflowExecutionSignaled(t *testing.T) { + mb := testMutableStateBuilder(t) + event := &types.HistoryEvent{ + ExternalWorkflowExecutionSignaledEventAttributes: &types.ExternalWorkflowExecutionSignaledEventAttributes{ + InitiatedEventID: 1, + }, + } + info := &persistence.SignalInfo{ + InitiatedID: 1, + SignalRequestID: "101", + } + mb.pendingSignalInfoIDs[int64(1)] = info + mb.updateSignalInfos[int64(1)] = info + err := mb.ReplicateExternalWorkflowExecutionSignaled(event) + assert.NoError(t, err) + assert.NotNil(t, mb.deleteSignalInfos[int64(1)]) +} + +func Test__ReplicateSignalExternalWorkflowExecutionFailedEvent(t *testing.T) { + mb := testMutableStateBuilder(t) + event := &types.HistoryEvent{ + SignalExternalWorkflowExecutionFailedEventAttributes: &types.SignalExternalWorkflowExecutionFailedEventAttributes{ + InitiatedEventID: 1, + }, + } + info := &persistence.SignalInfo{ + InitiatedID: 1, + SignalRequestID: "101", + } + mb.pendingSignalInfoIDs[int64(1)] = info + mb.updateSignalInfos[int64(1)] = info + err := mb.ReplicateSignalExternalWorkflowExecutionFailedEvent(event) + assert.NoError(t, err) + assert.NotNil(t, mb.deleteSignalInfos[int64(1)]) +} + +func Test__AddSignalRequested(t *testing.T) { + mb := testMutableStateBuilder(t) + requestID := "101" + mb.pendingSignalRequestedIDs = nil + mb.updateSignalRequestedIDs = nil + mb.AddSignalRequested(requestID) + assert.NotNil(t, mb.pendingSignalRequestedIDs[requestID]) + assert.NotNil(t, mb.updateSignalRequestedIDs[requestID]) +} + +func Test__DeleteSignalRequested(t *testing.T) { + mb := testMutableStateBuilder(t) + requestID := "101" + mb.pendingSignalRequestedIDs[requestID] = struct{}{} + mb.updateSignalRequestedIDs[requestID] = struct{}{} + mb.DeleteSignalRequested(requestID) + assert.NotNil(t, mb.deleteSignalRequestedIDs[requestID]) +} + +func Test__AddExternalWorkflowExecutionSignaled(t *testing.T) { + mb := testMutableStateBuilder(t) + t.Run("error workflow finished", func(t *testing.T) { + mbCompleted := testMutableStateBuilder(t) + mbCompleted.executionInfo.State = persistence.WorkflowStateCompleted + _, err := mbCompleted.AddExternalWorkflowExecutionSignaled(1, "test-domain", "wid", "rid", []byte{10}) + assert.Error(t, err) + assert.Equal(t, ErrWorkflowFinished, err) + }) + t.Run("error getting signal info", func(t *testing.T) { + _, err := mb.AddExternalWorkflowExecutionSignaled(1, "test-domain", "wid", "rid", []byte{10}) + assert.Error(t, err) + assert.Equal(t, "add-externalworkflow-signal-requested-event operation failed", err.Error()) + }) + t.Run("success", func(t *testing.T) { + si := &persistence.SignalInfo{ + InitiatedID: 1, + } + mb.pendingSignalInfoIDs[1] = si + mb.hBuilder = NewHistoryBuilder(mb) + event, err := mb.AddExternalWorkflowExecutionSignaled(1, "test-domain", "wid", "rid", []byte{10}) + assert.NoError(t, err) + assert.Equal(t, int64(1), event.ExternalWorkflowExecutionSignaledEventAttributes.GetInitiatedEventID()) + }) +} + +func Test__AddSignalExternalWorkflowExecutionFailedEvent(t *testing.T) { + mb := testMutableStateBuilder(t) + t.Run("error workflow finished", func(t *testing.T) { + mbCompleted := testMutableStateBuilder(t) + mbCompleted.executionInfo.State = persistence.WorkflowStateCompleted + _, err := mbCompleted.AddSignalExternalWorkflowExecutionFailedEvent(1, 1, "test-domain", "wid", "rid", []byte{10}, types.SignalExternalWorkflowExecutionFailedCauseWorkflowAlreadyCompleted) + assert.Error(t, err) + assert.Equal(t, ErrWorkflowFinished, err) + }) + t.Run("error getting signal info", func(t *testing.T) { + _, err := mb.AddSignalExternalWorkflowExecutionFailedEvent(1, 1, "test-domain", "wid", "rid", []byte{10}, types.SignalExternalWorkflowExecutionFailedCauseWorkflowAlreadyCompleted) + assert.Error(t, err) + assert.Equal(t, "add-externalworkflow-signal-failed-event operation failed", err.Error()) + }) + t.Run("success", func(t *testing.T) { + si := &persistence.SignalInfo{ + InitiatedID: 1, + } + mb.pendingSignalInfoIDs[1] = si + mb.hBuilder = NewHistoryBuilder(mb) + event, err := mb.AddSignalExternalWorkflowExecutionFailedEvent(1, 1, "test-domain", "wid", "rid", []byte{10}, types.SignalExternalWorkflowExecutionFailedCauseWorkflowAlreadyCompleted) + assert.NoError(t, err) + assert.Equal(t, int64(1), event.SignalExternalWorkflowExecutionFailedEventAttributes.GetInitiatedEventID()) + }) +} + +func Test__AddSignalExternalWorkflowExecutionInitiatedEvent(t *testing.T) { + mb := testMutableStateBuilder(t) + request := &types.SignalExternalWorkflowExecutionDecisionAttributes{ + Domain: constants.TestDomainName, + Execution: &types.WorkflowExecution{ + WorkflowID: "wid", + RunID: "rid", + }, + SignalName: "test-signal", + Input: make([]byte, 0), + } + mb.executionInfo = &persistence.WorkflowExecutionInfo{ + DomainID: constants.TestDomainID, + WorkflowID: "wid", + RunID: "rid", + } + t.Run("error workflow finished", func(t *testing.T) { + mbCompleted := testMutableStateBuilder(t) + mbCompleted.executionInfo.State = persistence.WorkflowStateCompleted + _, _, err := mbCompleted.AddSignalExternalWorkflowExecutionInitiatedEvent(1, "101", request) + assert.Error(t, err) + assert.Equal(t, ErrWorkflowFinished, err) + }) + t.Run("success", func(t *testing.T) { + mb.hBuilder = NewHistoryBuilder(mb) + event, si, err := mb.AddSignalExternalWorkflowExecutionInitiatedEvent(1, "101", request) + assert.NoError(t, err) + assert.Equal(t, request.Execution, event.SignalExternalWorkflowExecutionInitiatedEventAttributes.GetWorkflowExecution()) + assert.Equal(t, "101", si.SignalRequestID) + }) +} From e92ddbe284a58228d58816660222580443587d1a Mon Sep 17 00:00:00 2001 From: sankari gopalakrishnan Date: Fri, 24 May 2024 10:37:29 +0200 Subject: [PATCH 2/4] Update mutable_state_builder_methods_signal_test.go --- .../mutable_state_builder_methods_signal_test.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/service/history/execution/mutable_state_builder_methods_signal_test.go b/service/history/execution/mutable_state_builder_methods_signal_test.go index a3e011120d6..fde404a944b 100644 --- a/service/history/execution/mutable_state_builder_methods_signal_test.go +++ b/service/history/execution/mutable_state_builder_methods_signal_test.go @@ -57,6 +57,10 @@ func Test__ReplicateExternalWorkflowExecutionSignaled(t *testing.T) { err := mb.ReplicateExternalWorkflowExecutionSignaled(event) assert.NoError(t, err) assert.NotNil(t, mb.deleteSignalInfos[int64(1)]) + _, ok := mb.pendingSignalInfoIDs[int64(1)] + assert.False(t, ok) + _, ok = mb.updateSignalInfos[int64(1)] + assert.False(t, ok) } func Test__ReplicateSignalExternalWorkflowExecutionFailedEvent(t *testing.T) { @@ -75,6 +79,10 @@ func Test__ReplicateSignalExternalWorkflowExecutionFailedEvent(t *testing.T) { err := mb.ReplicateSignalExternalWorkflowExecutionFailedEvent(event) assert.NoError(t, err) assert.NotNil(t, mb.deleteSignalInfos[int64(1)]) + _, ok := mb.pendingSignalInfoIDs[int64(1)] + assert.False(t, ok) + _, ok = mb.updateSignalInfos[int64(1)] + assert.False(t, ok) } func Test__AddSignalRequested(t *testing.T) { From de22c64a2e5d0ecc0c0aa94714ee60128e2b1d9f Mon Sep 17 00:00:00 2001 From: sankari gopalakrishnan Date: Fri, 24 May 2024 10:39:44 +0200 Subject: [PATCH 3/4] Update mutable_state_builder_methods_signal_test.go --- ...table_state_builder_methods_signal_test.go | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/service/history/execution/mutable_state_builder_methods_signal_test.go b/service/history/execution/mutable_state_builder_methods_signal_test.go index fde404a944b..f7b8acbb774 100644 --- a/service/history/execution/mutable_state_builder_methods_signal_test.go +++ b/service/history/execution/mutable_state_builder_methods_signal_test.go @@ -9,13 +9,14 @@ import ( ) func Test__IsSignalRequested(t *testing.T) { - mb := testMutableStateBuilder(t) requestID := "101" t.Run("signal not found", func(t *testing.T) { + mb := testMutableStateBuilder(t) result := mb.IsSignalRequested(requestID) assert.False(t, result) }) t.Run("signal found", func(t *testing.T) { + mb := testMutableStateBuilder(t) mb.pendingSignalRequestedIDs[requestID] = struct{}{} result := mb.IsSignalRequested(requestID) assert.True(t, result) @@ -23,17 +24,18 @@ func Test__IsSignalRequested(t *testing.T) { } func Test__GetSignalInfo(t *testing.T) { - mb := testMutableStateBuilder(t) initiatedEventID := int64(1) info := &persistence.SignalInfo{ InitiatedID: 1, SignalRequestID: "101", } t.Run("signal not found", func(t *testing.T) { + mb := testMutableStateBuilder(t) _, ok := mb.GetSignalInfo(initiatedEventID) assert.False(t, ok) }) t.Run("signal found", func(t *testing.T) { + mb := testMutableStateBuilder(t) mb.pendingSignalInfoIDs[initiatedEventID] = info result, ok := mb.GetSignalInfo(initiatedEventID) assert.True(t, ok) @@ -105,7 +107,6 @@ func Test__DeleteSignalRequested(t *testing.T) { } func Test__AddExternalWorkflowExecutionSignaled(t *testing.T) { - mb := testMutableStateBuilder(t) t.Run("error workflow finished", func(t *testing.T) { mbCompleted := testMutableStateBuilder(t) mbCompleted.executionInfo.State = persistence.WorkflowStateCompleted @@ -114,11 +115,13 @@ func Test__AddExternalWorkflowExecutionSignaled(t *testing.T) { assert.Equal(t, ErrWorkflowFinished, err) }) t.Run("error getting signal info", func(t *testing.T) { + mb := testMutableStateBuilder(t) _, err := mb.AddExternalWorkflowExecutionSignaled(1, "test-domain", "wid", "rid", []byte{10}) assert.Error(t, err) assert.Equal(t, "add-externalworkflow-signal-requested-event operation failed", err.Error()) }) t.Run("success", func(t *testing.T) { + mb := testMutableStateBuilder(t) si := &persistence.SignalInfo{ InitiatedID: 1, } @@ -131,7 +134,6 @@ func Test__AddExternalWorkflowExecutionSignaled(t *testing.T) { } func Test__AddSignalExternalWorkflowExecutionFailedEvent(t *testing.T) { - mb := testMutableStateBuilder(t) t.Run("error workflow finished", func(t *testing.T) { mbCompleted := testMutableStateBuilder(t) mbCompleted.executionInfo.State = persistence.WorkflowStateCompleted @@ -140,11 +142,13 @@ func Test__AddSignalExternalWorkflowExecutionFailedEvent(t *testing.T) { assert.Equal(t, ErrWorkflowFinished, err) }) t.Run("error getting signal info", func(t *testing.T) { + mb := testMutableStateBuilder(t) _, err := mb.AddSignalExternalWorkflowExecutionFailedEvent(1, 1, "test-domain", "wid", "rid", []byte{10}, types.SignalExternalWorkflowExecutionFailedCauseWorkflowAlreadyCompleted) assert.Error(t, err) assert.Equal(t, "add-externalworkflow-signal-failed-event operation failed", err.Error()) }) t.Run("success", func(t *testing.T) { + mb := testMutableStateBuilder(t) si := &persistence.SignalInfo{ InitiatedID: 1, } @@ -157,7 +161,6 @@ func Test__AddSignalExternalWorkflowExecutionFailedEvent(t *testing.T) { } func Test__AddSignalExternalWorkflowExecutionInitiatedEvent(t *testing.T) { - mb := testMutableStateBuilder(t) request := &types.SignalExternalWorkflowExecutionDecisionAttributes{ Domain: constants.TestDomainName, Execution: &types.WorkflowExecution{ @@ -167,11 +170,6 @@ func Test__AddSignalExternalWorkflowExecutionInitiatedEvent(t *testing.T) { SignalName: "test-signal", Input: make([]byte, 0), } - mb.executionInfo = &persistence.WorkflowExecutionInfo{ - DomainID: constants.TestDomainID, - WorkflowID: "wid", - RunID: "rid", - } t.Run("error workflow finished", func(t *testing.T) { mbCompleted := testMutableStateBuilder(t) mbCompleted.executionInfo.State = persistence.WorkflowStateCompleted @@ -180,6 +178,12 @@ func Test__AddSignalExternalWorkflowExecutionInitiatedEvent(t *testing.T) { assert.Equal(t, ErrWorkflowFinished, err) }) t.Run("success", func(t *testing.T) { + mb := testMutableStateBuilder(t) + mb.executionInfo = &persistence.WorkflowExecutionInfo{ + DomainID: constants.TestDomainID, + WorkflowID: "wid", + RunID: "rid", + } mb.hBuilder = NewHistoryBuilder(mb) event, si, err := mb.AddSignalExternalWorkflowExecutionInitiatedEvent(1, "101", request) assert.NoError(t, err) From a0c80cf769b88d63ba59f5f1b8e00fb9c3e7c7dc Mon Sep 17 00:00:00 2001 From: sankari gopalakrishnan Date: Fri, 24 May 2024 11:16:43 +0200 Subject: [PATCH 4/4] fix lint issues --- ...ble_state_builder_methods_activity_test.go | 2 +- ...table_state_builder_methods_signal_test.go | 26 ++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/service/history/execution/mutable_state_builder_methods_activity_test.go b/service/history/execution/mutable_state_builder_methods_activity_test.go index 2381ce2f239..89351cff0c0 100644 --- a/service/history/execution/mutable_state_builder_methods_activity_test.go +++ b/service/history/execution/mutable_state_builder_methods_activity_test.go @@ -24,7 +24,6 @@ package execution import ( "context" - "github.com/uber/cadence/common/cache" "testing" "time" @@ -32,6 +31,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/uber/cadence/common" + "github.com/uber/cadence/common/cache" "github.com/uber/cadence/common/log" "github.com/uber/cadence/common/persistence" "github.com/uber/cadence/common/types" diff --git a/service/history/execution/mutable_state_builder_methods_signal_test.go b/service/history/execution/mutable_state_builder_methods_signal_test.go index f7b8acbb774..ba4c7100f67 100644 --- a/service/history/execution/mutable_state_builder_methods_signal_test.go +++ b/service/history/execution/mutable_state_builder_methods_signal_test.go @@ -1,11 +1,35 @@ +// The MIT License (MIT) + +// Copyright (c) 2017-2020 Uber Technologies Inc. + +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + package execution import ( + "testing" + "github.com/stretchr/testify/assert" + "github.com/uber/cadence/common/persistence" "github.com/uber/cadence/common/types" "github.com/uber/cadence/service/history/constants" - "testing" ) func Test__IsSignalRequested(t *testing.T) {