8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures

Reviewed-by: psandoz, vlivanov, simonis
This commit is contained in:
Aleksey Shipilev 2016-05-04 17:17:10 +03:00
parent fbac01c4e5
commit a8fb392baf
20 changed files with 84 additions and 156 deletions

View File

@ -40,6 +40,7 @@ import static org.testng.Assert.*;
public class JdkInternalMiscUnsafeAccessTestBoolean {
static final int ITERS = Integer.getInteger("iters", 1);
static final int WEAK_ATTEMPTS = Integer.getInteger("weakAttempts", 10);
static final jdk.internal.misc.Unsafe UNSAFE;
@ -148,4 +149,3 @@ public class JdkInternalMiscUnsafeAccessTestBoolean {
}

View File

@ -40,6 +40,7 @@ import static org.testng.Assert.*;
public class JdkInternalMiscUnsafeAccessTestByte {
static final int ITERS = Integer.getInteger("iters", 1);
static final int WEAK_ATTEMPTS = Integer.getInteger("weakAttempts", 10);
static final jdk.internal.misc.Unsafe UNSAFE;
@ -185,4 +186,3 @@ public class JdkInternalMiscUnsafeAccessTestByte {
}
}

View File

@ -40,6 +40,7 @@ import static org.testng.Assert.*;
public class JdkInternalMiscUnsafeAccessTestChar {
static final int ITERS = Integer.getInteger("iters", 1);
static final int WEAK_ATTEMPTS = Integer.getInteger("weakAttempts", 10);
static final jdk.internal.misc.Unsafe UNSAFE;
@ -203,4 +204,3 @@ public class JdkInternalMiscUnsafeAccessTestChar {
}
}

View File

@ -40,6 +40,7 @@ import static org.testng.Assert.*;
public class JdkInternalMiscUnsafeAccessTestDouble {
static final int ITERS = Integer.getInteger("iters", 1);
static final int WEAK_ATTEMPTS = Integer.getInteger("weakAttempts", 10);
static final jdk.internal.misc.Unsafe UNSAFE;
@ -185,4 +186,3 @@ public class JdkInternalMiscUnsafeAccessTestDouble {
}
}

View File

@ -40,6 +40,7 @@ import static org.testng.Assert.*;
public class JdkInternalMiscUnsafeAccessTestFloat {
static final int ITERS = Integer.getInteger("iters", 1);
static final int WEAK_ATTEMPTS = Integer.getInteger("weakAttempts", 10);
static final jdk.internal.misc.Unsafe UNSAFE;
@ -185,4 +186,3 @@ public class JdkInternalMiscUnsafeAccessTestFloat {
}
}

View File

@ -40,6 +40,7 @@ import static org.testng.Assert.*;
public class JdkInternalMiscUnsafeAccessTestInt {
static final int ITERS = Integer.getInteger("iters", 1);
static final int WEAK_ATTEMPTS = Integer.getInteger("weakAttempts", 10);
static final jdk.internal.misc.Unsafe UNSAFE;
@ -251,22 +252,31 @@ public class JdkInternalMiscUnsafeAccessTestInt {
}
{
boolean r = UNSAFE.weakCompareAndSwapInt(base, offset, 1, 2);
assertEquals(r, true, "weakCompareAndSwap int");
boolean success = false;
for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
success = UNSAFE.weakCompareAndSwapInt(base, offset, 1, 2);
}
assertEquals(success, true, "weakCompareAndSwap int");
int x = UNSAFE.getInt(base, offset);
assertEquals(x, 2, "weakCompareAndSwap int value");
}
{
boolean r = UNSAFE.weakCompareAndSwapIntAcquire(base, offset, 2, 1);
assertEquals(r, true, "weakCompareAndSwapAcquire int");
boolean success = false;
for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
success = UNSAFE.weakCompareAndSwapIntAcquire(base, offset, 2, 1);
}
assertEquals(success, true, "weakCompareAndSwapAcquire int");
int x = UNSAFE.getInt(base, offset);
assertEquals(x, 1, "weakCompareAndSwapAcquire int");
}
{
boolean r = UNSAFE.weakCompareAndSwapIntRelease(base, offset, 1, 2);
assertEquals(r, true, "weakCompareAndSwapRelease int");
boolean success = false;
for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
success = UNSAFE.weakCompareAndSwapIntRelease(base, offset, 1, 2);
}
assertEquals(success, true, "weakCompareAndSwapRelease int");
int x = UNSAFE.getInt(base, offset);
assertEquals(x, 2, "weakCompareAndSwapRelease int");
}
@ -286,7 +296,7 @@ public class JdkInternalMiscUnsafeAccessTestInt {
int o = UNSAFE.getAndAddInt(base, offset, 2);
assertEquals(o, 1, "getAndAdd int");
int x = UNSAFE.getInt(base, offset);
assertEquals(x, 1 + 2, "weakCompareAndSwapRelease int");
assertEquals(x, 1 + 2, "getAndAdd int");
}
}
@ -300,4 +310,3 @@ public class JdkInternalMiscUnsafeAccessTestInt {
}
}

