priority_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright 2014 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package http2
  5. import (
  6. "testing"
  7. )
  8. func TestPriority(t *testing.T) {
  9. // A -> B
  10. // move A's parent to B
  11. streams := make(map[uint32]*stream)
  12. a := &stream{
  13. parent: nil,
  14. weight: 16,
  15. }
  16. streams[1] = a
  17. b := &stream{
  18. parent: a,
  19. weight: 16,
  20. }
  21. streams[2] = b
  22. adjustStreamPriority(streams, 1, PriorityParam{
  23. Weight: 20,
  24. StreamDep: 2,
  25. })
  26. if a.parent != b {
  27. t.Errorf("Expected A's parent to be B")
  28. }
  29. if a.weight != 20 {
  30. t.Errorf("Expected A's weight to be 20; got %d", a.weight)
  31. }
  32. if b.parent != nil {
  33. t.Errorf("Expected B to have no parent")
  34. }
  35. if b.weight != 16 {
  36. t.Errorf("Expected B's weight to be 16; got %d", b.weight)
  37. }
  38. }
  39. func TestPriorityExclusiveZero(t *testing.T) {
  40. // A B and C are all children of the 0 stream.
  41. // Exclusive reprioritization to any of the streams
  42. // should bring the rest of the streams under the
  43. // reprioritized stream
  44. streams := make(map[uint32]*stream)
  45. a := &stream{
  46. parent: nil,
  47. weight: 16,
  48. }
  49. streams[1] = a
  50. b := &stream{
  51. parent: nil,
  52. weight: 16,
  53. }
  54. streams[2] = b
  55. c := &stream{
  56. parent: nil,
  57. weight: 16,
  58. }
  59. streams[3] = c
  60. adjustStreamPriority(streams, 3, PriorityParam{
  61. Weight: 20,
  62. StreamDep: 0,
  63. Exclusive: true,
  64. })
  65. if a.parent != c {
  66. t.Errorf("Expected A's parent to be C")
  67. }
  68. if a.weight != 16 {
  69. t.Errorf("Expected A's weight to be 16; got %d", a.weight)
  70. }
  71. if b.parent != c {
  72. t.Errorf("Expected B's parent to be C")
  73. }
  74. if b.weight != 16 {
  75. t.Errorf("Expected B's weight to be 16; got %d", b.weight)
  76. }
  77. if c.parent != nil {
  78. t.Errorf("Expected C to have no parent")
  79. }
  80. if c.weight != 20 {
  81. t.Errorf("Expected C's weight to be 20; got %d", b.weight)
  82. }
  83. }
  84. func TestPriorityOwnParent(t *testing.T) {
  85. streams := make(map[uint32]*stream)
  86. a := &stream{
  87. parent: nil,
  88. weight: 16,
  89. }
  90. streams[1] = a
  91. b := &stream{
  92. parent: a,
  93. weight: 16,
  94. }
  95. streams[2] = b
  96. adjustStreamPriority(streams, 1, PriorityParam{
  97. Weight: 20,
  98. StreamDep: 1,
  99. })
  100. if a.parent != nil {
  101. t.Errorf("Expected A's parent to be nil")
  102. }
  103. if a.weight != 20 {
  104. t.Errorf("Expected A's weight to be 20; got %d", a.weight)
  105. }
  106. if b.parent != a {
  107. t.Errorf("Expected B's parent to be A")
  108. }
  109. if b.weight != 16 {
  110. t.Errorf("Expected B's weight to be 16; got %d", b.weight)
  111. }
  112. }