@Test public void testAcquireAndGet() throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException { int entries = 1000 * 1000; SharedHashMap<CharSequence, LongValue> map = getSharedMap(entries, 128, 24); LongValue value = new LongValueNative(); LongValue value2 = new LongValueNative(); LongValue value3 = new LongValueNative(); for (int j = 1; j <= 3; j++) { for (int i = 0; i < entries; i++) { CharSequence userCS = getUserCharSequence(i); if (j > 1) { assertNotNull(map.getUsing(userCS, value)); } else { map.acquireUsing(userCS, value); } assertEquals(j - 1, value.getValue()); value.addAtomicValue(1); assertEquals(value2, map.acquireUsing(userCS, value2)); assertEquals(j, value2.getValue()); assertEquals(value3, map.getUsing(userCS, value3)); assertEquals(j, value3.getValue()); } } map.close(); }
@Override public void run() { try { LongValue value = new LongValueNative(); barrier.await(); for (int i = 0; i < iterations; i++) { map.acquireUsing(key, value); value.addAtomicValue(1); } } catch (Exception e) { e.printStackTrace(); } }
@Test public void testAcquireFromMultipleThreads() throws Exception { SharedHashMap<CharSequence, LongValue> map = getSharedMap(1000 * 1000, 128, 24); CharSequence key = getUserCharSequence(0); map.acquireUsing(key, new LongValueNative()); int iterations = 1000; int noOfThreads = 10; CyclicBarrier barrier = new CyclicBarrier(noOfThreads); Thread[] threads = new Thread[noOfThreads]; for (int t = 0; t < noOfThreads; t++) { threads[t] = new Thread(new IncrementRunnable(map, key, iterations, barrier)); threads[t].start(); } for (int t = 0; t < noOfThreads; t++) { threads[t].join(); } assertEquals(noOfThreads * iterations, map.acquireUsing(key, new LongValueNative()).getValue()); map.close(); }
@Test public void testGetWithNullContainer() throws Exception { SharedHashMap<CharSequence, LongValue> map = getSharedMap(10 * 1000, 128, 24); map.acquireUsing("key", new LongValueNative()); assertEquals(0, map.getUsing("key", null).getValue()); }
@Test public void testAcquireWithNullKey() throws Exception { SharedHashMap<CharSequence, LongValue> map = getSharedMap(10 * 1000, 128, 24); assertNull(map.acquireUsing(null, new LongValueNative())); }