View File

@ -40,6 +40,7 @@ import static org.testng.Assert.*;
public class JdkInternalMiscUnsafeAccessTestLong {
static final int ITERS = Integer.getInteger("iters", 1);
static final int WEAK_ATTEMPTS = Integer.getInteger("weakAttempts", 10);
static final jdk.internal.misc.Unsafe UNSAFE;
@ -251,22 +252,31 @@ public class JdkInternalMiscUnsafeAccessTestLong {
}
{
boolean r = UNSAFE.weakCompareAndSwapLong(base, offset, 1L, 2L);
assertEquals(r, true, "weakCompareAndSwap long");
boolean success = false;
for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
success = UNSAFE.weakCompareAndSwapLong(base, offset, 1L, 2L);
}
assertEquals(success, true, "weakCompareAndSwap long");
long x = UNSAFE.getLong(base, offset);
assertEquals(x, 2L, "weakCompareAndSwap long value");
}
{
boolean r = UNSAFE.weakCompareAndSwapLongAcquire(base, offset, 2L, 1L);
assertEquals(r, true, "weakCompareAndSwapAcquire long");
boolean success = false;
for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
success = UNSAFE.weakCompareAndSwapLongAcquire(base, offset, 2L, 1L);
}
assertEquals(success, true, "weakCompareAndSwapAcquire long");
long x = UNSAFE.getLong(base, offset);
assertEquals(x, 1L, "weakCompareAndSwapAcquire long");
}
{
boolean r = UNSAFE.weakCompareAndSwapLongRelease(base, offset, 1L, 2L);
assertEquals(r, true, "weakCompareAndSwapRelease long");
boolean success = false;
for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
success = UNSAFE.weakCompareAndSwapLongRelease(base, offset, 1L, 2L);
}
assertEquals(success, true, "weakCompareAndSwapRelease long");
long x = UNSAFE.getLong(base, offset);
assertEquals(x, 2L, "weakCompareAndSwapRelease long");
}
@ -286,7 +296,7 @@ public class JdkInternalMiscUnsafeAccessTestLong {
long o = UNSAFE.getAndAddLong(base, offset, 2L);
assertEquals(o, 1L, "getAndAdd long");
long x = UNSAFE.getLong(base, offset);
assertEquals(x, 1L + 2L, "weakCompareAndSwapRelease long");
assertEquals(x, 1L + 2L, "getAndAdd long");
}
}
@ -300,4 +310,3 @@ public class JdkInternalMiscUnsafeAccessTestLong {
}
}

View File

