Skip to content

Commit c7debc4

Browse files
committed
Added unit tests for queue package
1 parent 4582dfd commit c7debc4

File tree

11 files changed

+524
-39
lines changed

11 files changed

+524
-39
lines changed

src/main/java/com/exceptionless/exceptionlessclient/ExceptionlessClient.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ private void init() {
4343
@Override
4444
public void run() {
4545
try {
46-
configurationManager.getSettingsManager().updateSettingsThreadSafe();
46+
configurationManager.getSettingsManager().updateSettings();
4747
} catch (Exception e) {
4848
LOG.error("Error in updating settings", e);
4949
}
@@ -53,12 +53,12 @@ public void run() {
5353
configurationManager.getConfiguration().getUpdateSettingsWhenIdleInterval());
5454

5555
configurationManager.onChanged(
56-
ignored -> configurationManager.getSettingsManager().updateSettingsThreadSafe());
56+
ignored -> configurationManager.getSettingsManager().updateSettings());
5757
configurationManager
5858
.getQueue()
5959
.onEventsPosted(
6060
(ignored1, ignored2) ->
61-
configurationManager.getSettingsManager().updateSettingsThreadSafe());
61+
configurationManager.getSettingsManager().updateSettings());
6262
}
6363

6464
public static ExceptionlessClient from(String apiKey, String serverUrl) {

src/main/java/com/exceptionless/exceptionlessclient/models/submission/SubmissionResponse.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
@Value
99
@NonFinal
1010
public class SubmissionResponse {
11-
private int statusCode;
12-
private String message;
11+
int statusCode;
12+
String message;
1313

1414
public boolean isSuccess() {
1515
return statusCode >= 200 && statusCode <= 299;

src/main/java/com/exceptionless/exceptionlessclient/queue/DefaultEventQueue.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.exceptionless.exceptionlessclient.models.submission.SubmissionResponse;
88
import com.exceptionless.exceptionlessclient.storage.StorageProviderIF;
99
import com.exceptionless.exceptionlessclient.submission.SubmissionClientIF;
10+
import com.exceptionless.exceptionlessclient.utils.VisibleForTesting;
1011
import lombok.Builder;
1112
import org.slf4j.Logger;
1213
import org.slf4j.LoggerFactory;
@@ -61,6 +62,7 @@ public void run() {
6162
}
6263
}
6364
},
65+
processingIntervalInSecs * 1000,
6466
processingIntervalInSecs * 1000);
6567
}
6668

@@ -84,17 +86,23 @@ private void process() {
8486
process(false);
8587
}
8688

