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