github.com/matrixorigin/matrixone@v1.2.0/pkg/util/metric/testutil_test.go (about) 1 // Copyright 2022 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 // this file contains test utils. Name this file "*_test.go" to make 16 // compiler ignore it 17 18 package metric 19 20 import ( 21 "context" 22 "sync" 23 "time" 24 25 "github.com/matrixorigin/matrixone/pkg/common/moerr" 26 ) 27 28 // some tests will modify global variables, and something weird 29 // may happen if they run concurrently. 30 // use this mutex to make those tests execute sequentially 31 var configMu *sync.Mutex = new(sync.Mutex) 32 33 func withModifiedConfig(f func()) { 34 configMu.Lock() 35 defer configMu.Unlock() 36 f() 37 } 38 39 // waitWgTimeout returns an error if the WaitGroup doesn't return in timeout duration 40 func waitWgTimeout(wg *sync.WaitGroup, after time.Duration) error { 41 c := make(chan struct{}) 42 go func() { 43 defer close(c) 44 wg.Wait() 45 }() 46 select { 47 case <-time.After(time.Second): 48 return moerr.NewInternalError(context.Background(), "timeout") 49 case <-c: 50 return nil 51 } 52 } 53 54 type dummySwitch struct{} 55 56 func (dummySwitch) Start(ctx context.Context) bool { return true } 57 func (dummySwitch) Stop(graceful bool) (<-chan struct{}, bool) { return nil, false }