mirror of
https://github.com/kemko/nomad.git
synced 2026-01-06 02:15:43 +03:00
55 lines
995 B
Go
55 lines
995 B
Go
package client
|
|
|
|
import (
|
|
"reflect"
|
|
"regexp"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestGenerateUUID(t *testing.T) {
|
|
prev := generateUUID()
|
|
for i := 0; i < 100; i++ {
|
|
id := generateUUID()
|
|
if prev == id {
|
|
t.Fatalf("Should get a new ID!")
|
|
}
|
|
|
|
matched, err := regexp.MatchString(
|
|
"[\\da-f]{8}-[\\da-f]{4}-[\\da-f]{4}-[\\da-f]{4}-[\\da-f]{12}", id)
|
|
if !matched || err != nil {
|
|
t.Fatalf("expected match %s %v %s", id, matched, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRandomStagger(t *testing.T) {
|
|
intv := time.Minute
|
|
for i := 0; i < 10; i++ {
|
|
stagger := randomStagger(intv)
|
|
if stagger < 0 || stagger >= intv {
|
|
t.Fatalf("Bad: %v", stagger)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestShuffleStrings(t *testing.T) {
|
|
// Generate input
|
|
inp := make([]string, 10)
|
|
for idx := range inp {
|
|
inp[idx] = generateUUID()
|
|
}
|
|
|
|
// Copy the input
|
|
orig := make([]string, len(inp))
|
|
copy(orig, inp)
|
|
|
|
// Shuffle
|
|
shuffleStrings(inp)
|
|
|
|
// Ensure order is not the same
|
|
if reflect.DeepEqual(inp, orig) {
|
|
t.Fatalf("shuffle failed")
|
|
}
|
|
}
|