From 962b18411893e1fffd10c2b6370ac34aba62f146 Mon Sep 17 00:00:00 2001 From: Christopher Speller Date: Tue, 23 Feb 2016 14:07:01 -0500 Subject: Updating golang dependancies (godep) --- .../gopkg.in/throttled/throttled.v1/common_test.go | 65 ------------ .../throttled/throttled.v1/delayer_test.go | 65 ------------ .../throttled/throttled.v1/interval_test.go | 114 --------------------- .../throttled/throttled.v1/memstats_test.go | 64 ------------ .../gopkg.in/throttled/throttled.v1/rate_test.go | 101 ------------------ .../throttled/throttled.v1/store/mem_test.go | 43 -------- .../throttled/throttled.v1/store/redis_test.go | 66 ------------ .../gopkg.in/throttled/throttled.v1/varyby_test.go | 56 ---------- 8 files changed, 574 deletions(-) delete mode 100644 Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/common_test.go delete mode 100644 Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/delayer_test.go delete mode 100644 Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/interval_test.go delete mode 100644 Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/memstats_test.go delete mode 100644 Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/rate_test.go delete mode 100644 Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/store/mem_test.go delete mode 100644 Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/store/redis_test.go delete mode 100644 Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/varyby_test.go (limited to 'Godeps/_workspace/src/gopkg.in/throttled') diff --git a/Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/common_test.go b/Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/common_test.go deleted file mode 100644 index ddb57fb1c..000000000 --- a/Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/common_test.go +++ /dev/null @@ -1,65 +0,0 @@ -package throttled - -import ( - "fmt" - "net/http" - "net/http/httptest" - "sync" - "time" - - "github.com/PuerkitoBio/boom/commands" -) - -type stats struct { - sync.Mutex - ok int - dropped int - ts []time.Time - - body func() -} - -func (s *stats) ServeHTTP(w http.ResponseWriter, r *http.Request) { - if s.body != nil { - s.body() - } - s.Lock() - defer s.Unlock() - s.ts = append(s.ts, time.Now()) - s.ok++ - w.WriteHeader(200) -} - -func (s *stats) DeniedHTTP(w http.ResponseWriter, r *http.Request) { - s.Lock() - defer s.Unlock() - s.dropped++ - w.WriteHeader(deniedStatus) -} - -func (s *stats) Stats() (int, int, []time.Time) { - s.Lock() - defer s.Unlock() - return s.ok, s.dropped, s.ts -} - -func runTest(h http.Handler, b ...commands.Boom) []*commands.Report { - srv := httptest.NewServer(h) - defer srv.Close() - - var rpts []*commands.Report - var wg sync.WaitGroup - var mu sync.Mutex - wg.Add(len(b)) - for i, bo := range b { - bo.Req.Url = srv.URL + fmt.Sprintf("/%d", i) - go func(bo commands.Boom) { - mu.Lock() - defer mu.Unlock() - rpts = append(rpts, bo.Run()) - wg.Done() - }(bo) - } - wg.Wait() - return rpts -} diff --git a/Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/delayer_test.go b/Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/delayer_test.go deleted file mode 100644 index 822978e5d..000000000 --- a/Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/delayer_test.go +++ /dev/null @@ -1,65 +0,0 @@ -package throttled - -import ( - "testing" - "time" -) - -func TestDelayer(t *testing.T) { - cases := []struct { - in Delayer - out time.Duration - }{ - 0: {PerSec(1), time.Second}, - 1: {PerSec(2), 500 * time.Millisecond}, - 2: {PerSec(4), 250 * time.Millisecond}, - 3: {PerSec(5), 200 * time.Millisecond}, - 4: {PerSec(10), 100 * time.Millisecond}, - 5: {PerSec(100), 10 * time.Millisecond}, - 6: {PerSec(3), 333333333 * time.Nanosecond}, - 7: {PerMin(1), time.Minute}, - 8: {PerMin(2), 30 * time.Second}, - 9: {PerMin(4), 15 * time.Second}, - 10: {PerMin(5), 12 * time.Second}, - 11: {PerMin(10), 6 * time.Second}, - 12: {PerMin(60), time.Second}, - 13: {PerHour(1), time.Hour}, - 14: {PerHour(2), 30 * time.Minute}, - 15: {PerHour(4), 15 * time.Minute}, - 16: {PerHour(60), time.Minute}, - 17: {PerHour(120), 30 * time.Second}, - 18: {D(time.Second), time.Second}, - 19: {D(5 * time.Minute), 5 * time.Minute}, - 20: {PerSec(200), 5 * time.Millisecond}, - 21: {PerDay(24), time.Hour}, - } - for i, c := range cases { - got := c.in.Delay() - if got != c.out { - t.Errorf("%d: expected %s, got %s", i, c.out, got) - } - } -} - -func TestQuota(t *testing.T) { - cases := []struct { - q Quota - reqs int - win time.Duration - }{ - 0: {PerSec(10), 10, time.Second}, - 1: {PerMin(30), 30, time.Minute}, - 2: {PerHour(124), 124, time.Hour}, - 3: {PerDay(1), 1, 24 * time.Hour}, - 4: {Q{148, 17 * time.Second}, 148, 17 * time.Second}, - } - for i, c := range cases { - r, w := c.q.Quota() - if r != c.reqs { - t.Errorf("%d: expected %d requests, got %d", i, c.reqs, r) - } - if w != c.win { - t.Errorf("%d: expected %s window, got %s", i, c.win, w) - } - } -} diff --git a/Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/interval_test.go b/Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/interval_test.go deleted file mode 100644 index bc584e134..000000000 --- a/Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/interval_test.go +++ /dev/null @@ -1,114 +0,0 @@ -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) - } -} diff --git a/Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/memstats_test.go b/Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/memstats_test.go deleted file mode 100644 index 2b8faa721..000000000 --- a/Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/memstats_test.go +++ /dev/null @@ -1,64 +0,0 @@ -package throttled - -import ( - "net/http" - "runtime" - "testing" - "time" - - "github.com/PuerkitoBio/boom/commands" -) - -func TestMemStats(t *testing.T) { - if testing.Short() { - t.Skip() - } - cases := []struct { - n int - c int - gc uint32 - total uint64 - rate time.Duration - }{ - 0: {1000, 10, 3, 0, 0}, - 1: {200, 10, 0, 600000, 0}, - 2: {500, 10, 2, 555555, 10 * time.Millisecond}, - } - for i, c := range cases { - // Setup the stats handler - st := &stats{} - // Create the throttler - limit := MemThresholds(&runtime.MemStats{NumGC: c.gc, TotalAlloc: c.total}) - th := MemStats(limit, c.rate) - th.DeniedHandler = http.HandlerFunc(st.DeniedHTTP) - // Run the test - b := commands.Boom{ - Req: &commands.ReqOpts{}, - N: c.n, - C: c.c, - Output: "quiet", - } - rpts := runTest(th.Throttle(st), b) - // Assert results - assertStats(t, i, st, rpts) - assertMem(t, i, limit) - } -} - -func assertMem(t *testing.T, ix int, limit *runtime.MemStats) { - var mem runtime.MemStats - runtime.ReadMemStats(&mem) - if mem.NumGC < limit.NumGC { - t.Errorf("%d: expected gc to be at least %d, got %d", ix, limit.NumGC, mem.NumGC) - } - if mem.TotalAlloc < limit.TotalAlloc { - t.Errorf("%d: expected total alloc to be at least %dKb, got %dKb", ix, limit.TotalAlloc/1024, mem.TotalAlloc/1024) - } -} - -func BenchmarkReadMemStats(b *testing.B) { - var mem runtime.MemStats - for i := 0; i < b.N; i++ { - runtime.ReadMemStats(&mem) - } -} diff --git a/Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/rate_test.go b/Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/rate_test.go deleted file mode 100644 index 67dea74b1..000000000 --- a/Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/rate_test.go +++ /dev/null @@ -1,101 +0,0 @@ -package throttled - -import ( - "net/http" - "net/http/httptest" - "strconv" - "testing" - "time" -) - -const deniedStatus = 429 - -// Simple memory store for tests, unsafe for concurrent access -type mapStore struct { - cnt map[string]int - ts map[string]time.Time -} - -func newMapStore() *mapStore { - return &mapStore{ - make(map[string]int), - make(map[string]time.Time), - } -} -func (ms *mapStore) Incr(key string, window time.Duration) (int, int, error) { - if _, ok := ms.cnt[key]; !ok { - return 0, 0, ErrNoSuchKey - } - ms.cnt[key]++ - ts := ms.ts[key] - return ms.cnt[key], RemainingSeconds(ts, window), nil -} -func (ms *mapStore) Reset(key string, win time.Duration) error { - ms.cnt[key] = 1 - ms.ts[key] = time.Now().UTC() - return nil -} - -func TestRateLimit(t *testing.T) { - quota := Q{5, 5 * time.Second} - cases := []struct { - limit, remain, reset, status int - }{ - 0: {5, 4, 5, 200}, - 1: {5, 3, 4, 200}, - 2: {5, 2, 4, 200}, - 3: {5, 1, 3, 200}, - 4: {5, 0, 3, 200}, - 5: {5, 0, 2, deniedStatus}, - } - // Limit the requests to 2 per second - th := Interval(PerSec(2), 0, nil, 0) - // Rate limit - rl := RateLimit(quota, nil, newMapStore()) - // Create the stats - st := &stats{} - // Create the handler - h := th.Throttle(rl.Throttle(st)) - - // Start the server - srv := httptest.NewServer(h) - defer srv.Close() - for i, c := range cases { - callRateLimited(t, i, c.limit, c.remain, c.reset, c.status, srv.URL) - } - // Wait 3 seconds and call again, should start a new window - time.Sleep(3 * time.Second) - callRateLimited(t, len(cases), 5, 4, 5, 200, srv.URL) -} - -func callRateLimited(t *testing.T, i, limit, remain, reset, status int, url string) { - res, err := http.Get(url) - if err != nil { - t.Fatal(err) - } - defer res.Body.Close() - // Assert status code - if status != res.StatusCode { - t.Errorf("%d: expected status %d, got %d", i, status, res.StatusCode) - } - // Assert headers - if v := res.Header.Get("X-RateLimit-Limit"); v != strconv.Itoa(limit) { - t.Errorf("%d: expected limit header to be %d, got %s", i, limit, v) - } - if v := res.Header.Get("X-RateLimit-Remaining"); v != strconv.Itoa(remain) { - t.Errorf("%d: expected remain header to be %d, got %s", i, remain, v) - } - // Allow 1 second wiggle room - v := res.Header.Get("X-RateLimit-Reset") - vi, _ := strconv.Atoi(v) - if vi < reset-1 || vi > reset+1 { - t.Errorf("%d: expected reset header to be close to %d, got %d", i, reset, vi) - } - if status == deniedStatus { - v := res.Header.Get("Retry-After") - vi, _ := strconv.Atoi(v) - if vi < reset-1 || vi > reset+1 { - t.Errorf("%d: expected retry after header to be close to %d, got %d", i, reset, vi) - } - } -} diff --git a/Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/store/mem_test.go b/Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/store/mem_test.go deleted file mode 100644 index e8ef8d0da..000000000 --- a/Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/store/mem_test.go +++ /dev/null @@ -1,43 +0,0 @@ -package store - -import ( - "testing" - "time" -) - -func TestMemStore(t *testing.T) { - st := NewMemStore(0) - win := time.Second - - // Reset stores a key with count of 1, current timestamp - err := st.Reset("k", time.Second) - if err != nil { - t.Errorf("expected reset to return nil, got %s", err) - } - cnt, sec1, _ := st.Incr("k", win) - if cnt != 2 { - t.Errorf("expected reset+incr to set count to 2, got %d", cnt) - } - - // Incr increments the key, keeps same timestamp - cnt, sec2, err := st.Incr("k", win) - if err != nil { - t.Errorf("expected 2nd incr to return nil error, got %s", err) - } - if cnt != 3 { - t.Errorf("expected 2nd incr to return 3, got %d", cnt) - } - if sec1 != sec2 { - t.Errorf("expected 2nd incr to return %d secs, got %d", sec1, sec2) - } - - // Reset on existing key brings it back to 1, new timestamp - err = st.Reset("k", win) - if err != nil { - t.Errorf("expected reset on existing key to return nil, got %s", err) - } - cnt, _, _ = st.Incr("k", win) - if cnt != 2 { - t.Errorf("expected last reset+incr to return 2, got %d", cnt) - } -} diff --git a/Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/store/redis_test.go b/Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/store/redis_test.go deleted file mode 100644 index a282d6d25..000000000 --- a/Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/store/redis_test.go +++ /dev/null @@ -1,66 +0,0 @@ -package store - -import ( - "testing" - "time" - - "github.com/garyburd/redigo/redis" -) - -func getPool() *redis.Pool { - pool := &redis.Pool{ - MaxIdle: 3, - IdleTimeout: 30 * time.Second, - Dial: func() (redis.Conn, error) { - return redis.Dial("tcp", ":6379") - }, - TestOnBorrow: func(c redis.Conn, t time.Time) error { - _, err := c.Do("PING") - return err - }, - } - return pool -} - -func TestRedisStore(t *testing.T) { - pool := getPool() - c := pool.Get() - if _, err := redis.String(c.Do("PING")); err != nil { - c.Close() - t.Skip("redis server not available on localhost port 6379") - } - st := NewRedisStore(pool, "throttled:", 1) - win := 2 * time.Second - - // Incr increments the key, even if it does not exist - cnt, secs, err := st.Incr("k", win) - if err != nil { - t.Errorf("expected initial incr to return nil error, got %s", err) - } - if cnt != 1 { - t.Errorf("expected initial incr to return 1, got %d", cnt) - } - if secs != int(win.Seconds()) { - t.Errorf("expected initial incr to return %d secs, got %d", int(win.Seconds()), secs) - } - - // Waiting a second diminishes the remaining seconds - time.Sleep(time.Second) - _, sec2, _ := st.Incr("k", win) - if sec2 != secs-1 { - t.Errorf("expected 2nd incr after a 1s sleep to return %d secs, got %d", secs-1, sec2) - } - - // Waiting a second so the key expires, Incr should set back to 1, initial secs - time.Sleep(1100 * time.Millisecond) - cnt, sec3, err := st.Incr("k", win) - if err != nil { - t.Errorf("expected last incr to return nil error, got %s", err) - } - if cnt != 1 { - t.Errorf("expected last incr to return 1, got %d", cnt) - } - if sec3 != int(win.Seconds()) { - t.Errorf("expected last incr to return %d secs, got %d", int(win.Seconds()), sec3) - } -} diff --git a/Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/varyby_test.go b/Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/varyby_test.go deleted file mode 100644 index 91b7ae0ae..000000000 --- a/Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/varyby_test.go +++ /dev/null @@ -1,56 +0,0 @@ -package throttled - -import ( - "net/http" - "net/url" - "testing" -) - -func TestVaryBy(t *testing.T) { - u, err := url.Parse("http://localhost/test/path?q=s") - if err != nil { - panic(err) - } - ck := &http.Cookie{Name: "ssn", Value: "test"} - cases := []struct { - vb *VaryBy - r *http.Request - k string - }{ - 0: {nil, &http.Request{}, ""}, - 1: {&VaryBy{RemoteAddr: true}, &http.Request{RemoteAddr: "::"}, "::\n"}, - 2: { - &VaryBy{Method: true, Path: true}, - &http.Request{Method: "POST", URL: u}, - "post\n/test/path\n", - }, - 3: { - &VaryBy{Headers: []string{"Content-length"}}, - &http.Request{Header: http.Header{"Content-Type": []string{"text/plain"}, "Content-Length": []string{"123"}}}, - "123\n", - }, - 4: { - &VaryBy{Separator: ",", Method: true, Headers: []string{"Content-length"}, Params: []string{"q", "user"}}, - &http.Request{Method: "GET", Header: http.Header{"Content-Type": []string{"text/plain"}, "Content-Length": []string{"123"}}, Form: url.Values{"q": []string{"s"}, "pwd": []string{"secret"}, "user": []string{"test"}}}, - "get,123,s,test,", - }, - 5: { - &VaryBy{Cookies: []string{"ssn"}}, - &http.Request{Header: http.Header{"Cookie": []string{ck.String()}}}, - "test\n", - }, - 6: { - &VaryBy{Cookies: []string{"ssn"}, RemoteAddr: true, Custom: func(r *http.Request) string { - return "blah" - }}, - &http.Request{Header: http.Header{"Cookie": []string{ck.String()}}}, - "blah", - }, - } - for i, c := range cases { - got := c.vb.Key(c.r) - if got != c.k { - t.Errorf("%d: expected '%s' (%d), got '%s' (%d)", i, c.k, len(c.k), got, len(got)) - } - } -} -- cgit v1.2.3-1-g7c22