github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/colexec/merge/merge_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 merge
    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/testutil"
    26  	"github.com/matrixorigin/matrixone/pkg/vm"
    27  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  const (
    32  	Rows = 10 // default rows
    33  )
    34  
    35  // add unit tests for cases
    36  type mergeTestCase struct {
    37  	arg    *Argument
    38  	types  []types.Type
    39  	proc   *process.Process
    40  	cancel context.CancelFunc
    41  }
    42  
    43  var (
    44  	tcs []mergeTestCase
    45  )
    46  
    47  func init() {
    48  	tcs = []mergeTestCase{
    49  		newTestCase(),
    50  	}
    51  }
    52  
    53  func TestString(t *testing.T) {
    54  	buf := new(bytes.Buffer)
    55  	for _, tc := range tcs {
    56  		tc.arg.String(buf)
    57  	}
    58  }
    59  
    60  func TestPrepare(t *testing.T) {
    61  	for _, tc := range tcs {
    62  		err := tc.arg.Prepare(tc.proc)
    63  		require.NoError(t, err)
    64  	}
    65  }
    66  
    67  func TestMerge(t *testing.T) {
    68  	for _, tc := range tcs {
    69  		err := tc.arg.Prepare(tc.proc)
    70  		require.NoError(t, err)
    71  		tc.proc.Reg.MergeReceivers[0].Ch <- newBatch(tc.types, tc.proc, Rows)
    72  		tc.proc.Reg.MergeReceivers[0].Ch <- batch.EmptyBatch
    73  		tc.proc.Reg.MergeReceivers[0].Ch <- nil
    74  		tc.proc.Reg.MergeReceivers[1].Ch <- newBatch(tc.types, tc.proc, Rows)
    75  		tc.proc.Reg.MergeReceivers[1].Ch <- batch.EmptyBatch
    76  		tc.proc.Reg.MergeReceivers[1].Ch <- nil
    77  		for {
    78  			ok, err := tc.arg.Call(tc.proc)
    79  			if ok.Status == vm.ExecStop || err != nil {
    80  				break
    81  			}
    82  		}
    83  		// for i := 0; i < len(tc.proc.Reg.MergeReceivers); i++ { // simulating the end of a pipeline
    84  		// 	for len(tc.proc.Reg.MergeReceivers[i].Ch) > 0 {
    85  		// 		bat := <-tc.proc.Reg.MergeReceivers[i].Ch
    86  		// 		if bat != nil {
    87  		// 			bat.Clean(tc.proc.Mp())
    88  		// 		}
    89  		// 	}
    90  		// }
    91  		tc.arg.Free(tc.proc, false, nil)
    92  		tc.proc.FreeVectors()
    93  		require.Equal(t, int64(0), tc.proc.Mp().CurrNB())
    94  	}
    95  }
    96  
    97  func newTestCase() mergeTestCase {
    98  	proc := testutil.NewProcessWithMPool(mpool.MustNewZero())
    99  	proc.Reg.MergeReceivers = make([]*process.WaitRegister, 2)
   100  	ctx, cancel := context.WithCancel(context.Background())
   101  	proc.Reg.MergeReceivers[0] = &process.WaitRegister{
   102  		Ctx: ctx,
   103  		Ch:  make(chan *batch.Batch, 3),
   104  	}
   105  	proc.Reg.MergeReceivers[1] = &process.WaitRegister{
   106  		Ctx: ctx,
   107  		Ch:  make(chan *batch.Batch, 3),
   108  	}
   109  	cases := mergeTestCase{
   110  		proc: proc,
   111  		types: []types.Type{
   112  			types.T_int8.ToType(),
   113  		},
   114  		arg:    new(Argument),
   115  		cancel: cancel,
   116  	}
   117  	cases.arg.OperatorBase.OperatorInfo =
   118  		vm.OperatorInfo{
   119  			Idx:     0,
   120  			IsFirst: false,
   121  			IsLast:  false,
   122  		}
   123  	return cases
   124  }
   125  
   126  // create a new block based on the type information
   127  func newBatch(ts []types.Type, proc *process.Process, rows int64) *batch.Batch {
   128  	return testutil.NewBatch(ts, false, int(rows), proc.Mp())
   129  }