server_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package http
  2. import (
  3. http "net/http"
  4. "sync"
  5. "testing"
  6. "time"
  7. bm "go-common/library/net/http/blademaster"
  8. xtime "go-common/library/time"
  9. )
  10. var once sync.Once
  11. func startServer() {
  12. c := &ServerConfig{
  13. Addr: "localhost:18080",
  14. Timeout: xtime.Duration(time.Second),
  15. ReadTimeout: xtime.Duration(time.Second),
  16. WriteTimeout: xtime.Duration(time.Second),
  17. }
  18. engine := bm.Default()
  19. engine.GET("/test", func(ctx *bm.Context) {
  20. ctx.JSON("", nil)
  21. })
  22. Serve(engine, c)
  23. }
  24. func TestServer2(t *testing.T) {
  25. once.Do(startServer)
  26. resp, err := http.Get("http://localhost:18080/test")
  27. if err != nil {
  28. t.Errorf("HTTPServ: get error(%v)", err)
  29. }
  30. if resp.StatusCode != http.StatusOK {
  31. t.Errorf("http.Get get error code:%d", resp.StatusCode)
  32. }
  33. resp.Body.Close()
  34. }
  35. func BenchmarkServer2(b *testing.B) {
  36. once.Do(startServer)
  37. b.ResetTimer()
  38. b.RunParallel(func(pb *testing.PB) {
  39. for pb.Next() {
  40. resp, err := http.Get("http://localhost:18080/test")
  41. if err != nil {
  42. b.Errorf("HTTPServ: get error(%v)", err)
  43. continue
  44. }
  45. if resp.StatusCode != http.StatusOK {
  46. b.Errorf("HTTPServ: get error status code:%d", resp.StatusCode)
  47. }
  48. resp.Body.Close()
  49. }
  50. })
  51. }