github.com/suiyunonghen/DxCommonLib@v0.5.3/worker_test.go (about) 1 package DxCommonLib 2 3 import ( 4 "fmt" 5 "testing" 6 "time" 7 ) 8 9 func worktest(data ...interface{}) { 10 if len(data) < 3 { 11 Sleep(time.Second) 12 } 13 fmt.Println(data...) 14 } 15 16 func TestWorker(t *testing.T) { 17 work := NewWorkers(100, 0) 18 for i := 0; i <= 10; i++ { 19 work.PostFunc(worktest, 3, 4) 20 work.PostFunc(worktest, 3, 4, 6, 786, 867) 21 work.PostFunc(worktest, 345, 34, "$56456") 22 } 23 <-After(time.Second * 12) 24 fmt.Println("准备关闭") 25 work.Stop() 26 } 27 28 func TestTimeWheelWorker_After(t *testing.T) { 29 30 mm := NewTimeWheelWorker(time.Millisecond*2, 6000) //目前只能精确到2毫秒,低于2毫秒,就不行了 31 start := time.Now() 32 mm.AfterFunc(time.Second, func(data ...interface{}) { 33 fmt.Println("一秒之后执行,", time.Now().Sub(start).String()) 34 }, start) 35 c1 := mm.After(time.Second * 8) 36 c4 := mm.After(time.Second * 2) 37 //mm.Sleep(time.Millisecond * 980) 38 c2 := mm.After(time.Second * 1) 39 c3 := mm.After(time.Second * 18) 40 c11 := mm.After(time.Millisecond * 500) 41 c12 := mm.After(time.Millisecond * 500) 42 mm.AfterFunc(time.Millisecond*500, func(data ...interface{}) { 43 fmt.Println("500毫秒之后执行,", time.Now().Sub(start).String()) 44 }, start) 45 46 go func() { 47 select { 48 case <-c11: 49 fmt.Println("C11触发:", time.Now().Sub(start)) 50 } 51 }() 52 53 go func() { 54 select { 55 case <-c12: 56 fmt.Println("C12触发:", time.Now().Sub(start)) 57 } 58 }() 59 60 c5 := mm.After(time.Second * 2) 61 go func() { 62 select { 63 case <-c2: 64 fmt.Println("C2 1秒触发:", time.Now().Sub(start)) 65 } 66 }() 67 68 go func() { 69 select { 70 case <-c4: 71 fmt.Println("C4 2s后触发:", time.Now().Sub(start)) 72 } 73 }() 74 75 go func() { 76 select { 77 case <-c5: 78 fmt.Println("C5触发:", time.Now().Sub(start)) 79 } 80 }() 81 82 go func() { 83 select { 84 case <-c3: 85 fmt.Println("C3触发:", time.Now().Sub(start)) 86 } 87 }() 88 89 go func() { 90 select { 91 case <-c1: 92 fmt.Println("C1触发:", time.Now().Sub(start)) 93 } 94 }() 95 96 time.Sleep(time.Second * 10) 97 }