You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by GitBox <gi...@apache.org> on 2021/10/28 06:32:24 UTC

[GitHub] [ignite-3] rpuch commented on a change in pull request #414: IGNITE-15536 Use VarHandle to replace UNSAFE in IgniteSpinReadWriteLock

rpuch commented on a change in pull request #414:
URL: https://github.com/apache/ignite-3/pull/414#discussion_r738059046



##########
File path: modules/core/src/test/java/org/apache/ignite/internal/util/IgniteSpinReadWriteLockTest.java
##########
@@ -0,0 +1,530 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.ignite.internal.util;
+
+import java.time.Duration;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Timeout;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Tests for {@link IgniteSpinReadWriteLock}.
+ */
+@Timeout(20)
+class IgniteSpinReadWriteLockTest {
+    /** The lock under test. */
+    private final IgniteSpinReadWriteLock lock = new IgniteSpinReadWriteLock();
+
+    /** Poller used to poll for 10 seconds. */
+    private final Poller poller10Sec = new Poller(Duration.ofSeconds(10), Duration.ofMillis(10));
+
+    /** Executor service used to run tasks in threads different from the main test thread. */
+    private final ExecutorService executor = Executors.newCachedThreadPool();
+
+    /**
+     * Cleans up after a test.
+     *
+     * @throws Exception if something goes wrong
+     */
+    @AfterEach
+    void cleanup() throws Exception {
+        releaseReadLockHeldByCurrentThread();
+        releaseWriteLockHeldByCurrentThread();
+
+        executor.shutdownNow();
+        executor.awaitTermination(3, TimeUnit.SECONDS);
+    }
+
+    /***/
+    private void releaseReadLockHeldByCurrentThread() {
+        while (true) {
+            try {
+                lock.readUnlock();
+            }
+            catch (IllegalMonitorStateException e) {
+                // released our read lock completely
+                break;
+            }
+        }
+    }
+
+    /***/
+    private void releaseWriteLockHeldByCurrentThread() {
+        while (lock.writeLockedByCurrentThread())
+            lock.writeUnlock();
+    }
+
+    /**
+     *
+     */
+    @Test
+    void readLockDoesNotAllowWriteLockToBeAcquired() {
+        lock.readLock();
+
+        assertThatWriteLockAcquireAttemptBlocksForever();
+
+        lock.readUnlock();
+    }
+
+    /***/
+    private void assertThatWriteLockAcquireAttemptBlocksForever() {
+        Future<?> future = executor.submit(lock::writeLock);
+
+        assertThrows(TimeoutException.class, () -> future.get(500, TimeUnit.MILLISECONDS));
+    }
+
+    /**
+     *
+     */
+    @Test
+    void readLockDoesNotAllowWriteLockWithoutSleepsToBeAcquired() {
+        lock.readLock();
+
+        assertThatWriteLockAcquireAttemptWithoutSleepsBlocksForever();
+
+        lock.readUnlock();
+    }
+
+    /**
+     *
+     */
+    @Test
+    void readLockDoesNotAllowWriteLockToBeAcquiredWithTimeout() throws Exception {
+        lock.readLock();
+
+        Boolean acquired = callWithTimeout(() -> lock.tryWriteLock(1, TimeUnit.MILLISECONDS));
+        assertThat(acquired, is(false));
+
+        lock.readUnlock();
+    }
+
+    /**
+     *
+     */
+    @Test
+    void readLockAllowsReadLockToBeAcquired() throws Exception {
+        lock.readLock();
+
+        assertThatReadLockCanBeAcquired();
+    }
+
+    /***/
+    private void assertThatReadLockCanBeAcquired() throws InterruptedException, ExecutionException, TimeoutException {
+        Future<?> readLockAttemptFuture = executor.submit(lock::readLock);
+        getWithTimeout(readLockAttemptFuture);
+    }
+
+    /***/
+    private <T> T callWithTimeout(Callable<T> call) throws ExecutionException, InterruptedException, TimeoutException {
+        Future<T> future = executor.submit(call);
+        return getWithTimeout(future);
+    }
+
+    /***/
+    private void runWithTimeout(Runnable runnable) throws ExecutionException, InterruptedException, TimeoutException {
+        Future<?> future = executor.submit(runnable);
+        getWithTimeout(future);
+    }
+
+    /***/
+    private <T> T getWithTimeout(Future<? extends T> future) throws ExecutionException,

Review comment:
       Why make a method static when we never use it in a static context?
   
   Generally, I think we should have method applicability (non-static over static; private over package-local/protected/public, etc) as low as possible to make sure that someone thinks twice before using it in a broader context.
   
   In this case, it doesn't matter much, it's a private method, but I would still like to hear motivation behind this suggestion :)




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org