/*
* Copyright (c) 2008-2017, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hazelcast.concurrent.lock;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.test.HazelcastParallelClassRunner;
import com.hazelcast.test.HazelcastTestSupport;
import com.hazelcast.test.annotation.NightlyTest;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@RunWith(HazelcastParallelClassRunner.class)
@Category(NightlyTest.class)
public class LockStressTest extends HazelcastTestSupport {
private static final int TIMEOUT_MILLS = 4 * 60 * 1000;
/**
* Test for issue 267
*/
@Test(timeout = TIMEOUT_MILLS)
public void testHighConcurrentLockAndUnlock() {
final HazelcastInstance hz = createHazelcastInstance();
final String key = "key";
final int threadCount = 100;
final int lockCountPerThread = 5000;
final int locks = 50;
final CountDownLatch latch = new CountDownLatch(threadCount);
final AtomicInteger totalCount = new AtomicInteger();
class InnerTest implements Runnable {
public void run() {
boolean live = true;
Random rand = new Random();
try {
for (int j = 0; j < lockCountPerThread && live; j++) {
final Lock lock = hz.getLock(key + rand.nextInt(locks));
lock.lock();
try {
totalCount.incrementAndGet();
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
break;
} finally {
try {
lock.unlock();
} catch (Exception e) {
e.printStackTrace();
live = false;
}
}
}
} finally {
latch.countDown();
}
}
}
ExecutorService executorService = Executors.newCachedThreadPool();
for (int i = 0; i < threadCount; i++) {
executorService.execute(new InnerTest());
}
try {
assertTrue("Lock tasks stuck!", latch.await(TIMEOUT_MILLS, TimeUnit.MILLISECONDS));
assertEquals((threadCount * lockCountPerThread), totalCount.get());
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
try {
hz.getLifecycleService().terminate();
} catch (Throwable ignored) {
}
executorService.shutdownNow();
}
}
}