-
Notifications
You must be signed in to change notification settings - Fork 477
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Made MessageCache add/drain/iteration thread-safe (https://issues.red…
- Loading branch information
Showing
3 changed files
with
120 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package org.jgroups.util; | ||
|
||
import org.jgroups.Address; | ||
import org.jgroups.Message; | ||
|
||
import java.util.Collection; | ||
import java.util.Map; | ||
import java.util.Queue; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentLinkedQueue; | ||
|
||
/** | ||
* A cache associating members and messages | ||
* @author Bela Ban | ||
* @since 5.3.2 | ||
*/ | ||
public class MessageCache { | ||
protected final Map<Address,Queue<Message>> map=new ConcurrentHashMap<>(); | ||
protected volatile boolean is_empty=true; | ||
|
||
public MessageCache add(Address sender, Message msg) { | ||
Queue<Message> list=map.computeIfAbsent(sender, addr -> new ConcurrentLinkedQueue<>()); | ||
list.add(msg); | ||
is_empty=false; | ||
return this; | ||
} | ||
|
||
public Collection<Message> drain(Address sender) { | ||
if(sender == null) | ||
return null; | ||
Queue<Message> queue=map.remove(sender); | ||
if(map.isEmpty()) | ||
is_empty=true; | ||
return queue; | ||
} | ||
|
||
public MessageCache clear() { | ||
map.clear(); | ||
is_empty=true; | ||
return this; | ||
} | ||
|
||
/** Returns a count of all messages */ | ||
public int size() { | ||
return map.values().stream().mapToInt(Collection::size).sum(); | ||
} | ||
|
||
public boolean isEmpty() { | ||
return is_empty; | ||
} | ||
|
||
public String toString() { | ||
return String.format("%d message(s)", size()); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
tests/junit-functional/org/jgroups/tests/MessageCacheTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package org.jgroups.tests; | ||
|
||
import org.jgroups.Address; | ||
import org.jgroups.Global; | ||
import org.jgroups.Message; | ||
import org.jgroups.ObjectMessage; | ||
import org.jgroups.util.MessageCache; | ||
import org.jgroups.util.Util; | ||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Test; | ||
|
||
import java.util.Collection; | ||
|
||
/** | ||
* Tests {@link org.jgroups.util.MessageCache} | ||
* @author Bela Ban | ||
* @since 5.3.2 | ||
*/ | ||
@Test(groups= Global.FUNCTIONAL,singleThreaded=true) | ||
public class MessageCacheTest { | ||
protected MessageCache cache; | ||
protected static final Address A=Util.createRandomAddress("A"), B=Util.createRandomAddress("B"), | ||
C=Util.createRandomAddress("C"); | ||
|
||
@BeforeMethod protected void setup() { | ||
cache=new MessageCache(); | ||
} | ||
|
||
public void testCreation() { | ||
assert cache.isEmpty(); | ||
} | ||
|
||
public void testAdd() { | ||
for(int i=1; i <= 5; i++) { | ||
cache.add(A, new ObjectMessage(A, i)); | ||
cache.add(B, new ObjectMessage(B, i+10)); | ||
} | ||
assert !cache.isEmpty(); | ||
assert cache.size() == 10; | ||
} | ||
|
||
public void testDrain() { | ||
testAdd(); | ||
Collection<Message> list=cache.drain(null); | ||
assert list == null; | ||
list=cache.drain(C); | ||
assert list == null; | ||
list=cache.drain(B); | ||
assert list.size() == 5; | ||
assert cache.size() == 5; | ||
assert !cache.isEmpty(); | ||
list=cache.drain(A); | ||
assert list.size() == 5; | ||
assert cache.isEmpty(); | ||
} | ||
} |