priority_test.go 2.5 KB

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