@ -40,6 +40,7 @@ import static org.testng.Assert.*;
public class JdkInternalMiscUnsafeAccessTestObject {
static final int ITERS = Integer.getInteger("iters", 1);
static final int WEAK_ATTEMPTS = Integer.getInteger("weakAttempts", 10);
static final jdk.internal.misc.Unsafe UNSAFE;
@ -204,22 +205,31 @@ public class JdkInternalMiscUnsafeAccessTestObject {
}
{
boolean r = UNSAFE.weakCompareAndSwapObject(base, offset, "foo", "bar");
assertEquals(r, true, "weakCompareAndSwap Object");
boolean success = false;
for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
success = UNSAFE.weakCompareAndSwapObject(base, offset, "foo", "bar");
}
assertEquals(success, true, "weakCompareAndSwap Object");
Object x = UNSAFE.getObject(base, offset);
assertEquals(x, "bar", "weakCompareAndSwap Object value");
}
{
boolean r = UNSAFE.weakCompareAndSwapObjectAcquire(base, offset, "bar", "foo");
assertEquals(r, true, "weakCompareAndSwapAcquire Object");
boolean success = false;
for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
success = UNSAFE.weakCompareAndSwapObjectAcquire(base, offset, "bar", "foo");
}
assertEquals(success, true, "weakCompareAndSwapAcquire Object");
Object x = UNSAFE.getObject(base, offset);
assertEquals(x, "foo", "weakCompareAndSwapAcquire Object");
}
{
boolean r = UNSAFE.weakCompareAndSwapObjectRelease(base, offset, "foo", "bar");
assertEquals(r, true, "weakCompareAndSwapRelease Object");
boolean success = false;
for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
success = UNSAFE.weakCompareAndSwapObjectRelease(base, offset, "foo", "bar");
}
assertEquals(success, true, "weakCompareAndSwapRelease Object");
Object x = UNSAFE.getObject(base, offset);
assertEquals(x, "bar", "weakCompareAndSwapRelease Object");
}
@ -236,4 +246,3 @@ public class JdkInternalMiscUnsafeAccessTestObject {
}

View File

@ -40,6 +40,7 @@ import static org.testng.Assert.*;
public class JdkInternalMiscUnsafeAccessTestShort {
static final int ITERS = Integer.getInteger("iters", 1);
static final int WEAK_ATTEMPTS = Integer.getInteger("weakAttempts", 10);
static final jdk.internal.misc.Unsafe UNSAFE;
@ -203,4 +204,3 @@ public class JdkInternalMiscUnsafeAccessTestShort {
}
}

View File

@ -40,6 +40,7 @@ import static org.testng.Assert.*;
public class SunMiscUnsafeAccessTestBoolean {
static final int ITERS = Integer.getInteger("iters", 1);
static final int WEAK_ATTEMPTS = Integer.getInteger("weakAttempts", 10);
static final sun.misc.Unsafe UNSAFE;
@ -131,9 +132,7 @@ public class SunMiscUnsafeAccessTestBoolean {
}
}

View File

