|
1 | 1 | package kpq
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
4 | 5 | "errors"
|
5 | 6 | "fmt"
|
6 | 7 | "testing"
|
| 8 | + "time" |
7 | 9 | )
|
8 | 10 |
|
9 | 11 | func TestNewKeyedPriorityQueue_NilCmp(t *testing.T) {
|
10 | 12 | defer func() {
|
11 | 13 | if err := recover(); err == nil {
|
12 |
| - t.Error("want NewKeyedPriorityQueue to panic when receiving a nil comparison cunction") |
| 14 | + t.Error("want NewKeyedPriorityQueue to panic when receiving a nil comparison function") |
13 | 15 | }
|
14 | 16 | }()
|
15 | 17 |
|
@@ -217,7 +219,7 @@ func TestKeyedPriorityQueue_Pop(t *testing.T) {
|
217 | 219 | t.Run(fmt.Sprintf("%s_%d", tc.wantKey, tc.wantValue), func(t *testing.T) {
|
218 | 220 | gotKey, gotValue, ok := pq.Pop()
|
219 | 221 | if !ok {
|
220 |
| - t.Fatal("pq.Pop(): got unexpected empty prioriy queue") |
| 222 | + t.Fatal("pq.Pop(): got unexpected empty priority queue") |
221 | 223 | }
|
222 | 224 |
|
223 | 225 | if gotKey != tc.wantKey {
|
@@ -255,7 +257,171 @@ func TestKeyedPriorityQueue_Pop(t *testing.T) {
|
255 | 257 |
|
256 | 258 | _, _, ok := pq.Pop()
|
257 | 259 | if ok {
|
258 |
| - t.Errorf("pq.Pop(): got unexpected non-empty priorit queue") |
| 260 | + t.Errorf("pq.Pop(): got unexpected non-empty priority queue") |
| 261 | + } |
| 262 | + }) |
| 263 | +} |
| 264 | + |
| 265 | +func TestKeyedPriorityQueue_BlockingPop(t *testing.T) { |
| 266 | + t.Run("Block before pushing into queue", func(t *testing.T) { |
| 267 | + pq := NewKeyedPriorityQueue[string](func(x, y int) bool { |
| 268 | + return x < y |
| 269 | + }) |
| 270 | + |
| 271 | + items := []struct { |
| 272 | + key string |
| 273 | + val int |
| 274 | + }{ |
| 275 | + {key: "fourth", val: 10}, |
| 276 | + {key: "second", val: 8}, |
| 277 | + {key: "third", val: 9}, |
| 278 | + {key: "first", val: 6}, |
| 279 | + {key: "last", val: 20}, |
| 280 | + } |
| 281 | + |
| 282 | + go func() { |
| 283 | + time.Sleep(10 * time.Millisecond) // let reader block via BlockingPop() |
| 284 | + |
| 285 | + for _, item := range items { |
| 286 | + err := pq.Push(item.key, item.val) |
| 287 | + if err != nil { |
| 288 | + panic(fmt.Sprintf("Push(%v, %v): got unexpected error %v", item.key, item.val, err)) |
| 289 | + } |
| 290 | + } |
| 291 | + }() |
| 292 | + |
| 293 | + testCases := []struct { |
| 294 | + wantKey string |
| 295 | + wantValue int |
| 296 | + wantPeekKey string |
| 297 | + wantPeekValue int |
| 298 | + wantLen int |
| 299 | + }{ |
| 300 | + { |
| 301 | + wantKey: "fourth", // this element was put first into queue, when reader was already blocked |
| 302 | + wantValue: 10, |
| 303 | + wantPeekKey: "first", // this is an already sorted element in the queue |
| 304 | + wantPeekValue: 6, |
| 305 | + wantLen: 4, |
| 306 | + }, |
| 307 | + { |
| 308 | + wantKey: "first", |
| 309 | + wantValue: 6, |
| 310 | + wantPeekKey: "second", |
| 311 | + wantPeekValue: 8, |
| 312 | + wantLen: 3, |
| 313 | + }, |
| 314 | + } |
| 315 | + |
| 316 | + for _, tc := range testCases { |
| 317 | + t.Run(fmt.Sprintf("%s_%d", tc.wantKey, tc.wantValue), func(t *testing.T) { |
| 318 | + gotKey, gotValue, _ := pq.BlockingPop(context.Background()) |
| 319 | + |
| 320 | + if gotKey != tc.wantKey { |
| 321 | + t.Errorf("pq.BlockingPop(): got key %q; want %q", gotKey, tc.wantKey) |
| 322 | + } |
| 323 | + |
| 324 | + if gotValue != tc.wantValue { |
| 325 | + t.Errorf("pq.BlockingPop(): got value %d; want %d", gotValue, tc.wantValue) |
| 326 | + } |
| 327 | + |
| 328 | + time.Sleep(10 * time.Millisecond) // give queue a chance to process all the rest Push'es |
| 329 | + |
| 330 | + gotPeekKey, gotPeekValue, ok := pq.Peek() |
| 331 | + if !ok { |
| 332 | + t.Fatal("got no min key and value in the priority queue") |
| 333 | + } |
| 334 | + |
| 335 | + if gotPeekKey != tc.wantPeekKey { |
| 336 | + t.Errorf("pq.Peek(): got key %q; want %q", gotPeekKey, tc.wantPeekKey) |
| 337 | + } |
| 338 | + |
| 339 | + if gotPeekValue != tc.wantPeekValue { |
| 340 | + t.Errorf("pq.Peek(): got value %d; want %d", gotPeekValue, tc.wantPeekValue) |
| 341 | + } |
| 342 | + |
| 343 | + if got := pq.Len(); got != tc.wantLen { |
| 344 | + t.Errorf("pq.Len(): got %d; want %d", got, tc.wantLen) |
| 345 | + } |
| 346 | + }) |
| 347 | + } |
| 348 | + }) |
| 349 | + |
| 350 | + t.Run("Do not actually block as queue is not empty", func(t *testing.T) { |
| 351 | + pq := NewKeyedPriorityQueue[string](func(x, y int) bool { |
| 352 | + return x < y |
| 353 | + }) |
| 354 | + |
| 355 | + items := []struct { |
| 356 | + key string |
| 357 | + val int |
| 358 | + }{ |
| 359 | + {key: "fourth", val: 10}, |
| 360 | + {key: "second", val: 8}, |
| 361 | + {key: "third", val: 9}, |
| 362 | + {key: "first", val: 6}, |
| 363 | + {key: "last", val: 20}, |
| 364 | + } |
| 365 | + |
| 366 | + for _, item := range items { |
| 367 | + err := pq.Push(item.key, item.val) |
| 368 | + if err != nil { |
| 369 | + panic(fmt.Sprintf("Push(%v, %v): got unexpected error %v", item.key, item.val, err)) |
| 370 | + } |
| 371 | + } |
| 372 | + |
| 373 | + testCases := []struct { |
| 374 | + wantKey string |
| 375 | + wantValue int |
| 376 | + wantPeekKey string |
| 377 | + wantPeekValue int |
| 378 | + wantLen int |
| 379 | + }{ |
| 380 | + { |
| 381 | + wantKey: "first", |
| 382 | + wantValue: 6, |
| 383 | + wantPeekKey: "second", |
| 384 | + wantPeekValue: 8, |
| 385 | + wantLen: 4, |
| 386 | + }, |
| 387 | + { |
| 388 | + wantKey: "second", |
| 389 | + wantValue: 8, |
| 390 | + wantPeekKey: "third", |
| 391 | + wantPeekValue: 9, |
| 392 | + wantLen: 3, |
| 393 | + }, |
| 394 | + } |
| 395 | + |
| 396 | + for _, tc := range testCases { |
| 397 | + t.Run(fmt.Sprintf("%s_%d", tc.wantKey, tc.wantValue), func(t *testing.T) { |
| 398 | + gotKey, gotValue, _ := pq.BlockingPop(context.Background()) |
| 399 | + |
| 400 | + if gotKey != tc.wantKey { |
| 401 | + t.Errorf("pq.Pop(): got key %q; want %q", gotKey, tc.wantKey) |
| 402 | + } |
| 403 | + |
| 404 | + if gotValue != tc.wantValue { |
| 405 | + t.Errorf("pq.Pop(): got value %d; want %d", gotValue, tc.wantValue) |
| 406 | + } |
| 407 | + |
| 408 | + gotPeekKey, gotPeekValue, ok := pq.Peek() |
| 409 | + if !ok { |
| 410 | + t.Fatal("got no min key and value in the priority queue") |
| 411 | + } |
| 412 | + |
| 413 | + if gotPeekKey != tc.wantPeekKey { |
| 414 | + t.Errorf("pq.Peek(): got key %q; want %q", gotPeekKey, tc.wantPeekKey) |
| 415 | + } |
| 416 | + |
| 417 | + if gotPeekValue != tc.wantPeekValue { |
| 418 | + t.Errorf("pq.Peek(): got value %d; want %d", gotPeekValue, tc.wantPeekValue) |
| 419 | + } |
| 420 | + |
| 421 | + if got := pq.Len(); got != tc.wantLen { |
| 422 | + t.Errorf("pq.Len(): got %d; want %d", got, tc.wantLen) |
| 423 | + } |
| 424 | + }) |
259 | 425 | }
|
260 | 426 | })
|
261 | 427 | }
|
@@ -385,7 +551,7 @@ func TestKeyedPriorityQueue_PeekKey_EmptyQueue(t *testing.T) {
|
385 | 551 | }
|
386 | 552 | }
|
387 | 553 |
|
388 |
| -func TestKeyedPriorityQeue_Contains(t *testing.T) { |
| 554 | +func TestKeyedPriorityQueue_Contains(t *testing.T) { |
389 | 555 | pq := NewKeyedPriorityQueue[string](func(x, y int) bool { return x < y })
|
390 | 556 |
|
391 | 557 | k := "user"
|
|
0 commit comments