github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/testutils/helpers.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 testutils
    16  
    17  import (
    18  	"context"
    19  	"os"
    20  	"path/filepath"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  func WaitExpect(timeout int, expect func() bool) {
    29  	end := time.Now().Add(time.Duration(timeout) * time.Millisecond)
    30  	interval := time.Duration(timeout) * time.Millisecond / 400
    31  	for time.Now().Before(end) && !expect() {
    32  		time.Sleep(interval)
    33  	}
    34  }
    35  
    36  func WaitChTimeout[T any](
    37  	ctx context.Context,
    38  	after time.Duration,
    39  	ch <-chan T,
    40  	onRecvCheck func(element T, closed bool) (moveOn bool, err error),
    41  ) error {
    42  	timeout := time.After(after)
    43  	for {
    44  		select {
    45  		case <-timeout:
    46  			return moerr.NewInternalError(ctx, "timeout")
    47  		case item, ok := <-ch:
    48  			moveOn, err := onRecvCheck(item, !ok)
    49  			if err != nil {
    50  				return err
    51  			}
    52  			if !ok || !moveOn {
    53  				return nil
    54  			}
    55  		}
    56  	}
    57  }
    58  
    59  func GetDefaultTestPath(module string, t *testing.T) string {
    60  	return filepath.Join("/tmp", module, t.Name())
    61  }
    62  
    63  func MakeDefaultTestPath(module string, t *testing.T) string {
    64  	path := GetDefaultTestPath(module, t)
    65  	err := os.MkdirAll(path, os.FileMode(0755))
    66  	assert.Nil(t, err)
    67  	return path
    68  }
    69  
    70  func RemoveDefaultTestPath(module string, t *testing.T) {
    71  	path := GetDefaultTestPath(module, t)
    72  	os.RemoveAll(path)
    73  }
    74  
    75  func InitTestEnv(module string, t *testing.T) string {
    76  	RemoveDefaultTestPath(module, t)
    77  	return MakeDefaultTestPath(module, t)
    78  }
    79  
    80  func EnsureNoLeak(t *testing.T) {
    81  	// assert.Zerof(t, common.DefaultAllocator.CurrNB(), common.DefaultAllocator.Stats().Report(""))
    82  	// XXX MPOOL: Too noisy
    83  	// if common.DefaultAllocator.CurrNB() != 0 {
    84  	// 	t.Log(common.DefaultAllocator.Stats().Report(""))
    85  	// }
    86  }
    87  
    88  func AfterTest(t *testing.T) func() {
    89  	return func() {}
    90  	// return leaktest.AfterTest(t)
    91  }