github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/colexec/dispatch/dispatch_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 dispatch 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 dispatchTestCase struct { 38 arg *Argument 39 types []types.Type 40 proc *process.Process 41 cancel context.CancelFunc 42 } 43 44 var ( 45 tcs []dispatchTestCase 46 ) 47 48 func init() { 49 tcs = []dispatchTestCase{ 50 newTestCase(), 51 newTestCase(), 52 } 53 } 54 55 func TestString(t *testing.T) { 56 buf := new(bytes.Buffer) 57 for _, tc := range tcs { 58 tc.arg.String(buf) 59 } 60 } 61 62 func TestPrepare(t *testing.T) { 63 for _, tc := range tcs { 64 err := tc.arg.Prepare(tc.proc) 65 require.NoError(t, err) 66 } 67 } 68 69 func TestDispatch(t *testing.T) { 70 for _, tc := range tcs { 71 err := tc.arg.Prepare(tc.proc) 72 require.NoError(t, err) 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 tc.arg.Free(tc.proc, false, nil) 87 tc.arg.Children[0].Free(tc.proc, false, nil) 88 for _, re := range tc.arg.LocalRegs { 89 for len(re.Ch) > 0 { 90 bat := <-re.Ch 91 if bat == nil { 92 break 93 } 94 bat.Clean(tc.proc.Mp()) 95 } 96 } 97 tc.proc.FreeVectors() 98 require.Equal(t, int64(0), tc.proc.Mp().CurrNB()) 99 } 100 } 101 102 func newTestCase() dispatchTestCase { 103 proc := testutil.NewProcessWithMPool(mpool.MustNewZero()) 104 proc.Reg.MergeReceivers = make([]*process.WaitRegister, 2) 105 ctx, cancel := context.WithCancel(context.Background()) 106 reg := &process.WaitRegister{Ctx: ctx, Ch: make(chan *batch.Batch, 3)} 107 return dispatchTestCase{ 108 proc: proc, 109 types: []types.Type{types.T_int8.ToType()}, 110 arg: &Argument{ 111 FuncId: SendToAllLocalFunc, 112 LocalRegs: []*process.WaitRegister{reg}, 113 OperatorBase: vm.OperatorBase{ 114 OperatorInfo: vm.OperatorInfo{ 115 Idx: 0, 116 IsFirst: false, 117 IsLast: false, 118 }, 119 }, 120 }, 121 cancel: cancel, 122 } 123 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 } 130 131 func resetChildren(arg *Argument, bats []*batch.Batch) { 132 if len(arg.Children) == 0 { 133 arg.AppendChild(&value_scan.Argument{ 134 Batchs: bats, 135 }) 136 137 } else { 138 arg.Children = arg.Children[:0] 139 arg.AppendChild(&value_scan.Argument{ 140 Batchs: bats, 141 }) 142 } 143 }