1
2
3
4
5
6
7 package quic
8
9 import (
10 "context"
11 "io"
12 "testing"
13 "time"
14 )
15
16 func TestQueue(t *testing.T) {
17 nonblocking, cancel := context.WithCancel(context.Background())
18 cancel()
19
20 q := newQueue[int]()
21 if got, err := q.get(nonblocking, nil); err != context.Canceled {
22 t.Fatalf("q.get() = %v, %v, want nil, contex.Canceled", got, err)
23 }
24
25 if !q.put(1) {
26 t.Fatalf("q.put(1) = false, want true")
27 }
28 if !q.put(2) {
29 t.Fatalf("q.put(2) = false, want true")
30 }
31 if got, err := q.get(nonblocking, nil); got != 1 || err != nil {
32 t.Fatalf("q.get() = %v, %v, want 1, nil", got, err)
33 }
34 if got, err := q.get(nonblocking, nil); got != 2 || err != nil {
35 t.Fatalf("q.get() = %v, %v, want 2, nil", got, err)
36 }
37 if got, err := q.get(nonblocking, nil); err != context.Canceled {
38 t.Fatalf("q.get() = %v, %v, want nil, contex.Canceled", got, err)
39 }
40
41 go func() {
42 time.Sleep(1 * time.Millisecond)
43 q.put(3)
44 }()
45 if got, err := q.get(context.Background(), nil); got != 3 || err != nil {
46 t.Fatalf("q.get() = %v, %v, want 3, nil", got, err)
47 }
48
49 if !q.put(4) {
50 t.Fatalf("q.put(2) = false, want true")
51 }
52 q.close(io.EOF)
53 if got, err := q.get(context.Background(), nil); got != 0 || err != io.EOF {
54 t.Fatalf("q.get() = %v, %v, want 0, io.EOF", got, err)
55 }
56 if q.put(5) {
57 t.Fatalf("q.put(5) = true, want false")
58 }
59 }
60
View as plain text