implement a RTTGenerator for generating random RTTs for the UDP Proxy

ref #233
This commit is contained in:
Marten Seemann
2016-08-01 14:22:07 +07:00
parent aaeedafc21
commit e35276b17e
6 changed files with 100 additions and 13 deletions

View File

@@ -0,0 +1,56 @@
package proxy
import (
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("RTT settings", func() {
Context("no variance", func() {
It("always gets the same value", func() {
rttGen := newRttGenerator(10*time.Millisecond, 10*time.Millisecond)
for i := 0; i < 100; i++ {
Expect(rttGen.getRTT()).To(Equal(10 * time.Millisecond))
}
})
})
Context("random RTT", func() {
var rttGen rttGenerator
BeforeEach(func() {
rttGen = newRttGenerator(10*time.Millisecond, 30*time.Millisecond)
})
It("has the right mean value", func() {
var rttSum time.Duration
rep := 1000
for i := 0; i < rep; i++ {
rttSum += rttGen.getRTT()
}
averageRTT := rttSum.Nanoseconds() / 1000 / int64(rep) // in microseconds
Expect(averageRTT).To(BeNumerically("~", 20000, 1000)) // between 19 and 21 microseconds
})
It("covers the whole interval", func() {
var max time.Duration
min := time.Hour
rep := 1000
for i := 0; i < rep; i++ {
rtt := rttGen.getRTT()
if rtt > max {
max = rtt
}
if rtt < min {
min = rtt
}
}
Expect(min.Nanoseconds() / 1000).To(BeNumerically("<", 11000))
Expect(max.Nanoseconds() / 1000).To(BeNumerically(">", 29000))
})
})
})