consul: adding randomStagger util method

pull/939/head
Armon Dadgar 10 years ago
parent 926b8bc7fa
commit c51c888142

@ -4,12 +4,14 @@ import (
crand "crypto/rand"
"encoding/binary"
"fmt"
"math/rand"
"net"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
"github.com/hashicorp/serf/serf"
)
@ -222,3 +224,8 @@ func generateUUID() string {
buf[8:10],
buf[10:16])
}
// Returns a random stagger interval between 0 and the duration
func randomStagger(intv time.Duration) time.Duration {
return time.Duration(uint64(rand.Int63()) % uint64(intv))
}

@ -4,6 +4,7 @@ import (
"net"
"regexp"
"testing"
"time"
"github.com/hashicorp/serf/serf"
)
@ -124,3 +125,13 @@ func TestGenerateUUID(t *testing.T) {
}
}
}
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)
}
}
}

Loading…
Cancel
Save