@@ -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+
773792func newCheckpointContext (parent context.Context , cancelAt int , cause error ) * checkpointContext {
774793 return & checkpointContext {
775794 Context : parent ,
0 commit comments