Brad Fitzpatrick | 03abeab | 2014-11-11 11:55:55 -0800 | [diff] [blame] | 1 | // Copyright 2014 The Go Authors. |
| 2 | // See https://code.google.com/p/go/source/browse/CONTRIBUTORS |
| 3 | // Licensed under the same terms as Go itself: |
| 4 | // https://code.google.com/p/go/source/browse/LICENSE |
| 5 | |
| 6 | package http2 |
| 7 | |
| 8 | import ( |
| 9 | "testing" |
| 10 | "time" |
| 11 | ) |
| 12 | |
Brad Fitzpatrick | 03abeab | 2014-11-11 11:55:55 -0800 | [diff] [blame] | 13 | func TestFlow(t *testing.T) { |
| 14 | f := newFlow(10) |
| 15 | if got, want := f.cur(), int32(10); got != want { |
| 16 | t.Fatalf("size = %d; want %d", got, want) |
| 17 | } |
Brad Fitzpatrick | 4c687c6 | 2014-11-23 00:07:43 -0800 | [diff] [blame] | 18 | if got, want := f.wait(1), int32(1); got != want { |
| 19 | t.Errorf("wait = %d; want %d", got, want) |
Brad Fitzpatrick | 03abeab | 2014-11-11 11:55:55 -0800 | [diff] [blame] | 20 | } |
| 21 | if got, want := f.cur(), int32(9); got != want { |
| 22 | t.Fatalf("size = %d; want %d", got, want) |
| 23 | } |
Brad Fitzpatrick | 4c687c6 | 2014-11-23 00:07:43 -0800 | [diff] [blame] | 24 | if got, want := f.wait(20), int32(9); got != want { |
| 25 | t.Errorf("wait = %d; want %d", got, want) |
| 26 | } |
| 27 | if got, want := f.cur(), int32(0); got != want { |
| 28 | t.Fatalf("size = %d; want %d", got, want) |
| 29 | } |
Brad Fitzpatrick | 03abeab | 2014-11-11 11:55:55 -0800 | [diff] [blame] | 30 | |
| 31 | // Wait for 10, which should block, so start a background goroutine |
| 32 | // to refill it. |
| 33 | go func() { |
| 34 | time.Sleep(50 * time.Millisecond) |
| 35 | f.add(50) |
| 36 | }() |
Brad Fitzpatrick | 4c687c6 | 2014-11-23 00:07:43 -0800 | [diff] [blame] | 37 | if got, want := f.wait(1), int32(1); got != want { |
| 38 | t.Errorf("after block, got %d; want %d", got, want) |
Brad Fitzpatrick | 03abeab | 2014-11-11 11:55:55 -0800 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | if got, want := f.cur(), int32(49); got != want { |
| 42 | t.Fatalf("size = %d; want %d", got, want) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | func TestFlowAdd(t *testing.T) { |
| 47 | f := newFlow(0) |
| 48 | if !f.add(1) { |
| 49 | t.Fatal("failed to add 1") |
| 50 | } |
| 51 | if !f.add(-1) { |
| 52 | t.Fatal("failed to add -1") |
| 53 | } |
| 54 | if got, want := f.cur(), int32(0); got != want { |
| 55 | t.Fatalf("size = %d; want %d", got, want) |
| 56 | } |
| 57 | if !f.add(1<<31 - 1) { |
| 58 | t.Fatal("failed to add 2^31-1") |
| 59 | } |
| 60 | if got, want := f.cur(), int32(1<<31-1); got != want { |
| 61 | t.Fatalf("size = %d; want %d", got, want) |
| 62 | } |
| 63 | if f.add(1) { |
| 64 | t.Fatal("adding 1 to max shouldn't be allowed") |
| 65 | } |
| 66 | |
| 67 | } |
Brad Fitzpatrick | 6d3aa4f | 2014-11-15 14:55:57 -0800 | [diff] [blame] | 68 | |
| 69 | func TestFlowClose(t *testing.T) { |
| 70 | f := newFlow(0) |
| 71 | |
| 72 | // Wait for 10, which should block, so start a background goroutine |
| 73 | // to refill it. |
| 74 | go func() { |
| 75 | time.Sleep(50 * time.Millisecond) |
| 76 | f.close() |
| 77 | }() |
Brad Fitzpatrick | 4c687c6 | 2014-11-23 00:07:43 -0800 | [diff] [blame] | 78 | gotc := make(chan int32) |
Brad Fitzpatrick | 6d3aa4f | 2014-11-15 14:55:57 -0800 | [diff] [blame] | 79 | go func() { |
Brad Fitzpatrick | 4c687c6 | 2014-11-23 00:07:43 -0800 | [diff] [blame] | 80 | gotc <- f.wait(1) |
Brad Fitzpatrick | 6d3aa4f | 2014-11-15 14:55:57 -0800 | [diff] [blame] | 81 | }() |
| 82 | select { |
Brad Fitzpatrick | 4c687c6 | 2014-11-23 00:07:43 -0800 | [diff] [blame] | 83 | case got := <-gotc: |
| 84 | if got != 0 { |
| 85 | t.Errorf("got %d; want 0", got) |
| 86 | } |
Brad Fitzpatrick | 6d3aa4f | 2014-11-15 14:55:57 -0800 | [diff] [blame] | 87 | case <-time.After(2 * time.Second): |
| 88 | t.Error("timeout") |
| 89 | } |
| 90 | } |