github.com/glide-im/glide@v1.6.0/pkg/timingwheel/timingwheel_test.go (about) 1 package timingwheel 2 3 import ( 4 "fmt" 5 "github.com/stretchr/testify/assert" 6 "math/rand" 7 "strconv" 8 "testing" 9 "time" 10 ) 11 12 func TestNewTimingWheel(t *testing.T) { 13 14 tw := NewTimingWheel(time.Millisecond*100, 3, 20) 15 assert.NotNil(t, tw) 16 assert.Equal(t, 20, tw.wheel.slot.len) 17 assert.Equal(t, 400, tw.wheel.slotCap) 18 } 19 20 func (s *slot) tasks() [][]*Task { 21 sl := s 22 for sl.index != 0 { 23 sl = sl.next 24 } 25 var t [][]*Task 26 27 t = append(t, sl.valueArray()) 28 sl = sl.next 29 if sl.index != 0 { 30 t = append(t, sl.valueArray()) 31 sl = sl.next 32 } 33 t = append(t, sl.valueArray()) 34 return t 35 } 36 37 func (w *wheel) status() string { 38 var s []string 39 sl := w.slot 40 for ; sl.index != sl.len-1; sl = sl.next { 41 42 } 43 for i := 0; i != sl.len; i++ { 44 sl = sl.next 45 if sl.index == w.slot.index { 46 s = append(s, strconv.Itoa(i)) 47 continue 48 } 49 if sl.isEmpty() { 50 s = append(s, "_") 51 } else { 52 s = append(s, "#") 53 } 54 } 55 56 var ts []string 57 for _, tasks := range w.slot.tasks() { 58 var tt []string 59 for _, t := range tasks { 60 tt = append(tt, strconv.Itoa(t.offset)) 61 } 62 ts = append(ts, fmt.Sprintf("%v", tt)) 63 } 64 65 return fmt.Sprintf("%v %v %d", s, ts, w.remain) 66 } 67 68 func sleepRndMilleSec(start int32, end int32) { 69 n := rand.Int31n(end - start) 70 n = start + n 71 time.Sleep(time.Duration(n) * time.Millisecond) 72 }