-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
37 lines (33 loc) · 774 Bytes
/
utils.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
package testutils
import (
"math/rand"
"testing"
"time"
)
const chars = "abcdefghijklmnopqrstuvwxyz" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
"~!@#$%^&*()-_+={}[]\\|<,>.?/\"';:` "
// Timeout calls f and waits for 100ms for it to complete.
// If it doesn't, it causes the tests to fail.
// t must be a valid testing context.
func Timeout(t *testing.T, f func()) {
onTimeout := time.After(100 * time.Millisecond)
onDone := make(chan bool)
go func() {
f()
close(onDone)
}()
select {
case <-onTimeout:
t.Fatalf("timeout")
case <-onDone:
}
}
// RandomString returns random string of specified length
func RandomString(length int) string {
res := make([]byte, length)
for i := 0; i < length; i++ {
res[i] = chars[rand.Intn(len(chars))]
}
return string(res)
}