github.com/asynkron/protoactor-go@v0.0.0-20240308120642-ef91a6abee75/actor/priority_queue_test.go (about) 1 package actor 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 8 "github.com/asynkron/protoactor-go/internal/queue/goring" 9 "github.com/asynkron/protoactor-go/internal/queue/mpsc" 10 ) 11 12 type Message interface { 13 GetMessage() string 14 } 15 16 type TestPriorityMessage struct { 17 message string 18 priority int8 19 } 20 21 type TestMessage struct { 22 message string 23 } 24 25 func (tpm *TestPriorityMessage) GetPriority() int8 { 26 return tpm.priority 27 } 28 29 func (tpm *TestPriorityMessage) GetMessage() string { 30 return tpm.message 31 } 32 33 func (tm *TestMessage) GetMessage() string { 34 return tm.message 35 } 36 37 func newTestGoringPriorityQueue() *priorityQueue { 38 return NewPriorityQueue(func() queue { 39 return &unboundedMailboxQueue{ 40 userMailbox: goring.New(1), 41 } 42 }) 43 } 44 45 func newTestMpscPriorityQueue() *priorityQueue { 46 return NewPriorityQueue(func() queue { 47 return mpsc.New() 48 }) 49 } 50 51 func TestPushPopGoring(t *testing.T) { 52 q := newTestGoringPriorityQueue() 53 q.Push("hello") 54 res := q.Pop() 55 assert.Equal(t, "hello", res) 56 } 57 58 func TestPushPopGoringPriority(t *testing.T) { 59 q := newTestGoringPriorityQueue() 60 61 // pushes 62 63 for i := 0; i < 2; i++ { 64 q.Push(&TestPriorityMessage{ 65 message: "7 hello", 66 priority: 7, 67 }) 68 } 69 70 for i := 0; i < 2; i++ { 71 q.Push(&TestPriorityMessage{ 72 message: "5 hello", 73 priority: 5, 74 }) 75 } 76 77 for i := 0; i < 2; i++ { 78 q.Push(&TestPriorityMessage{ 79 message: "0 hello", 80 priority: 0, 81 }) 82 } 83 84 for i := 0; i < 2; i++ { 85 q.Push(&TestPriorityMessage{ 86 message: "6 hello", 87 priority: 6, 88 }) 89 } 90 91 for i := 0; i < 2; i++ { 92 q.Push(&TestMessage{message: "hello"}) 93 } 94 95 // pops in priority order 96 97 for i := 0; i < 2; i++ { 98 res := q.Pop() 99 assert.Equal(t, "7 hello", res.(Message).GetMessage()) 100 } 101 102 for i := 0; i < 2; i++ { 103 res := q.Pop() 104 assert.Equal(t, "6 hello", res.(Message).GetMessage()) 105 } 106 107 for i := 0; i < 2; i++ { 108 res := q.Pop() 109 assert.Equal(t, "5 hello", res.(Message).GetMessage()) 110 } 111 112 for i := 0; i < 2; i++ { 113 res := q.Pop() 114 assert.Equal(t, "hello", res.(Message).GetMessage()) 115 } 116 117 for i := 0; i < 2; i++ { 118 res := q.Pop() 119 assert.Equal(t, "0 hello", res.(Message).GetMessage()) 120 } 121 } 122 123 func TestPushPopMpsc(t *testing.T) { 124 q := newTestMpscPriorityQueue() 125 q.Push("hello") 126 res := q.Pop() 127 assert.Equal(t, "hello", res) 128 } 129 130 func TestPushPopMpscPriority(t *testing.T) { 131 q := newTestMpscPriorityQueue() 132 133 // pushes 134 135 for i := 0; i < 2; i++ { 136 q.Push(&TestPriorityMessage{ 137 message: "7 hello", 138 priority: 7, 139 }) 140 } 141 142 for i := 0; i < 2; i++ { 143 q.Push(&TestPriorityMessage{ 144 message: "5 hello", 145 priority: 5, 146 }) 147 } 148 149 for i := 0; i < 2; i++ { 150 q.Push(&TestPriorityMessage{ 151 message: "0 hello", 152 priority: 0, 153 }) 154 } 155 156 for i := 0; i < 2; i++ { 157 q.Push(&TestPriorityMessage{ 158 message: "6 hello", 159 priority: 6, 160 }) 161 } 162 163 for i := 0; i < 2; i++ { 164 q.Push(&TestMessage{message: "hello"}) 165 } 166 167 // pops in priority order 168 169 for i := 0; i < 2; i++ { 170 res := q.Pop() 171 assert.Equal(t, "7 hello", res.(Message).GetMessage()) 172 } 173 174 for i := 0; i < 2; i++ { 175 res := q.Pop() 176 assert.Equal(t, "6 hello", res.(Message).GetMessage()) 177 } 178 179 for i := 0; i < 2; i++ { 180 res := q.Pop() 181 assert.Equal(t, "5 hello", res.(Message).GetMessage()) 182 } 183 184 for i := 0; i < 2; i++ { 185 res := q.Pop() 186 assert.Equal(t, "hello", res.(Message).GetMessage()) 187 } 188 189 for i := 0; i < 2; i++ { 190 res := q.Pop() 191 assert.Equal(t, "0 hello", res.(Message).GetMessage()) 192 } 193 }