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.

31 lines
697 B

  1. /**
  2. * @Author: Aceld
  3. * @Date: 2019/5/5 10:14
  4. * @Mail: danbing.at@gmail.com
  5. *
  6. * 针对timer.go做单元测试主要测试定时器相关接口 依赖模块delayFunc.go
  7. */
  8. package ztimer
  9. import (
  10. "fmt"
  11. "testing"
  12. "time"
  13. )
  14. //定义一个超时函数
  15. func myFunc(v ...interface{}) {
  16. fmt.Printf("No.%d function calld. delay %d second(s)\n", v[0].(int), v[1].(int))
  17. }
  18. func TestTimer(t *testing.T) {
  19. for i:=0; i < 5;i ++ {
  20. go func(i int) {
  21. NewTimerAfter(NewDelayFunc(myFunc,[]interface{}{i, 2*i}), time.Duration(2*i)*time.Second).Run()
  22. }(i)
  23. }
  24. //主进程等待其他go,由于Run()方法是用一个新的go承载延迟方法,这里不能用waitGroup
  25. time.Sleep(1*time.Minute)
  26. }