Skip to content

Commit 28cde1b

Browse files
committed
chore: organize code
1 parent b260083 commit 28cde1b

File tree

3 files changed

+74
-21
lines changed

3 files changed

+74
-21
lines changed

.github/workflows/maven.yml

+2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ jobs:
7272
needs: [build, test]
7373
steps:
7474
- uses: actions/checkout@v3
75+
with:
76+
fetch-depth: 0
7577
- name: Set up JDK 17
7678
uses: actions/setup-java@v3
7779
with:

amazon-sns-java-messaging-lib-template/src/main/java/com/amazon/sns/messaging/lib/concurrent/RingBufferBlockingQueue.java

+10-17
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,11 @@ public int readSequence() {
113113
}
114114

115115
@Override
116-
@SneakyThrows
117116
public E peek() {
118-
if (isEmpty()) {
119-
return null;
120-
}
121-
122-
return buffer[wrap(readSequence.get())].getValue();
117+
return isEmpty() ? null : buffer[wrap(readSequence.get())].getValue();
123118
}
124119

125120
@Override
126-
@SneakyThrows
127121
public void put(final E element) {
128122
try {
129123
reentrantLock.lock();
@@ -134,7 +128,6 @@ public void put(final E element) {
134128
}
135129

136130
@Override
137-
@SneakyThrows
138131
public E take() {
139132
try {
140133
reentrantLock.lock();
@@ -154,47 +147,47 @@ static class Entry<E> {
154147

155148
@Override
156149
public boolean offer(final E e) {
157-
throw new UnsupportedOperationException();
150+
throw new UnsupportedOperationException(); // NOSONAR
158151
}
159152

160153
@Override
161154
public E poll() {
162-
throw new UnsupportedOperationException();
155+
throw new UnsupportedOperationException(); // NOSONAR
163156
}
164157

165158
@Override
166159
public Iterator<E> iterator() {
167-
throw new UnsupportedOperationException();
160+
throw new UnsupportedOperationException(); // NOSONAR
168161
}
169162

170163
@Override
171164
public boolean add(final E e) {
172-
throw new UnsupportedOperationException();
165+
throw new UnsupportedOperationException(); // NOSONAR
173166
}
174167

175168
@Override
176169
public boolean offer(final E e, final long timeout, final TimeUnit unit) throws InterruptedException {
177-
throw new UnsupportedOperationException();
170+
throw new UnsupportedOperationException(); // NOSONAR
178171
}
179172

180173
@Override
181174
public E poll(final long timeout, final TimeUnit unit) throws InterruptedException {
182-
throw new UnsupportedOperationException();
175+
throw new UnsupportedOperationException(); // NOSONAR
183176
}
184177

185178
@Override
186179
public int remainingCapacity() {
187-
throw new UnsupportedOperationException();
180+
throw new UnsupportedOperationException(); // NOSONAR
188181
}
189182

190183
@Override
191184
public int drainTo(final Collection<? super E> c) {
192-
throw new UnsupportedOperationException();
185+
throw new UnsupportedOperationException(); // NOSONAR
193186
}
194187

195188
@Override
196189
public int drainTo(final Collection<? super E> c, final int maxElements) {
197-
throw new UnsupportedOperationException();
190+
throw new UnsupportedOperationException(); // NOSONAR
198191
}
199192

200193
}

amazon-sns-java-messaging-lib-template/src/test/java/com/amazon/sns/messaging/lib/concurrent/RingBufferBlockingQueueTest.java

+62-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static org.hamcrest.CoreMatchers.is;
2121
import static org.hamcrest.MatcherAssert.assertThat;
2222
import static org.hamcrest.Matchers.hasSize;
23+
import static org.mockito.Mockito.spy;
2324

2425
import java.util.LinkedList;
2526
import java.util.List;
@@ -36,12 +37,12 @@
3637

3738
class RingBufferBlockingQueueTest {
3839

39-
private final ExecutorService producer = Executors.newSingleThreadExecutor();
40-
41-
private final ScheduledExecutorService consumer = Executors.newSingleThreadScheduledExecutor();
42-
4340
@Test
4441
void testSuccess() throws InterruptedException {
42+
final ExecutorService producer = Executors.newSingleThreadExecutor();
43+
44+
final ScheduledExecutorService consumer = Executors.newSingleThreadScheduledExecutor();
45+
4546
final List<RequestEntry<Integer>> requestEntriesOut = new LinkedList<>();
4647

4748
final RingBufferBlockingQueue<RequestEntry<Integer>> ringBlockingQueue = new RingBufferBlockingQueue<>(5120);
@@ -80,4 +81,61 @@ void testSuccess() throws InterruptedException {
8081
}
8182
}
8283

84+
@Test
85+
void testSuccessWhenIsEmpty() throws InterruptedException {
86+
final RingBufferBlockingQueue<RequestEntry<Integer>> ringBlockingQueue = spy(new RingBufferBlockingQueue<>());
87+
88+
final ExecutorService producer = Executors.newSingleThreadExecutor();
89+
90+
final ExecutorService consumer = Executors.newSingleThreadExecutor();
91+
92+
consumer.submit(() -> {
93+
final RequestEntry<Integer> entry = ringBlockingQueue.take();
94+
assertThat(entry.getValue(), is(0));
95+
});
96+
97+
Thread.sleep(2000);
98+
99+
producer.submit(() -> {
100+
ringBlockingQueue.put(RequestEntry.<Integer>builder().withValue(0).build());
101+
});
102+
103+
await().atMost(1, TimeUnit.MINUTES).until(() -> ringBlockingQueue.writeSequence() == 0);
104+
producer.shutdownNow();
105+
106+
await().atMost(1, TimeUnit.MINUTES).until(() -> ringBlockingQueue.readSequence() == 1);
107+
consumer.shutdownNow();
108+
109+
assertThat(ringBlockingQueue.isEmpty(), is(true));
110+
}
111+
112+
@Test
113+
void testSuccessWhenIsFull() throws InterruptedException {
114+
final RingBufferBlockingQueue<RequestEntry<Integer>> ringBlockingQueue = spy(new RingBufferBlockingQueue<>(1));
115+
116+
final ExecutorService producer = Executors.newSingleThreadExecutor();
117+
118+
final ExecutorService consumer = Executors.newSingleThreadExecutor();
119+
120+
producer.submit(() -> {
121+
ringBlockingQueue.put(RequestEntry.<Integer>builder().withValue(0).build());
122+
ringBlockingQueue.put(RequestEntry.<Integer>builder().withValue(1).build());
123+
});
124+
125+
Thread.sleep(2000);
126+
127+
consumer.submit(() -> {
128+
assertThat(ringBlockingQueue.take().getValue(), is(0));
129+
assertThat(ringBlockingQueue.take().getValue(), is(1));
130+
});
131+
132+
await().atMost(1, TimeUnit.MINUTES).until(() -> ringBlockingQueue.writeSequence() == 1);
133+
producer.shutdownNow();
134+
135+
await().atMost(1, TimeUnit.MINUTES).until(() -> ringBlockingQueue.readSequence() == 2);
136+
consumer.shutdownNow();
137+
138+
assertThat(ringBlockingQueue.isEmpty(), is(true));
139+
}
140+
83141
}

0 commit comments

Comments
 (0)