Skip to content

Commit 0c1b371

Browse files
author
Yegor Bugayenko
committed
Locks.exists()
1 parent cf60cc2 commit 0c1b371

File tree

7 files changed

+65
-0
lines changed

7 files changed

+65
-0
lines changed

src/main/java/co/stateful/Locks.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@
4242
@Immutable
4343
public interface Locks {
4444

45+
/**
46+
* Does it exist?
47+
* @param name Name of lock
48+
* @return TRUE if the lock exists
49+
* @throws IOException If fails
50+
* @since 0.10
51+
*/
52+
boolean exists(String name) throws IOException;
53+
4554
/**
4655
* Get one lock by name.
4756
* @param name Name of lock

src/main/java/co/stateful/RtLocks.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,18 @@ final class RtLocks implements Locks {
7070
.as(XmlResponse.class);
7171
}
7272

73+
@Override
74+
public boolean exists(final String name) throws IOException {
75+
return !this.response
76+
.rel("/page/links/link[@rel='self']/@href")
77+
.method(Request.GET)
78+
.fetch()
79+
.as(XmlResponse.class)
80+
.xml()
81+
.nodes(String.format("/page/locks/lock[name='%s']", name))
82+
.isEmpty();
83+
}
84+
7385
@Override
7486
public Lock get(final String name) {
7587
return new RtLock(

src/main/java/co/stateful/RtSttc.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import com.jcabi.http.response.XmlResponse;
3939
import com.jcabi.http.wire.OneMinuteWire;
4040
import com.jcabi.http.wire.RetryWire;
41+
import com.jcabi.http.wire.VerboseWire;
4142
import com.jcabi.urn.URN;
4243
import java.io.IOException;
4344
import java.net.HttpURLConnection;
@@ -81,6 +82,7 @@ public RtSttc(final URN urn, final String token) {
8182
this.request = new JdkRequest("http://www.stateful.co")
8283
.through(OneMinuteWire.class)
8384
.through(RetryWire.class)
85+
.through(VerboseWire.class)
8486
.header("X-Sttc-URN", urn.toString())
8587
.header("X-Sttc-Token", token)
8688
.header(HttpHeaders.ACCEPT, MediaType.TEXT_XML)

src/main/java/co/stateful/cached/CdLocks.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ public CdLocks(final Locks orgn) {
6565
this.origin = orgn;
6666
}
6767

68+
@Override
69+
public boolean exists(final String name) throws IOException {
70+
return this.origin.exists(name);
71+
}
72+
6873
@Override
6974
@Cacheable(lifetime = 1, unit = TimeUnit.HOURS)
7075
public Lock get(final String name) throws IOException {

src/main/java/co/stateful/mock/MkLocks.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@
4949
@EqualsAndHashCode
5050
final class MkLocks implements Locks {
5151

52+
@Override
53+
public boolean exists(final String name) {
54+
return false;
55+
}
56+
5257
@Override
5358
public Lock get(final String name) {
5459
return new MkLock();

src/main/java/co/stateful/retry/ReLocks.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,15 @@ public ReLocks(final Locks orgn) {
6666
this.origin = orgn;
6767
}
6868

69+
@Override
70+
@RetryOnFailure(
71+
verbose = false, attempts = Tv.TWENTY,
72+
delay = Tv.FIVE, unit = TimeUnit.SECONDS
73+
)
74+
public boolean exists(final String name) throws IOException {
75+
return this.origin.exists(name);
76+
}
77+
6978
@Override
7079
@RetryOnFailure(
7180
verbose = false, attempts = Tv.TWENTY,

src/test/java/co/stateful/RtLocksITCase.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,27 @@ public Void call() throws Exception {
109109
);
110110
}
111111

112+
/**
113+
* RtLocks can manage locks in one simple thread.
114+
* @throws Exception If some problem inside
115+
*/
116+
@Test
117+
public void locksAndUnlocksInOneThread() throws Exception {
118+
final Sttc sttc = this.srule.get();
119+
final Locks locks = sttc.locks();
120+
final String name = String.format(
121+
"test2-%s", RtLocksITCase.RANDOM.nextInt(Integer.MAX_VALUE)
122+
);
123+
MatcherAssert.assertThat(locks.exists(name), Matchers.is(false));
124+
final Lock lock = locks.get(name);
125+
lock.lock();
126+
try {
127+
MatcherAssert.assertThat(locks.exists(name), Matchers.is(true));
128+
lock.unlock();
129+
MatcherAssert.assertThat(locks.exists(name), Matchers.is(false));
130+
} finally {
131+
lock.unlock();
132+
}
133+
}
134+
112135
}

0 commit comments

Comments
 (0)