github.com/matrixorigin/matrixone@v0.7.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 "sync/atomic" 24 "time" 25 26 "github.com/matrixorigin/matrixone/pkg/common/moerr" 27 ) 28 29 // some tests will modify global variables, and something weird 30 // may happen if they run concurrently. 31 // use this mutex to make those tests execute sequentially 32 var configMu *sync.Mutex = new(sync.Mutex) 33 34 func withModifiedConfig(f func()) { 35 configMu.Lock() 36 defer configMu.Unlock() 37 f() 38 } 39 40 // waitWgTimeout returns an error if the WaitGroup doesn't return in timeout duration 41 func waitWgTimeout(wg *sync.WaitGroup, after time.Duration) error { 42 c := make(chan struct{}) 43 go func() { 44 defer close(c) 45 wg.Wait() 46 }() 47 select { 48 case <-time.After(time.Second): 49 return moerr.NewInternalError(context.Background(), "timeout") 50 case <-c: 51 return nil 52 } 53 } 54 55 func makeDummyClock(startOffset int64) func() int64 { 56 var tick = startOffset - 1 57 return func() int64 { 58 return atomic.AddInt64(&tick, 1) 59 } 60 } 61 62 type dummySwitch struct{} 63 64 func (dummySwitch) Start(ctx context.Context) bool { return true } 65 func (dummySwitch) Stop(graceful bool) (<-chan struct{}, bool) { return nil, false }