2014-05-10 04:11:24 +00:00
|
|
|
package testutils
|
|
|
|
|
|
|
|
import (
|
2014-06-22 11:58:43 +00:00
|
|
|
"math/rand"
|
2014-05-10 04:11:24 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2014-06-22 11:58:43 +00:00
|
|
|
const chars = "abcdefghijklmnopqrstuvwxyz" +
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
|
|
|
|
"~!@#$%^&*()-_+={}[]\\|<,>.?/\"';:` "
|
|
|
|
|
2014-05-10 04:11:24 +00:00
|
|
|
// 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:
|
|
|
|
}
|
|
|
|
}
|
2014-06-22 11:58:43 +00:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|