Describe("RandomClient", func() { Context("with a non-empty client pool", func() { It("chooses a client with roughly uniform distribution", func() { s := map[string]string{ "1": "udp://host:1", "2": "udp://host:2", "3": "udp://host:3", "4": "udp://host:4", "5": "udp://host:5", } pool.Set(s, nil) counts := make(map[loggregatorclient.Client]int) for i := 0; i < 100000; i++ { pick, _ := pool.RandomClient() counts[pick]++ } for _, count := range counts { Expect(count).To(BeNumerically("~", 20000, 500)) } }) }) Context("with an empty client pool", func() { It("returns an error", func() { _, err := pool.RandomClient() Expect(err).To(Equal(clientpool.ErrorEmptyClientPool)) }) })
Describe("RandomClient", func() { Context("with non-empty client pool", func() { BeforeEach(func() { close(mockClientCreator.CreateClientOutput.err) mockClientCreator.CreateClientOutput.client <- newMockClient() mockClientCreator.CreateClientOutput.client <- newMockClient() }) JustBeforeEach(func() { pool.SetAddresses(addresses) }) It("returns a random client", func() { client, err := pool.RandomClient() Expect(err).ToNot(HaveOccurred()) Expect(client).ToNot(BeNil()) }) }) Context("with empty client pool", func() { JustBeforeEach(func() { pool.SetAddresses([]string{}) }) It("returns ErrorEmptyClientPool", func() { client, err := pool.RandomClient()