github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/colexec/cancel_checker_test.go (about)

     1  // Copyright 2019 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package colexec
    12  
    13  import (
    14  	"context"
    15  	"testing"
    16  
    17  	"github.com/cockroachdb/cockroach/pkg/sql/colexecbase"
    18  	"github.com/cockroachdb/cockroach/pkg/sql/colexecbase/colexecerror"
    19  	"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
    20  	"github.com/cockroachdb/cockroach/pkg/sql/types"
    21  	"github.com/cockroachdb/cockroach/pkg/util/leaktest"
    22  	"github.com/cockroachdb/errors"
    23  	"github.com/stretchr/testify/require"
    24  )
    25  
    26  // TestCancelChecker verifies that CancelChecker panics with appropriate error
    27  // when the context is canceled.
    28  func TestCancelChecker(t *testing.T) {
    29  	defer leaktest.AfterTest(t)()
    30  	ctx, cancel := context.WithCancel(context.Background())
    31  	typs := []*types.T{types.Int}
    32  	batch := testAllocator.NewMemBatch(typs)
    33  	op := NewCancelChecker(NewNoop(colexecbase.NewRepeatableBatchSource(testAllocator, batch, typs)))
    34  	cancel()
    35  	err := colexecerror.CatchVectorizedRuntimeError(func() {
    36  		op.Next(ctx)
    37  	})
    38  	require.True(t, errors.Is(err, sqlbase.QueryCanceledError))
    39  }