mirror of
https://github.com/YspCoder/clawgo.git
synced 2026-05-02 07:07:29 +08:00
Stop planned task spam after cancellation
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
package agent
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPlannedProgressMilestones(t *testing.T) {
|
||||
t.Parallel()
|
||||
@@ -16,20 +20,44 @@ func TestShouldPublishPlannedTaskProgress(t *testing.T) {
|
||||
|
||||
milestones := plannedProgressMilestones(12)
|
||||
notified := map[int]struct{}{}
|
||||
if shouldPublishPlannedTaskProgress(12, 1, plannedTaskResult{}, milestones, notified) {
|
||||
if shouldPublishPlannedTaskProgress(context.Background(), 12, 1, plannedTaskResult{}, milestones, notified) {
|
||||
t.Fatalf("did not expect early success notification")
|
||||
}
|
||||
if !shouldPublishPlannedTaskProgress(12, 4, plannedTaskResult{}, milestones, notified) {
|
||||
if !shouldPublishPlannedTaskProgress(context.Background(), 12, 4, plannedTaskResult{}, milestones, notified) {
|
||||
t.Fatalf("expected milestone notification")
|
||||
}
|
||||
notified[4] = struct{}{}
|
||||
if shouldPublishPlannedTaskProgress(12, 4, plannedTaskResult{}, milestones, notified) {
|
||||
if shouldPublishPlannedTaskProgress(context.Background(), 12, 4, plannedTaskResult{}, milestones, notified) {
|
||||
t.Fatalf("did not expect duplicate milestone notification")
|
||||
}
|
||||
if !shouldPublishPlannedTaskProgress(12, 5, plannedTaskResult{ErrText: "boom"}, milestones, notified) {
|
||||
if !shouldPublishPlannedTaskProgress(context.Background(), 12, 5, plannedTaskResult{ErrText: "boom"}, milestones, notified) {
|
||||
t.Fatalf("expected failure notification")
|
||||
}
|
||||
if shouldPublishPlannedTaskProgress(3, 3, plannedTaskResult{}, plannedProgressMilestones(3), map[int]struct{}{}) {
|
||||
if shouldPublishPlannedTaskProgress(context.Background(), 3, 3, plannedTaskResult{}, plannedProgressMilestones(3), map[int]struct{}{}) {
|
||||
t.Fatalf("did not expect final success notification")
|
||||
}
|
||||
if shouldPublishPlannedTaskProgress(context.Background(), 12, 5, plannedTaskResult{Err: context.Canceled, ErrText: context.Canceled.Error()}, milestones, notified) {
|
||||
t.Fatalf("did not expect cancellation notification")
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
if shouldPublishPlannedTaskProgress(ctx, 12, 5, plannedTaskResult{Err: errors.New("worker exited after parent stop"), ErrText: "worker exited after parent stop"}, milestones, notified) {
|
||||
t.Fatalf("did not expect notification after parent cancellation")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsPlannedTaskCancellation(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
if !isPlannedTaskCancellation(context.Background(), plannedTaskResult{Err: context.Canceled, ErrText: context.Canceled.Error()}) {
|
||||
t.Fatalf("expected direct context cancellation to be detected")
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
if !isPlannedTaskCancellation(ctx, plannedTaskResult{Err: errors.New("worker exited after parent stop"), ErrText: "worker exited after parent stop"}) {
|
||||
t.Fatalf("expected canceled parent context to suppress planned task result")
|
||||
}
|
||||
if isPlannedTaskCancellation(context.Background(), plannedTaskResult{Err: errors.New("boom"), ErrText: "boom"}) {
|
||||
t.Fatalf("did not expect non-cancellation error to be suppressed")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user