001package org.eclipse.aether.named.hazelcast;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.eclipse.aether.named.NamedLock;
023import org.eclipse.aether.named.NamedLockFactory;
024import org.junit.Assert;
025import org.junit.Rule;
026import org.junit.Test;
027import org.junit.rules.TestName;
028
029import java.util.concurrent.CountDownLatch;
030import java.util.concurrent.TimeUnit;
031
032import static org.hamcrest.MatcherAssert.assertThat;
033import static org.hamcrest.Matchers.is;
034import static org.hamcrest.Matchers.not;
035import static org.hamcrest.Matchers.sameInstance;
036
037/**
038 * UT support for {@link NamedLockFactory}.
039 */
040public abstract class NamedLockFactoryTestSupport {
041
042    protected static NamedLockFactory namedLockFactory;
043
044    @Rule
045    public TestName testName = new TestName();
046
047    @Test
048    public void refCounting() {
049        final String name = testName.getMethodName();
050        try (NamedLock one = namedLockFactory.getLock(name);
051             NamedLock two = namedLockFactory.getLock(name)) {
052            assertThat(one, sameInstance(two));
053            one.close();
054            two.close();
055
056            try (NamedLock three = namedLockFactory.getLock(name)) {
057                assertThat(three, not(sameInstance(two)));
058            }
059        }
060    }
061
062    @Test(expected = IllegalStateException.class)
063    public void unlockWoLock() {
064        final String name = testName.getMethodName();
065        try (NamedLock one = namedLockFactory.getLock(name)) {
066            one.unlock();
067        }
068    }
069
070    @Test
071    public void wwBoxing() throws InterruptedException {
072        final String name = testName.getMethodName();
073        try (NamedLock one = namedLockFactory.getLock(name)) {
074            assertThat(one.lockExclusively(1L, TimeUnit.MILLISECONDS), is(true));
075            assertThat(one.lockExclusively(1L, TimeUnit.MILLISECONDS), is(true));
076            one.unlock();
077            one.unlock();
078        }
079    }
080
081    @Test
082    public void rrBoxing() throws InterruptedException {
083        final String name = testName.getMethodName();
084        try (NamedLock one = namedLockFactory.getLock(name)) {
085            assertThat(one.lockShared(1L, TimeUnit.MILLISECONDS), is(true));
086            assertThat(one.lockShared(1L, TimeUnit.MILLISECONDS), is(true));
087            one.unlock();
088            one.unlock();
089        }
090    }
091
092    @Test
093    public void wrBoxing() throws InterruptedException {
094        final String name = testName.getMethodName();
095        try (NamedLock one = namedLockFactory.getLock(name)) {
096            assertThat(one.lockExclusively(1L, TimeUnit.MILLISECONDS), is(true));
097            assertThat(one.lockShared(1L, TimeUnit.MILLISECONDS), is(true));
098            one.unlock();
099            one.unlock();
100        }
101    }
102
103    @Test
104    public void rwBoxing() throws InterruptedException {
105        final String name = testName.getMethodName();
106        try (NamedLock one = namedLockFactory.getLock(name)) {
107            assertThat(one.lockShared(1L, TimeUnit.MILLISECONDS), is(true));
108            assertThat(one.lockExclusively(1L, TimeUnit.MILLISECONDS), is(false));
109            one.unlock();
110        }
111    }
112
113    @Test(timeout = 5000)
114    public void sharedAccess() throws InterruptedException {
115        final String name = testName.getMethodName();
116        CountDownLatch winners = new CountDownLatch(2); // we expect 2 winner
117        CountDownLatch losers = new CountDownLatch(0); // we expect 0 loser
118        Thread t1 = new Thread(new Access(namedLockFactory, name, true, winners, losers));
119        Thread t2 = new Thread(new Access(namedLockFactory, name, true, winners, losers));
120        t1.start();
121        t2.start();
122        t1.join();
123        t2.join();
124        winners.await();
125        losers.await();
126    }
127
128    @Test(timeout = 5000)
129    public void exclusiveAccess() throws InterruptedException {
130        final String name = testName.getMethodName();
131        CountDownLatch winners = new CountDownLatch(1); // we expect 1 winner
132        CountDownLatch losers = new CountDownLatch(1); // we expect 1 loser
133        Thread t1 = new Thread(new Access(namedLockFactory, name, false, winners, losers));
134        Thread t2 = new Thread(new Access(namedLockFactory, name, false, winners, losers));
135        t1.start();
136        t2.start();
137        t1.join();
138        t2.join();
139        winners.await();
140        losers.await();
141    }
142
143    @Test(timeout = 5000)
144    public void mixedAccess() throws InterruptedException {
145        final String name = testName.getMethodName();
146        CountDownLatch winners = new CountDownLatch(1); // we expect 1 winner
147        CountDownLatch losers = new CountDownLatch(1); // we expect 1 loser
148        Thread t1 = new Thread(new Access(namedLockFactory, name, true, winners, losers));
149        Thread t2 = new Thread(new Access(namedLockFactory, name, false, winners, losers));
150        t1.start();
151        t2.start();
152        t1.join();
153        t2.join();
154        winners.await();
155        losers.await();
156    }
157
158    private static class Access implements Runnable {
159        final NamedLockFactory namedLockFactory;
160        final String name;
161        final boolean shared;
162        final CountDownLatch winner;
163        final CountDownLatch loser;
164
165        public Access(NamedLockFactory namedLockFactory,
166                      String name,
167                      boolean shared,
168                      CountDownLatch winner,
169                      CountDownLatch loser) {
170            this.namedLockFactory = namedLockFactory;
171            this.name = name;
172            this.shared = shared;
173            this.winner = winner;
174            this.loser = loser;
175        }
176
177        @Override
178        public void run() {
179            try (NamedLock lock = namedLockFactory.getLock(name)) {
180                if (shared ? lock.lockShared(100L, TimeUnit.MILLISECONDS) : lock.lockExclusively(100L, TimeUnit.MILLISECONDS)) {
181                    try {
182                        winner.countDown();
183                        loser.await();
184                    } finally {
185                        lock.unlock();
186                    }
187                } else {
188                    loser.countDown();
189                    winner.await();
190                }
191            } catch (InterruptedException e) {
192                Assert.fail(e.getMessage());
193            }
194        }
195    }
196}