flow_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 "testing"
  7. func TestFlow(t *testing.T) {
  8. var st flow
  9. var conn flow
  10. st.add(3)
  11. conn.add(2)
  12. if got, want := st.available(), int32(3); got != want {
  13. t.Errorf("available = %d; want %d", got, want)
  14. }
  15. st.setConnFlow(&conn)
  16. if got, want := st.available(), int32(2); got != want {
  17. t.Errorf("after parent setup, available = %d; want %d", got, want)
  18. }
  19. st.take(2)
  20. if got, want := conn.available(), int32(0); got != want {
  21. t.Errorf("after taking 2, conn = %d; want %d", got, want)
  22. }
  23. if got, want := st.available(), int32(0); got != want {
  24. t.Errorf("after taking 2, stream = %d; want %d", got, want)
  25. }
  26. }
  27. func TestFlowAdd(t *testing.T) {
  28. var f flow
  29. if !f.add(1) {
  30. t.Fatal("failed to add 1")
  31. }
  32. if !f.add(-1) {
  33. t.Fatal("failed to add -1")
  34. }
  35. if got, want := f.available(), int32(0); got != want {
  36. t.Fatalf("size = %d; want %d", got, want)
  37. }
  38. if !f.add(1<<31 - 1) {
  39. t.Fatal("failed to add 2^31-1")
  40. }
  41. if got, want := f.available(), int32(1<<31-1); got != want {
  42. t.Fatalf("size = %d; want %d", got, want)
  43. }
  44. if f.add(1) {
  45. t.Fatal("adding 1 to max shouldn't be allowed")
  46. }
  47. }