89+
@VisibleForTesting
90+
Boolean isProcessingCurrentlySuspended(){
91+
return shouldSuspendProcessing();
92+
}
93+
8794
@Override
8895
public void enqueue(Event event) {
8996
if (shouldDiscard()) {
9097
LOG.info("Queue items are currently being discarded. This event will not be enqueued");
98+
return;
9199
}
92100

93101
long timestamp = storageProvider.getQueue().save(event);
94102
String logText =
95103
String.format("type: %s", event.getType())
96104
+ (event.getReferenceId() != null
97-
? String.format("refId: %s", event.getReferenceId())
105+
? String.format(", refId: %s", event.getReferenceId())
98106
: "");
99107
LOG.info(String.format("Enqueueing event: %s %s", timestamp, logText));
100108
}
@@ -109,17 +117,24 @@ private boolean shouldDiscard() {
109117

110118
@Override
111119
public void process(boolean isAppExiting) {
112-
if (processingQueue) {
113-
return;
120+
synchronized (this){
121+
if (processingQueue) {
122+
LOG.trace("Currently processing queue; Returning...");
123+
return;
124+
}
125+
processingQueue = true;
114126
}
115127

116-
processingQueue = true;
117128
try {
118129
List<StorageItem<Event>> storedEvents =
119130
storageProvider.getQueue().get(currentSubmissionBatchSize);
131+
if (storedEvents.isEmpty()) {
132+
LOG.trace("No events found to submit; Returning...");
133+
return;
134+
}
135+
120136
List<Event> events =
121137
storedEvents.stream().map(StorageItem::getValue).collect(Collectors.toList());
122-
123138
LOG.info(
124139
String.format("Sending %s events to %s", events.size(), configuration.getServerUrl()));
125140
SubmissionResponse response = submissionClient.postEvents(events, isAppExiting);
@@ -129,7 +144,9 @@ public void process(boolean isAppExiting) {
129144
LOG.error("Error processing queue", e);
130145
suspendProcessing();
131146
} finally {
132-
processingQueue = false;
147+
synchronized (this){
148+
processingQueue = false;
149+
}
133150
}
134151
}
135152

src/main/java/com/exceptionless/exceptionlessclient/queue/EventDataFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ private Object filter(Object data, int currDepth) {
3939

4040
if (data instanceof List) {
4141
List<Object> dataList = (List<Object>) data;
42-
return dataList.stream().map(val -> filter(val, currDepth + 1)).collect(Collectors.toList());
42+
return dataList.stream().map(val -> filter(val, currDepth)).collect(Collectors.toList());
4343
}
4444

4545
return ((Map<String, Object>) data)

src/main/java/com/exceptionless/exceptionlessclient/queue/EventValidator.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.exceptionless.exceptionlessclient.queue;
22

3-
43
import com.exceptionless.exceptionlessclient.exceptions.BadEventDataException;
54

65
public final class EventValidator {
@@ -12,12 +11,15 @@ private EventValidator() {}
1211
public static void validateIdentifier(String value) {
1312
if (value.length() < VALID_IDENTIFIER_MIN_LENGTH
1413
|| value.length() > VALID_IDENTIFIER_MAX_LENGTH) {
15-
throw new BadEventDataException("Value must contain between 8 and 100 characters");
14+
throw new BadEventDataException(
15+
String.format("Value must contain between 8 and 100 characters, found: [%s]", value));
1616
}
1717

1818
if (!value.chars().allMatch(ch -> Character.isLetterOrDigit(ch) || ch == '-')) {
1919
throw new BadEventDataException(
20-
"Value should contain only alphanumeric characters and minus sign");
20+
String.format(
21+
"Value should contain only alphanumeric characters and hyphen sign, found: [%s]",
22+
value));
2123
}
2224
}
2325

src/main/java/com/exceptionless/exceptionlessclient/settings/SettingsManager.java

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void checkVersion(long version) {
4040
}
4141

4242
LOG.info(String.format("Updating settings from v%s to v%s", currentVersion, version));
43-
updateSettingsThreadSafe();
43+
updateSettings();
4444
}
4545

4646
private long getVersion() {
@@ -56,32 +56,31 @@ public ServerSettings getSavedServerSettings() {
5656
return storageItem.getValue();
5757
}
5858

59-
// This method is thread safe as settings are updated both by the users and by the client at
60-
// regular intervals
61-
public synchronized void updateSettingsThreadSafe() {
62-
if (updatingSettings) {
63-
return;
59+
public void updateSettings() {
60+
synchronized (this) {
61+
if (updatingSettings) {
62+
LOG.trace("Already updating settings; Returning...");
63+
return;
64+
}
65+
updatingSettings = true;
6466
}
6567

66-
updatingSettings = true;
6768
try {
68-
updateSettings();
69-
} finally {
70-
updatingSettings = false;
71-
}
72-
}
69+
long currentVersion = getVersion();
70+
LOG.info(String.format("Checking for updated settings from: v%s", currentVersion));
7371

74-
private void updateSettings() {
75-
long currentVersion = getVersion();
76-
LOG.info(String.format("Checking for updated settings from: v%s", currentVersion));
77-
78-
SettingsResponse response = settingsClient.getSettings(currentVersion);
79-
if (!response.isSuccess()) {
80-
LOG.warn(String.format("Unable to update settings: %s:", response.getMessage()));
81-
return;
72+
SettingsResponse response = settingsClient.getSettings(currentVersion);
73+
if (!response.isSuccess()) {
74+
LOG.warn(String.format("Unable to update settings: %s:", response.getMessage()));
75+
return;
76+
}
77+
ServerSettings prevValue = storageProvider.getSettings().peek().getValue();
78+
storageProvider.getSettings().save(response.getSettings());
79+
propertyChangeSupport.firePropertyChange("settings", prevValue, response.getSettings());
80+
} finally {
81+
synchronized (this) {
82+
updatingSettings = false;
83+
}
8284
}
83-
ServerSettings prevValue = storageProvider.getSettings().peek().getValue();
84-
storageProvider.getSettings().save(response.getSettings());
85-
propertyChangeSupport.firePropertyChange("settings", prevValue, response.getSettings());
8685
}
8786
}

src/main/java/com/exceptionless/exceptionlessclient/storage/InMemoryStorage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public StorageItem<X> peek() {
3737

3838
@Override
3939
public List<StorageItem<X>> get(int limit) {
40-
return items.subList(0, Math.min(limit, items.size()));
40+
return new ArrayList<>(items.subList(0, Math.min(limit, items.size())));
4141
}
4242

4343
@Override
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.exceptionless.exceptionlessclient.utils;
2+
3+
public @interface VisibleForTesting {}

0 commit comments

Comments
 (0)