github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/logstore/sm/sm_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 sm 16 17 import ( 18 "sync" 19 "testing" 20 "time" 21 22 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/testutils" 23 "github.com/stretchr/testify/assert" 24 ) 25 26 func TestLoop1(t *testing.T) { 27 defer testutils.AfterTest(t)() 28 q1 := make(chan any, 100) 29 fn := func(batch []any, q chan any) { 30 for _, item := range batch { 31 t.Logf("loop1 %d", item.(int)) 32 } 33 } 34 loop := NewLoop(q1, nil, fn, 100) 35 loop.Start() 36 for i := 0; i < 10; i++ { 37 q1 <- i 38 } 39 loop.Stop() 40 } 41 42 func TestNewNonBlockingQueue(t *testing.T) { 43 wait := sync.WaitGroup{} 44 wait.Add(1) 45 defer func() { 46 testutils.AfterTest(t) 47 }() 48 49 queueSize := 10 50 batchSize := 0 51 queue := NewNonBlockingQueue(queueSize, batchSize, func(items ...any) { 52 // blocking handler 53 wait.Wait() 54 }) 55 56 queue.Start() 57 58 for i := 0; i < queueSize+1; i++ { 59 item, err := queue.Enqueue(i) 60 assert.NotNil(t, item) 61 assert.Nil(t, err) 62 time.Sleep(time.Millisecond * 10) 63 } 64 65 item, err := queue.Enqueue(11) 66 assert.NotNil(t, item) 67 assert.Equal(t, err, ErrFull) 68 69 wait.Done() 70 time.Sleep(time.Millisecond * 100) 71 72 }