|
1 /* |
|
2 * Copyright (c) 2016, 2017, 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 package jdk.internal.net.http.hpack; |
|
24 |
|
25 import org.testng.annotations.BeforeClass; |
|
26 import org.testng.annotations.Test; |
|
27 import jdk.internal.net.http.hpack.HeaderTable.CircularBuffer; |
|
28 |
|
29 import java.util.Queue; |
|
30 import java.util.Random; |
|
31 import java.util.concurrent.ArrayBlockingQueue; |
|
32 |
|
33 import static org.testng.Assert.assertEquals; |
|
34 import static jdk.internal.net.http.hpack.TestHelper.newRandom; |
|
35 |
|
36 public final class CircularBufferTest { |
|
37 |
|
38 private final Random r = newRandom(); |
|
39 |
|
40 @BeforeClass |
|
41 public void setUp() { |
|
42 r.setSeed(System.currentTimeMillis()); |
|
43 } |
|
44 |
|
45 @Test |
|
46 public void queue() { |
|
47 for (int capacity = 1; capacity <= 2048; capacity++) { |
|
48 queueOnce(capacity, 32); |
|
49 } |
|
50 } |
|
51 |
|
52 @Test |
|
53 public void resize() { |
|
54 for (int capacity = 1; capacity <= 4096; capacity++) { |
|
55 resizeOnce(capacity); |
|
56 } |
|
57 } |
|
58 |
|
59 @Test |
|
60 public void downSizeEmptyBuffer() { |
|
61 CircularBuffer<Integer> buffer = new CircularBuffer<>(16); |
|
62 buffer.resize(15); |
|
63 } |
|
64 |
|
65 private void resizeOnce(int capacity) { |
|
66 |
|
67 int nextNumberToPut = 0; |
|
68 |
|
69 Queue<Integer> referenceQueue = new ArrayBlockingQueue<>(capacity); |
|
70 CircularBuffer<Integer> buffer = new CircularBuffer<>(capacity); |
|
71 |
|
72 // Fill full, so the next add will wrap |
|
73 for (int i = 0; i < capacity; i++, nextNumberToPut++) { |
|
74 buffer.add(nextNumberToPut); |
|
75 referenceQueue.add(nextNumberToPut); |
|
76 } |
|
77 int gets = r.nextInt(capacity); // [0, capacity) |
|
78 for (int i = 0; i < gets; i++) { |
|
79 referenceQueue.poll(); |
|
80 buffer.remove(); |
|
81 } |
|
82 int puts = r.nextInt(gets + 1); // [0, gets] |
|
83 for (int i = 0; i < puts; i++, nextNumberToPut++) { |
|
84 buffer.add(nextNumberToPut); |
|
85 referenceQueue.add(nextNumberToPut); |
|
86 } |
|
87 |
|
88 Integer[] expected = referenceQueue.toArray(new Integer[0]); |
|
89 buffer.resize(expected.length); |
|
90 |
|
91 assertEquals(buffer.elements, expected); |
|
92 } |
|
93 |
|
94 private void queueOnce(int capacity, int numWraps) { |
|
95 |
|
96 Queue<Integer> referenceQueue = new ArrayBlockingQueue<>(capacity); |
|
97 CircularBuffer<Integer> buffer = new CircularBuffer<>(capacity); |
|
98 |
|
99 int nextNumberToPut = 0; |
|
100 int totalPuts = 0; |
|
101 int putsLimit = capacity * numWraps; |
|
102 int remainingCapacity = capacity; |
|
103 int size = 0; |
|
104 |
|
105 while (totalPuts < putsLimit) { |
|
106 assert remainingCapacity + size == capacity; |
|
107 int puts = r.nextInt(remainingCapacity + 1); // [0, remainingCapacity] |
|
108 remainingCapacity -= puts; |
|
109 size += puts; |
|
110 for (int i = 0; i < puts; i++, nextNumberToPut++) { |
|
111 referenceQueue.add(nextNumberToPut); |
|
112 buffer.add(nextNumberToPut); |
|
113 } |
|
114 totalPuts += puts; |
|
115 int gets = r.nextInt(size + 1); // [0, size] |
|
116 size -= gets; |
|
117 remainingCapacity += gets; |
|
118 for (int i = 0; i < gets; i++) { |
|
119 Integer expected = referenceQueue.poll(); |
|
120 Integer actual = buffer.remove(); |
|
121 assertEquals(actual, expected); |
|
122 } |
|
123 } |
|
124 } |
|
125 } |