@Test public void addShouldLeadToMonoticMSNToUIDConversionWhenConcurrent() throws Exception { int operationCount = 1000; int threadCount = 2; ConcurrentTestRunner.builder() .operation((threadNumber, step) -> testee.addUid(MessageUid.of((threadNumber * operationCount) + (step + 1)))) .threadCount(threadCount) .operationCount(operationCount) .runSuccessfullyWithin(Duration.ofSeconds(10)); ImmutableMap.Builder<Integer, MessageUid> resultBuilder = ImmutableMap.builder(); for (int i = 1; i <= threadCount * operationCount; i++) { resultBuilder.put(i, MessageUid.of(i)); } assertThat(mapTesteeInternalDataToMsnByUid().entrySet()) .containsExactlyElementsOf(resultBuilder.build().entrySet()); }
public synchronized void addUid(MessageUid uid) { if (uids.contains(uid)) { return; } if (isLastUid(uid)) { uids.add(uid); } else { uids.add(uid); Collections.sort(uids); } }
UidMsnConverter { public synchronized void addUid(MessageUid uid) { if (uids.contains(uid)) { return; } if (isLastUid(uid)) { uids.add(uid); } else { uids.add(uid); Collections.sort(uids); } } }
UidMsnConverter { public synchronized void addUid(MessageUid uid) { if (uids.contains(uid)) { return; } if (isLastUid(uid)) { uids.add(uid); } else { uids.add(uid); Collections.sort(uids); } } UidMsnConverter(); }
UidMsnConverter { public synchronized void addUid(MessageUid uid) { if (uids.contains(uid)) { return; } if (isLastUid(uid)) { uids.add(uid); } else { uids.add(uid); Collections.sort(uids); } } UidMsnConverter(); synchronized void addAll(List<MessageUid> addedUids); synchronized NullableMessageSequenceNumber getMsn(MessageUid uid); synchronized Optional<MessageUid> getUid(int msn); synchronized Optional<MessageUid> getLastUid(); synchronized Optional<MessageUid> getFirstUid(); synchronized int getNumMessage(); synchronized void remove(MessageUid uid); synchronized boolean isEmpty(); synchronized void clear(); synchronized void addUid(MessageUid uid); }
UidMsnConverter { public synchronized void addUid(MessageUid uid) { if (uids.contains(uid)) { return; } if (isLastUid(uid)) { uids.add(uid); } else { uids.add(uid); Collections.sort(uids); } } UidMsnConverter(); synchronized void addAll(List<MessageUid> addedUids); synchronized NullableMessageSequenceNumber getMsn(MessageUid uid); synchronized Optional<MessageUid> getUid(int msn); synchronized Optional<MessageUid> getLastUid(); synchronized Optional<MessageUid> getFirstUid(); synchronized int getNumMessage(); synchronized void remove(MessageUid uid); synchronized boolean isEmpty(); synchronized void clear(); synchronized void addUid(MessageUid uid); static final int FIRST_MSN; }