github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/logtail/service/stacker_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 service 16 17 import ( 18 "testing" 19 20 "github.com/stretchr/testify/require" 21 ) 22 23 func TestTableStacker(t *testing.T) { 24 s := NewTableStacker() 25 require.Equal(t, 0, len(s.ListTable())) 26 27 table := mockTable(1, 2, 3) 28 id := TableID(table.String()) 29 30 /* ---- 1. register table ---- */ 31 s.Register(id, table) 32 // only one table 33 require.Equal(t, 1, len(s.ListTable())) 34 35 /* ---- 2. register table repeatedly ---- */ 36 s.Register(id, table) 37 // there's only one table subcribed 38 require.Equal(t, 1, len(s.ListTable())) 39 40 /* ---- 3. unregister table ---- */ 41 s.Unregister(id) 42 // there should be still one table subscribed 43 require.Equal(t, 1, len(s.ListTable())) 44 45 /* ---- 4. unregister table again ---- */ 46 s.Unregister(id) 47 // there should be no table subscribed 48 require.Equal(t, 0, len(s.ListTable())) 49 } 50 51 func TestTableInfo(t *testing.T) { 52 table := mockTable(1, 2, 3) 53 id := TableID(table.String()) 54 55 info := newTableInfo(id, table) 56 57 cases := []struct { 58 op func() int32 59 expect int32 60 }{ 61 { 62 op: info.Ref, 63 expect: 1, 64 }, 65 { 66 op: info.Ref, 67 expect: 2, 68 }, 69 { 70 op: info.Ref, 71 expect: 3, 72 }, 73 { 74 op: info.Deref, 75 expect: 2, 76 }, 77 { 78 op: info.Deref, 79 expect: 1, 80 }, 81 { 82 op: info.Deref, 83 expect: 0, 84 }, 85 } 86 87 for _, c := range cases { 88 require.Equal(t, c.op(), c.expect) 89 } 90 }