< prev index next >
test/jdk/java/net/httpclient/http2/jdk.incubator.httpclient/jdk/incubator/http/internal/hpack/HeaderTableTest.java
Print this page
@@ -1,7 +1,7 @@
/*
- * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
@@ -114,11 +114,11 @@
private static final int STATIC_TABLE_LENGTH = createStaticEntries().size();
private final Random rnd = newRandom();
@Test
public void staticData() {
- HeaderTable table = new HeaderTable(0);
+ HeaderTable table = new HeaderTable(0, HPACK.getLogger());
Map<Integer, HeaderField> staticHeaderFields = createStaticEntries();
Map<String, Integer> minimalIndexes = new HashMap<>();
for (Map.Entry<Integer, HeaderField> e : staticHeaderFields.entrySet()) {
@@ -157,69 +157,69 @@
}
@Test
public void constructorSetsMaxSize() {
int size = rnd.nextInt(64);
- HeaderTable t = new HeaderTable(size);
+ HeaderTable t = new HeaderTable(size, HPACK.getLogger());
assertEquals(t.size(), 0);
assertEquals(t.maxSize(), size);
}
@Test
public void negativeMaximumSize() {
int maxSize = -(rnd.nextInt(100) + 1); // [-100, -1]
IllegalArgumentException e =
assertVoidThrows(IllegalArgumentException.class,
- () -> new HeaderTable(0).setMaxSize(maxSize));
+ () -> new HeaderTable(0, HPACK.getLogger()).setMaxSize(maxSize));
assertExceptionMessageContains(e, "maxSize");
}
@Test
public void zeroMaximumSize() {
- HeaderTable table = new HeaderTable(0);
+ HeaderTable table = new HeaderTable(0, HPACK.getLogger());
table.setMaxSize(0);
assertEquals(table.maxSize(), 0);
}
@Test
public void negativeIndex() {
int idx = -(rnd.nextInt(256) + 1); // [-256, -1]
- IllegalArgumentException e =
- assertVoidThrows(IllegalArgumentException.class,
- () -> new HeaderTable(0).get(idx));
+ IndexOutOfBoundsException e =
+ assertVoidThrows(IndexOutOfBoundsException.class,
+ () -> new HeaderTable(0, HPACK.getLogger()).get(idx));
assertExceptionMessageContains(e, "index");
}
@Test
public void zeroIndex() {
- IllegalArgumentException e =
- assertThrows(IllegalArgumentException.class,
- () -> new HeaderTable(0).get(0));
+ IndexOutOfBoundsException e =
+ assertThrows(IndexOutOfBoundsException.class,
+ () -> new HeaderTable(0, HPACK.getLogger()).get(0));
assertExceptionMessageContains(e, "index");
}
@Test
public void length() {
- HeaderTable table = new HeaderTable(0);
+ HeaderTable table = new HeaderTable(0, HPACK.getLogger());
assertEquals(table.length(), STATIC_TABLE_LENGTH);
}
@Test
public void indexOutsideStaticRange() {
- HeaderTable table = new HeaderTable(0);
+ HeaderTable table = new HeaderTable(0, HPACK.getLogger());
int idx = table.length() + (rnd.nextInt(256) + 1);
- IllegalArgumentException e =
- assertThrows(IllegalArgumentException.class,
+ IndexOutOfBoundsException e =
+ assertThrows(IndexOutOfBoundsException.class,
() -> table.get(idx));
assertExceptionMessageContains(e, "index");
}
@Test
public void entryPutAfterStaticArea() {
- HeaderTable table = new HeaderTable(256);
+ HeaderTable table = new HeaderTable(256, HPACK.getLogger());
int idx = table.length() + 1;
- assertThrows(IllegalArgumentException.class, () -> table.get(idx));
+ assertThrows(IndexOutOfBoundsException.class, () -> table.get(idx));
byte[] bytes = new byte[32];
rnd.nextBytes(bytes);
String name = new String(bytes, StandardCharsets.ISO_8859_1);
String value = "custom-value";
@@ -230,17 +230,17 @@
assertEquals(value, f.value);
}
@Test
public void staticTableHasZeroSize() {
- HeaderTable table = new HeaderTable(0);
+ HeaderTable table = new HeaderTable(0, HPACK.getLogger());
assertEquals(0, table.size());
}
@Test
public void lowerIndexPriority() {
- HeaderTable table = new HeaderTable(256);
+ HeaderTable table = new HeaderTable(256, HPACK.getLogger());
int oldLength = table.length();
table.put("bender", "rodriguez");
table.put("bender", "rodriguez");
table.put("bender", "rodriguez");
@@ -249,11 +249,11 @@
assertEquals(oldLength + 1, i);
}
@Test
public void lowerIndexPriority2() {
- HeaderTable table = new HeaderTable(256);
+ HeaderTable table = new HeaderTable(256, HPACK.getLogger());
int oldLength = table.length();
int idx = rnd.nextInt(oldLength) + 1;
HeaderField f = table.get(idx);
table.put(f.name, f.value);
assertEquals(table.length(), oldLength + 1);
@@ -265,13 +265,15 @@
// TODO: ensure full table clearance when adding huge header field
// TODO: ensure eviction deletes minimum needed entries, not more
@Test
public void fifo() {
- HeaderTable t = new HeaderTable(Integer.MAX_VALUE);
// Let's add a series of header fields
int NUM_HEADERS = 32;
+ HeaderTable t = new HeaderTable((32 + 4) * NUM_HEADERS, HPACK.getLogger());
+ // ^ ^
+ // entry overhead symbols per entry (max 2x2 digits)
for (int i = 1; i <= NUM_HEADERS; i++) {
String s = String.valueOf(i);
t.put(s, s);
}
// They MUST appear in a FIFO order:
@@ -291,13 +293,15 @@
}
}
@Test
public void indexOf() {
- HeaderTable t = new HeaderTable(Integer.MAX_VALUE);
// Let's put a series of header fields
int NUM_HEADERS = 32;
+ HeaderTable t = new HeaderTable((32 + 4) * NUM_HEADERS, HPACK.getLogger());
+ // ^ ^
+ // entry overhead symbols per entry (max 2x2 digits)
for (int i = 1; i <= NUM_HEADERS; i++) {
String s = String.valueOf(i);
t.put(s, s);
}
// and verify indexOf (reverse lookup) returns correct indexes for
@@ -324,22 +328,29 @@
testToString0();
}
@Test
public void testToStringDifferentLocale() {
+ Locale locale = Locale.getDefault();
Locale.setDefault(Locale.FRENCH);
+ try {
String s = format("%.1f", 3.1);
assertEquals("3,1", s); // assumption of the test, otherwise the test is useless
testToString0();
+ } finally {
+ Locale.setDefault(locale);
+ }
}
private void testToString0() {
- HeaderTable table = new HeaderTable(0);
+ HeaderTable table = new HeaderTable(0, HPACK.getLogger());
{
- table.setMaxSize(2048);
- String expected =
- format("entries: %d; used %s/%s (%.1f%%)", 0, 0, 2048, 0.0);
+ int maxSize = 2048;
+ table.setMaxSize(maxSize);
+ String expected = format(
+ "dynamic length: %s, full length: %s, used space: %s/%s (%.1f%%)",
+ 0, STATIC_TABLE_LENGTH, 0, maxSize, 0.0);
assertEquals(expected, table.toString());
}
{
String name = "custom-name";
@@ -351,29 +362,31 @@
String s = table.toString();
int used = name.length() + value.length() + 32;
double ratio = used * 100.0 / size;
- String expected =
- format("entries: 1; used %s/%s (%.1f%%)", used, size, ratio);
+ String expected = format(
+ "dynamic length: %s, full length: %s, used space: %s/%s (%.1f%%)",
+ 1, STATIC_TABLE_LENGTH + 1, used, size, ratio);
assertEquals(expected, s);
}
{
table.setMaxSize(78);
table.put(":method", "");
table.put(":status", "");
String s = table.toString();
String expected =
- format("entries: %d; used %s/%s (%.1f%%)", 2, 78, 78, 100.0);
+ format("dynamic length: %s, full length: %s, used space: %s/%s (%.1f%%)",
+ 2, STATIC_TABLE_LENGTH + 2, 78, 78, 100.0);
assertEquals(expected, s);
}
}
@Test
public void stateString() {
- HeaderTable table = new HeaderTable(256);
+ HeaderTable table = new HeaderTable(256, HPACK.getLogger());
table.put("custom-key", "custom-header");
// @formatter:off
assertEquals("[ 1] (s = 55) custom-key: custom-header\n" +
" Table size: 55", table.getStateString());
// @formatter:on
< prev index next >