zlimit_test.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package zlimit
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. "go-common/library/cache/redis"
  7. "go-common/library/container/pool"
  8. "go-common/library/database/sql"
  9. xtime "go-common/library/time"
  10. )
  11. func TestZlimit(t *testing.T) {
  12. c := &Config{
  13. DB: &sql.Config{
  14. Addr: "172.16.33.54:3306",
  15. DSN: "test:test@tcp(172.16.33.54:3306)/bilibili_oversea?timeout=5s&readTimeout=5s&writeTimeout=5s&parseTime=true&loc=Local&charset=utf8,utf8mb4",
  16. Active: 5,
  17. Idle: 2,
  18. },
  19. Redis: &Redis{
  20. Config: &redis.Config{
  21. Config: &pool.Config{
  22. Active: 20,
  23. Idle: 10,
  24. IdleTimeout: xtime.Duration(80 * time.Millisecond),
  25. },
  26. Name: "business/zlimit",
  27. Proto: "tcp",
  28. Addr: "172.16.33.54:6380",
  29. DialTimeout: xtime.Duration(50 * time.Millisecond),
  30. ReadTimeout: xtime.Duration(100 * time.Millisecond),
  31. WriteTimeout: xtime.Duration(100 * time.Millisecond),
  32. },
  33. Expire: xtime.Duration(1 * time.Second),
  34. },
  35. IPFile: "/data/conf/iprepo/iprepo.txt",
  36. FlushTime: xtime.Duration(1 * time.Second),
  37. }
  38. s := New(c)
  39. testFind(t, s, 0, "", "")
  40. testForbid(t, s, "", "")
  41. testFindByPid(t, s, []int64{0}, "")
  42. testFindByGid(t, s, 0, "", "")
  43. }
  44. func testFind(t *testing.T, s *Service, aid int64, ipaddr, cdnip string) {
  45. ret, retdown, err := s.Find(context.TODO(), aid, ipaddr, cdnip)
  46. if err != nil {
  47. t.Errorf("Service: find err: %v", err)
  48. } else {
  49. t.Logf("Service: find %d,%d", ret, retdown)
  50. }
  51. }
  52. func testForbid(t *testing.T, s *Service, pstr, ipaddr string) {
  53. if err := s.Forbid(context.TODO(), pstr, ipaddr); err != nil {
  54. t.Errorf("Service: forbid err: %v", err)
  55. }
  56. }
  57. func testFindByPid(t *testing.T, s *Service, pids []int64, ipaddr string) {
  58. ret, retdown := s.FindByPid(context.TODO(), pids, ipaddr)
  59. t.Logf("Service: findByPid %d,%d", ret, retdown)
  60. }
  61. func testFindByGid(t *testing.T, s *Service, gid int64, ipaddr, cdnip string) {
  62. ret, retdown := s.FindByGid(context.TODO(), gid, ipaddr, cdnip)
  63. t.Logf("Service: findByGid %d,%d", ret, retdown)
  64. }