github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/colexec/connector/connector_test.go (about)

     1  // Copyright 2021 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package connector
    16  
    17  import (
    18  	"bytes"
    19  	"context"
    20  	"testing"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    23  	"github.com/matrixorigin/matrixone/pkg/container/batch"
    24  	"github.com/matrixorigin/matrixone/pkg/container/types"
    25  	"github.com/matrixorigin/matrixone/pkg/sql/colexec/value_scan"
    26  	"github.com/matrixorigin/matrixone/pkg/testutil"
    27  	"github.com/matrixorigin/matrixone/pkg/vm"
    28  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    29  	"github.com/stretchr/testify/require"
    30  )
    31  
    32  const (
    33  	Rows = 10 // default rows
    34  )
    35  
    36  // add unit tests for cases
    37  type connectorTestCase struct {
    38  	arg    *Argument
    39  	types  []types.Type
    40  	proc   *process.Process
    41  	cancel context.CancelFunc
    42  }
    43  
    44  var (
    45  	tcs []connectorTestCase
    46  )
    47  
    48  func init() {
    49  	tcs = []connectorTestCase{
    50  		newTestCase(),
    51  	}
    52  }
    53  
    54  func TestString(t *testing.T) {
    55  	buf := new(bytes.Buffer)
    56  	for _, tc := range tcs {
    57  		tc.arg.String(buf)
    58  	}
    59  }
    60  
    61  func TestPrepare(t *testing.T) {
    62  	for _, tc := range tcs {
    63  		err := tc.arg.Prepare(tc.proc)
    64  		require.NoError(t, err)
    65  	}
    66  }
    67  
    68  func TestConnector(t *testing.T) {
    69  	for _, tc := range tcs {
    70  		err := tc.arg.Prepare(tc.proc)
    71  		require.NoError(t, err)
    72  
    73  		bats := []*batch.Batch{
    74  			newBatch(tc.types, tc.proc, Rows),
    75  			batch.EmptyBatch,
    76  		}
    77  		resetChildren(tc.arg, bats)
    78  		/*{
    79  			for _, vec := range bat.Vecs {
    80  				if vec.IsOriginal() {
    81  					vec.FreeOriginal(tc.proc.Mp())
    82  				}
    83  			}
    84  		}*/
    85  		_, _ = tc.arg.Call(tc.proc)
    86  		for len(tc.arg.Reg.Ch) > 0 {
    87  			bat := <-tc.arg.Reg.Ch
    88  			if bat == nil {
    89  				break
    90  			}
    91  			if bat.IsEmpty() {
    92  				continue
    93  			}
    94  			bat.Clean(tc.proc.Mp())
    95  		}
    96  		tc.arg.Free(tc.proc, false, nil)
    97  		tc.arg.GetChildren(0).Free(tc.proc, false, nil)
    98  		tc.proc.FreeVectors()
    99  		require.Equal(t, int64(0), tc.proc.Mp().CurrNB())
   100  	}
   101  }
   102  
   103  func newTestCase() connectorTestCase {
   104  	proc := testutil.NewProcessWithMPool(mpool.MustNewZero())
   105  	proc.Reg.MergeReceivers = make([]*process.WaitRegister, 2)
   106  	ctx, cancel := context.WithCancel(context.Background())
   107  	return connectorTestCase{
   108  		proc:  proc,
   109  		types: []types.Type{types.T_int8.ToType()},
   110  		arg: &Argument{
   111  			Reg: &process.WaitRegister{
   112  				Ctx: ctx,
   113  				Ch:  make(chan *batch.Batch, 3),
   114  			},
   115  			OperatorBase: vm.OperatorBase{
   116  				OperatorInfo: vm.OperatorInfo{
   117  					Idx:     0,
   118  					IsFirst: false,
   119  					IsLast:  false,
   120  				},
   121  			},
   122  		},
   123  		cancel: cancel,
   124  	}
   125  
   126  }
   127  
   128  // create a new block based on the type information
   129  func newBatch(ts []types.Type, proc *process.Process, rows int64) *batch.Batch {
   130  	return testutil.NewBatch(ts, false, int(rows), proc.Mp())
   131  }
   132  
   133  func resetChildren(arg *Argument, bats []*batch.Batch) {
   134  	arg.GetOperatorBase().SetChildren(
   135  		[]vm.Operator{
   136  			&value_scan.Argument{
   137  				Batchs: bats,
   138  			},
   139  		},
   140  	)
   141  }