-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
66 lines (57 loc) · 1.34 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package gta
import (
"context"
"fmt"
"math"
"math/rand"
"runtime/debug"
"sync/atomic"
"time"
"github.com/sirupsen/logrus"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
const (
randomIntervalFactor = 0.2
)
// randomInterval generates random interval in [interval,interval+randomIntervalFactor*interval)
func randomInterval(interval time.Duration) time.Duration {
return interval + time.Duration(randomIntervalFactor*rand.Float64()*float64(interval))
}
func panicHandler() {
if r := recover(); r != nil {
logrus.Errorf("panic: %v\n%s", r, string(debug.Stack()))
}
}
func minInt64(i ...int64) int64 {
min := int64(math.MaxInt64)
for _, a := range i {
if a < min {
min = a
}
}
return min
}
func testDB(dbName string) *gorm.DB {
dbName = dbName + fmt.Sprintf("_%d.db", rand.Int())
db, _ := gorm.Open(sqlite.Open(dbName), &gorm.Config{Logger: logger.Default.LogMode(logger.Silent)})
_ = db.Migrator().AutoMigrate(&Task{})
return db
}
func testCountHandler(run *int64) TaskHandler {
return func(ctx context.Context, arg interface{}) (err error) {
atomic.AddInt64(run, 1)
return nil
}
}
func testWrappedHandler(fs ...TaskHandler) TaskHandler {
return func(ctx context.Context, arg interface{}) (err error) {
for _, f := range fs {
if err := f(ctx, arg); err != nil {
return err
}
}
return nil
}
}