diff options
author | Yuri Tkachenko <yuri.tam.tkachenko@gmail.com> | 2015-11-30 12:15:28 +0300 |
---|---|---|
committer | Yuri Tkachenko <yuri.tam.tkachenko@gmail.com> | 2015-11-30 12:15:28 +0300 |
commit | 8b982f7effa336a503ab61c676ee0f2473de6e3b (patch) | |
tree | ecd49a12e3cb9e4bff398057df01d44eade70da4 /Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/common_test.go | |
parent | 71b548ef052d4e84ea0d067df51e4850ffdba572 (diff) | |
parent | d4eb8743e3bd36b6cd2e7939c9a698d893b215d7 (diff) | |
download | chat-8b982f7effa336a503ab61c676ee0f2473de6e3b.tar.gz chat-8b982f7effa336a503ab61c676ee0f2473de6e3b.tar.bz2 chat-8b982f7effa336a503ab61c676ee0f2473de6e3b.zip |
Merge remote-tracking branch 'mattermost/master' into patch-1
Diffstat (limited to 'Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/common_test.go')
-rw-r--r-- | Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/common_test.go | 65 |
1 files changed, 65 insertions, 0 deletions
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 new file mode 100644 index 000000000..ddb57fb1c --- /dev/null +++ b/Godeps/_workspace/src/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 +} |