示例#1
0
  // Do some simple concurrent testing
  public void testConcurrentSimple() throws InterruptedException {
    final NonBlockingIdentityHashMap<String, String> nbhm =
        new NonBlockingIdentityHashMap<String, String>();
    final String[] keys = new String[20000];
    for (int i = 0; i < 20000; i++) keys[i] = "k" + i;

    // In 2 threads, add & remove even & odd elements concurrently
    Thread t1 =
        new Thread() {
          public void run() {
            work_helper(nbhm, "T1", 1, keys);
          }
        };
    t1.start();
    work_helper(nbhm, "T0", 0, keys);
    t1.join();

    // In the end, all members should be removed
    StringBuffer buf = new StringBuffer();
    buf.append("Should be emptyset but has these elements: {");
    boolean found = false;
    for (String x : nbhm.keySet()) {
      buf.append(" ").append(x);
      found = true;
    }
    if (found) System.out.println(buf + " }");
    assertThat("concurrent size=0", nbhm.size(), is(0));
    for (String x : nbhm.keySet()) {
      assertTrue("No elements so never get here", false);
    }
  }
示例#2
0
  // Concurrent insertion & then iterator test.
  public static void testNonBlockingIdentityHashMapIterator() throws InterruptedException {
    final int ITEM_COUNT1 = 1000;
    final int THREAD_COUNT = 5;
    final int PER_CNT = ITEM_COUNT1 / THREAD_COUNT;
    final int ITEM_COUNT = PER_CNT * THREAD_COUNT; // fix roundoff for odd thread counts

    NonBlockingIdentityHashMap<Long, TestKey> nbhml =
        new NonBlockingIdentityHashMap<Long, TestKey>();
    // use a barrier to open the gate for all threads at once to avoid rolling
    // start and no actual concurrency
    final CyclicBarrier barrier = new CyclicBarrier(THREAD_COUNT);
    final ExecutorService ex = Executors.newFixedThreadPool(THREAD_COUNT);
    final CompletionService<Object> co = new ExecutorCompletionService<Object>(ex);
    for (int i = 0; i < THREAD_COUNT; i++) {
      co.submit(new NBHMLFeeder(nbhml, PER_CNT, barrier, i * PER_CNT));
    }
    for (int retCount = 0; retCount < THREAD_COUNT; retCount++) {
      co.take();
    }
    ex.shutdown();

    assertEquals("values().size()", ITEM_COUNT, nbhml.values().size());
    assertEquals("entrySet().size()", ITEM_COUNT, nbhml.entrySet().size());
    int itemCount = 0;
    for (TestKey K : nbhml.values()) itemCount++;
    assertEquals("values().iterator() count", ITEM_COUNT, itemCount);
  }
示例#3
0
  public void testSerial() {
    assertTrue(_nbhm.isEmpty());
    final String k1 = "k1";
    final String k2 = "k2";
    assertThat(_nbhm.put(k1, "v1"), nullValue());
    assertThat(_nbhm.put(k2, "v2"), nullValue());

    // Serialize it out
    try {
      FileOutputStream fos = new FileOutputStream("NBHM_test.txt");
      ObjectOutputStream out = new ObjectOutputStream(fos);
      out.writeObject(_nbhm);
      out.close();
    } catch (IOException ex) {
      ex.printStackTrace();
    }

    // Read it back
    try {
      File f = new File("NBHM_test.txt");
      FileInputStream fis = new FileInputStream(f);
      ObjectInputStream in = new ObjectInputStream(fis);
      NonBlockingIdentityHashMap nbhm = (NonBlockingIdentityHashMap) in.readObject();
      in.close();
      assertThat(
          "serialization works",
          nbhm.toString(),
          anyOf(is("{k1=v1, k2=v2}"), is("{k2=v2, k1=v1}")));
      if (!f.delete()) throw new IOException("delete failed");
    } catch (IOException ex) {
      ex.printStackTrace();
    } catch (ClassNotFoundException ex) {
      ex.printStackTrace();
    }
  }
示例#4
0
 // Check all iterators for correct size counts
 private void checkSizes(int expectedSize) {
   assertEquals("size()", _nbhm.size(), expectedSize);
   Collection<String> vals = _nbhm.values();
   checkSizes("values()", vals.size(), vals.iterator(), expectedSize);
   Set<String> keys = _nbhm.keySet();
   checkSizes("keySet()", keys.size(), keys.iterator(), expectedSize);
   Set<Entry<String, String>> ents = _nbhm.entrySet();
   checkSizes("entrySet()", ents.size(), ents.iterator(), expectedSize);
 }
