use of org.apache.pulsar.common.util.collections.ConcurrentLongPairSet in project incubator-pulsar by apache.
the class ConcurrentLongPairSetTest method concurrentInsertions.
@Test
public void concurrentInsertions() throws Throwable {
ConcurrentLongPairSet set = new ConcurrentLongPairSet();
ExecutorService executor = Executors.newCachedThreadPool();
final int nThreads = 16;
final int N = 100_000;
List<Future<?>> futures = new ArrayList<>();
for (int i = 0; i < nThreads; i++) {
final int threadIdx = i;
futures.add(executor.submit(() -> {
Random random = new Random();
for (int j = 0; j < N; j++) {
long key = random.nextLong();
// Ensure keys are unique
key -= key % (threadIdx + 1);
key = Math.abs(key);
set.add(key, key);
}
}));
}
for (Future<?> future : futures) {
future.get();
}
assertEquals(set.size(), N * nThreads);
executor.shutdown();
}
use of org.apache.pulsar.common.util.collections.ConcurrentLongPairSet in project incubator-pulsar by apache.
the class ConcurrentLongPairSetTest method simpleInsertions.
@Test
public void simpleInsertions() {
ConcurrentLongPairSet set = new ConcurrentLongPairSet(16);
assertTrue(set.isEmpty());
assertTrue(set.add(1, 1));
assertFalse(set.isEmpty());
assertTrue(set.add(2, 2));
assertTrue(set.add(3, 3));
assertEquals(set.size(), 3);
assertTrue(set.contains(1, 1));
assertEquals(set.size(), 3);
assertTrue(set.remove(1, 1));
assertEquals(set.size(), 2);
assertFalse(set.contains(1, 1));
assertFalse(set.contains(5, 5));
assertEquals(set.size(), 2);
assertTrue(set.add(1, 1));
assertEquals(set.size(), 3);
assertFalse(set.add(1, 1));
assertEquals(set.size(), 3);
}
use of org.apache.pulsar.common.util.collections.ConcurrentLongPairSet in project incubator-pulsar by apache.
the class ConcurrentLongPairSetTest method testRehashing.
@Test
public void testRehashing() {
int n = 16;
ConcurrentLongPairSet set = new ConcurrentLongPairSet(n / 2, 1);
assertEquals(set.capacity(), n);
assertEquals(set.size(), 0);
for (int i = 0; i < n; i++) {
set.add(i, 1);
}
assertEquals(set.capacity(), 2 * n);
assertEquals(set.size(), n);
}
use of org.apache.pulsar.common.util.collections.ConcurrentLongPairSet in project incubator-pulsar by apache.
the class ConcurrentLongPairSetTest method concurrentInsertionsAndReads.
@Test
public void concurrentInsertionsAndReads() throws Throwable {
ConcurrentLongPairSet map = new ConcurrentLongPairSet();
ExecutorService executor = Executors.newCachedThreadPool();
final int nThreads = 16;
final int N = 100_000;
List<Future<?>> futures = new ArrayList<>();
for (int i = 0; i < nThreads; i++) {
final int threadIdx = i;
futures.add(executor.submit(() -> {
Random random = new Random();
for (int j = 0; j < N; j++) {
long key = random.nextLong();
// Ensure keys are unique
key -= key % (threadIdx + 1);
key = Math.abs(key);
map.add(key, key);
}
}));
}
for (Future<?> future : futures) {
future.get();
}
assertEquals(map.size(), N * nThreads);
executor.shutdown();
}
use of org.apache.pulsar.common.util.collections.ConcurrentLongPairSet in project incubator-pulsar by apache.
the class ConcurrentLongPairSetTest method testToString.
@Test
public void testToString() {
ConcurrentLongPairSet set = new ConcurrentLongPairSet();
set.add(0, 0);
set.add(1, 1);
set.add(3, 3);
final String toString = "{[3:3], [0:0], [1:1]}";
assertEquals(set.toString(), toString);
}
Aggregations