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