ratelimit_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package dao
  2. import (
  3. "context"
  4. "flag"
  5. "go-common/app/service/live/live-dm/conf"
  6. "path/filepath"
  7. "testing"
  8. )
  9. func init() {
  10. dir, _ := filepath.Abs("../cmd/test.toml")
  11. flag.Set("conf", dir)
  12. var err error
  13. if err = conf.Init(); err != nil {
  14. panic(err)
  15. }
  16. InitAPI()
  17. InitGrpc(conf.Conf)
  18. }
  19. //group=qa01 DEPLOY_ENV=uat go test -run TestLimitPerSec
  20. func TestLimitPerSec(t *testing.T) {
  21. l := LimitCheckInfo{
  22. UID: 111,
  23. RoomID: 222,
  24. Msg: "6666",
  25. Dao: New(conf.Conf),
  26. MsgType: 0,
  27. Conf: &LimitConf{
  28. DmNum: 20,
  29. DMPercent: 25,
  30. },
  31. }
  32. if err := l.LimitPerSec(context.TODO()); err != nil {
  33. t.Error("每秒限制错误:", err)
  34. }
  35. }
  36. //group=qa01 DEPLOY_ENV=uat go test -run TestLimitSameMsg
  37. func TestLimitSameMsg(t *testing.T) {
  38. l := LimitCheckInfo{
  39. UID: 111,
  40. RoomID: 222,
  41. Msg: "6666",
  42. Dao: New(conf.Conf),
  43. MsgType: 0,
  44. Conf: &LimitConf{
  45. DmNum: 20,
  46. DMPercent: 25,
  47. },
  48. }
  49. if err := l.LimitSameMsg(context.TODO()); err != nil {
  50. t.Error("5秒相同发言错误:", err)
  51. }
  52. }
  53. //group=qa01 DEPLOY_ENV=uat go test -run TestLimitRoomPerSecond
  54. func TestLimitRoomPerSecond(t *testing.T) {
  55. l := LimitCheckInfo{
  56. UID: 111,
  57. RoomID: 222,
  58. Msg: "6666",
  59. Dao: New(conf.Conf),
  60. MsgType: 0,
  61. Conf: &LimitConf{
  62. DmNum: 20,
  63. DMPercent: 25,
  64. },
  65. }
  66. if err := l.LimitRoomPerSecond(context.TODO()); err != nil {
  67. t.Error("每秒20弹幕错误:", err)
  68. }
  69. }