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