示例#5
0
  public final void testNonBlockingIdentityHashMapSize() {
    NonBlockingIdentityHashMap<Long, String> items = new NonBlockingIdentityHashMap<Long, String>();
    items.put(Long.valueOf(100), "100");
    items.put(Long.valueOf(101), "101");

    assertEquals("keySet().size()", 2, items.keySet().size());
    assertTrue("keySet().contains(100)", items.keySet().contains(Long.valueOf(100)));
    assertTrue("keySet().contains(101)", items.keySet().contains(Long.valueOf(101)));

    assertEquals("values().size()", 2, items.values().size());
    assertTrue("values().contains(\"100\")", items.values().contains("100"));
    assertTrue("values().contains(\"101\")", items.values().contains("101"));

    assertEquals("entrySet().size()", 2, items.entrySet().size());
    boolean found100 = false;
    boolean found101 = false;
    for (Entry<Long, String> entry : items.entrySet()) {
      if (entry.getKey().equals(Long.valueOf(100))) {
        assertEquals("entry[100].getValue()==\"100\"", "100", entry.getValue());
        found100 = true;
      } else if (entry.getKey().equals(Long.valueOf(101))) {
        assertEquals("entry[101].getValue()==\"101\"", "101", entry.getValue());
        found101 = true;
      }
    }
    assertTrue("entrySet().contains([100])", found100);
    assertTrue("entrySet().contains([101])", found101);
  }
示例#6
0
 public void testIterationBig2() {
   final int CNT = 10000;
   NonBlockingIdentityHashMap<Integer, String> nbhm =
       new NonBlockingIdentityHashMap<Integer, String>();
   final String v = "v";
   for (int i = 0; i < CNT; i++) {
     final Integer z = new Integer(i);
     String s0 = nbhm.get(z);
     assertThat(s0, nullValue());
     nbhm.put(z, v);
     String s1 = nbhm.get(z);
     assertThat(s1, is(v));
   }
   assertThat(nbhm.size(), is(CNT));
 }
示例#7
0
 public Integer call() throws Exception {
   _barrier.await();
   int count = 0;
   for (TestKey item : _items) {
     if (_map.contains(item._id)) {
       System.err.printf("COLLISION DETECTED: %s exists\n", item.toString());
     }
     final TestKey exists = _map.putIfAbsent(item._id, item);
     if (exists == null) {
       count++;
     } else {
       System.err.printf(
           "COLLISION DETECTED: %s exists as %s\n", item.toString(), exists.toString());
     }
   }
   return count;
 }
示例#8
0
 public Object call() throws Exception {
   _barrier.await(); // barrier, to force racing start
   for (long j = 0; j < _count; j++)
     _map.put(
         j + _offset,
         new TestKey(_rand.nextLong(), _rand.nextInt(), (short) _rand.nextInt(Short.MAX_VALUE)));
   return null;
 }
示例#9
0
 void work_helper(
     NonBlockingIdentityHashMap<String, String> nbhm, String thrd, int d, String[] keys) {
   final int ITERS = 20000;
   for (int j = 0; j < 10; j++) {
     long start = System.nanoTime();
     for (int i = d; i < ITERS; i += 2)
       assertThat(
           "this key not in there, so putIfAbsent must work",
           nbhm.putIfAbsent(keys[i], thrd),
           is((String) null));
     for (int i = d; i < ITERS; i += 2) assertTrue(nbhm.remove(keys[i], thrd));
     double delta_nanos = System.nanoTime() - start;
     double delta_secs = delta_nanos / 1000000000.0;
     double ops = ITERS * 2;
     // System.out.println("Thrd"+thrd+" "+(ops/delta_secs)+" ops/sec size="+nbhm.size());
   }
 }
示例#10
0
  public void testIteration() {
    assertTrue(_nbhm.isEmpty());
    assertThat(_nbhm.put("k1", "v1"), nullValue());
    assertThat(_nbhm.put("k2", "v2"), nullValue());

    String str1 = "";
    for (Iterator<Map.Entry<String, String>> i = _nbhm.entrySet().iterator(); i.hasNext(); ) {
      Map.Entry<String, String> e = i.next();
      str1 += e.getKey();
    }
    assertThat("found all entries", str1, anyOf(is("k1k2"), is("k2k1")));

    String str2 = "";
    for (Iterator<String> i = _nbhm.keySet().iterator(); i.hasNext(); ) {
      String key = i.next();
      str2 += key;
    }
    assertThat("found all keys", str2, anyOf(is("k1k2"), is("k2k1")));

    String str3 = "";
    for (Iterator<String> i = _nbhm.values().iterator(); i.hasNext(); ) {
      String val = i.next();
      str3 += val;
    }
    assertThat("found all vals", str3, anyOf(is("v1v2"), is("v2v1")));

    assertThat(
        "toString works", _nbhm.toString(), anyOf(is("{k1=v1, k2=v2}"), is("{k2=v2, k1=v1}")));
  }
