github.com/grubernaut/docker@v1.6.0-rc2/pkg/testutils/utils.go (about)

     1  package testutils
     2  
     3  import (
     4  	"math/rand"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  const chars = "abcdefghijklmnopqrstuvwxyz" +
    10  	"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
    11  	"~!@#$%^&*()-_+={}[]\\|<,>.?/\"';:` "
    12  
    13  // Timeout calls f and waits for 100ms for it to complete.
    14  // If it doesn't, it causes the tests to fail.
    15  // t must be a valid testing context.
    16  func Timeout(t *testing.T, f func()) {
    17  	onTimeout := time.After(100 * time.Millisecond)
    18  	onDone := make(chan bool)
    19  	go func() {
    20  		f()
    21  		close(onDone)
    22  	}()
    23  	select {
    24  	case <-onTimeout:
    25  		t.Fatalf("timeout")
    26  	case <-onDone:
    27  	}
    28  }
    29  
    30  // RandomString returns random string of specified length
    31  func RandomString(length int) string {
    32  	res := make([]byte, length)
    33  	for i := 0; i < length; i++ {
    34  		res[i] = chars[rand.Intn(len(chars))]
    35  	}
    36  	return string(res)
    37  }