Skip to content

Commit

Permalink
Fixed non-uniform distribution (fixes aerokube#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
vania-pooh committed Jun 13, 2017
1 parent 9c78f01 commit a402b6a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion config.go
Expand Up @@ -132,7 +132,7 @@ func (hosts Hosts) choose() (*Host, int) {
total += h.Count
}
if total > 0 {
r := rand.Intn(total)
r := rand.Intn(total + 1)
for i, host := range hosts {
r -= host.Count
if r <= 0 {
Expand Down
16 changes: 16 additions & 0 deletions config_test.go
Expand Up @@ -39,6 +39,22 @@ func TestChooseLast(t *testing.T) {
AssertThat(t, index, EqualTo{2})
}

func TestChoosingAllHosts(t *testing.T) {
//NOTE: the same weights for all hosts are important!
hosts := Hosts{Host{Name: "first", Count: 1}, Host{Name: "mid", Count: 1}, Host{Name: "last", Count: 1}}
chosenHosts := make(map[string] struct{})
for i := 0; i < 100; i++ {
host, _ := hosts.choose()
chosenHosts[host.Name] = struct{}{}
}
_, firstIsPresent := chosenHosts["first"]
AssertThat(t, firstIsPresent, Is{true})
_, midIsPresent := chosenHosts["mid"]
AssertThat(t, midIsPresent, Is{true})
_, lastIsPresent := chosenHosts["last"]
AssertThat(t, lastIsPresent, Is{true})
}

var (
browsersWithMultipleVersions = &Browsers{Browsers: []Browser{
{Name: "browser", DefaultVersion: "2.0", Versions: []Version{
Expand Down

0 comments on commit a402b6a

Please sign in to comment.