task_control_test.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. package task_control
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/allanpk716/ChineseSubFinder/internal/pkg/log_helper"
  6. "github.com/sirupsen/logrus"
  7. "golang.org/x/net/context"
  8. "sync"
  9. "testing"
  10. "time"
  11. )
  12. func TestTaskControl_Invoke(t *testing.T) {
  13. type args struct {
  14. timeTester TimeTester
  15. }
  16. tests := []struct {
  17. name string
  18. args args
  19. successProcessCount int
  20. wantErr bool
  21. }{
  22. // 不超时的情况
  23. {
  24. name: "00", args: args{
  25. TimeTester{PoolName: "00",
  26. ConcurrentCount: 1,
  27. JobCount: 5,
  28. OneJobWaitTime: 1,
  29. OneJobTimeOut: 2,
  30. }},
  31. successProcessCount: 5,
  32. },
  33. {
  34. name: "01", args: args{
  35. TimeTester{PoolName: "01",
  36. ConcurrentCount: 2,
  37. JobCount: 5,
  38. OneJobWaitTime: 1,
  39. OneJobTimeOut: 2,
  40. }},
  41. successProcessCount: 5,
  42. },
  43. {
  44. name: "02", args: args{
  45. TimeTester{PoolName: "02",
  46. ConcurrentCount: 3,
  47. JobCount: 5,
  48. OneJobWaitTime: 1,
  49. OneJobTimeOut: 2,
  50. }},
  51. successProcessCount: 5,
  52. },
  53. // 超时的情况
  54. {
  55. name: "03", args: args{
  56. TimeTester{PoolName: "03",
  57. ConcurrentCount: 2,
  58. JobCount: 5,
  59. TimeAfterRelease: 5,
  60. OneJobWaitTime: 2,
  61. OneJobTimeOut: 1,
  62. NeedRelease: true}},
  63. successProcessCount: 0,
  64. },
  65. {
  66. name: "04", args: args{
  67. TimeTester{PoolName: "04",
  68. ConcurrentCount: 2,
  69. JobCount: 5,
  70. TimeAfterRelease: 5,
  71. OneJobWaitTime: 2,
  72. OneJobTimeOut: 1,
  73. NeedRelease: false}},
  74. successProcessCount: 0,
  75. },
  76. {
  77. name: "05", args: args{
  78. TimeTester{PoolName: "05",
  79. ConcurrentCount: 2,
  80. JobCount: 5,
  81. TimeAfterRelease: 5,
  82. OneJobWaitTime: 2,
  83. OneJobTimeOut: 1,
  84. NeedRelease: true}},
  85. successProcessCount: 0,
  86. },
  87. // 主动触发 painic
  88. {
  89. name: "06", args: args{
  90. TimeTester{PoolName: "06",
  91. ConcurrentCount: 2,
  92. JobCount: 5,
  93. TimeAfterRelease: 5,
  94. OneJobWaitTime: 2,
  95. OneJobTimeOut: 1,
  96. NeedRelease: true,
  97. WantPanic: true}},
  98. successProcessCount: 0,
  99. },
  100. {
  101. name: "07", args: args{
  102. TimeTester{PoolName: "07",
  103. ConcurrentCount: 2,
  104. JobCount: 5,
  105. TimeAfterRelease: 5,
  106. OneJobWaitTime: 2,
  107. OneJobTimeOut: 1,
  108. NeedRelease: false,
  109. WantPanic: true}},
  110. successProcessCount: 0,
  111. },
  112. {
  113. name: "08", args: args{
  114. TimeTester{PoolName: "08",
  115. ConcurrentCount: 2,
  116. JobCount: 5,
  117. TimeAfterRelease: 5,
  118. OneJobWaitTime: 2,
  119. OneJobTimeOut: 1,
  120. NeedRelease: true,
  121. WantPanic: true}},
  122. successProcessCount: 0,
  123. },
  124. // 部分超时
  125. {
  126. name: "09", args: args{
  127. TimeTester{PoolName: "09",
  128. ConcurrentCount: 2,
  129. JobCount: 5,
  130. TimeAfterRelease: 5,
  131. OneJobWaitTime: 2,
  132. OneJobTimeOut: 3,
  133. NeedRelease: true,
  134. IndexOverThanAddMoreTime: 2}},
  135. successProcessCount: 3,
  136. },
  137. {
  138. name: "10", args: args{
  139. TimeTester{PoolName: "10",
  140. ConcurrentCount: 2,
  141. JobCount: 5,
  142. TimeAfterRelease: 5,
  143. OneJobWaitTime: 2,
  144. OneJobTimeOut: 3,
  145. NeedRelease: false,
  146. IndexOverThanAddMoreTime: 2}},
  147. successProcessCount: 3,
  148. },
  149. {
  150. name: "11", args: args{
  151. TimeTester{PoolName: "11",
  152. ConcurrentCount: 2,
  153. JobCount: 5,
  154. TimeAfterRelease: 5,
  155. OneJobWaitTime: 2,
  156. OneJobTimeOut: 3,
  157. NeedRelease: true,
  158. IndexOverThanAddMoreTime: 3}},
  159. successProcessCount: 4,
  160. },
  161. // 使用 Release 取消
  162. {
  163. name: "12", args: args{
  164. TimeTester{PoolName: "12",
  165. ConcurrentCount: 1,
  166. JobCount: 5,
  167. TimeAfterRelease: 2,
  168. OneJobWaitTime: 3,
  169. OneJobTimeOut: 4,
  170. NeedRelease: true}},
  171. successProcessCount: 0,
  172. },
  173. {
  174. name: "13", args: args{
  175. TimeTester{PoolName: "13",
  176. ConcurrentCount: 2,
  177. JobCount: 5,
  178. TimeAfterRelease: 2,
  179. OneJobWaitTime: 3,
  180. OneJobTimeOut: 4,
  181. NeedRelease: true}},
  182. successProcessCount: 0,
  183. },
  184. {
  185. name: "14", args: args{
  186. TimeTester{PoolName: "14",
  187. ConcurrentCount: 2,
  188. JobCount: 5,
  189. TimeAfterRelease: 4,
  190. OneJobWaitTime: 3,
  191. OneJobTimeOut: 4,
  192. NeedRelease: true}},
  193. successProcessCount: 2,
  194. },
  195. {
  196. name: "15", args: args{
  197. TimeTester{PoolName: "15",
  198. ConcurrentCount: 1,
  199. JobCount: 5,
  200. TimeAfterRelease: 3,
  201. OneJobWaitTime: 2,
  202. OneJobTimeOut: 4,
  203. NeedRelease: true}},
  204. successProcessCount: 1,
  205. },
  206. {
  207. name: "16", args: args{
  208. TimeTester{PoolName: "16",
  209. ConcurrentCount: 3,
  210. JobCount: 5,
  211. TimeAfterRelease: 4,
  212. OneJobWaitTime: 3,
  213. OneJobTimeOut: 4,
  214. NeedRelease: true}},
  215. successProcessCount: 3,
  216. },
  217. {
  218. name: "17", args: args{
  219. TimeTester{PoolName: "17",
  220. ConcurrentCount: 4,
  221. JobCount: 5,
  222. TimeAfterRelease: 4,
  223. OneJobWaitTime: 3,
  224. OneJobTimeOut: 4,
  225. NeedRelease: true}},
  226. successProcessCount: 4,
  227. },
  228. {
  229. name: "18", args: args{
  230. TimeTester{PoolName: "18",
  231. ConcurrentCount: 5,
  232. JobCount: 5,
  233. TimeAfterRelease: 4,
  234. OneJobWaitTime: 3,
  235. OneJobTimeOut: 4,
  236. NeedRelease: true}},
  237. successProcessCount: 5,
  238. },
  239. }
  240. for _, tt := range tests {
  241. t.Run(tt.name, func(t *testing.T) {
  242. successList, _, _, err := process(tt.name, tt.args.timeTester)
  243. if err != nil {
  244. t.Fatal(err)
  245. }
  246. if tt.successProcessCount != len(successList) {
  247. t.Fatal("want successProcessCount =", tt.successProcessCount, "now =", len(successList))
  248. }
  249. })
  250. }
  251. }
  252. func process(name string, timeTester TimeTester) ([]int, []int, []int, error) {
  253. once := sync.Once{}
  254. tc, err := NewTaskControl(timeTester.ConcurrentCount, log_helper.NewLogHelper(name, logrus.DebugLevel, time.Duration(7*24)*time.Hour, time.Duration(24)*time.Hour))
  255. if err != nil {
  256. return nil, nil, nil, err
  257. }
  258. tc.SetCtxProcessFunc(timeTester.PoolName, waitTimes, timeTester.OneJobTimeOut)
  259. for i := 0; i < timeTester.JobCount; i++ {
  260. once.Do(func() {
  261. go func() {
  262. if timeTester.NeedRelease == false {
  263. tc.log.Infoln("Do not need Release")
  264. return
  265. }
  266. tc.log.Infoln("Release After", timeTester.TimeAfterRelease, "Second")
  267. time.Sleep(time.Duration(timeTester.TimeAfterRelease) * time.Second)
  268. tc.Release()
  269. }()
  270. })
  271. err := tc.Invoke(&TaskData{Index: i,
  272. DataEx: DataEx{
  273. OneJobWaitTime: timeTester.OneJobWaitTime,
  274. WantPanic: timeTester.WantPanic,
  275. IndexOverThanAddMoreTime: timeTester.IndexOverThanAddMoreTime,
  276. }})
  277. if err != nil {
  278. tc.log.Errorln("Index:", i, "Error", err)
  279. }
  280. }
  281. tc.Hold()
  282. fmt.Println("-------------------------------")
  283. // 获取提前终止的计数器以及完成的计数器
  284. successList, noExecuteList, errorList := tc.GetExecuteInfo()
  285. return successList, noExecuteList, errorList, nil
  286. }
  287. func waitTimes(ctx context.Context, inData interface{}) error {
  288. phase0 := make(chan interface{}, 1)
  289. index := inData.(*TaskData)
  290. dataEx := index.DataEx.(DataEx)
  291. if dataEx.WantPanic == true {
  292. panic("want panic")
  293. }
  294. go func() {
  295. fmt.Println("Index:", index.Index, "Start 0")
  296. if dataEx.IndexOverThanAddMoreTime == 0 {
  297. time.Sleep(time.Duration(dataEx.OneJobWaitTime) * time.Second)
  298. } else {
  299. if index.Index > dataEx.IndexOverThanAddMoreTime {
  300. time.Sleep(time.Duration(dataEx.OneJobWaitTime+10) * time.Second)
  301. } else {
  302. time.Sleep(time.Duration(dataEx.OneJobWaitTime) * time.Second)
  303. }
  304. }
  305. phase0 <- 1
  306. fmt.Println("Index:", index.Index, "End 0")
  307. }()
  308. select {
  309. case <-ctx.Done():
  310. {
  311. fmt.Println("Index:", index.Index, "timeout 0")
  312. return errors.New("timeout jump")
  313. }
  314. case <-phase0:
  315. break
  316. }
  317. fmt.Println("Index:", index.Index, "Start 1")
  318. fmt.Println("Index:", index.Index, "End 1")
  319. return nil
  320. }
  321. type TimeTester struct {
  322. PoolName string // 名称
  323. ConcurrentCount int // 并发数
  324. JobCount int // 总任务数
  325. TimeAfterRelease int // 开始后等待多久执行 Release 操作
  326. OneJobWaitTime int // 单个任务得耗时
  327. OneJobTimeOut int // 单个任务的超时时间
  328. NeedRelease bool // 是否需要主动执行 Release
  329. WantPanic bool // 触发 panic
  330. IndexOverThanAddMoreTime int // waitTimes函数中某个 Index 之后都会在等待处理上多加延时以便触发超时逻辑
  331. }
  332. type DataEx struct {
  333. OneJobWaitTime int
  334. WantPanic bool
  335. IndexOverThanAddMoreTime int
  336. }