|
1 /* |
|
2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 package jdk.internal.net.http; |
|
25 |
|
26 import org.testng.annotations.Test; |
|
27 import static jdk.internal.net.http.frame.SettingsFrame.DEFAULT_INITIAL_WINDOW_SIZE; |
|
28 import static org.testng.Assert.assertEquals; |
|
29 import static org.testng.Assert.assertThrows; |
|
30 |
|
31 public class WindowControllerTest { |
|
32 |
|
33 @Test |
|
34 public void testConnectionWindowOverflow() { |
|
35 WindowController wc = new WindowController(); |
|
36 assertEquals(wc.connectionWindowSize(), DEFAULT_INITIAL_WINDOW_SIZE); |
|
37 assertEquals(wc.increaseConnectionWindow(Integer.MAX_VALUE), false); |
|
38 assertEquals(wc.increaseConnectionWindow(Integer.MAX_VALUE), false); |
|
39 assertEquals(wc.increaseConnectionWindow(Integer.MAX_VALUE), false); |
|
40 assertEquals(wc.connectionWindowSize(), DEFAULT_INITIAL_WINDOW_SIZE); |
|
41 |
|
42 wc.registerStream(1, DEFAULT_INITIAL_WINDOW_SIZE); |
|
43 wc.tryAcquire(DEFAULT_INITIAL_WINDOW_SIZE - 1, 1, null); |
|
44 assertEquals(wc.connectionWindowSize(), 1); |
|
45 assertEquals(wc.increaseConnectionWindow(Integer.MAX_VALUE), false); |
|
46 assertEquals(wc.increaseConnectionWindow(Integer.MAX_VALUE), false); |
|
47 assertEquals(wc.increaseConnectionWindow(Integer.MAX_VALUE), false); |
|
48 assertEquals(wc.connectionWindowSize(), 1); |
|
49 |
|
50 wc.increaseConnectionWindow(Integer.MAX_VALUE - 1 -1); |
|
51 assertEquals(wc.connectionWindowSize(), Integer.MAX_VALUE - 1); |
|
52 assertEquals(wc.increaseConnectionWindow(Integer.MAX_VALUE), false); |
|
53 assertEquals(wc.increaseConnectionWindow(Integer.MAX_VALUE), false); |
|
54 assertEquals(wc.increaseConnectionWindow(Integer.MAX_VALUE), false); |
|
55 assertEquals(wc.connectionWindowSize(), Integer.MAX_VALUE - 1); |
|
56 |
|
57 wc.increaseConnectionWindow(1); |
|
58 assertEquals(wc.connectionWindowSize(), Integer.MAX_VALUE); |
|
59 assertEquals(wc.increaseConnectionWindow(1), false); |
|
60 assertEquals(wc.increaseConnectionWindow(100), false); |
|
61 assertEquals(wc.increaseConnectionWindow(Integer.MAX_VALUE), false); |
|
62 assertEquals(wc.connectionWindowSize(), Integer.MAX_VALUE); |
|
63 } |
|
64 |
|
65 @Test |
|
66 public void testStreamWindowOverflow() { |
|
67 WindowController wc = new WindowController(); |
|
68 wc.registerStream(1, DEFAULT_INITIAL_WINDOW_SIZE); |
|
69 assertEquals(wc.increaseStreamWindow(Integer.MAX_VALUE, 1), false); |
|
70 assertEquals(wc.increaseStreamWindow(Integer.MAX_VALUE, 1), false); |
|
71 assertEquals(wc.increaseStreamWindow(Integer.MAX_VALUE, 1), false); |
|
72 |
|
73 wc.registerStream(3, DEFAULT_INITIAL_WINDOW_SIZE); |
|
74 assertEquals(wc.increaseStreamWindow(100, 3), true); |
|
75 assertEquals(wc.increaseStreamWindow(Integer.MAX_VALUE, 3), false); |
|
76 assertEquals(wc.increaseStreamWindow(Integer.MAX_VALUE, 3), false); |
|
77 |
|
78 wc.registerStream(5, 0); |
|
79 assertEquals(wc.increaseStreamWindow(Integer.MAX_VALUE, 5), true); |
|
80 assertEquals(wc.increaseStreamWindow(1, 5), false); |
|
81 assertEquals(wc.increaseStreamWindow(1, 5), false); |
|
82 assertEquals(wc.increaseStreamWindow(10, 5), false); |
|
83 |
|
84 wc.registerStream(7, -1); |
|
85 assertEquals(wc.increaseStreamWindow(Integer.MAX_VALUE, 7), true); |
|
86 assertEquals(wc.increaseStreamWindow(1, 7), true); |
|
87 assertEquals(wc.increaseStreamWindow(1, 7), false); |
|
88 assertEquals(wc.increaseStreamWindow(10, 7), false); |
|
89 |
|
90 wc.registerStream(9, -1); |
|
91 assertEquals(wc.increaseStreamWindow(1, 9), true); |
|
92 assertEquals(wc.increaseStreamWindow(1, 9), true); |
|
93 assertEquals(wc.increaseStreamWindow(1, 9), true); |
|
94 assertEquals(wc.increaseStreamWindow(10, 9), true); |
|
95 assertEquals(wc.increaseStreamWindow(Integer.MAX_VALUE, 9), false); |
|
96 |
|
97 wc.registerStream(11, -10); |
|
98 assertEquals(wc.increaseStreamWindow(1, 11), true); |
|
99 assertEquals(wc.increaseStreamWindow(1, 11), true); |
|
100 assertEquals(wc.increaseStreamWindow(1, 11), true); |
|
101 assertEquals(wc.increaseStreamWindow(1, 11), true); |
|
102 assertEquals(wc.increaseStreamWindow(1, 11), true); |
|
103 assertEquals(wc.increaseStreamWindow(1, 11), true); |
|
104 assertEquals(wc.increaseStreamWindow(1, 11), true); |
|
105 assertEquals(wc.increaseStreamWindow(1, 11), true); |
|
106 assertEquals(wc.increaseStreamWindow(1, 11), true); |
|
107 assertEquals(wc.increaseStreamWindow(1, 11), true); |
|
108 assertEquals(wc.increaseStreamWindow(1, 11), true); |
|
109 assertEquals(wc.streamWindowSize(11), 1); |
|
110 assertEquals(wc.increaseStreamWindow(Integer.MAX_VALUE, 11), false); |
|
111 assertEquals(wc.streamWindowSize(11), 1); |
|
112 } |
|
113 |
|
114 @Test |
|
115 public void testStreamAdjustment() { |
|
116 WindowController wc = new WindowController(); |
|
117 wc.registerStream(1, 100); |
|
118 wc.registerStream(3, 100); |
|
119 wc.registerStream(5, 100); |
|
120 |
|
121 // simulate some stream send activity before receiving the server's |
|
122 // SETTINGS frame, and staying within the connection window size |
|
123 wc.tryAcquire(49, 1 , null); |
|
124 wc.tryAcquire(50, 3 , null); |
|
125 wc.tryAcquire(51, 5 , null); |
|
126 |
|
127 wc.adjustActiveStreams(-200); |
|
128 assertEquals(wc.streamWindowSize(1), -149); |
|
129 assertEquals(wc.streamWindowSize(3), -150); |
|
130 assertEquals(wc.streamWindowSize(5), -151); |
|
131 } |
|
132 |
|
133 static final Class<InternalError> IE = InternalError.class; |
|
134 |
|
135 @Test |
|
136 public void testRemoveStream() { |
|
137 WindowController wc = new WindowController(); |
|
138 wc.registerStream(1, 999); |
|
139 wc.removeStream(1); |
|
140 assertThrows(IE, () -> wc.tryAcquire(5, 1, null)); |
|
141 |
|
142 wc.registerStream(3, 999); |
|
143 wc.tryAcquire(998, 3, null); |
|
144 wc.removeStream(3); |
|
145 assertThrows(IE, () -> wc.tryAcquire(5, 1, null)); |
|
146 } |
|
147 } |