limit_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package rate
  2. import (
  3. "context"
  4. "io/ioutil"
  5. "net/http"
  6. "testing"
  7. "time"
  8. bm "go-common/library/net/http/blademaster"
  9. )
  10. func TestLimiterUrl(t *testing.T) {
  11. l := New(&Config{
  12. URLs: map[string]*Limit{"/limit/test": &Limit{Limit: 1, Burst: 2}},
  13. })
  14. if !l.Allow("testApp", "/limit/test") {
  15. t.Logf("request should pass,but blocked")
  16. t.FailNow()
  17. }
  18. if !l.Allow("testApp", "/limit/test") {
  19. t.Logf("request should pass,but blocked")
  20. t.FailNow()
  21. }
  22. if l.Allow("testApp", "/limit/test") {
  23. t.Logf("request should block,but passed")
  24. t.FailNow()
  25. }
  26. }
  27. func TestLimiterApp(t *testing.T) {
  28. l := New(&Config{
  29. Apps: map[string]*Limit{"testApp": &Limit{Limit: 1, Burst: 2}},
  30. })
  31. if !l.Allow("testApp", "/limit/test") {
  32. t.Logf("request should pass,but blocked")
  33. t.FailNow()
  34. }
  35. if !l.Allow("testApp", "/limit/test") {
  36. t.Logf("request should pass,but blocked")
  37. t.FailNow()
  38. }
  39. if l.Allow("testApp", "/limit/test") {
  40. t.Logf("request should block,but passed")
  41. t.FailNow()
  42. }
  43. }
  44. func TestLimiterUrlApp(t *testing.T) {
  45. l := New(&Config{
  46. Apps: map[string]*Limit{"testApp": &Limit{Limit: 2, Burst: 1}},
  47. URLs: map[string]*Limit{"/limit/test": &Limit{Limit: 2, Burst: 1}},
  48. })
  49. if !l.Allow("testApp", "/limit/test") {
  50. t.Logf("request should pass,but blocked")
  51. t.FailNow()
  52. }
  53. if l.Allow("testApp", "/limit/test") {
  54. t.Logf("request should block,but passed")
  55. t.FailNow()
  56. }
  57. l.Reload(&Config{
  58. Apps: map[string]*Limit{"testApp": &Limit{Limit: 1, Burst: 2}},
  59. URLs: map[string]*Limit{"/limit/test": &Limit{Limit: 1, Burst: 2}},
  60. })
  61. if !l.Allow("testApp", "/limit/test") {
  62. t.Logf("request should pass,but blocked")
  63. t.FailNow()
  64. }
  65. if !l.Allow("testApp", "/limit/test") {
  66. t.Logf("request should pass,but blocked")
  67. t.FailNow()
  68. }
  69. if l.Allow("testApp", "/limit/test") {
  70. t.Logf("request should block,but passed")
  71. t.FailNow()
  72. }
  73. }
  74. func TestLimiterHandler(t *testing.T) {
  75. l := New(&Config{
  76. Apps: map[string]*Limit{"testApp": &Limit{Limit: 1, Burst: 1}},
  77. URLs: map[string]*Limit{"/limit/test": &Limit{Limit: 2, Burst: 4}},
  78. })
  79. engine := bm.New()
  80. engine.Use(l.Handler())
  81. engine.GET("/limit/test", func(c *bm.Context) {
  82. c.String(200, "pass")
  83. })
  84. go engine.Run(":18080")
  85. defer func() {
  86. engine.Server().Shutdown(context.TODO())
  87. }()
  88. time.Sleep(time.Millisecond * 20)
  89. code, content, err := httpGet("http://127.0.0.1:18080/limit/test?appkey=testApp")
  90. if err != nil {
  91. t.Logf("http get failed,err:=%v", err)
  92. t.FailNow()
  93. }
  94. if code != 200 || string(content) != "pass" {
  95. t.Logf("request should pass by limiter,but blocked")
  96. t.FailNow()
  97. }
  98. _, content, err = httpGet("http://127.0.0.1:18080/limit/test?appkey=testApp")
  99. if err != nil {
  100. t.Logf("http get failed,err:=%v", err)
  101. t.FailNow()
  102. }
  103. if string(content) == "pass" {
  104. t.Logf("request should block by limiter,but passed")
  105. t.FailNow()
  106. }
  107. }
  108. func httpGet(url string) (code int, content []byte, err error) {
  109. resp, err := http.Get(url)
  110. if err != nil {
  111. return
  112. }
  113. defer resp.Body.Close()
  114. content, err = ioutil.ReadAll(resp.Body)
  115. if err != nil {
  116. return
  117. }
  118. code = resp.StatusCode
  119. return
  120. }