github.com/songzhibin97/gkit@v1.2.13/delayed/do_test.go (about) 1 package delayed 2 3 import ( 4 "fmt" 5 "github.com/stretchr/testify/assert" 6 "os" 7 "testing" 8 "time" 9 ) 10 11 type mockDelayed struct { 12 exec int64 13 index int 14 } 15 16 func (m mockDelayed) Do() { 17 fmt.Println(m.index) 18 } 19 20 func (m mockDelayed) ExecTime() int64 { 21 return m.exec 22 } 23 24 func (m mockDelayed) Identify() string { 25 return "mock" 26 } 27 28 func nowMockDelayed(exec int64) Delayed { 29 return mockDelayed{exec: exec} 30 } 31 32 func TestDispatchingDelayed_AddDelayed(t *testing.T) { 33 34 type fields struct { 35 taskList []int64 36 expectation []int64 37 } 38 tests := []struct { 39 name string 40 fields fields 41 }{ 42 { 43 name: "顺序递增", 44 fields: fields{ 45 taskList: []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 46 expectation: []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 47 }, 48 }, 49 { 50 name: "顺序递减", 51 fields: fields{ 52 taskList: []int64{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}, 53 expectation: []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 54 }, 55 }, 56 { 57 name: "随机", 58 fields: fields{ 59 taskList: []int64{5, 4, 3, 2, 1, 6, 7, 8, 9, 10}, 60 expectation: []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 61 }, 62 }, 63 { 64 name: "随机", 65 fields: fields{ 66 taskList: []int64{10, 9, 8, 7, 6, 5, 1, 2, 3, 4}, 67 expectation: []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 68 }, 69 }, 70 71 { 72 name: "顺序递增(奇数)", 73 fields: fields{ 74 taskList: []int64{1, 2, 3, 4, 5, 6, 7, 8, 9}, 75 expectation: []int64{1, 2, 3, 4, 5, 6, 7, 8, 9}, 76 }, 77 }, 78 { 79 name: "顺序递减(奇数)", 80 fields: fields{ 81 taskList: []int64{9, 8, 7, 6, 5, 4, 3, 2, 1}, 82 expectation: []int64{1, 2, 3, 4, 5, 6, 7, 8, 9}, 83 }, 84 }, 85 { 86 name: "随机(奇数)", 87 fields: fields{ 88 taskList: []int64{5, 4, 3, 2, 1, 6, 7, 8, 9}, 89 expectation: []int64{1, 2, 3, 4, 5, 6, 7, 8, 9}, 90 }, 91 }, 92 { 93 name: "随机(奇数)", 94 fields: fields{ 95 taskList: []int64{9, 8, 7, 6, 5, 1, 2, 3, 4}, 96 expectation: []int64{1, 2, 3, 4, 5, 6, 7, 8, 9}, 97 }, 98 }, 99 } 100 for _, tt := range tests { 101 t.Run(tt.name, func(t *testing.T) { 102 n := DispatchingDelayed{} 103 var cur []int64 104 for _, task := range tt.fields.taskList { 105 n.AddDelayed(nowMockDelayed(task)) 106 } 107 for range n.delays { 108 cur = append(cur, n.delDelayedTop().ExecTime()) 109 } 110 assert.Equal(t, tt.fields.expectation, cur) 111 }) 112 } 113 } 114 115 func TestDispatchingDelayed_AddDelayed2(t *testing.T) { 116 117 n := NewDispatchingDelayed() 118 for i := 0; i < 10; i++ { 119 n.AddDelayed(mockDelayed{exec: time.Now().Add(time.Duration(i) * time.Second).Unix(), index: i}) 120 } 121 // print 0...9 122 time.Sleep(15 * time.Second) 123 } 124 125 func TestDispatchingDelayed_AddDelayed3(t *testing.T) { 126 n := NewDispatchingDelayed(SetSingleCallback(func(signal os.Signal, d *DispatchingDelayed) { 127 t.Log("signal") 128 })) 129 for i := 0; i < 10; i++ { 130 n.AddDelayed(mockDelayed{exec: time.Now().Add(time.Duration(i) * time.Second).Unix(), index: i}) 131 } 132 // print 0...9 133 time.Sleep(time.Minute) 134 }