You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.3 KiB

  1. /**
  2. * @Author: Aceld(刘丹冰)
  3. * @Date: 2019/5/9 10:14
  4. * @Mail: danbing.at@gmail.com
  5. *
  6. * 时间轮定时器调度器单元测试
  7. */
  8. package ztimer
  9. import (
  10. "fmt"
  11. "testing"
  12. "time"
  13. "zinx/zlog"
  14. )
  15. //触发函数
  16. func foo(args ...interface{}){
  17. fmt.Printf("I am No. %d function, delay %d ms\n", args[0].(int), args[1].(int))
  18. }
  19. //手动创建调度器运转时间轮
  20. func TestNewTimerScheduler(t *testing.T) {
  21. timerScheduler := NewTimerScheduler()
  22. timerScheduler.Start()
  23. //在scheduler中添加timer
  24. for i := 1; i < 2000; i ++ {
  25. f := NewDelayFunc(foo, []interface{}{i, i*3})
  26. tid, err := timerScheduler.CreateTimerAfter(f, time.Duration(3*i) * time.Millisecond)
  27. if err != nil {
  28. zlog.Error("create timer error", tid, err)
  29. break
  30. }
  31. }
  32. //执行调度器触发函数
  33. go func() {
  34. delayFuncChan := timerScheduler.GetTriggerChan()
  35. for df := range delayFuncChan {
  36. df.Call()
  37. }
  38. }()
  39. //阻塞等待
  40. select{}
  41. }
  42. //采用自动调度器运转时间轮
  43. func TestNewAutoExecTimerScheduler(t *testing.T) {
  44. autoTS := NewAutoExecTimerScheduler()
  45. //给调度器添加Timer
  46. for i := 0; i < 2000; i ++ {
  47. f := NewDelayFunc(foo, []interface{}{i, i*3})
  48. tid, err := autoTS.CreateTimerAfter(f, time.Duration(3*i) * time.Millisecond)
  49. if err != nil {
  50. zlog.Error("create timer error", tid, err)
  51. break
  52. }
  53. }
  54. //阻塞等待
  55. select{}
  56. }