github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/txn/txnimpl/command_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 txnimpl
    16  
    17  import (
    18  	"bytes"
    19  	"testing"
    20  
    21  	"github.com/RoaringBitmap/roaring"
    22  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/catalog"
    23  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/testutils"
    24  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/txn/txnbase"
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  func TestPointerCmd(t *testing.T) {
    29  	defer testutils.AfterTest(t)()
    30  	testutils.EnsureNoLeak(t)
    31  	groups := uint32(10)
    32  	maxLsn := uint64(10)
    33  	for group := uint32(1); group <= groups; group++ {
    34  		for lsn := uint64(1); lsn <= maxLsn; lsn++ {
    35  			cmd := new(txnbase.PointerCmd)
    36  			cmd.Group = group
    37  			cmd.Lsn = lsn
    38  			mashalled, err := cmd.Marshal()
    39  			assert.Nil(t, err)
    40  			r := bytes.NewBuffer(mashalled)
    41  			cmd2, _, err := txnbase.BuildCommandFrom(r)
    42  			assert.Nil(t, err)
    43  			assert.Equal(t, cmd.Group, cmd2.(*txnbase.PointerCmd).Group)
    44  			assert.Equal(t, cmd.Lsn, cmd2.(*txnbase.PointerCmd).Lsn)
    45  		}
    46  	}
    47  }
    48  
    49  func TestComposedCmd(t *testing.T) {
    50  	defer testutils.AfterTest(t)()
    51  	testutils.EnsureNoLeak(t)
    52  	composed := txnbase.NewComposedCmd()
    53  	defer composed.Close()
    54  	groups := uint32(10)
    55  	maxLsn := uint64(10)
    56  	for group := uint32(1); group <= groups; group++ {
    57  		for lsn := uint64(1); lsn <= maxLsn; lsn++ {
    58  			cmd := new(txnbase.PointerCmd)
    59  			cmd.Group = group
    60  			cmd.Lsn = lsn
    61  			composed.AddCmd(cmd)
    62  		}
    63  	}
    64  	batCnt := 5
    65  
    66  	schema := catalog.MockSchema(4, 0)
    67  	for i := 0; i < batCnt; i++ {
    68  		bat := catalog.MockBatch(schema, (i+1)*5)
    69  		defer bat.Close()
    70  		batCmd := txnbase.NewBatchCmd(bat)
    71  		del := roaring.NewBitmap()
    72  		del.Add(uint32(i))
    73  		delCmd := txnbase.NewDeleteBitmapCmd(del)
    74  		comp := txnbase.NewComposedCmd()
    75  		comp.AddCmd(batCmd)
    76  		comp.AddCmd(delCmd)
    77  		composed.AddCmd(comp)
    78  	}
    79  	var w bytes.Buffer
    80  	_, err := composed.WriteTo(&w)
    81  	assert.Nil(t, err)
    82  
    83  	buf := w.Bytes()
    84  
    85  	r := bytes.NewBuffer(buf)
    86  	composed2, _, err := txnbase.BuildCommandFrom(r)
    87  	assert.Nil(t, err)
    88  	defer composed2.Close()
    89  	cmd1 := composed.Cmds
    90  	cmd2 := composed2.(*txnbase.ComposedCmd).Cmds
    91  
    92  	assert.Equal(t, len(cmd1), len(cmd2))
    93  	for i, c1 := range cmd1 {
    94  		c2 := cmd2[i]
    95  		assert.Equal(t, c1.GetType(), c2.GetType())
    96  		switch c1.GetType() {
    97  		case txnbase.CmdPointer:
    98  			assert.Equal(t, c1.(*txnbase.PointerCmd).Group, c2.(*txnbase.PointerCmd).Group)
    99  			assert.Equal(t, c1.(*txnbase.PointerCmd).Group, c2.(*txnbase.PointerCmd).Group)
   100  		case txnbase.CmdComposed:
   101  			comp1 := c1.(*txnbase.ComposedCmd)
   102  			comp2 := c2.(*txnbase.ComposedCmd)
   103  			for j, cc1 := range comp1.Cmds {
   104  				cc2 := comp2.Cmds[j]
   105  				assert.Equal(t, cc1.GetType(), cc2.GetType())
   106  				switch cc1.GetType() {
   107  				case txnbase.CmdPointer:
   108  					assert.Equal(t, cc1.(*txnbase.PointerCmd).Group, cc2.(*txnbase.PointerCmd).Group)
   109  					assert.Equal(t, cc1.(*txnbase.PointerCmd).Group, cc2.(*txnbase.PointerCmd).Group)
   110  				case txnbase.CmdDeleteBitmap:
   111  					assert.True(t, cc1.(*txnbase.DeleteBitmapCmd).Bitmap.Equals(cc1.(*txnbase.DeleteBitmapCmd).Bitmap))
   112  				case txnbase.CmdBatch:
   113  					b1 := cc1.(*txnbase.BatchCmd)
   114  					b2 := cc2.(*txnbase.BatchCmd)
   115  					assert.True(t, b1.Bat.Equals(b2.Bat))
   116  				}
   117  			}
   118  		}
   119  	}
   120  }