From 38ee83e45b4de7edf89bf9f0ef629eb4c6ad0fa8 Mon Sep 17 00:00:00 2001 From: Christopher Speller Date: Thu, 12 May 2016 23:56:07 -0400 Subject: Moving to glide --- .../gopkg.in/throttled/throttled.v1/common_test.go | 65 ++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 vendor/gopkg.in/throttled/throttled.v1/common_test.go (limited to 'vendor/gopkg.in/throttled/throttled.v1/common_test.go') diff --git a/vendor/gopkg.in/throttled/throttled.v1/common_test.go b/vendor/gopkg.in/throttled/throttled.v1/common_test.go new file mode 100644 index 000000000..ddb57fb1c --- /dev/null +++ b/vendor/gopkg.in/throttled/throttled.v1/common_test.go @@ -0,0 +1,65 @@ +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 +} -- cgit v1.2.3-1-g7c22