flow_test.go 1.4 KB

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