Skip to content

Commit 1752c10

Browse files
committed
test(diff): derive cancellation checkpoints
Signed-off-by: Tjark Gunnar Rasche <trasche@nvidia.com>
1 parent 3281878 commit 1752c10

3 files changed

Lines changed: 52 additions & 14 deletions

File tree

pkg/client/v1/diff.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,6 @@ func copySnapshotDiffString(value *string) *string {
213213
if value == nil {
214214
return nil
215215
}
216-
copy := *value
217-
return &copy
216+
copied := *value
217+
return &copied
218218
}

pkg/client/v1/diff_test.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,19 @@ func TestDiffSnapshots_MidTraversalContextCancellation(t *testing.T) {
255255
}
256256
baseline := diffTestSnapshot(baselineData, nil, nil)
257257
target := diffTestSnapshot(targetData, nil, nil)
258+
probeCtx := &snapshotDiffCountingContext{Context: t.Context()}
259+
probeResult, err := client.DiffSnapshots(probeCtx, baseline, target, aicr.SnapshotDiffOptions{})
260+
if err != nil {
261+
t.Fatalf("DiffSnapshots() probe error = %v", err)
262+
}
263+
// Exclude the facade's one mapping checkpoint per change, then cancel
264+
// halfway through the comparison's final summary traversal. This proves
265+
// the internal partial result is discarded rather than failing in mapping.
266+
comparisonChecks := probeCtx.checks - len(probeResult.Changes)
267+
cancelAt := comparisonChecks - probeResult.Summary.Total/2
268+
if cancelAt <= 0 || cancelAt >= comparisonChecks {
269+
t.Fatalf("derived cancellation checkpoint = %d, comparison checks = %d", cancelAt, comparisonChecks)
270+
}
258271

259272
tests := []struct {
260273
name string
@@ -267,10 +280,6 @@ func TestDiffSnapshots_MidTraversalContextCancellation(t *testing.T) {
267280

268281
for _, tt := range tests {
269282
t.Run(tt.name, func(t *testing.T) {
270-
// The first 212 checkpoints index and sort this fixture. Canceling
271-
// later proves that an in-progress result with accumulated changes
272-
// is discarded rather than escaping through the facade.
273-
const cancelAt = 230
274283
ctx := newSnapshotDiffCheckpointContext(t.Context(), cancelAt, tt.cause)
275284
result, err := client.DiffSnapshots(ctx, baseline, target, aicr.SnapshotDiffOptions{})
276285
if result != nil {
@@ -394,6 +403,16 @@ type snapshotDiffCheckpointContext struct {
394403
closed bool
395404
}
396405

406+
type snapshotDiffCountingContext struct {
407+
context.Context
408+
checks int
409+
}
410+
411+
func (c *snapshotDiffCountingContext) Err() error {
412+
c.checks++
413+
return c.Context.Err()
414+
}
415+
397416
func newSnapshotDiffCheckpointContext(
398417
parent context.Context,
399418
cancelAt int,

pkg/diff/diff_test.go

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -700,22 +700,31 @@ func TestSnapshotsWithContext_MidTraversalCancellation(t *testing.T) {
700700
}
701701
baseline := makeSnapshot(makeMeasurement(measurement.TypeK8s, makeSubtype("server", baselineData)))
702702
target := makeSnapshot(makeMeasurement(measurement.TypeK8s, makeSubtype("server", targetData)))
703+
probeCtx := &countingContext{Context: t.Context()}
704+
probeResult, err := SnapshotsWithContext(probeCtx, baseline, target)
705+
if err != nil {
706+
t.Fatalf("SnapshotsWithContext() probe error = %v", err)
707+
}
708+
// The final summary traversal checks the context once per accumulated
709+
// change. Cancel halfway through it without depending on the number of
710+
// checkpoints used by earlier comparison stages.
711+
cancelAt := probeCtx.checks - probeResult.Summary.Total/2
712+
if cancelAt <= 0 || cancelAt >= probeCtx.checks {
713+
t.Fatalf("derived cancellation checkpoint = %d, probe checks = %d", cancelAt, probeCtx.checks)
714+
}
703715

704716
tests := []struct {
705717
name string
706718
cause error
707719
wantCode aicrerrors.ErrorCode
708-
cancelAt int
709720
}{
710-
// The first 212 checkpoints index and sort this fixture. Canceling
711-
// later proves that accumulated changes are discarded with the result.
712-
{name: "canceled", cause: context.Canceled, wantCode: aicrerrors.ErrCodeCanceled, cancelAt: 230},
713-
{name: "deadline", cause: context.DeadlineExceeded, wantCode: aicrerrors.ErrCodeTimeout, cancelAt: 230},
721+
{name: "canceled", cause: context.Canceled, wantCode: aicrerrors.ErrCodeCanceled},
722+
{name: "deadline", cause: context.DeadlineExceeded, wantCode: aicrerrors.ErrCodeTimeout},
714723
}
715724

716725
for _, tt := range tests {
717726
t.Run(tt.name, func(t *testing.T) {
718-
ctx := newCheckpointContext(t.Context(), tt.cancelAt, tt.cause)
727+
ctx := newCheckpointContext(t.Context(), cancelAt, tt.cause)
719728
result, err := SnapshotsWithContext(ctx, baseline, target)
720729
if result != nil {
721730
t.Fatalf("SnapshotsWithContext() result = %#v, want nil after cancellation", result)
@@ -726,8 +735,8 @@ func TestSnapshotsWithContext_MidTraversalCancellation(t *testing.T) {
726735
if !stderrors.Is(err, tt.cause) {
727736
t.Errorf("SnapshotsWithContext() error = %v, want cause %v", err, tt.cause)
728737
}
729-
if ctx.checks < tt.cancelAt {
730-
t.Errorf("context checks = %d, want at least %d to prove traversal began", ctx.checks, tt.cancelAt)
738+
if ctx.checks < cancelAt {
739+
t.Errorf("context checks = %d, want at least %d to prove traversal began", ctx.checks, cancelAt)
731740
}
732741
})
733742
}
@@ -770,6 +779,16 @@ type checkpointContext struct {
770779
closed bool
771780
}
772781

782+
type countingContext struct {
783+
context.Context
784+
checks int
785+
}
786+
787+
func (c *countingContext) Err() error {
788+
c.checks++
789+
return c.Context.Err()
790+
}
791+
773792
func newCheckpointContext(parent context.Context, cancelAt int, cause error) *checkpointContext {
774793
return &checkpointContext{
775794
Context: parent,

0 commit comments

Comments
 (0)