flow_test.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. package http2
  6. import (
  7. "testing"
  8. "time"
  9. )
  10. func (f *flow) cur() int32 {
  11. f.c.L.Lock()
  12. defer f.c.L.Unlock()
  13. return f.size
  14. }
  15. func TestFlow(t *testing.T) {
  16. f := newFlow(10)
  17. if got, want := f.cur(), int32(10); got != want {
  18. t.Fatalf("size = %d; want %d", got, want)
  19. }
  20. if waits := f.acquire(1); waits != 0 {
  21. t.Errorf("waits = %d; want 0", waits)
  22. }
  23. if got, want := f.cur(), int32(9); got != want {
  24. t.Fatalf("size = %d; want %d", got, want)
  25. }
  26. // Wait for 10, which should block, so start a background goroutine
  27. // to refill it.
  28. go func() {
  29. time.Sleep(50 * time.Millisecond)
  30. f.add(50)
  31. }()
  32. if waits := f.acquire(10); waits != 1 {
  33. t.Errorf("waits for 50 = %d; want 0", waits)
  34. }
  35. if got, want := f.cur(), int32(49); got != want {
  36. t.Fatalf("size = %d; want %d", got, want)
  37. }
  38. }
  39. func TestFlowAdd(t *testing.T) {
  40. f := newFlow(0)
  41. if !f.add(1) {
  42. t.Fatal("failed to add 1")
  43. }
  44. if !f.add(-1) {
  45. t.Fatal("failed to add -1")
  46. }
  47. if got, want := f.cur(), int32(0); got != want {
  48. t.Fatalf("size = %d; want %d", got, want)
  49. }
  50. if !f.add(1<<31 - 1) {
  51. t.Fatal("failed to add 2^31-1")
  52. }
  53. if got, want := f.cur(), int32(1<<31-1); got != want {
  54. t.Fatalf("size = %d; want %d", got, want)
  55. }
  56. if f.add(1) {
  57. t.Fatal("adding 1 to max shouldn't be allowed")
  58. }
  59. }