6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
< |
import junit.framework.*; |
10 |
< |
import java.util.*; |
9 |
> |
import java.util.ArrayList; |
10 |
> |
import java.util.Arrays; |
11 |
> |
import java.util.Collection; |
12 |
> |
import java.util.Collections; |
13 |
> |
import java.util.Enumeration; |
14 |
> |
import java.util.Iterator; |
15 |
> |
import java.util.Map; |
16 |
> |
import java.util.Random; |
17 |
> |
import java.util.Set; |
18 |
|
import java.util.concurrent.ConcurrentHashMap; |
19 |
+ |
import java.util.concurrent.ExecutorService; |
20 |
+ |
import java.util.concurrent.Executors; |
21 |
+ |
|
22 |
+ |
import junit.framework.Test; |
23 |
+ |
import junit.framework.TestSuite; |
24 |
|
|
25 |
|
public class ConcurrentHashMapTest extends JSR166TestCase { |
26 |
|
public static void main(String[] args) { |
27 |
< |
junit.textui.TestRunner.run(suite()); |
27 |
> |
main(suite(), args); |
28 |
|
} |
29 |
|
public static Test suite() { |
30 |
|
return new TestSuite(ConcurrentHashMapTest.class); |
47 |
|
} |
48 |
|
|
49 |
|
/** Re-implement Integer.compare for old java versions */ |
50 |
< |
static int compare(int x, int y) { return x < y ? -1 : x > y ? 1 : 0; } |
50 |
> |
static int compare(int x, int y) { |
51 |
> |
return (x < y) ? -1 : (x > y) ? 1 : 0; |
52 |
> |
} |
53 |
|
|
54 |
|
// classes for testing Comparable fallbacks |
55 |
|
static class BI implements Comparable<BI> { |
291 |
|
assertEquals("A", (String)map.get(one)); |
292 |
|
ConcurrentHashMap empty = new ConcurrentHashMap(); |
293 |
|
assertNull(map.get("anything")); |
294 |
+ |
assertNull(empty.get("anything")); |
295 |
|
} |
296 |
|
|
297 |
|
/** |
537 |
|
/** |
538 |
|
* Constructor (initialCapacity, loadFactor) throws |
539 |
|
* IllegalArgumentException if either argument is negative |
540 |
< |
*/ |
540 |
> |
*/ |
541 |
|
public void testConstructor2() { |
542 |
|
try { |
543 |
|
new ConcurrentHashMap(-1, .75f); |
544 |
|
shouldThrow(); |
545 |
< |
} catch (IllegalArgumentException e) {} |
545 |
> |
} catch (IllegalArgumentException success) {} |
546 |
|
|
547 |
|
try { |
548 |
|
new ConcurrentHashMap(16, -1); |
549 |
|
shouldThrow(); |
550 |
< |
} catch (IllegalArgumentException e) {} |
550 |
> |
} catch (IllegalArgumentException success) {} |
551 |
|
} |
552 |
|
|
553 |
|
/** |
558 |
|
try { |
559 |
|
new ConcurrentHashMap(-1, .75f, 1); |
560 |
|
shouldThrow(); |
561 |
< |
} catch (IllegalArgumentException e) {} |
561 |
> |
} catch (IllegalArgumentException success) {} |
562 |
|
|
563 |
|
try { |
564 |
|
new ConcurrentHashMap(16, -1, 1); |
565 |
|
shouldThrow(); |
566 |
< |
} catch (IllegalArgumentException e) {} |
566 |
> |
} catch (IllegalArgumentException success) {} |
567 |
|
|
568 |
|
try { |
569 |
|
new ConcurrentHashMap(16, .75f, -1); |
570 |
|
shouldThrow(); |
571 |
< |
} catch (IllegalArgumentException e) {} |
571 |
> |
} catch (IllegalArgumentException success) {} |
572 |
|
} |
573 |
|
|
574 |
|
/** |
579 |
|
try { |
580 |
|
new ConcurrentHashMap(null); |
581 |
|
shouldThrow(); |
582 |
< |
} catch (NullPointerException e) {} |
582 |
> |
} catch (NullPointerException success) {} |
583 |
|
} |
584 |
|
|
585 |
|
/** |
594 |
|
assertFalse(map2.equals(map1)); |
595 |
|
} |
596 |
|
|
582 |
– |
|
597 |
|
/** |
598 |
|
* get(null) throws NPE |
599 |
|
*/ |
600 |
|
public void testGet_NullPointerException() { |
601 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
602 |
|
try { |
588 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
603 |
|
c.get(null); |
604 |
|
shouldThrow(); |
605 |
|
} catch (NullPointerException success) {} |
609 |
|
* containsKey(null) throws NPE |
610 |
|
*/ |
611 |
|
public void testContainsKey_NullPointerException() { |
612 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
613 |
|
try { |
599 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
614 |
|
c.containsKey(null); |
615 |
|
shouldThrow(); |
616 |
|
} catch (NullPointerException success) {} |
620 |
|
* containsValue(null) throws NPE |
621 |
|
*/ |
622 |
|
public void testContainsValue_NullPointerException() { |
623 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
624 |
|
try { |
610 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
625 |
|
c.containsValue(null); |
626 |
|
shouldThrow(); |
627 |
|
} catch (NullPointerException success) {} |
631 |
|
* contains(null) throws NPE |
632 |
|
*/ |
633 |
|
public void testContains_NullPointerException() { |
634 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
635 |
|
try { |
621 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
636 |
|
c.contains(null); |
637 |
|
shouldThrow(); |
638 |
|
} catch (NullPointerException success) {} |
642 |
|
* put(null,x) throws NPE |
643 |
|
*/ |
644 |
|
public void testPut1_NullPointerException() { |
645 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
646 |
|
try { |
632 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
647 |
|
c.put(null, "whatever"); |
648 |
|
shouldThrow(); |
649 |
|
} catch (NullPointerException success) {} |
653 |
|
* put(x, null) throws NPE |
654 |
|
*/ |
655 |
|
public void testPut2_NullPointerException() { |
656 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
657 |
|
try { |
643 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
658 |
|
c.put("whatever", null); |
659 |
|
shouldThrow(); |
660 |
|
} catch (NullPointerException success) {} |
664 |
|
* putIfAbsent(null, x) throws NPE |
665 |
|
*/ |
666 |
|
public void testPutIfAbsent1_NullPointerException() { |
667 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
668 |
|
try { |
654 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
669 |
|
c.putIfAbsent(null, "whatever"); |
670 |
|
shouldThrow(); |
671 |
|
} catch (NullPointerException success) {} |
675 |
|
* replace(null, x) throws NPE |
676 |
|
*/ |
677 |
|
public void testReplace_NullPointerException() { |
678 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
679 |
|
try { |
665 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
680 |
|
c.replace(null, "whatever"); |
681 |
|
shouldThrow(); |
682 |
|
} catch (NullPointerException success) {} |
686 |
|
* replace(null, x, y) throws NPE |
687 |
|
*/ |
688 |
|
public void testReplaceValue_NullPointerException() { |
689 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
690 |
|
try { |
676 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
691 |
|
c.replace(null, one, "whatever"); |
692 |
|
shouldThrow(); |
693 |
|
} catch (NullPointerException success) {} |
697 |
|
* putIfAbsent(x, null) throws NPE |
698 |
|
*/ |
699 |
|
public void testPutIfAbsent2_NullPointerException() { |
700 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
701 |
|
try { |
687 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
702 |
|
c.putIfAbsent("whatever", null); |
703 |
|
shouldThrow(); |
704 |
|
} catch (NullPointerException success) {} |
708 |
|
* replace(x, null) throws NPE |
709 |
|
*/ |
710 |
|
public void testReplace2_NullPointerException() { |
711 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
712 |
|
try { |
698 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
713 |
|
c.replace("whatever", null); |
714 |
|
shouldThrow(); |
715 |
|
} catch (NullPointerException success) {} |
719 |
|
* replace(x, null, y) throws NPE |
720 |
|
*/ |
721 |
|
public void testReplaceValue2_NullPointerException() { |
722 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
723 |
|
try { |
709 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
724 |
|
c.replace("whatever", null, "A"); |
725 |
|
shouldThrow(); |
726 |
|
} catch (NullPointerException success) {} |
730 |
|
* replace(x, y, null) throws NPE |
731 |
|
*/ |
732 |
|
public void testReplaceValue3_NullPointerException() { |
733 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
734 |
|
try { |
720 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
735 |
|
c.replace("whatever", one, null); |
736 |
|
shouldThrow(); |
737 |
|
} catch (NullPointerException success) {} |
741 |
|
* remove(null) throws NPE |
742 |
|
*/ |
743 |
|
public void testRemove1_NullPointerException() { |
744 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
745 |
+ |
c.put("sadsdf", "asdads"); |
746 |
|
try { |
731 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
732 |
– |
c.put("sadsdf", "asdads"); |
747 |
|
c.remove(null); |
748 |
|
shouldThrow(); |
749 |
|
} catch (NullPointerException success) {} |
753 |
|
* remove(null, x) throws NPE |
754 |
|
*/ |
755 |
|
public void testRemove2_NullPointerException() { |
756 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
757 |
+ |
c.put("sadsdf", "asdads"); |
758 |
|
try { |
743 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
744 |
– |
c.put("sadsdf", "asdads"); |
759 |
|
c.remove(null, "whatever"); |
760 |
|
shouldThrow(); |
761 |
|
} catch (NullPointerException success) {} |
805 |
|
} |
806 |
|
} |
807 |
|
|
808 |
+ |
/** |
809 |
+ |
* Tests performance of removeAll when the other collection is much smaller. |
810 |
+ |
* ant -Djsr166.tckTestClass=ConcurrentHashMapTest -Djsr166.methodFilter=testRemoveAll_performance -Djsr166.expensiveTests=true tck |
811 |
+ |
*/ |
812 |
+ |
public void testRemoveAll_performance() { |
813 |
+ |
final int mapSize = expensiveTests ? 1_000_000 : 100; |
814 |
+ |
final int iterations = expensiveTests ? 500 : 2; |
815 |
+ |
final ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>(); |
816 |
+ |
for (int i = 0; i < mapSize; i++) |
817 |
+ |
map.put(i, i); |
818 |
+ |
Set<Integer> keySet = map.keySet(); |
819 |
+ |
Collection<Integer> removeMe = Arrays.asList(new Integer[] { -99, -86 }); |
820 |
+ |
for (int i = 0; i < iterations; i++) |
821 |
+ |
assertFalse(keySet.removeAll(removeMe)); |
822 |
+ |
assertEquals(mapSize, map.size()); |
823 |
+ |
} |
824 |
+ |
|
825 |
+ |
/** |
826 |
+ |
* Tests performance of computeIfAbsent when the element is present. |
827 |
+ |
* See JDK-8161372 |
828 |
+ |
* ant -Djsr166.tckTestClass=ConcurrentHashMapTest -Djsr166.methodFilter=testcomputeIfAbsent_performance -Djsr166.expensiveTests=true tck |
829 |
+ |
*/ |
830 |
+ |
public void testcomputeIfAbsent_performance() { |
831 |
+ |
final int mapSize = 20; |
832 |
+ |
final int iterations = expensiveTests ? (1 << 23) : mapSize * 2; |
833 |
+ |
final int threads = expensiveTests ? 10 : 2; |
834 |
+ |
final ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>(); |
835 |
+ |
for (int i = 0; i < mapSize; i++) |
836 |
+ |
map.put(i, i); |
837 |
+ |
final ExecutorService pool = Executors.newFixedThreadPool(2); |
838 |
+ |
try (PoolCleaner cleaner = cleaner(pool)) { |
839 |
+ |
Runnable r = new CheckedRunnable() { |
840 |
+ |
public void realRun() { |
841 |
+ |
int result = 0; |
842 |
+ |
for (int i = 0; i < iterations; i++) |
843 |
+ |
result += map.computeIfAbsent(i % mapSize, (k) -> k + k); |
844 |
+ |
if (result == -42) throw new Error(); |
845 |
+ |
}}; |
846 |
+ |
for (int i = 0; i < threads; i++) |
847 |
+ |
pool.execute(r); |
848 |
+ |
} |
849 |
+ |
} |
850 |
+ |
|
851 |
|
} |