blob: 284ccbdb53578b61bc9160a1e3fe6aae0adcb82d [file] [log] [blame]
Brad Fitzpatrick03abeab2014-11-11 11:55:55 -08001// 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
6package http2
7
8import (
9 "testing"
10 "time"
11)
12
Brad Fitzpatrick03abeab2014-11-11 11:55:55 -080013func 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 Fitzpatrick4c687c62014-11-23 00:07:43 -080018 if got, want := f.wait(1), int32(1); got != want {
19 t.Errorf("wait = %d; want %d", got, want)
Brad Fitzpatrick03abeab2014-11-11 11:55:55 -080020 }
21 if got, want := f.cur(), int32(9); got != want {
22 t.Fatalf("size = %d; want %d", got, want)
23 }
Brad Fitzpatrick4c687c62014-11-23 00:07:43 -080024 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 Fitzpatrick03abeab2014-11-11 11:55:55 -080030
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 Fitzpatrick4c687c62014-11-23 00:07:43 -080037 if got, want := f.wait(1), int32(1); got != want {
38 t.Errorf("after block, got %d; want %d", got, want)
Brad Fitzpatrick03abeab2014-11-11 11:55:55 -080039 }
40
41 if got, want := f.cur(), int32(49); got != want {
42 t.Fatalf("size = %d; want %d", got, want)
43 }
44}
45
46func 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 Fitzpatrick6d3aa4f2014-11-15 14:55:57 -080068
69func 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 Fitzpatrick4c687c62014-11-23 00:07:43 -080078 gotc := make(chan int32)
Brad Fitzpatrick6d3aa4f2014-11-15 14:55:57 -080079 go func() {
Brad Fitzpatrick4c687c62014-11-23 00:07:43 -080080 gotc <- f.wait(1)
Brad Fitzpatrick6d3aa4f2014-11-15 14:55:57 -080081 }()
82 select {
Brad Fitzpatrick4c687c62014-11-23 00:07:43 -080083 case got := <-gotc:
84 if got != 0 {
85 t.Errorf("got %d; want 0", got)
86 }
Brad Fitzpatrick6d3aa4f2014-11-15 14:55:57 -080087 case <-time.After(2 * time.Second):
88 t.Error("timeout")
89 }
90}