github.com/angenalZZZ/gofunc@v0.0.0-20210507121333-48ff1be3917b/http/longpoll/priority_queue_test.go (about) 1 package longpoll 2 3 import ( 4 "container/heap" 5 "container/list" 6 "testing" 7 "time" 8 ) 9 10 func Test_priorityQueue_Len(t *testing.T) { 11 pq := make(priorityQueue, 0) 12 if pq.Len() != 0 { 13 t.Errorf("priorityQueue had unexpected Len(). was: %d, expected: %d", 14 pq.Len(), 0) 15 } 16 // add an item 17 now_ms := time.Now().UnixNano() / int64(time.Millisecond) 18 buf := &eventBuffer{ 19 list.New(), 20 100, 21 now_ms, 22 } 23 expiringBuf := &expiringBuffer{ 24 eventBuffer_ptr: buf, 25 category: "some random category", 26 priority: now_ms, 27 } 28 heap.Push(&pq, expiringBuf) 29 if pq.Len() != 1 { 30 t.Errorf("priorityQueue had unexpected Len(). was: %d, expected: %d", 31 pq.Len(), 1) 32 } 33 // add another 34 buf2 := &eventBuffer{ 35 list.New(), 36 100, 37 now_ms, 38 } 39 expiringBuf2 := &expiringBuffer{ 40 eventBuffer_ptr: buf2, 41 category: "some different category", 42 priority: now_ms, 43 } 44 heap.Push(&pq, expiringBuf2) 45 if pq.Len() != 2 { 46 t.Errorf("priorityQueue had unexpected Len(). was: %d, expected: %d", 47 pq.Len(), 2) 48 } 49 // Remove an item 50 pq.Pop() 51 if pq.Len() != 1 { 52 t.Errorf("priorityQueue had unexpected Len(). was: %d, expected: %d", 53 pq.Len(), 1) 54 } 55 pq.Pop() 56 if pq.Len() != 0 { 57 t.Errorf("priorityQueue had unexpected Len(). was: %d, expected: %d", 58 pq.Len(), 0) 59 } 60 } 61 62 func Test_priorityQueue(t *testing.T) { 63 now_ms := time.Now().UnixNano() / int64(time.Millisecond) 64 pq := make(priorityQueue, 0) 65 heap.Init(&pq) 66 67 if _, e := pq.peakTopPriority(); e == nil { 68 t.Errorf("No error returned when calling peakTopPriority on an empty priorityQueue") 69 } 70 71 buf1 := &eventBuffer{ 72 list.New(), 73 100, 74 now_ms, 75 } 76 expiringBuf1 := &expiringBuffer{ 77 eventBuffer_ptr: buf1, 78 category: "some random category", 79 priority: 10003, // lower number is a higher ( priority 1 > priority 2) 80 } 81 heap.Push(&pq, expiringBuf1) 82 if p, e := pq.peakTopPriority(); e != nil { 83 t.Errorf("Error returned when calling peakTopPriority: %v", e) 84 } else { 85 if p != 10003 { 86 t.Errorf("Unexpected peakTopPriority result. was: %d, expected: %d.", 87 10003, p) 88 } 89 } 90 91 buf2 := &eventBuffer{ 92 list.New(), 93 100, 94 now_ms, 95 } 96 expiringBuf2 := &expiringBuffer{ 97 eventBuffer_ptr: buf2, 98 category: "some random category", 99 priority: 10001, 100 } 101 heap.Push(&pq, expiringBuf2) 102 if p, e := pq.peakTopPriority(); e != nil { 103 t.Errorf("Error returned when calling peakTopPriority: %v", e) 104 } else { 105 if p != 10001 { 106 t.Errorf("Unexpected peakTopPriority result. was: %d, expected: %d.", 107 10001, p) 108 } 109 } 110 111 buf3 := &eventBuffer{ 112 list.New(), 113 100, 114 now_ms, 115 } 116 expiringBuf3 := &expiringBuffer{ 117 eventBuffer_ptr: buf3, 118 category: "some random category", 119 priority: 10051, 120 } 121 heap.Push(&pq, expiringBuf3) 122 if p, e := pq.peakTopPriority(); e != nil { 123 t.Errorf("Error returned when calling peakTopPriority: %v", e) 124 } else { 125 if p != 10001 { 126 t.Errorf("Unexpected peakTopPriority result. was: %d, expected: %d.", 127 10001, p) 128 } 129 } 130 131 buf4 := &eventBuffer{ 132 list.New(), 133 100, 134 now_ms, 135 } 136 expiringBuf4 := &expiringBuffer{ 137 eventBuffer_ptr: buf4, 138 category: "some random category", 139 priority: 10011, 140 } 141 heap.Push(&pq, expiringBuf4) 142 if p, e := pq.peakTopPriority(); e != nil { 143 t.Errorf("Error returned when calling peakTopPriority: %v", e) 144 } else { 145 if p != 10001 { 146 t.Errorf("Unexpected peakTopPriority result. was: %d, expected: %d.", 147 10001, p) 148 } 149 } 150 151 if item := heap.Pop(&pq).(*expiringBuffer); item != expiringBuf2 { 152 t.Errorf("Expected popped item != expiringBuf2") 153 } 154 if p, _ := pq.peakTopPriority(); p != 10003 { 155 t.Errorf("Unexpected peakTopPriority result. was: %d, expected: %d.", 156 10003, p) 157 } 158 159 if item := heap.Pop(&pq).(*expiringBuffer); item != expiringBuf1 { 160 t.Errorf("Expected popped item != expiringBuf1") 161 } 162 if p, _ := pq.peakTopPriority(); p != 10011 { 163 t.Errorf("Unexpected peakTopPriority result. was: %d, expected: %d.", 164 10011, p) 165 } 166 167 // Now stir the pot by updating expiringBuf3 to higher priority than expiringBuf4 168 pq.updatePriority(expiringBuf3, 10008) 169 if p, _ := pq.peakTopPriority(); p != 10008 { 170 t.Errorf("Unexpected peakTopPriority result. was: %d, expected: %d.", 171 10008, p) 172 } 173 174 if item := heap.Pop(&pq).(*expiringBuffer); item != expiringBuf3 { 175 t.Errorf("Expected popped item != expiringBuf3") 176 } 177 if p, _ := pq.peakTopPriority(); p != 10011 { 178 t.Errorf("Unexpected peakTopPriority result. was: %d, expected: %d.", 179 10011, p) 180 } 181 if item := heap.Pop(&pq).(*expiringBuffer); item != expiringBuf4 { 182 t.Errorf("Expected popped item != expiringBuf4") 183 } 184 185 if _, e := pq.peakTopPriority(); e == nil { 186 t.Errorf("No error returned when calling peakTopPriority on an empty priorityQueue") 187 } 188 }