Improved support for BytesMarshallable and Marshallable, closes https://github.com/OpenHFT/Chronicle-Map/issues/235

This commit is contained in:
Peter Lawrey
2020-10-15 18:22:28 +01:00
parent 3823ac1ee5
commit 25a2bfb4c5
9 changed files with 243 additions and 148 deletions
@@ -16,6 +16,8 @@
package net.openhft.chronicle.map;
import net.openhft.chronicle.hash.serialization.impl.TypedMarshallableReaderWriter;
import net.openhft.chronicle.wire.Marshallable;
import net.openhft.chronicle.wire.SelfDescribingMarshallable;
import org.junit.Test;
@@ -34,6 +36,19 @@ public final class AbstractMarshallableKeyValueTest {
assertThat(map.get(new Key()).number, is(new Value().number));
}
@Test
public void shouldAcceptAbstractMarshallableComponents2() throws Exception {
final ChronicleMap<Key, Marshallable> map = ChronicleMapBuilder.of(Key.class, Marshallable.class).entries(10)
.averageKey(new Key()).averageValue(new Value())
.valueMarshaller(new TypedMarshallableReaderWriter<>(Marshallable.class))
.create();
map.put(new Key(), new Value());
Value value = (Value) map.get(new Key());
assertThat(value.number, is(new Value().number));
}
private static final class Key extends SelfDescribingMarshallable {
private String k = "key";
}
@@ -468,7 +468,7 @@ public class ReplicatedChronicleMapTest extends JSR166TestCase {
ChronicleMap c = newShmIntString();
c.get(null);
shouldThrow();
} catch (NullPointerException success) {
} catch (NullPointerException|IllegalArgumentException success) {
}
}
@@ -481,7 +481,7 @@ public class ReplicatedChronicleMapTest extends JSR166TestCase {
ChronicleMap c = newShmIntString();
c.containsKey(null);
shouldThrow();
} catch (NullPointerException success) {
} catch (NullPointerException|IllegalArgumentException success) {
}
}
@@ -494,7 +494,7 @@ public class ReplicatedChronicleMapTest extends JSR166TestCase {
ChronicleMap c = newShmIntString();
c.put(null, "whatever");
shouldThrow();
} catch (NullPointerException success) {
} catch (NullPointerException|IllegalArgumentException success) {
}
}
@@ -507,7 +507,7 @@ public class ReplicatedChronicleMapTest extends JSR166TestCase {
ChronicleMap c = newShmIntString();
c.put(JSR166TestCase.notPresent, null);
shouldThrow();
} catch (NullPointerException success) {
} catch (NullPointerException|IllegalArgumentException success) {
}
}
@@ -520,7 +520,7 @@ public class ReplicatedChronicleMapTest extends JSR166TestCase {
ChronicleMap c = newShmIntString();
c.putIfAbsent(null, "whatever");
shouldThrow();
} catch (NullPointerException success) {
} catch (NullPointerException|IllegalArgumentException success) {
}
}
@@ -568,7 +568,7 @@ public class ReplicatedChronicleMapTest extends JSR166TestCase {
ChronicleMap c = newShmIntString();
c.replace(null, "whatever");
shouldThrow();
} catch (NullPointerException success) {
} catch (NullPointerException|IllegalArgumentException success) {
}
}
@@ -581,7 +581,7 @@ public class ReplicatedChronicleMapTest extends JSR166TestCase {
ChronicleMap c = newShmIntString();
c.replace(null, "A", "whatever");
shouldThrow();
} catch (NullPointerException success) {
} catch (NullPointerException|IllegalArgumentException success) {
}
}
@@ -593,7 +593,7 @@ public class ReplicatedChronicleMapTest extends JSR166TestCase {
ChronicleMap c = newShmIntString(5);
c.containsValue(null);
shouldThrow();
} catch (NullPointerException success) {
} catch (NullPointerException|IllegalArgumentException success) {
}
}*/
@@ -606,7 +606,7 @@ public class ReplicatedChronicleMapTest extends JSR166TestCase {
ChronicleMap c = newShmIntString(5);
c.contains(null);
shouldThrow();
} catch (NullPointerException success) {
} catch (NullPointerException|IllegalArgumentException success) {
}
}
*/
@@ -620,7 +620,7 @@ public class ReplicatedChronicleMapTest extends JSR166TestCase {
ChronicleMap c = newShmIntString();
c.putIfAbsent(JSR166TestCase.notPresent, null);
shouldThrow();
} catch (NullPointerException success) {
} catch (NullPointerException|IllegalArgumentException success) {
}
}
@@ -633,7 +633,7 @@ public class ReplicatedChronicleMapTest extends JSR166TestCase {
ChronicleMap c = newShmIntString();
c.replace(JSR166TestCase.notPresent, null);
shouldThrow();
} catch (NullPointerException success) {
} catch (NullPointerException|IllegalArgumentException success) {
}
}
@@ -646,7 +646,7 @@ public class ReplicatedChronicleMapTest extends JSR166TestCase {
ChronicleMap c = newShmIntString();
c.replace(JSR166TestCase.notPresent, null, "A");
shouldThrow();
} catch (NullPointerException success) {
} catch (NullPointerException|IllegalArgumentException success) {
}
}
@@ -659,7 +659,7 @@ public class ReplicatedChronicleMapTest extends JSR166TestCase {
ChronicleMap c = newShmIntString();
c.replace(JSR166TestCase.notPresent, "A", null);
shouldThrow();
} catch (NullPointerException success) {
} catch (NullPointerException|IllegalArgumentException success) {
}
}
@@ -673,7 +673,7 @@ public class ReplicatedChronicleMapTest extends JSR166TestCase {
c.put("sadsdf", "asdads");
c.remove(null);
shouldThrow();
} catch (NullPointerException success) {
} catch (NullPointerException|IllegalArgumentException success) {
}
}
@@ -687,7 +687,7 @@ public class ReplicatedChronicleMapTest extends JSR166TestCase {
c.put("sadsdf", "asdads");
c.remove(null, "whatever");
shouldThrow();
} catch (NullPointerException success) {
} catch (NullPointerException|IllegalArgumentException success) {
}
}
@@ -1,6 +1,7 @@
package net.openhft.chronicle.map;
import net.openhft.chronicle.wire.BytesInBinaryMarshallable;
import net.openhft.chronicle.wire.Marshallable;
import net.openhft.chronicle.wire.SelfDescribingMarshallable;
import org.junit.Test;
@@ -94,6 +95,26 @@ public class SerializableTest {
assertEquals(expected, actual);
}
@Test
public void test2d() {
ChronicleMap<Integer, Marshallable> map = ChronicleMapBuilder.simpleMapOf(Integer.class, Marshallable.class)
.name("bar")
.averageValueSize(1024)
.entries(10)
.create();
String expected = IntStream.range(0, 4096)
.mapToObj(i -> i % 50 == 0 ? String.format("\n%04d", i) : "" + i % 10)
.collect(Collectors.joining(""));
Bar2 value = new Bar2(expected);
map.put(1, value);
Bar2 bar2 = (Bar2) map.get(1);
String actual = bar2.x;
assertEquals(expected, actual);
}
public static class Foo implements Serializable {
public String x;
@@ -420,7 +420,7 @@ public class ChronicleMapTest extends JSR166TestCase {
try (ChronicleMap c = newShmIntString(8076)) {
c.get(null);
shouldThrow();
} catch (NullPointerException success) {
} catch (NullPointerException | IllegalArgumentException success) {
}
}
@@ -432,7 +432,7 @@ public class ChronicleMapTest extends JSR166TestCase {
try (ChronicleMap c = newShmIntString(8076)) {
c.containsKey(null);
shouldThrow();
} catch (NullPointerException success) {
} catch (NullPointerException | IllegalArgumentException success) {
}
}
@@ -444,7 +444,7 @@ public class ChronicleMapTest extends JSR166TestCase {
try (ChronicleMap c = newShmIntString(8076)) {
c.put(null, "whatever");
shouldThrow();
} catch (NullPointerException success) {
} catch (NullPointerException | IllegalArgumentException success) {
}
}
@@ -457,7 +457,7 @@ public class ChronicleMapTest extends JSR166TestCase {
try (ChronicleMap c = newShmIntString(8076)) {
c.put(notPresent, null);
shouldThrow();
} catch (NullPointerException success) {
} catch (NullPointerException | IllegalArgumentException success) {
}
}
@@ -470,7 +470,7 @@ public class ChronicleMapTest extends JSR166TestCase {
try (ChronicleMap c = newShmIntString(8076)) {
c.putIfAbsent(null, "whatever");
shouldThrow();
} catch (NullPointerException success) {
} catch (NullPointerException | IllegalArgumentException success) {
}
}
@@ -483,7 +483,7 @@ public class ChronicleMapTest extends JSR166TestCase {
try (ChronicleMap c = newShmIntString(8076)) {
c.replace(null, "whatever");
shouldThrow();
} catch (NullPointerException success) {
} catch (NullPointerException | IllegalArgumentException success) {
}
}
@@ -496,7 +496,7 @@ public class ChronicleMapTest extends JSR166TestCase {
try (ChronicleMap c = newShmIntString(8076)) {
c.replace(null, "A", "whatever");
shouldThrow();
} catch (NullPointerException success) {
} catch (NullPointerException | IllegalArgumentException success) {
}
}
@@ -508,7 +508,7 @@ public class ChronicleMapTest extends JSR166TestCase {
try (ChronicleMap c = newShmIntString(8076)) {
c.putIfAbsent(notPresent, null);
shouldThrow();
} catch (NullPointerException success) {
} catch (NullPointerException | IllegalArgumentException success) {
}
}
@@ -520,7 +520,7 @@ public class ChronicleMapTest extends JSR166TestCase {
try (ChronicleMap c = newShmIntString(8076)) {
c.replace(notPresent, null);
shouldThrow();
} catch (NullPointerException success) {
} catch (NullPointerException | IllegalArgumentException success) {
}
}
@@ -528,12 +528,11 @@ public class ChronicleMapTest extends JSR166TestCase {
* replace(x, null, y) throws NPE
*/
@Test(timeout = 5000)
public void testReplaceValue2_NullPointerException
() throws IOException {
public void testReplaceValue2_NullPointerException() throws IOException {
try (ChronicleMap c = newShmIntString(8076)) {
c.replace(notPresent, null, "A");
shouldThrow();
} catch (NullPointerException success) {
} catch (NullPointerException | IllegalArgumentException success) {
}
}
@@ -545,7 +544,7 @@ public class ChronicleMapTest extends JSR166TestCase {
try (ChronicleMap c = newShmIntString(8076)) {
c.replace(notPresent, "A", null);
shouldThrow();
} catch (NullPointerException success) {
} catch (NullPointerException | IllegalArgumentException success) {
}
}
@@ -558,7 +557,7 @@ public class ChronicleMapTest extends JSR166TestCase {
c.put("sadsdf", "asdads");
c.remove(null);
shouldThrow();
} catch (NullPointerException success) {
} catch (NullPointerException | IllegalArgumentException success) {
}
}
@@ -572,7 +571,7 @@ public class ChronicleMapTest extends JSR166TestCase {
c.put("sadsdf", "asdads");
c.remove(null, "whatever");
shouldThrow();
} catch (NullPointerException success) {
} catch (NullPointerException | IllegalArgumentException success) {
}
}
@@ -588,6 +587,37 @@ public class ChronicleMapTest extends JSR166TestCase {
}
}
@Test
public void testPercentageComplete() {
try (ChronicleMap<Integer, Integer> map = ChronicleMap
.of(Integer.class, Integer.class)
.entries(1600)
.actualSegments(3)
.maxBloatFactor(2)
.create()) {
long remainingAutoResizes = 0;
short percentageFreeSpace = 0;
try {
for (int i = 0; ; i++) {
map.put(i, 0);
remainingAutoResizes = map.remainingAutoResizes();
percentageFreeSpace = map.percentageFreeSpace();
}
} catch (IllegalStateException e) {
if (e.getMessage().contains("Attempt to allocate")) {
Assert.assertEquals(0, (int) remainingAutoResizes);
Assert.assertTrue(percentageFreeSpace < 6);
return;
}
Assert.fail();
}
}
}
// classes for testing Comparable fallbacks
static class BI implements Comparable<BI> {
private final int value;
@@ -671,36 +701,5 @@ public class ChronicleMapTest extends JSR166TestCase {
}
}
@Test
public void testPercentageComplete() {
try (ChronicleMap<Integer, Integer> map = ChronicleMap
.of(Integer.class, Integer.class)
.entries(1600)
.actualSegments(3)
.maxBloatFactor(2)
.create()) {
long remainingAutoResizes = 0;
short percentageFreeSpace = 0;
try {
for (int i = 0; ; i++) {
map.put(i, 0);
remainingAutoResizes = map.remainingAutoResizes();
percentageFreeSpace = map.percentageFreeSpace();
}
} catch (IllegalStateException e) {
if (e.getMessage().contains("Attempt to allocate")) {
Assert.assertEquals(0, (int) remainingAutoResizes);
Assert.assertTrue(percentageFreeSpace < 6);
return;
}
Assert.fail();
}
}
}
}