Skip to content

Commit f6be7e1

Browse files
committed
Reworked initialize function of broker tester
1 parent a2c7fb9 commit f6be7e1

File tree

11 files changed

+165
-176
lines changed

11 files changed

+165
-176
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# AmqpRunner
22
[![Build Status](https://travis-ci.org/mmichailidis/AmqpRunner.svg?branch=master)](https://travis-ci.org/mmichailidis/AmqpRunner)
3-
43
[![codecov](https://codecov.io/gh/mmichailidis/AmqpRunner/branch/master/graph/badge.svg)](https://codecov.io/gh/mmichailidis/AmqpRunner)
5-
64
[![Maven Central](https://img.shields.io/maven-central/v/gr.mmichailidis/amqprunner.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22gr.mmichailidis%22%20AND%20a:%22amqprunner%22)
5+
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/a26f2d10a3384dec9832bec64fd082fa)](https://www.codacy.com/app/mmichailidis/AmqpRunner?utm_source=github.com&utm_medium=referral&utm_content=mmichailidis/AmqpRunner&utm_campaign=Badge_Grade)
6+
[![License](https://img.shields.io/github/license/mmichailidis/amqprunner.svg)](https://opensource.org/licenses/Apache-2.0)
77

88
AmqpRunner is a JUnit runner that provides a fluent api for creating in
99
memory amqp and validating the packets that reached it. It extends

src/main/java/gr/mmichaildis/amqprunner/BrokerManager.java

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,19 @@
2121
import gr.mmichaildis.amqprunner.util.PortExtractingLauncherListener;
2222
import io.vavr.collection.Stream;
2323
import io.vavr.control.Option;
24+
import io.vavr.control.Try;
2425
import lombok.extern.slf4j.Slf4j;
2526
import org.apache.qpid.server.SystemLauncher;
2627
import org.apache.qpid.server.SystemLauncherListener.DefaultSystemLauncherListener;
2728

2829
import java.io.File;
2930
import java.net.URL;
31+
import java.util.Map;
3032
import java.util.*;
3133

34+
import static gr.mmichaildis.amqprunner.util.StreamHelpers.not;
35+
import static gr.mmichaildis.amqprunner.util.StreamHelpers.replaceWith;
36+
import static io.vavr.API.*;
3237
import static java.lang.Thread.sleep;
3338
import static org.junit.Assert.assertNotNull;
3439
import static org.junit.Assert.fail;
@@ -47,7 +52,11 @@ public class BrokerManager {
4752
private static final Long SLEEP_STEP = 100L;
4853
private final String introduction;
4954

50-
private static ReferenceHolder refHolder;
55+
/**
56+
* The path that contains the configuration file for qpid initialization.
57+
*/
58+
private static final String INITIAL_CONFIG_PATH = "amqp.json";
59+
private static final ReferenceHolder refHolder = new ReferenceHolder();
5160

5261
private final String username;
5362
private final String password;
@@ -82,8 +91,9 @@ public BrokerManager(final String username,
8291
this.requestedAmqpPort = requestedAmqpPort;
8392
this.requestedWorkPath = requestedWorkPath;
8493
this.requestedLogPath = requestedLogPath;
85-
refHolder = new ReferenceHolder();
86-
refHolder.setCleanUpList(Collections.synchronizedList(new LinkedList<>()));
94+
95+
refHolder.setQueueCleanUpList(Collections.synchronizedList(new LinkedList<>()));
96+
refHolder.setExchangeCleanUpList(Collections.synchronizedList(new LinkedList<>()));
8797

8898
introduction = "[BrokerManager" + (name.isEmpty() ? "" : "-" + name) + "] ";
8999
// this.systemLauncher = new SystemLauncher();
@@ -97,11 +107,6 @@ public BrokerManager(final String username,
97107
this.uuid = UUID.randomUUID();
98108
}
99109

100-
/**
101-
* The path that contains the configuration file for qpid initialization.
102-
*/
103-
private static final String INITIAL_CONFIG_PATH = "amqp.json";
104-
private static final String INITIAL_CONFIG_PATH_NETWORK = "amqpNetwork.json";
105110

106111
/**
107112
* Start the broker with the properties that was initialized with.
@@ -160,15 +165,15 @@ public void stopBroker() {
160165
systemLauncher.shutdown();
161166
log.info("SystemLauncher shutdown complete. Cleaning up.");
162167

163-
File db = new File(requestedWorkPath + uuid);
164-
File log = new File(requestedLogPath + uuid);
168+
final File db = new File(requestedWorkPath + uuid);
169+
final File log = new File(requestedLogPath + uuid);
165170

166171
Stream.of(db, log)
167172
.filter(File::exists)
168173
.forEach(BrokerManager::deleteFolder);
169174

170175
try {
171-
Thread.sleep(5000);
176+
sleep(5000);
172177
} catch (InterruptedException e) {
173178
e.printStackTrace();
174179
}
@@ -200,22 +205,27 @@ private static void deleteFolder(File folder) {
200205

201206
private static void deleteFile(File file, Integer retryStep) {
202207
try {
203-
Thread.sleep(2500 * retryStep);
208+
sleep(2500 * retryStep);
204209
} catch (InterruptedException e) {
205210
e.printStackTrace();
206211
}
207-
if (!file.delete() && retryStep < 3) {
208-
deleteFile(file, retryStep + 1);
209-
} else {
210-
log.error("File {} failed to be deleted after {} retries", file, retryStep);
211-
}
212+
Try.run(() -> sleep(2500 * retryStep))
213+
.filter(ignore -> retryStep < 3)
214+
.map(ignore -> file.delete())
215+
.filter(not(Boolean::booleanValue))
216+
.map(replaceWith(retryStep < 3))
217+
.forEach(hasMoreSteps -> Match(hasMoreSteps).of(
218+
Case($(true), run(() -> deleteFile(file, retryStep + 1))),
219+
Case($(false), run(() -> log.error("File {} failed to be deleted after {} retries", file, retryStep)))
220+
));
212221
}
213222

214223
/**
215224
* Cleans up all the queues and exchanges in the broker.
216225
*/
217226
public void cleanUp() {
218-
refHolder.getCleanUpList().forEach(r -> r.apply(null));
227+
refHolder.getQueueCleanUpList().forEach(r -> r.apply(null));
228+
refHolder.getExchangeCleanUpList().forEach(r -> r.apply(null));
219229
}
220230

221231
private Map<String, Object> createSystemConfig() {

src/main/java/gr/mmichaildis/amqprunner/broker/AssertionTime.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ final class AssertionTime {
4242
* set the target to {@code totalMillis} and reset the counter.
4343
*
4444
* @param totalMillis The millis that will be either setup as the new target or will put the current target ahead.
45-
* @param override {@code true} to override the target, {@code false} to push ahead.
45+
* @param override {@code true} to override the target, {@code false} to push ahead.
4646
*/
47-
void updateTotalSeconds(Long totalMillis, Boolean override) {
47+
protected void updateTotalSeconds(Long totalMillis, Boolean override) {
4848
if (override) {
4949
this.totalSeconds.set(totalMillis);
5050
currentMillis.set(0L);
@@ -60,7 +60,7 @@ void updateTotalSeconds(Long totalMillis, Boolean override) {
6060
* @param sleepStep the amount of time to update the counter.
6161
* @return The difference between the counter and the target.
6262
*/
63-
Long updateCurrentMillis(Long sleepStep) {
63+
protected Long updateCurrentMillis(Long sleepStep) {
6464
return totalSeconds.get() - currentMillis.updateAndGet(curr -> curr + sleepStep);
6565
}
6666
}

src/main/java/gr/mmichaildis/amqprunner/broker/AssertionVerification.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ public boolean isValid() {
6969
* the {@link AssertionError} found in all the registered assertions.
7070
*
7171
* @return the {@link List} which contains all the {@link AssertionError} found.
72-
* If the {@link AssertionVerification#isValid()} was not called before
73-
* this then the {@link List} is empty.
72+
* If the {@link AssertionVerification#isValid()} was not called before
73+
* this then the {@link List} is empty.
7474
*/
7575
public List<AssertionError> getErrors() {
7676
return assertionErrors;
@@ -82,7 +82,7 @@ public List<AssertionError> getErrors() {
8282
* @param identifier The assertions identifier.
8383
* @param valid The valid status of the identified assertion.
8484
*/
85-
void setValid(String identifier, boolean valid) {
85+
protected void setValid(String identifier, boolean valid) {
8686
final AssertionPair pair = this.response.get(identifier);
8787
pair.setValid(valid);
8888
this.response.put(identifier, pair);
@@ -94,7 +94,7 @@ void setValid(String identifier, boolean valid) {
9494
* @param identifier The assertions identifier.
9595
* @param e The {@link AssertionError} that will be registered for the identified assertion.
9696
*/
97-
void setAssertionError(String identifier, AssertionError e) {
97+
protected void setAssertionError(String identifier, AssertionError e) {
9898
final AssertionPair pair = this.response.get(identifier);
9999
pair.setAssertionError(e);
100100
this.response.put(identifier, pair);
@@ -106,7 +106,7 @@ void setAssertionError(String identifier, AssertionError e) {
106106
*
107107
* @param identifier The identifier that will identify each assertion.
108108
*/
109-
void register(String identifier, String userProvidedIdentifier) {
109+
protected void register(String identifier, String userProvidedIdentifier) {
110110
response.put(identifier, AssertionPair.initial(userProvidedIdentifier));
111111
block.put(identifier, new AssertionTime(0));
112112
}
@@ -117,9 +117,9 @@ void register(String identifier, String userProvidedIdentifier) {
117117
*
118118
* @param nextName The assertion name which will be used to create the unique identifier.
119119
* @return The unique identifier which should be used for registration and identification on
120-
* the main {@link Thread}.
120+
* the main {@link Thread}.
121121
*/
122-
String getNextName(String nextName) {
122+
protected String getNextName(String nextName) {
123123
Integer identifier = nameProvider.getOrDefault(nextName, 0);
124124
identifier += 1;
125125
nameProvider.put(nextName, identifier);
@@ -131,7 +131,7 @@ String getNextName(String nextName) {
131131
*
132132
* @param identifier The assertions identifier.
133133
*/
134-
void clearAssertionError(String identifier) {
134+
protected void clearAssertionError(String identifier) {
135135
final AssertionPair pair = response.get(identifier);
136136
pair.setAssertionError(null);
137137
response.put(identifier, pair);
@@ -143,7 +143,7 @@ void clearAssertionError(String identifier) {
143143
* @param identifier The identifier that will have it sleep increased.
144144
* @param sleepTime The N <b>seconds</b> that will be increase.
145145
*/
146-
void updateBlock(String identifier, Integer sleepTime) {
146+
protected void updateBlock(String identifier, Integer sleepTime) {
147147
this.block.get(identifier)
148148
.updateTotalSeconds(sleepTime * SECOND_TO_MILLIS, false);
149149
}
@@ -166,7 +166,7 @@ void setBlock(String identifier, Integer sleepTime) {
166166
*
167167
* @param sleepStep the step in time
168168
* @return {@code true} if at least one assertion requires more time. {@code false} if and only if all
169-
* the assertions are completed and responds they require no more time.
169+
* the assertions are completed and responds they require no more time.
170170
*/
171171
public boolean shouldBlock(Long sleepStep) {
172172
return block.values().stream()

0 commit comments

Comments
 (0)