From 8451efa11facfa35eb7b1f1227dae32dae6341c5 Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Wed, 23 Jan 2013 19:30:11 +0200 Subject: [PATCH] add multi-threaded test for AtomicInteger DATAREDIS-108 --- .../support/atomic/RedisAtomicTests.java | 29 +++++++++++++++++-- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicTests.java b/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicTests.java index a321c1783..970d01125 100644 --- a/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicTests.java +++ b/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicTests.java @@ -15,9 +15,13 @@ */ package org.springframework.data.redis.support.atomic; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import java.util.Collection; +import java.util.concurrent.atomic.AtomicBoolean; import org.junit.After; import org.junit.AfterClass; @@ -28,8 +32,6 @@ import org.junit.runners.Parameterized.Parameters; import org.springframework.data.redis.ConnectionFactoryTracker; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.connection.RedisConnectionFactory; -import org.springframework.data.redis.support.atomic.RedisAtomicInteger; -import org.springframework.data.redis.support.atomic.RedisAtomicLong; /** * @author Costin Leau @@ -114,4 +116,25 @@ public class RedisAtomicTests { RedisAtomicLong keyCopy = new RedisAtomicLong(longCounter.getKey(), factory); assertEquals(longCounter.get(), keyCopy.get()); } + + // DATAREDIS-108 + @Test + public void testCompareSet() throws Exception { + final AtomicBoolean flag = new AtomicBoolean(false); + for (int i = 0; i < 500; i++) { + new Thread(new Runnable() { + public void run() { + RedisAtomicInteger atomicInteger = new RedisAtomicInteger("key", factory); + if (atomicInteger.compareAndSet(0, 1)) { + if (flag.get()) { + fail("counter already modified"); + } + + flag.set(true); + } + } + + }).start(); + } + } } \ No newline at end of file