-
Notifications
You must be signed in to change notification settings - Fork 24
fix: 'run' exiting on disconnected api errors when streaming activity logs #132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2f593d0
0d6e830
77a3d7d
583bea7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -88,11 +88,12 @@ func Activity( | |
| } | ||
|
|
||
| latestCreatedTimestamp, _, err := printLatestActivity(ctx, clients, token, activityRequest, token) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Return an error from the activity API if the logs are not being tailed | ||
| if !args.TailArg { | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return nil | ||
|
Comment on lines
+94
to
97
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔍 Small preference to make the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, I prefer this clarify as well! |
||
| } | ||
|
|
||
|
|
@@ -108,9 +109,11 @@ func Activity( | |
| case <-ticker.C: | ||
| // Try to grab new logs using the last logs timestamp | ||
| activityRequest.MinimumDateCreated = latestCreatedTimestamp + 1 | ||
|
|
||
| // Avoid exit on error from the activity API when tailing logs | ||
| newLatestCreatedTimestamp, count, err := printLatestActivity(ctx, clients, token, activityRequest, token) | ||
| if err != nil { | ||
| return slackerror.New(slackerror.ErrStreamingActivityLogs).WithRootCause(err) | ||
| clients.IO.PrintDebug(ctx, "%s\n", err) | ||
| } | ||
|
|
||
| if count > 0 { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -110,28 +110,80 @@ func TestPlatformActivity_StreamingLogs(t *testing.T) { | |
| }, | ||
| "should return error if session validation fails": { | ||
| Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) context.Context { | ||
| cm.API.On("ValidateSession", mock.Anything, mock.Anything).Return(api.AuthSession{}, slackerror.New("boomsies")) | ||
| cm.API.On("ValidateSession", mock.Anything, mock.Anything).Return(api.AuthSession{}, slackerror.New("mock_broken_validation")) | ||
| return ctx | ||
| }, | ||
| ExpectedError: slackerror.New("boomsies"), | ||
| ExpectedError: slackerror.New("mock_broken_validation"), | ||
| }, | ||
| "should return error if activity request fails": { | ||
| "should return error from Activity API request if TailArg is not set": { | ||
| Args: types.ActivityArgs{ | ||
| TailArg: false, | ||
| }, | ||
| Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) context.Context { | ||
| cm.API.On("Activity", mock.Anything, mock.Anything, mock.Anything).Return(api.ActivityResult{}, slackerror.New("explosions")) | ||
| cm.API.On("Activity", mock.Anything, mock.Anything, mock.Anything).Return(api.ActivityResult{}, slackerror.New("mock_broken_logs")) | ||
| return ctx | ||
| }, | ||
| ExpectedError: slackerror.New("explosions"), | ||
| ExpectedError: slackerror.New("mock_broken_logs"), | ||
| ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) { | ||
| cm.API.AssertNumberOfCalls(t, "Activity", 1) | ||
| }, | ||
| }, | ||
| "should return nil and invoke Activity API only once if TailArg is not set": { | ||
| Args: types.ActivityArgs{ | ||
| TailArg: false, | ||
| TailArg: false, | ||
| IdleTimeoutM: 1, | ||
| PollingIntervalMS: 20, // poll activity every 20 ms | ||
| }, | ||
| Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) context.Context { | ||
| cm.API.On("Activity", mock.Anything, mock.Anything, mock.Anything).Return(api.ActivityResult{}, nil) | ||
| cm.API.On("Activity", mock.Anything, mock.Anything, mock.Anything). | ||
| Return(api.ActivityResult{ | ||
| Activities: []api.Activity{ | ||
| { | ||
| Level: types.WARN, | ||
| ComponentID: "a123", | ||
| TraceID: "tr123", | ||
| }, | ||
| { | ||
| Level: types.ERROR, | ||
| ComponentID: "a456", | ||
| TraceID: "tr456", | ||
| }, | ||
| }, | ||
| }, nil) | ||
| ctx, cancel := context.WithCancel(ctx) | ||
| go func() { | ||
| time.Sleep(time.Millisecond * 50) // cancel activity in 50 ms | ||
| cancel() | ||
| }() | ||
| return ctx | ||
| }, | ||
| ExpectedError: nil, | ||
| ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) { | ||
| // with the above tail argument not included we call the method once. | ||
| cm.API.AssertNumberOfCalls(t, "Activity", 1) | ||
| assert.Contains(t, cm.GetStdoutOutput(), "[warn] [a123] (Trace=tr123)") | ||
| assert.Contains(t, cm.GetStdoutOutput(), "[error] [a456] (Trace=tr456)") | ||
| }, | ||
| }, | ||
| "should return nil and invoke Activity API twice if TailArg is set while polling": { | ||
| Args: types.ActivityArgs{ | ||
| TailArg: true, | ||
| IdleTimeoutM: 0, | ||
| PollingIntervalMS: 20, // poll activity every 20 ms | ||
| }, | ||
| Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) context.Context { | ||
| cm.API.On("Activity", mock.Anything, mock.Anything, mock.Anything).Return(api.ActivityResult{}, nil) | ||
| ctx, cancel := context.WithCancel(ctx) | ||
| go func() { | ||
| time.Sleep(time.Millisecond * 50) // cancel activity in 50 ms | ||
| cancel() | ||
| }() | ||
| return ctx | ||
| }, | ||
| ExpectedError: nil, | ||
| ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) { | ||
| // with the above polling/canceling setup, expectation is activity called two times. | ||
| cm.API.AssertNumberOfCalls(t, "Activity", 2) | ||
| }, | ||
| }, | ||
| "should return nil if TailArg is set and context is canceled": { | ||
|
|
@@ -148,11 +200,33 @@ func TestPlatformActivity_StreamingLogs(t *testing.T) { | |
| }() | ||
| return ctx | ||
| }, | ||
| ExpectedError: nil, | ||
| ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) { | ||
| // with the above polling/canceling setup, expectation is activity called only once. | ||
| cm.API.AssertNumberOfCalls(t, "Activity", 1) | ||
| }, | ||
| }, | ||
| "should return nil if TailArg is set and activity request fails while polling": { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧪 The added For this test I think it's useful, but I'm of course open to reverting this!
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to revert, I like this as well! |
||
| Args: types.ActivityArgs{ | ||
| TailArg: true, | ||
| IdleTimeoutM: 1, | ||
| PollingIntervalMS: 20, // poll activity every 20 ms | ||
| }, | ||
| Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) context.Context { | ||
| cm.API.On("Activity", mock.Anything, mock.Anything, mock.Anything).Return(api.ActivityResult{}, slackerror.New("mock_broken_logs")) | ||
| ctx, cancel := context.WithCancel(ctx) | ||
| go func() { | ||
| time.Sleep(time.Millisecond * 50) // cancel activity in 50 ms | ||
| cancel() | ||
| }() | ||
| return ctx | ||
| }, | ||
| ExpectedError: nil, | ||
| ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) { | ||
| // with the above polling/canceling setup, expectation is activity called three times. | ||
| cm.API.AssertNumberOfCalls(t, "Activity", 3) | ||
| }, | ||
| }, | ||
| } { | ||
| t.Run(name, func(t *testing.T) { | ||
| // Create mocks | ||
|
|
@@ -172,6 +246,7 @@ func TestPlatformActivity_StreamingLogs(t *testing.T) { | |
|
|
||
| err := Activity(ctxMock, clients, &logger.Logger{}, tt.Args) | ||
| if tt.ExpectedError != nil { | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), tt.ExpectedError.Error(), err) | ||
| } else { | ||
| require.NoError(t, err) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📣 We create a new error here to avoid appending different repeated root causes to this same error: