diff options
author | Christopher Speller <crspeller@gmail.com> | 2016-05-12 23:56:07 -0400 |
---|---|---|
committer | Christopher Speller <crspeller@gmail.com> | 2016-05-12 23:56:07 -0400 |
commit | 38ee83e45b4de7edf89bf9f0ef629eb4c6ad0fa8 (patch) | |
tree | a4fde09672192b97d453ad605b030bd5a10c5a45 /vendor/gopkg.in/throttled/throttled.v1/interval_test.go | |
parent | 84d2482ddbff9564c9ad75b2d30af66e3ddfd44d (diff) | |
download | chat-38ee83e45b4de7edf89bf9f0ef629eb4c6ad0fa8.tar.gz chat-38ee83e45b4de7edf89bf9f0ef629eb4c6ad0fa8.tar.bz2 chat-38ee83e45b4de7edf89bf9f0ef629eb4c6ad0fa8.zip |
Moving to glide
Diffstat (limited to 'vendor/gopkg.in/throttled/throttled.v1/interval_test.go')
-rw-r--r-- | vendor/gopkg.in/throttled/throttled.v1/interval_test.go | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/vendor/gopkg.in/throttled/throttled.v1/interval_test.go b/vendor/gopkg.in/throttled/throttled.v1/interval_test.go new file mode 100644 index 000000000..bc584e134 --- /dev/null +++ b/vendor/gopkg.in/throttled/throttled.v1/interval_test.go @@ -0,0 +1,114 @@ +package throttled + +import ( + "net/http" + "testing" + + "github.com/PuerkitoBio/boom/commands" +) + +func TestInterval(t *testing.T) { + if testing.Short() { + t.Skip() + } + cases := []struct { + n int + c int + rps int + bursts int + }{ + 0: {60, 10, 20, 100}, + 1: {300, 20, 100, 100}, + 2: {10, 10, 1, 10}, + 3: {1000, 100, 1000, 100}, + } + for i, c := range cases { + // Setup the stats handler + st := &stats{} + // Create the throttler + th := Interval(PerSec(c.rps), c.bursts, nil, 0) + th.DeniedHandler = http.HandlerFunc(st.DeniedHTTP) + b := commands.Boom{ + Req: &commands.ReqOpts{}, + N: c.n, + C: c.c, + Output: "quiet", + } + // Run the test + rpts := runTest(th.Throttle(st), b) + // Assert results + for _, rpt := range rpts { + assertRPS(t, i, c.rps, rpt) + } + assertStats(t, i, st, rpts) + } +} + +func TestIntervalVary(t *testing.T) { + if testing.Short() { + t.Skip() + } + cases := []struct { + n int + c int + urls int + rps int + bursts int + }{ + 0: {60, 10, 3, 20, 100}, + 1: {300, 20, 3, 100, 100}, + 2: {10, 10, 3, 1, 10}, + 3: {500, 10, 2, 1000, 100}, + } + for i, c := range cases { + // Setup the stats handler + st := &stats{} + // Create the throttler + th := Interval(PerSec(c.rps), c.bursts, nil, 0) + th.DeniedHandler = http.HandlerFunc(st.DeniedHTTP) + var booms []commands.Boom + for j := 0; j < c.urls; j++ { + booms = append(booms, commands.Boom{ + Req: &commands.ReqOpts{}, + N: c.n, + C: c.c, + Output: "quiet", + }) + } + // Run the test + rpts := runTest(th.Throttle(st), booms...) + // Assert results + for _, rpt := range rpts { + assertRPS(t, i, c.rps, rpt) + } + assertStats(t, i, st, rpts) + } +} + +func assertRPS(t *testing.T, ix int, exp int, rpt *commands.Report) { + wigglef := 0.2 * float64(exp) + if rpt.SuccessRPS < float64(exp)-wigglef || rpt.SuccessRPS > float64(exp)+wigglef { + t.Errorf("%d: expected RPS to be around %d, got %f", ix, exp, rpt.SuccessRPS) + } +} + +func assertStats(t *testing.T, ix int, st *stats, rpts []*commands.Report) { + ok, ko, _ := st.Stats() + var twos, fives, max int + for _, rpt := range rpts { + twos += rpt.StatusCodeDist[200] + fives += rpt.StatusCodeDist[deniedStatus] + if len(rpt.StatusCodeDist) > max { + max = len(rpt.StatusCodeDist) + } + } + if ok != twos { + t.Errorf("%d: expected %d status 200, got %d", ix, twos, ok) + } + if ko != fives { + t.Errorf("%d: expected %d status 429, got %d", ix, fives, ok) + } + if max > 2 { + t.Errorf("%d: expected at most 2 different status codes, got %d", ix, max) + } +} |