github.com/weedge/lib@v0.0.0-20230424045628-a36dcc1d90e4/container/queue/example_delay_queue_test.go (about) 1 package queue 2 3 import ( 4 "fmt" 5 "time" 6 ) 7 8 type Task struct { 9 Id int 10 Exp int64 11 } 12 13 func (t *Task) Do() { 14 fmt.Println(t.Id, t.Exp) 15 } 16 17 func ExampleDelayQueue_Ops() { 18 dq := NewDelayQueue(10) 19 20 pollExitCh, doExitCh := make(chan struct{}), make(chan struct{}) 21 go dq.Poll(pollExitCh, func() int64 { return 1000 }) 22 go dq.Do(doExitCh, func(item interface{}) { 23 task := item.(*Task) 24 task.Do() 25 }) 26 27 tasks := []*Task{ 28 {Id: 2, Exp: 2000}, 29 {Id: 3, Exp: 3000}, 30 {Id: 1, Exp: 1000}, 31 {Id: 4, Exp: 4000}, 32 } 33 34 for _, task := range tasks { 35 dq.Offer(task, task.Exp) 36 } 37 38 time.Sleep(3 * time.Second) 39 40 pollExitCh <- struct{}{} 41 doExitCh <- struct{}{} 42 43 // Output: 44 // 1 1000 45 }