Remove ChronicleMap.putMapped() as a duplicate of computeIfPresent(); Reduce the number of ignored tests

This commit is contained in:
leventov
2015-09-27 15:41:07 +03:00
parent 22b964e069
commit 440b075f9d
14 changed files with 149 additions and 297 deletions
@@ -1,91 +0,0 @@
/*
* Copyright (C) 2015 higherfrequencytrading.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.openhft.chronicle.map;
import org.junit.Ignore;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicLong;
import static org.junit.Assert.assertEquals;
@Ignore("event listener API is not yet determined")
public class CHMMetaDataTest {
private static final String TMP = System.getProperty("java.io.tmpdir");
@Test
public void testAccessTimes() throws IOException {
File file = new File(TMP, "testAccessTimes");
// MapBytesEventListener listener =
// new StringStringMapEventListener(new AtomicLong(1));
ChronicleMap<String, String> map = ChronicleMapBuilder.of(String.class, String.class)
.metaDataBytes(8)
// .bytesEventListener(listener)
.create();
try {
map.put("a", "aye");
map.put("b", "bee");
map.put("c", "see");
map.put("d", "dee");
// assertEquals(5, timeStamps.longValue());
assertEquals("aye", map.get("a"));
assertEquals("bee", map.get("b"));
assertEquals("see", map.get("c"));
assertEquals("dee", map.get("d"));
// assertEquals(9, timeStamps.longValue());
assertEquals("aye", map.remove("a"));
assertEquals("bee", map.remove("b"));
assertEquals("see", map.remove("c"));
assertEquals("dee", map.remove("d"));
// assertEquals(9, timeStamps.longValue());
} finally {
map.close();
file.delete();
}
}
// private static class StringStringMapEventListener
// extends MapBytesEventListener {
// private static final long serialVersionUID = 0L;
//
// private final AtomicLong timeStamps;
//
// public StringStringMapEventListener(AtomicLong timeStamps) {
// this.timeStamps = timeStamps;
// }
//
// @Override
// public void onGetFound(MapBytesEvent event) {
// entry.writeLong(metaDataPos, timeStamps.incrementAndGet());
// }
//
// @Override
// public void onPut(MapPutBytesEvent event) {
// if (added)
// assertEquals(0, entry.readLong(metaDataPos));
// entry.writeLong(metaDataPos, timeStamps.incrementAndGet());
// }
//
// @Override
// public void onRemove(MapBytesEvent event) {
// System.out.println("Removed entry with ts of " + entry.readLong(metaDataPos));;
// }
// }
}
@@ -44,6 +44,7 @@ import java.nio.ByteBuffer;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.function.BiFunction;
import static java.util.Arrays.asList;
import static net.openhft.chronicle.map.fromdocs.OpenJDKAndHashMapExamplesTest.parseYYYYMMDD;
@@ -404,29 +405,23 @@ public class CHMUseCasesTest {
}
}
@Ignore
@Test
public void testByteArrayArrayValue()
throws ExecutionException, InterruptedException, IOException {
for (int i = 0; i < 10000; i++) {
int valueSize = 10;
ChronicleMapBuilder<byte[], byte[][]> builder = ChronicleMapBuilder
.of(byte[].class, byte[][].class)
.averageKey("Key".getBytes())
.averageValue(new byte[][]{"value1".getBytes(), "value2".getBytes()});
char[] expected = new char[valueSize];
Arrays.fill(expected, 'X');
try (ChronicleMap<byte[], byte[][]> map = newInstance(builder)) {
byte[] bytes1 = "value1".getBytes();
byte[] bytes2 = "value2".getBytes();
byte[][] value = {bytes1, bytes2};
map.put("Key".getBytes(), value);
ChronicleMapBuilder<byte[], byte[][]> builder = ChronicleMapBuilder
.of(byte[].class, byte[][].class);
try (ChronicleMap<byte[], byte[][]> map = newInstance(builder)) {
byte[] bytes1 = "value1".getBytes();
byte[] bytes2 = "value2".getBytes();
byte[][] value = {bytes1, bytes2};
map.put("Key".getBytes(), value);
assertEquals(value, map.get("Key".getBytes()));
mapChecks();
}
assertEquals(value, map.get("Key".getBytes()));
mapChecks();
}
}
@@ -556,7 +551,8 @@ public class CHMUseCasesTest {
}
private static class StringPrefixUnaryOperator implements UnaryOperator<String>, Serializable {
private static class StringPrefixUnaryOperator
implements BiFunction<String, String, String>, Serializable {
private String prefix;
@@ -565,10 +561,9 @@ public class CHMUseCasesTest {
}
@Override
public String update(String s) {
return prefix + s;
public String apply(String k, String v) {
return prefix + v;
}
}
@@ -580,7 +575,7 @@ public class CHMUseCasesTest {
try (ChronicleMap<String, String> map = newInstance(builder)) {
map.put("Hello", "World");
map.putMapped("Hello", new StringPrefixUnaryOperator("New "));
map.computeIfPresent("Hello", new StringPrefixUnaryOperator("New "));
mapChecks();
}
}
@@ -622,7 +617,9 @@ public class CHMUseCasesTest {
* CharSequence is more efficient when object creation is avoided.
* * The key can only be on heap and variable length serialised.
*/
@Ignore("TODO HCOLL-306")
@Ignore("This test fails because assumes StringBuilder is fed to lambdas, but actually " +
"because interned in CharSequenceReader. Defer fixing this until Chronicle Map " +
"serialization framework is moved from Lang Bytes to Chronicle Bytes")
@Test
public void testCharSequenceCharSequenceMap()
throws ExecutionException, InterruptedException, IOException {
@@ -647,40 +644,21 @@ public class CHMUseCasesTest {
assertNull(map.getUsing(key, value));
assertEquals("New World", map.getMapped("Hello", new
SerializableFunction<CharSequence, CharSequence>() {
@Override
public CharSequence apply(CharSequence s) {
return "New " + s;
}
}));
assertEquals(null, map.getMapped("No key", new
SerializableFunction<CharSequence, CharSequence>() {
@Override
public CharSequence apply(CharSequence s) {
return "New " + s;
}
}));
assertEquals("New World", map.getMapped("Hello", s -> "New " + s));
assertEquals(null, map.getMapped("No key",
(SerializableFunction<CharSequence, CharSequence>) s -> "New " + s));
assertEquals("New World !!", map.putMapped("Hello", new
UnaryOperator<CharSequence>() {
@Override
public CharSequence update(CharSequence s) {
((StringBuilder) s).append(" !!");
return "New " + s;
}
}));
assertEquals("New World !!", map.computeIfPresent("Hello", (k, s) -> {
((StringBuilder) s).append(" !!");
return "New " + s;
}));
assertEquals("New World !!", map.get("Hello").toString());
assertEquals("New !!", map.putMapped("no-key", new
UnaryOperator<CharSequence>() {
@Override
public CharSequence update(CharSequence s) {
((StringBuilder) s).append("!!");
return "New " + s;
}
}));
assertEquals("New !!", map.computeIfPresent("no-key", (k, s) -> {
((StringBuilder) s).append("!!");
return "New " + s;
}));
mapChecks();
}
@@ -1013,12 +991,7 @@ public class CHMUseCasesTest {
mapChecks();
try {
map.putMapped(1, new UnaryOperator<Integer>() {
@Override
public Integer update(Integer s) {
return s + 1;
}
});
map.computeIfPresent(1, (k, s) -> s + 1);
} catch (Exception todoMoreSpecificException) {
}
mapChecks();
@@ -1053,22 +1026,12 @@ public class CHMUseCasesTest {
return 10 * s;
}
}));
assertEquals(null, map.getMapped(-1L, new SerializableFunction<Long, Long>() {
@Override
public Long apply(Long s) {
return 10 * s;
}
}));
assertEquals(null, map.getMapped(-1L, (SerializableFunction<Long, Long>) s -> 10 * s));
mapChecks();
try {
map.putMapped(1L, new UnaryOperator<Long>() {
@Override
public Long update(Long s) {
return s + 1;
}
});
map.computeIfPresent(1L, (k, s) -> s + 1);
} catch (Exception todoMoreSpecificException) {
}
@@ -1101,20 +1064,10 @@ public class CHMUseCasesTest {
return 10 * s;
}
}));
assertEquals(null, map.getMapped(-1.0, new SerializableFunction<Double, Double>() {
@Override
public Double apply(Double s) {
return 10 * s;
}
}));
assertEquals(null, map.getMapped(-1.0, (SerializableFunction<Double, Double>) s -> 10 * s));
try {
map.putMapped(1.0, new UnaryOperator<Double>() {
@Override
public Double update(Double s) {
return s + 1;
}
});
map.computeIfPresent(1.0, (k, s) -> s + 1);
} catch (Exception todoMoreSpecificException) {
}
@@ -1159,13 +1112,10 @@ public class CHMUseCasesTest {
}));
assertTrue(Arrays.equals(new byte[]{12, 10},
map.putMapped(key1, new UnaryOperator<byte[]>() {
@Override
public byte[] update(byte[] s) {
s[0]++;
s[1]--;
return Arrays.copyOf(s, 2);
}
map.computeIfPresent(key1, (k, s) -> {
s[0]++;
s[1]--;
return Arrays.copyOf(s, 2);
})));
byte[] a2 = map.get(key1);
@@ -1243,13 +1193,10 @@ public class CHMUseCasesTest {
assertEquals(null, map.getMapped(key2, function));
mapChecks();
assertBBEquals(ByteBuffer.wrap(new byte[]{12, 10}),
map.putMapped(key1, new UnaryOperator<ByteBuffer>() {
@Override
public ByteBuffer update(ByteBuffer s) {
s.put(0, (byte) (s.get(0) + 1));
s.put(1, (byte) (s.get(1) - 1));
return function.apply(s);
}
map.computeIfPresent(key1, (k, s) -> {
s.put(0, (byte) (s.get(0) + 1));
s.put(1, (byte) (s.get(1) - 1));
return function.apply(s);
}));
assertBBEquals(ByteBuffer.wrap(new byte[]{12, 10}), map.get(key1));
@@ -1362,7 +1309,7 @@ public class CHMUseCasesTest {
assertEquals(null, map.getMapped(key2, function));
mapChecks();
assertBBEquals(ByteBuffer.wrap(new byte[]{12, 10}),
map.putMapped(key1, s -> {
map.computeIfPresent(key1, (k, s) -> {
s.put(0, (byte) (s.get(0) + 1));
s.put(1, (byte) (s.get(1) - 1));
return function.apply(s);
@@ -1480,31 +1427,6 @@ public class CHMUseCasesTest {
}
}
@Test
@Ignore("HCOLL-241 Generated code creates a field too large ie. it ignores the @Range")
public void testUnsignedIntValueUnsignedIntValueMapEntrySize() throws IOException {
// TODO once this is working, merge the next test.
ChronicleMapBuilder<UnsignedIntValue, UnsignedIntValue> builder = ChronicleMapBuilder
.of(UnsignedIntValue.class, UnsignedIntValue.class);
try (ChronicleMap<UnsignedIntValue, UnsignedIntValue> map = newInstance(builder)) {
// this may change due to alignment
//assertEquals(8, entrySize(map));
assertEquals(1, ((VanillaChronicleMap) map).maxChunksPerEntry);
UnsignedIntValue key1 = map.newKeyInstance();
UnsignedIntValue value1 = map.newValueInstance();
key1.setValue(1);
value1.setValue(11);
map.put(key1, value1);
assertEquals(value1, map.get(key1));
mapChecks();
}
}
/**
* For unsigned int -> unsigned int entries, the key can be on heap or off heap.
*/
@@ -1516,10 +1438,18 @@ public class CHMUseCasesTest {
try (ChronicleMap<UnsignedIntValue, UnsignedIntValue> map = newInstance(builder)) {
assertEquals(1, ((VanillaChronicleMap) map).maxChunksPerEntry);
UnsignedIntValue key1 = map.newKeyInstance();
UnsignedIntValue key2 = map.newKeyInstance();
UnsignedIntValue value1 = map.newValueInstance();
key1.setValue(1);
value1.setValue(11);
map.put(key1, value1);
assertEquals(value1, map.get(key1));
key1 = map.newKeyInstance();
UnsignedIntValue key2 = map.newKeyInstance();
value1 = map.newValueInstance();
UnsignedIntValue value2 = map.newValueInstance();
key1.setValue(1);
@@ -499,7 +499,6 @@ public class ChronicleMapTest {
}
@Test
@Ignore("TODO clarify requrements -- if acquireUsing() should work with null value")
public void testAcquireWithNullContainer() {
try (ChronicleMap<CharSequence, LongValue> map =
ChronicleMapBuilder.of(CharSequence.class, LongValue.class)
@@ -690,7 +689,6 @@ public class ChronicleMapTest {
}
@Ignore("Possible candidate for TC JVM crashes")
@Test
public void testAcquireFromMultipleThreads() throws InterruptedException {
int entries = 1000 * 1000;
@@ -16,7 +16,6 @@
package net.openhft.chronicle.map;
import org.junit.Ignore;
import org.junit.Test;
import java.io.File;
@@ -30,7 +29,6 @@ import static org.junit.Assert.assertEquals;
public class DefaultValueTest {
@Test
@Ignore("Cannot reuse List instance in acquireUsing()")
public void test() throws IllegalAccessException, InstantiationException, IOException {
File file = Builder.getPersistenceFile();
try {
@@ -16,10 +16,14 @@
package net.openhft.chronicle.map;
import net.openhft.chronicle.hash.Data;
import net.openhft.chronicle.hash.replication.TcpTransportAndNetworkConfig;
import net.openhft.chronicle.map.replication.MapRemoteOperations;
import net.openhft.chronicle.map.replication.MapRemoteQueryContext;
import net.openhft.chronicle.map.replication.MapReplicableEntry;
import org.jetbrains.annotations.NotNull;
import org.junit.After;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import java.io.Closeable;
@@ -35,7 +39,6 @@ import java.util.concurrent.atomic.AtomicReference;
*
* @author Rob Austin.
*/
@Ignore("event listening API yet determined")
public class EventListenerWithTCPSocketReplicationTest {
ChronicleMap<Integer, CharSequence> map1;
@@ -50,24 +53,64 @@ public class EventListenerWithTCPSocketReplicationTest {
final AtomicBoolean wasRemoved = new AtomicBoolean(false);
final AtomicReference<CharSequence> valueRemoved = new AtomicReference<CharSequence>();
// final MapEventListener<Integer, CharSequence> eventListener = new
// MapEventListener<Integer, CharSequence>() {
// private static final long serialVersionUID = 1L;
//
// @Override
// public void onPut(MapPutEvent<Integer, CharSequence> event) {
// putWasCalled.getAndSet(true);
// keyRef.set(key);
// valueRef.set(value);
// replacedValueRef.set(replacedValue);
// }
//
// @Override
// public void onRemove(MapEvent<Integer, CharSequence> event) {
// wasRemoved.set(true);
// valueRemoved.set(value);
// }
// };
final MapEntryOperations<Integer, CharSequence, Void> entryOperations = new
MapEntryOperations<Integer, CharSequence, Void>() {
@Override
public Void remove(@NotNull MapEntry<Integer, CharSequence> entry) {
wasRemoved.set(true);
valueRemoved.set(entry.value().get());
entry.doRemove();
return null;
}
@Override
public Void replaceValue(@NotNull MapEntry<Integer, CharSequence> entry,
Data<CharSequence> newValue) {
putWasCalled.getAndSet(true);
keyRef.set(entry.key().get());
valueRef.set(newValue.get());
replacedValueRef.set(entry.value().get());
entry.doReplaceValue(newValue);
return null;
}
@Override
public Void insert(@NotNull MapAbsentEntry<Integer, CharSequence> absentEntry,
Data<CharSequence> value) {
putWasCalled.getAndSet(true);
keyRef.set(absentEntry.absentKey().get());
valueRef.set(value.get());
replacedValueRef.set(null);
absentEntry.doInsert(value);
return null;
}
};
final MapRemoteOperations<Integer, CharSequence, Void> remoteOperations =
new MapRemoteOperations<Integer, CharSequence, Void>() {
@Override
public void remove(MapRemoteQueryContext<Integer, CharSequence, Void> q) {
MapReplicableEntry<Integer, CharSequence> entry = q.entry();
if (entry != null) {
wasRemoved.set(true);
valueRemoved.set(entry.value().get());
}
MapRemoteOperations.super.remove(q);
}
@Override
public void put(MapRemoteQueryContext<Integer, CharSequence, Void> q,
Data<CharSequence> newValue) {
MapReplicableEntry<Integer, CharSequence> entry = q.entry();
CharSequence oldValue = entry != null ? entry.value().get() : null;
putWasCalled.getAndSet(true);
keyRef.set(q.queriedKey().get());
valueRef.set(newValue.get());
replacedValueRef.set(oldValue);
MapRemoteOperations.super.put(q, newValue);
}
};
@After
public void tearDown() throws InterruptedException {
@@ -222,8 +265,9 @@ public class EventListenerWithTCPSocketReplicationTest {
.autoReconnectedUponDroppedConnection(true);
return ChronicleMapBuilder.of(Integer.class, CharSequence.class)
.entries(20000L)
.replication((byte) 1, tcpConfig);
// .eventListener(eventListener);
.replication((byte) 1, tcpConfig)
.entryOperations(entryOperations)
.remoteOperations(remoteOperations);
}
private void waitTillReplicated(final int timeOutMs) throws InterruptedException {
@@ -252,7 +296,8 @@ public class EventListenerWithTCPSocketReplicationTest {
}
private ChronicleMapBuilder<Integer, CharSequence> serverBuilder() {
TcpTransportAndNetworkConfig tcpConfig = TcpTransportAndNetworkConfig.of(8076, new InetSocketAddress("localhost", 8077))
TcpTransportAndNetworkConfig tcpConfig = TcpTransportAndNetworkConfig
.of(8076, new InetSocketAddress("localhost", 8077))
.autoReconnectedUponDroppedConnection(true);
return ChronicleMapBuilder.of(Integer.class, CharSequence.class)
.entries(20000L)
@@ -224,11 +224,6 @@ public class ReplicationCheckingMap<K, V> implements ChronicleMap<K, V> {
return map1.getMapped(key, function);
}
@Override
public V putMapped(K key, @NotNull UnaryOperator<V> unaryOperator) {
return map1.putMapped(key, unaryOperator);
}
@Override
public void getAll(File toFile) {
throw new UnsupportedOperationException();
@@ -146,7 +146,7 @@ public class TestEntriesSpanningSeveralChunksReplicationHub {
public static final int NUMBER_OF_CHANNELS = 1000;
@Ignore
@Ignore("TODO performance test? invalid test? a test revealing a bug?")
@Test(timeout = 100000)
public void test1000Channels()
throws IOException, InterruptedException {
@@ -22,6 +22,7 @@ import org.junit.Test;
public class WarnOnWindowsTest {
@Test
// TODO this does not emit a proper warning, there are TODOs in ChronicleMapBuilder/VanillaHash
@Ignore("This test results to OOM/jvm crash, run manually to verify warning output")
public void warnOnWindowsTest() {
ChronicleMapBuilder.of(Long.class, Long.class)
@@ -82,7 +82,6 @@ public class SimpleMapOperationsListeningTest {
}
@Test
@Ignore("Example test, run manually to see the output")
public void simpleLoggingTest() {
ChronicleMap<Integer, IntValue> map = ChronicleMapBuilder
.of(Integer.class, IntValue.class)