Jordan Halterman | 4743258 | 2018-01-25 16:56:45 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.store.primitives.resources.impl; |
| 17 | |
| 18 | import java.time.Duration; |
| 19 | import java.util.Optional; |
| 20 | import java.util.concurrent.CompletableFuture; |
| 21 | |
| 22 | import io.atomix.protocols.raft.proxy.RaftProxy; |
| 23 | import io.atomix.protocols.raft.service.RaftService; |
| 24 | import org.junit.Test; |
| 25 | import org.onosproject.store.service.Version; |
| 26 | |
| 27 | import static org.junit.Assert.assertFalse; |
| 28 | import static org.junit.Assert.assertTrue; |
| 29 | |
| 30 | /** |
| 31 | * Raft lock test. |
| 32 | */ |
| 33 | public class AtomixDistributedLockTest extends AtomixTestBase<AtomixDistributedLock> { |
| 34 | @Override |
| 35 | protected RaftService createService() { |
| 36 | return new AtomixDistributedLockService(); |
| 37 | } |
| 38 | |
| 39 | @Override |
| 40 | protected AtomixDistributedLock createPrimitive(RaftProxy proxy) { |
| 41 | return new AtomixDistributedLock(proxy); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Tests locking and unlocking a lock. |
| 46 | */ |
| 47 | @Test |
| 48 | public void testLockUnlock() throws Throwable { |
| 49 | AtomixDistributedLock lock = newPrimitive("test-lock-unlock"); |
| 50 | lock.lock().join(); |
| 51 | lock.unlock().join(); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Tests releasing a lock when the client's session is closed. |
| 56 | */ |
| 57 | @Test |
| 58 | public void testReleaseOnClose() throws Throwable { |
| 59 | AtomixDistributedLock lock1 = newPrimitive("test-lock-release-on-close"); |
| 60 | AtomixDistributedLock lock2 = newPrimitive("test-lock-release-on-close"); |
| 61 | lock1.lock().join(); |
| 62 | CompletableFuture<Version> future = lock2.lock(); |
| 63 | lock1.close(); |
| 64 | future.join(); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Tests attempting to acquire a lock. |
| 69 | */ |
| 70 | @Test |
| 71 | public void testTryLockFail() throws Throwable { |
| 72 | AtomixDistributedLock lock1 = newPrimitive("test-try-lock-fail"); |
| 73 | AtomixDistributedLock lock2 = newPrimitive("test-try-lock-fail"); |
| 74 | |
| 75 | lock1.lock().join(); |
| 76 | |
| 77 | assertFalse(lock2.tryLock().join().isPresent()); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Tests attempting to acquire a lock. |
| 82 | */ |
| 83 | @Test |
| 84 | public void testTryLockSucceed() throws Throwable { |
| 85 | AtomixDistributedLock lock = newPrimitive("test-try-lock-succeed"); |
| 86 | assertTrue(lock.tryLock().join().isPresent()); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Tests attempting to acquire a lock with a timeout. |
| 91 | */ |
| 92 | @Test |
| 93 | public void testTryLockFailWithTimeout() throws Throwable { |
| 94 | AtomixDistributedLock lock1 = newPrimitive("test-try-lock-fail-with-timeout"); |
| 95 | AtomixDistributedLock lock2 = newPrimitive("test-try-lock-fail-with-timeout"); |
| 96 | |
| 97 | lock1.lock().join(); |
| 98 | |
| 99 | assertFalse(lock2.tryLock(Duration.ofSeconds(1)).join().isPresent()); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Tests attempting to acquire a lock with a timeout. |
| 104 | */ |
| 105 | @Test |
| 106 | public void testTryLockSucceedWithTimeout() throws Throwable { |
| 107 | AtomixDistributedLock lock1 = newPrimitive("test-try-lock-succeed-with-timeout"); |
| 108 | AtomixDistributedLock lock2 = newPrimitive("test-try-lock-succeed-with-timeout"); |
| 109 | |
| 110 | lock1.lock().join(); |
| 111 | |
| 112 | CompletableFuture<Optional<Version>> future = lock2.tryLock(Duration.ofSeconds(1)); |
| 113 | lock1.unlock().join(); |
| 114 | assertTrue(future.join().isPresent()); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Tests unlocking a lock with a blocking call in the event thread. |
| 119 | */ |
| 120 | @Test |
| 121 | public void testBlockingUnlock() throws Throwable { |
| 122 | AtomixDistributedLock lock1 = newPrimitive("test-blocking-unlock"); |
| 123 | AtomixDistributedLock lock2 = newPrimitive("test-blocking-unlock"); |
| 124 | |
| 125 | lock1.lock().thenRun(() -> { |
| 126 | lock1.unlock().join(); |
| 127 | }).join(); |
| 128 | |
| 129 | lock2.lock().join(); |
| 130 | } |
| 131 | } |