@Test(invocationCount=10)
public void testConcurrentInsertions() throws BrokenBarrierException, InterruptedException {
Sender[] senders=new Sender[NUM_THREADS];
ConcurrentMap<Long,AtomicInteger> successful_adds=new ConcurrentHashMap<Long,AtomicInteger>();
for(int i=1; i <= NUM_MSGS; i++)
successful_adds.put((long)i, new AtomicInteger(0));
for(int i=0; i < senders.length; i++) {
senders[i]=new Sender(NUM_MSGS, win, sender, barrier, successful_adds);
senders[i].start();
}
Util.sleep(2000);
System.out.println("Concurrently inserting " + NUM_MSGS + " messages with " + NUM_THREADS + " threads");
barrier.await();
for(int i=0; i < senders.length; i++)
senders[i].join(20000);
System.out.println("OK: " + NUM_MSGS + " were added to the NakReceiverWindow concurrently by " + NUM_THREADS + " threads");
Set<Long> keys=successful_adds.keySet();
System.out.println("checking for missing or duplicate seqnos in " + keys.size() + " seqnos:");
for(int i=1; i <= NUM_MSGS; i++) {
AtomicInteger val=successful_adds.get((long)i);
if(val.get() != 1)
System.err.println(i + " was not added exactly once (successful insertions=" + val.get() + ")");
}
for(int i=1; i <= NUM_MSGS; i++) {
AtomicInteger val=successful_adds.get((long)i);
assert val != null : i + " is missing in " + successful_adds.keySet();
assert val.get() == 1 : i + " was not added exactly once (successful insertions=" + val.get() + ")";
}
System.out.println("OK: " + keys.size() + " seqnos were added exactly once");
}