Skip to content

Commit

Permalink
Changed to use timeSource instead of time.
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobht committed May 30, 2024
1 parent 18ad578 commit 6ee642c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
2 changes: 1 addition & 1 deletion service/history/execution/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -1214,7 +1214,7 @@ func getWorkflowExecutionWithRetry(
// If error is shard closed, only log error if shard has been closed for a while,
// otherwise always log
var shardClosedError *shard.ErrShardClosed
if !errors.As(err, &shardClosedError) || time.Since(shardClosedError.ClosedAt) > shard.TimeBeforeShardClosedIsError {
if !errors.As(err, &shardClosedError) || shardContext.GetTimeSource().Since(shardClosedError.ClosedAt) > shard.TimeBeforeShardClosedIsError {
logger.Error("Persistent fetch operation failure", tag.StoreOperationGetWorkflowExecution, tag.Error(err))
}

Expand Down
22 changes: 12 additions & 10 deletions service/history/execution/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2933,7 +2933,7 @@ func TestGetWorkflowExecutionWithRetry(t *testing.T) {
testCases := []struct {
name string
request *persistence.GetWorkflowExecutionRequest
mockSetup func(*shard.MockContext, *log.MockLogger)
mockSetup func(*shard.MockContext, *log.MockLogger, clock.MockedTimeSource)
want *persistence.GetWorkflowExecutionResponse
wantErr bool
assertErr func(*testing.T, error)
Expand All @@ -2943,7 +2943,7 @@ func TestGetWorkflowExecutionWithRetry(t *testing.T) {
request: &persistence.GetWorkflowExecutionRequest{
RangeID: 100,
},
mockSetup: func(mockShard *shard.MockContext, mockLogger *log.MockLogger) {
mockSetup: func(mockShard *shard.MockContext, mockLogger *log.MockLogger, timeSource clock.MockedTimeSource) {
mockShard.EXPECT().GetWorkflowExecution(gomock.Any(), &persistence.GetWorkflowExecutionRequest{
RangeID: 100,
}).Return(&persistence.GetWorkflowExecutionResponse{
Expand All @@ -2964,7 +2964,7 @@ func TestGetWorkflowExecutionWithRetry(t *testing.T) {
request: &persistence.GetWorkflowExecutionRequest{
RangeID: 100,
},
mockSetup: func(mockShard *shard.MockContext, mockLogger *log.MockLogger) {
mockSetup: func(mockShard *shard.MockContext, mockLogger *log.MockLogger, timeSource clock.MockedTimeSource) {
mockShard.EXPECT().GetWorkflowExecution(gomock.Any(), gomock.Any()).Return(nil, &types.EntityNotExistsError{})
},
wantErr: true,
Expand All @@ -2977,9 +2977,9 @@ func TestGetWorkflowExecutionWithRetry(t *testing.T) {
request: &persistence.GetWorkflowExecutionRequest{
RangeID: 100,
},
mockSetup: func(mockShard *shard.MockContext, mockLogger *log.MockLogger) {
mockSetup: func(mockShard *shard.MockContext, mockLogger *log.MockLogger, timeSource clock.MockedTimeSource) {
mockShard.EXPECT().GetWorkflowExecution(gomock.Any(), gomock.Any()).Return(nil, &shard.ErrShardClosed{
ClosedAt: time.Now().Add(-shard.TimeBeforeShardClosedIsError / 2),
ClosedAt: timeSource.Now().Add(-shard.TimeBeforeShardClosedIsError / 2),
})
// We do _not_ expect a log call
},
Expand All @@ -2993,9 +2993,9 @@ func TestGetWorkflowExecutionWithRetry(t *testing.T) {
request: &persistence.GetWorkflowExecutionRequest{
RangeID: 100,
},
mockSetup: func(mockShard *shard.MockContext, mockLogger *log.MockLogger) {
mockSetup: func(mockShard *shard.MockContext, mockLogger *log.MockLogger, timeSource clock.MockedTimeSource) {
err := &shard.ErrShardClosed{
ClosedAt: time.Now().Add(-shard.TimeBeforeShardClosedIsError * 2),
ClosedAt: timeSource.Now().Add(-shard.TimeBeforeShardClosedIsError * 2),
}
mockShard.EXPECT().GetWorkflowExecution(gomock.Any(), gomock.Any()).Return(nil, err)
expectLog(mockLogger, err)
Expand All @@ -3010,7 +3010,7 @@ func TestGetWorkflowExecutionWithRetry(t *testing.T) {
request: &persistence.GetWorkflowExecutionRequest{
RangeID: 100,
},
mockSetup: func(mockShard *shard.MockContext, mockLogger *log.MockLogger) {
mockSetup: func(mockShard *shard.MockContext, mockLogger *log.MockLogger, timeSource clock.MockedTimeSource) {
mockShard.EXPECT().GetWorkflowExecution(gomock.Any(), gomock.Any()).Return(nil, errors.New("some error"))
expectLog(mockLogger, errors.New("some error"))
},
Expand All @@ -3024,7 +3024,7 @@ func TestGetWorkflowExecutionWithRetry(t *testing.T) {
request: &persistence.GetWorkflowExecutionRequest{
RangeID: 100,
},
mockSetup: func(mockShard *shard.MockContext, mockLogger *log.MockLogger) {
mockSetup: func(mockShard *shard.MockContext, mockLogger *log.MockLogger, timeSource clock.MockedTimeSource) {
mockShard.EXPECT().GetWorkflowExecution(gomock.Any(), gomock.Any()).Return(nil, &types.ServiceBusyError{})
mockShard.EXPECT().GetWorkflowExecution(gomock.Any(), gomock.Any()).Return(&persistence.GetWorkflowExecutionResponse{
MutableStateStats: &persistence.MutableStateStats{
Expand All @@ -3046,10 +3046,12 @@ func TestGetWorkflowExecutionWithRetry(t *testing.T) {
mockCtrl := gomock.NewController(t)
mockShard := shard.NewMockContext(mockCtrl)
mockLogger := new(log.MockLogger)
timeSource := clock.NewMockedTimeSource()
mockShard.EXPECT().GetTimeSource().Return(timeSource).AnyTimes()
policy := backoff.NewExponentialRetryPolicy(time.Millisecond)
policy.SetMaximumAttempts(1)
if tc.mockSetup != nil {
tc.mockSetup(mockShard, mockLogger)
tc.mockSetup(mockShard, mockLogger, timeSource)
}
resp, err := getWorkflowExecutionWithRetry(context.Background(), mockShard, mockLogger, policy, tc.request)
if tc.wantErr {
Expand Down

0 comments on commit 6ee642c

Please sign in to comment.