task_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "sync"
  6. "testing"
  7. "time"
  8. "go-common/app/admin/main/videoup/model/archive"
  9. "go-common/app/admin/main/videoup/model/utils"
  10. )
  11. func TestLockVideo(t *testing.T) {
  12. svr.lockVideo()
  13. }
  14. func TestConsumer(t *testing.T) {
  15. var c = context.TODO()
  16. e := svr.HandsUp(c, 10, "10")
  17. if e != nil {
  18. t.Fatal(e)
  19. }
  20. e = svr.HandsUp(c, 20, "20")
  21. if e != nil {
  22. t.Fatal(e)
  23. }
  24. e = svr.HandsOff(c, 10, 20)
  25. if e != nil {
  26. t.Fatal(e)
  27. }
  28. e = svr.HandsUp(c, 20, "20")
  29. if e != nil {
  30. t.Fatal(e)
  31. }
  32. e = svr.HandsOff(c, 20, 10)
  33. if e == nil {
  34. t.Fatal("只有组长能强制踢出")
  35. }
  36. cms, err := svr.Online(c)
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. if len(cms) != 2 {
  41. t.Fatal("在线人数错误")
  42. }
  43. for _, v := range cms {
  44. if (v.UID != 10 && v.UID != 20) || v.State != 1 {
  45. t.Fatal("在线人信息错误")
  46. }
  47. }
  48. }
  49. func BenchmarkMultiGetNextTask(b *testing.B) {
  50. var (
  51. mux sync.RWMutex
  52. wg = sync.WaitGroup{}
  53. ConMap = make(map[int64]struct{})
  54. )
  55. Audit := func(w *sync.WaitGroup, uid int64) {
  56. defer w.Done()
  57. for {
  58. tl, err := svr.Next(context.TODO(), uid)
  59. if err != nil {
  60. panic(err)
  61. }
  62. if tl == nil {
  63. fmt.Println("任务领取完成")
  64. return
  65. }
  66. mux.RLock()
  67. _, ok := ConMap[tl.ID]
  68. mux.RUnlock()
  69. if ok {
  70. panic(fmt.Sprintf("%d 重复下发:%d", uid, tl.ID))
  71. } else {
  72. mux.Lock()
  73. ConMap[tl.ID] = struct{}{}
  74. mux.Unlock()
  75. }
  76. fmt.Printf("uid=%d 领取任务:%d, weight=%d\n", uid, tl.ID, tl.Weight)
  77. time.Sleep(time.Millisecond * 50)
  78. if err = svr.Delay(context.TODO(), tl.ID, uid, "test-reson"); err != nil {
  79. panic(err)
  80. }
  81. }
  82. }
  83. for i := 1; i <= 10; i++ {
  84. wg.Add(1)
  85. go Audit(&wg, int64(i))
  86. }
  87. wg.Wait()
  88. }
  89. func TestFree(t *testing.T) {
  90. var c = context.TODO()
  91. rows := svr.Free(c, 481)
  92. if rows == 0 {
  93. t.Fail()
  94. }
  95. t.Fail()
  96. }
  97. func Test_setWeightConf(t *testing.T) {
  98. err := svr.setWeightConf(context.TODO(), "7", map[int64]*archive.WCItem{
  99. 7: {
  100. Radio: 4,
  101. Weight: 18,
  102. Mtime: utils.NewFormatTime(time.Now()),
  103. Desc: "指派回流任务",
  104. },
  105. })
  106. if err != nil {
  107. t.Fatal(err)
  108. }
  109. }