示例#11
0
  public void testIterationBig() {
    final int CNT = 10000;
    String[] keys = new String[CNT];
    String[] vals = new String[CNT];
    assertThat(_nbhm.size(), is(0));
    for (int i = 0; i < CNT; i++) _nbhm.put(keys[i] = ("k" + i), vals[i] = ("v" + i));
    assertThat(_nbhm.size(), is(CNT));

    int sz = 0;
    int sum = 0;
    for (String s : _nbhm.keySet()) {
      sz++;
      assertThat("", s.charAt(0), is('k'));
      int x = Integer.parseInt(s.substring(1));
      sum += x;
      assertTrue(x >= 0 && x <= (CNT - 1));
    }
    assertThat("Found 10000 ints", sz, is(CNT));
    assertThat("Found all integers in list", sum, is(CNT * (CNT - 1) / 2));

    assertThat("can remove 3", _nbhm.remove(keys[3]), is(vals[3]));
    assertThat("can remove 4", _nbhm.remove(keys[4]), is(vals[4]));
    sz = 0;
    sum = 0;
    for (String s : _nbhm.keySet()) {
      sz++;
      assertThat("", s.charAt(0), is('k'));
      int x = Integer.parseInt(s.substring(1));
      sum += x;
      assertTrue(x >= 0 && x <= (CNT - 1));
      String v = _nbhm.get(s);
      assertThat("", v.charAt(0), is('v'));
      assertThat("", s.substring(1), is(v.substring(1)));
    }
    assertThat("Found " + (CNT - 2) + " ints", sz, is(CNT - 2));
    assertThat("Found all integers in list", sum, is(CNT * (CNT - 1) / 2 - (3 + 4)));
  }
示例#12
0
  // ---
  public void testNonBlockingIdentityHashMapIteratorMultithreaded()
      throws InterruptedException, ExecutionException {
    TestKeyFeeder feeder = getTestKeyFeeder();
    final int itemCount = feeder.size();

    // validate results
    final NonBlockingIdentityHashMap<Long, TestKey> items = feeder.getMapMultithreaded();
    assertEquals("size()", itemCount, items.size());

    assertEquals("values().size()", itemCount, items.values().size());

    assertEquals("entrySet().size()", itemCount, items.entrySet().size());

    int iteratorCount = 0;
    for (TestKey m : items.values()) iteratorCount++;
    // sometimes a different result comes back the second time
    int iteratorCount2 = 0;
    for (Iterator<TestKey> it = items.values().iterator(); it.hasNext(); ) {
      iteratorCount2++;
      it.next();
    }
    assertEquals("iterator counts differ", iteratorCount, iteratorCount2);
    assertEquals("values().iterator() count", itemCount, iteratorCount);
  }
示例#13
0
  // Test some basic stuff; add a few keys, remove a few keys
  public void testBasic() {
    assertTrue(_nbhm.isEmpty());
    assertThat(_nbhm.putIfAbsent("k1", "v1"), nullValue());
    checkSizes(1);
    assertThat(_nbhm.putIfAbsent("k2", "v2"), nullValue());
    checkSizes(2);
    assertTrue(_nbhm.containsKey("k2"));
    assertThat(_nbhm.put("k1", "v1a"), is("v1"));
    assertThat(_nbhm.put("k2", "v2a"), is("v2"));
    checkSizes(2);
    assertThat(_nbhm.putIfAbsent("k2", "v2b"), is("v2a"));
    assertThat(_nbhm.remove("k1"), is("v1a"));
    assertFalse(_nbhm.containsKey("k1"));
    checkSizes(1);
    assertThat(_nbhm.remove("k1"), nullValue());
    assertThat(_nbhm.remove("k2"), is("v2a"));
    checkSizes(0);
    assertThat(_nbhm.remove("k2"), nullValue());
    assertThat(_nbhm.remove("k3"), nullValue());
    assertTrue(_nbhm.isEmpty());

    assertThat(_nbhm.put("k0", "v0"), nullValue());
    assertTrue(_nbhm.containsKey("k0"));
    checkSizes(1);
    assertThat(_nbhm.remove("k0"), is("v0"));
    assertFalse(_nbhm.containsKey("k0"));
    checkSizes(0);

    assertThat(_nbhm.replace("k0", "v0"), nullValue());
    assertFalse(_nbhm.containsKey("k0"));
    assertThat(_nbhm.put("k0", "v0"), nullValue());
    assertEquals(_nbhm.replace("k0", "v0a"), "v0");
    assertEquals(_nbhm.get("k0"), "v0a");
    assertThat(_nbhm.remove("k0"), is("v0a"));
    assertFalse(_nbhm.containsKey("k0"));
    checkSizes(0);

    assertThat(_nbhm.replace("k1", "v1"), nullValue());
    assertFalse(_nbhm.containsKey("k1"));
    assertThat(_nbhm.put("k1", "v1"), nullValue());
    assertEquals(_nbhm.replace("k1", "v1a"), "v1");
    assertEquals(_nbhm.get("k1"), "v1a");
    assertThat(_nbhm.remove("k1"), is("v1a"));
    assertFalse(_nbhm.containsKey("k1"));
    checkSizes(0);

    // Insert & Remove KeyBonks until the table resizes and we start
    // finding Tombstone keys- and KeyBonk's equals-call with throw a
    // ClassCastException if it sees a non-KeyBonk.
    NonBlockingIdentityHashMap<KeyBonk, String> dumb =
        new NonBlockingIdentityHashMap<KeyBonk, String>();
    for (int i = 0; i < 10000; i++) {
      final KeyBonk happy1 = new KeyBonk(i);
      assertThat(dumb.put(happy1, "and"), nullValue());
      if ((i & 1) == 0) dumb.remove(happy1);
      final KeyBonk happy2 = new KeyBonk(i); // 'equals' but not '=='
      dumb.get(happy2);
    }
  }