interceptor_test.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package interceptor
  2. import (
  3. ctx "context"
  4. "errors"
  5. "sync"
  6. "testing"
  7. "go-common/library/ecode"
  8. "go-common/library/net/rpc/context"
  9. "golang.org/x/time/rate"
  10. )
  11. var (
  12. once sync.Once
  13. i *Interceptor
  14. c context.Context
  15. )
  16. func interceptor() {
  17. i = NewInterceptor("test token")
  18. c = context.NewContext(ctx.TODO(), "testMethod", "test user", 0)
  19. }
  20. func TestRate(t *testing.T) {
  21. once.Do(interceptor)
  22. if err := i.Rate(c); err != nil {
  23. t.Errorf("TestRate error(%v)", err)
  24. t.FailNow()
  25. }
  26. i.rateLimits["testMethod"] = rate.NewLimiter(1, 0)
  27. if err := i.Rate(c); err != ecode.Degrade {
  28. t.Errorf("TestRate error(%v)", err)
  29. t.FailNow()
  30. }
  31. }
  32. func TestStat(t *testing.T) {
  33. once.Do(interceptor)
  34. i.Stat(c, nil, errors.New("test error"))
  35. }
  36. func TestAuth(t *testing.T) {
  37. once.Do(interceptor)
  38. if err := i.Auth(c, nil, "test token"); err != nil {
  39. t.Errorf("TestAuth error(%v)", err)
  40. t.FailNow()
  41. }
  42. if err := i.Auth(c, nil, "token"); err != ecode.RPCNoAuth {
  43. t.Errorf("TestAuth error(%v)", err)
  44. t.FailNow()
  45. }
  46. }