github.com/suiyunonghen/dxcommonlib@v0.0.0-20190612012041-7f8547a81a67/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 mm := NewTimeWheelWorker(time.Millisecond*2,6000,nil)//目前只能精确到2毫秒,低于2毫秒,就不行了 30 fmt.Println(time.Now()) 31 c1 := mm.After(time.Second * 18) 32 c2 := mm.After(time.Second * 8) 33 c3 := mm.After(time.Second * 18) 34 c4 := mm.After(time.Millisecond * 500) 35 c5 := mm.After(time.Second * 2) 36 go func() { 37 select{ 38 case <-c2: 39 fmt.Println("C2触发:") 40 fmt.Println(time.Now()) 41 } 42 }() 43 44 go func() { 45 select{ 46 case <-c4: 47 fmt.Println("C4触发:") 48 fmt.Println(time.Now()) 49 } 50 }() 51 52 go func() { 53 select{ 54 case <-c5: 55 fmt.Println("C5触发:") 56 fmt.Println(time.Now()) 57 } 58 }() 59 60 go func() { 61 select{ 62 case <-c3: 63 fmt.Println("C3触发:") 64 fmt.Println(time.Now()) 65 } 66 }() 67 68 go func() { 69 select{ 70 case <-c1: 71 fmt.Println("C1触发:") 72 fmt.Println(time.Now()) 73 } 74 }() 75 76 <-c1 77 }