diff --git a/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java b/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java index 839eb6ed0..a24732d8d 100644 --- a/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java @@ -1158,7 +1158,7 @@ final class ReplicatedChronicleMap, if (eventListener != null) { eventListener.onPut(toKey.toInstance(copies, key, keySize), toValue.toInstance(copies, value, valueSize), null, false, - prevValueSize == 0); + false); } } } @@ -2376,7 +2376,7 @@ class VanillaChronicleMap, if (eventListener != null) { eventListener.onPut(toKey.toInstance(copies, key, keySize), toValue.toInstance(copies, value, valueSize), prevValueInstance, remote, - false); + entryIsDeleted); } return resultUnused ? null : prevValue; diff --git a/src/test/java/net/openhft/chronicle/map/MapEventListenerOnPutAddedFlagTest.java b/src/test/java/net/openhft/chronicle/map/MapEventListenerOnPutAddedFlagTest.java new file mode 100644 index 000000000..96ff62ba5 --- /dev/null +++ b/src/test/java/net/openhft/chronicle/map/MapEventListenerOnPutAddedFlagTest.java @@ -0,0 +1,77 @@ +/* + * 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 . + */ + +package net.openhft.chronicle.map; + +import org.jetbrains.annotations.Nullable; +import org.junit.Assert; +import org.junit.Test; + +public class MapEventListenerOnPutAddedFlagTest { + + class ExpectedAddFlagChecker extends MapEventListener { + boolean expected; + + public void setExpected(boolean expected) { + this.expected = expected; + } + + @Override + public void onPut(Object key, Object newValue, @Nullable Object replacedValue, + boolean replicationEvent, boolean added) { + Assert.assertEquals(expected, added); + } + } + + @Test + public void testAddedFlagVanilla() { + ExpectedAddFlagChecker checker = new ExpectedAddFlagChecker<>(); + test(ChronicleMapBuilder.of(Integer.class, CharSequence.class) + .eventListener(checker) + .entries(1).create(), checker); + } + + @Test + public void testAddedFlagReplicated() { + ExpectedAddFlagChecker checker = new ExpectedAddFlagChecker<>(); + test(ChronicleMapBuilder.of(Integer.class, CharSequence.class) + .eventListener(checker) + .entries(1).replication((byte) 1).create(), checker); + } + + private static void test(ChronicleMap map, + ExpectedAddFlagChecker checker) { + checker.setExpected(true); + map.acquireUsing(1, new StringBuilder()); + + checker.setExpected(false); + map.put(1, "2"); + + checker.setExpected(false); + map.update(1, "2"); + map.update(1, "3"); + + map.remove(1); + checker.setExpected(true); + map.update(1, "3"); + + map.remove(1); + map.update(1, "4"); + + map.remove(1); + map.put(1, "5"); + } +}