@ -40,6 +40,7 @@ import static org.testng.Assert.*;
public class SunMiscUnsafeAccessTestByte {
static final int ITERS = Integer.getInteger("iters", 1);
static final int WEAK_ATTEMPTS = Integer.getInteger("weakAttempts", 10);
static final sun.misc.Unsafe UNSAFE;
@ -160,7 +161,6 @@ public class SunMiscUnsafeAccessTestByte {
}
static void testAccess(long address) {
@ -173,4 +173,3 @@ public class SunMiscUnsafeAccessTestByte {
}
}

View File

@ -40,6 +40,7 @@ import static org.testng.Assert.*;
public class SunMiscUnsafeAccessTestChar {
static final int ITERS = Integer.getInteger("iters", 1);
static final int WEAK_ATTEMPTS = Integer.getInteger("weakAttempts", 10);
static final sun.misc.Unsafe UNSAFE;
@ -160,7 +161,6 @@ public class SunMiscUnsafeAccessTestChar {
}
static void testAccess(long address) {
@ -173,4 +173,3 @@ public class SunMiscUnsafeAccessTestChar {
}
}

View File

@ -40,6 +40,7 @@ import static org.testng.Assert.*;
public class SunMiscUnsafeAccessTestDouble {
static final int ITERS = Integer.getInteger("iters", 1);
static final int WEAK_ATTEMPTS = Integer.getInteger("weakAttempts", 10);
static final sun.misc.Unsafe UNSAFE;
@ -160,7 +161,6 @@ public class SunMiscUnsafeAccessTestDouble {
}
static void testAccess(long address) {
@ -173,4 +173,3 @@ public class SunMiscUnsafeAccessTestDouble {
}
}

View File

@ -40,6 +40,7 @@ import static org.testng.Assert.*;
public class SunMiscUnsafeAccessTestFloat {
static final int ITERS = Integer.getInteger("iters", 1);
static final int WEAK_ATTEMPTS = Integer.getInteger("weakAttempts", 10);
static final sun.misc.Unsafe UNSAFE;
@ -160,7 +161,6 @@ public class SunMiscUnsafeAccessTestFloat {
}
static void testAccess(long address) {
@ -173,4 +173,3 @@ public class SunMiscUnsafeAccessTestFloat {
}
}

View File

@ -40,6 +40,7 @@ import static org.testng.Assert.*;
public class SunMiscUnsafeAccessTestInt {
static final int ITERS = Integer.getInteger("iters", 1);
static final int WEAK_ATTEMPTS = Integer.getInteger("weakAttempts", 10);
static final sun.misc.Unsafe UNSAFE;
@ -165,7 +166,6 @@ public class SunMiscUnsafeAccessTestInt {
UNSAFE.putInt(base, offset, 1);
// Compare
@ -184,7 +184,6 @@ public class SunMiscUnsafeAccessTestInt {
}
// Compare set and get
{
int o = UNSAFE.getAndSetInt(base, offset, 1);
@ -200,7 +199,7 @@ public class SunMiscUnsafeAccessTestInt {
int o = UNSAFE.getAndAddInt(base, offset, 2);
assertEquals(o, 1, "getAndAdd int");
int x = UNSAFE.getInt(base, offset);
assertEquals(x, 1 + 2, "weakCompareAndSwapRelease int");
assertEquals(x, 1 + 2, "getAndAdd int");
}
}
@ -214,4 +213,3 @@ public class SunMiscUnsafeAccessTestInt {
}
}

View File

@ -40,6 +40,7 @@ import static org.testng.Assert.*;
public class SunMiscUnsafeAccessTestLong {
static final int ITERS = Integer.getInteger("iters", 1);
static final int WEAK_ATTEMPTS = Integer.getInteger("weakAttempts", 10);
static final sun.misc.Unsafe UNSAFE;
@ -165,7 +166,6 @@ public class SunMiscUnsafeAccessTestLong {
UNSAFE.putLong(base, offset, 1L);
// Compare
@ -184,7 +184,6 @@ public class SunMiscUnsafeAccessTestLong {
}
// Compare set and get
{
long o = UNSAFE.getAndSetLong(base, offset, 1L);
@ -200,7 +199,7 @@ public class SunMiscUnsafeAccessTestLong {
long o = UNSAFE.getAndAddLong(base, offset, 2L);
assertEquals(o, 1L, "getAndAdd long");
long x = UNSAFE.getLong(base, offset);
assertEquals(x, 1L + 2L, "weakCompareAndSwapRelease long");
assertEquals(x, 1L + 2L, "getAndAdd long");
}
}
@ -214,4 +213,3 @@ public class SunMiscUnsafeAccessTestLong {
}
}

View File

@ -40,6 +40,7 @@ import static org.testng.Assert.*;
public class SunMiscUnsafeAccessTestObject {
static final int ITERS = Integer.getInteger("iters", 1);
static final int WEAK_ATTEMPTS = Integer.getInteger("weakAttempts", 10);
static final sun.misc.Unsafe UNSAFE;
@ -136,7 +137,6 @@ public class SunMiscUnsafeAccessTestObject {
UNSAFE.putObject(base, offset, "foo");
// Compare
@ -155,7 +155,6 @@ public class SunMiscUnsafeAccessTestObject {
}
// Compare set and get
{
Object o = UNSAFE.getAndSetObject(base, offset, "foo");
@ -168,4 +167,3 @@ public class SunMiscUnsafeAccessTestObject {
}

View File

@ -40,6 +40,7 @@ import static org.testng.Assert.*;
public class SunMiscUnsafeAccessTestShort {
static final int ITERS = Integer.getInteger("iters", 1);
static final int WEAK_ATTEMPTS = Integer.getInteger("weakAttempts", 10);
static final sun.misc.Unsafe UNSAFE;
@ -160,7 +161,6 @@ public class SunMiscUnsafeAccessTestShort {
}
static void testAccess(long address) {
@ -173,4 +173,3 @@ public class SunMiscUnsafeAccessTestShort {
}
}

View File

@ -40,6 +40,7 @@ import static org.testng.Assert.*;
public class $Qualifier$UnsafeAccessTest$Type$ {
static final int ITERS = Integer.getInteger("iters", 1);
static final int WEAK_ATTEMPTS = Integer.getInteger("weakAttempts", 10);
static final $package$.Unsafe UNSAFE;
@ -273,22 +274,31 @@ public class $Qualifier$UnsafeAccessTest$Type$ {
}
{
boolean r = UNSAFE.weakCompareAndSwap$Type$(base, offset, $value1$, $value2$);
assertEquals(r, true, "weakCompareAndSwap $type$");
boolean success = false;
for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
success = UNSAFE.weakCompareAndSwap$Type$(base, offset, $value1$, $value2$);
}
assertEquals(success, true, "weakCompareAndSwap $type$");
$type$ x = UNSAFE.get$Type$(base, offset);
assertEquals(x, $value2$, "weakCompareAndSwap $type$ value");
}
{
boolean r = UNSAFE.weakCompareAndSwap$Type$Acquire(base, offset, $value2$, $value1$);
assertEquals(r, true, "weakCompareAndSwapAcquire $type$");
boolean success = false;
for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
success = UNSAFE.weakCompareAndSwap$Type$Acquire(base, offset, $value2$, $value1$);
}
assertEquals(success, true, "weakCompareAndSwapAcquire $type$");
$type$ x = UNSAFE.get$Type$(base, offset);
assertEquals(x, $value1$, "weakCompareAndSwapAcquire $type$");
}
{
boolean r = UNSAFE.weakCompareAndSwap$Type$Release(base, offset, $value1$, $value2$);
assertEquals(r, true, "weakCompareAndSwapRelease $type$");
boolean success = false;
for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
success = UNSAFE.weakCompareAndSwap$Type$Release(base, offset, $value1$, $value2$);
}
assertEquals(success, true, "weakCompareAndSwapRelease $type$");
$type$ x = UNSAFE.get$Type$(base, offset);
assertEquals(x, $value2$, "weakCompareAndSwapRelease $type$");
}
@ -311,7 +321,7 @@ public class $Qualifier$UnsafeAccessTest$Type$ {
$type$ o = UNSAFE.getAndAdd$Type$(base, offset, $value2$);
assertEquals(o, $value1$, "getAndAdd $type$");
$type$ x = UNSAFE.get$Type$(base, offset);
assertEquals(x, $value1$ + $value2$, "weakCompareAndSwapRelease $type$");
assertEquals(x, $value1$ + $value2$, "getAndAdd $type$");
}
#end[AtomicAdd]
}

View File

@ -1,97 +0,0 @@
#!/bin/bash
javac -d . ../../../../jdk/make/src/classes/build/tools/spp/Spp.java
SPP=build.tools.spp.Spp
# Generates unsafe access tests for objects and all primitive types
# $1 = package name to Unsafe, sun.misc | jdk.internal.misc
# $2 = test class qualifier name, SunMisc | JdkInternalMisc
function generate {
package=$1
Qualifier=$2
for type in boolean byte short char int long float double Object
do
Type="$(tr '[:lower:]' '[:upper:]' <<< ${type:0:1})${type:1}"
args="-K$type -Dtype=$type -DType=$Type"
case $type in
Object|int|long)
args="$args -KCAS -KOrdered"
;;
esac
case $type in
int|long)
args="$args -KAtomicAdd"
;;
esac
case $type in
short|char|int|long)
args="$args -KUnaligned"
;;
esac
case $type in
boolean)
value1=true
value2=false
value3=false
;;
byte)
value1=(byte)1
value2=(byte)2
value3=(byte)3
;;
short)
value1=(short)1
value2=(short)2
value3=(short)3
;;
char)
value1=\'a\'
value2=\'b\'
value3=\'c\'
;;
int)
value1=1
value2=2
value3=3
;;
long)
value1=1L
value2=2L
value3=3L
;;
float)
value1=1.0f
value2=2.0f
value3=3.0f
;;
double)
value1=1.0d
value2=2.0d
value3=3.0d
;;
Object)
value1=\"foo\"
value2=\"bar\"
value3=\"baz\"
;;
esac
args="$args -Dvalue1=$value1 -Dvalue2=$value2 -Dvalue3=$value3"
echo $args
java $SPP -nel -K$Qualifier -Dpackage=$package -DQualifier=$Qualifier \
$args < X-UnsafeAccessTest.java.template > ${Qualifier}UnsafeAccessTest${Type}.java
done
}
generate sun.misc SunMisc
generate jdk.internal.misc JdkInternalMisc
rm -fr build