Skip to content

Commit f3a3221

Browse files
committed
Added unit tests for submission package
1 parent aa986c8 commit f3a3221

File tree

9 files changed

+300
-71
lines changed

9 files changed

+300
-71
lines changed

src/main/java/com/exceptionless/exceptionlessclient/configuration/ConfigurationManager.java

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

3-
import com.exceptionless.exceptionlessclient.exceptions.ClientException;
3+
import com.exceptionless.exceptionlessclient.exceptions.SubmissionException;
44
import com.exceptionless.exceptionlessclient.lastreferenceidmanager.DefaultLastReferenceIdManager;
55
import com.exceptionless.exceptionlessclient.lastreferenceidmanager.LastReferenceIdManagerIF;
66
import com.exceptionless.exceptionlessclient.logging.LogCapturerAppender;
@@ -134,7 +134,7 @@ private void checkApiKeyIsValid() {
134134
return;
135135
}
136136

137-
throw new ClientException(
137+
throw new SubmissionException(
138138
String.format("Apikey is not valid: [%s]", this.configuration.getApiKey()));
139139
}
140140

src/main/java/com/exceptionless/exceptionlessclient/exceptions/ClientException.java

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.exceptionless.exceptionlessclient.exceptions;
2+
3+
public class SubmissionException extends RuntimeException {
4+
public SubmissionException(Throwable cause) {
5+
super(cause);
6+
}
7+
8+
public SubmissionException(String message) {
9+
super(message);
10+
}
11+
}

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

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

33
import com.exceptionless.exceptionlessclient.configuration.Configuration;
4-
import com.exceptionless.exceptionlessclient.exceptions.ClientException;
4+
import com.exceptionless.exceptionlessclient.exceptions.SubmissionException;
55
import com.exceptionless.exceptionlessclient.models.Event;
66
import com.exceptionless.exceptionlessclient.models.storage.StorageItem;
77
import com.exceptionless.exceptionlessclient.models.submission.SubmissionResponse;
@@ -82,10 +82,6 @@ private boolean shouldSuspendProcessing() {
8282
return LocalDateTime.now().isBefore(suspendProcessingUntil);
8383
}
8484

85-
private void process() {
86-
process(false);
87-
}
88-
8985
@VisibleForTesting
9086
Boolean isProcessingCurrentlySuspended(){
9187
return shouldSuspendProcessing();
@@ -116,7 +112,7 @@ private boolean shouldDiscard() {
116112
}
117113

118114
@Override
119-
public void process(boolean isAppExiting) {
115+
public void process() {
120116
synchronized (this){
121117
if (processingQueue) {
122118
LOG.trace("Currently processing queue; Returning...");
@@ -137,10 +133,10 @@ public void process(boolean isAppExiting) {
137133
storedEvents.stream().map(StorageItem::getValue).collect(Collectors.toList());
138134
LOG.info(
139135
String.format("Sending %s events to %s", events.size(), configuration.getServerUrl()));
140-
SubmissionResponse response = submissionClient.postEvents(events, isAppExiting);
136+
SubmissionResponse response = submissionClient.postEvents(events);
141137
processSubmissionResponse(response, storedEvents);
142138
eventPosted(response, events);
143-
} catch (ClientException e) {
139+
} catch (SubmissionException e) {
144140
LOG.error("Error processing queue", e);
145141
suspendProcessing();
146142
} finally {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
public interface EventQueueIF {
1111
void enqueue(Event event);
1212

13-
void process(boolean isAppExiting);
13+
void process();
1414

1515
void suspendProcessing(Duration duration, boolean discardFutureQueueItems, boolean clearQueue);
1616

src/main/java/com/exceptionless/exceptionlessclient/submission/DefaultSubmissionClient.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
package com.exceptionless.exceptionlessclient.submission;
22

33
import com.exceptionless.exceptionlessclient.configuration.Configuration;
4-
import com.exceptionless.exceptionlessclient.exceptions.ClientException;
4+
import com.exceptionless.exceptionlessclient.exceptions.SubmissionException;
55
import com.exceptionless.exceptionlessclient.models.Event;
66
import com.exceptionless.exceptionlessclient.models.UserDescription;
77
import com.exceptionless.exceptionlessclient.models.submission.SubmissionResponse;
88
import com.exceptionless.exceptionlessclient.settings.SettingsManager;
99
import com.exceptionless.exceptionlessclient.utils.Utils;
10+
import com.exceptionless.exceptionlessclient.utils.VisibleForTesting;
1011
import lombok.Builder;
1112
import org.slf4j.Logger;
1213
import org.slf4j.LoggerFactory;
1314

14-
import java.io.IOException;
1515
import java.net.URI;
16-
import java.net.URISyntaxException;
1716
import java.net.http.HttpClient;
1817
import java.net.http.HttpHeaders;
1918
import java.net.http.HttpRequest;
@@ -31,15 +30,22 @@ public class DefaultSubmissionClient implements SubmissionClientIF {
3130
private final HttpClient httpClient;
3231

3332
@Builder
34-
private DefaultSubmissionClient(
35-
Configuration configuration, SettingsManager settingsManager) {
33+
public DefaultSubmissionClient(Configuration configuration, SettingsManager settingsManager) {
3634
this.configuration = configuration;
3735
this.settingsManager = settingsManager;
3836
this.httpClient = HttpClient.newHttpClient();
3937
}
4038

39+
@VisibleForTesting
40+
DefaultSubmissionClient(
41+
Configuration configuration, SettingsManager settingsManager, HttpClient httpClient) {
42+
this.configuration = configuration;
43+
this.settingsManager = settingsManager;
44+
this.httpClient = httpClient;
45+
}
46+
4147
@Override
42-
public SubmissionResponse postEvents(List<Event> events, boolean isAppExiting) {
48+
public SubmissionResponse postEvents(List<Event> events) {
4349
return postSubmission(
4450
String.format(
4551
"%s/api/v2/events?access_token=%s",
@@ -78,8 +84,8 @@ private SubmissionResponse postSubmission(String url, Object data) {
7884
.statusCode(response.statusCode())
7985
.message(response.body())
8086
.build();
81-
} catch (URISyntaxException | InterruptedException | IOException e) {
82-
throw new ClientException(e);
87+
} catch (Exception e) {
88+
throw new SubmissionException(e);
8389
}
8490
}
8591

@@ -108,7 +114,7 @@ public void sendHeartBeat(String sessionIdOrUserId, boolean closeSession) {
108114
.uri(uri)
109115
.GET()
110116
.header("X-Exceptionless-Client", Configuration.USER_AGENT)
111-
.timeout(Duration.ofMillis(configuration.getSettingsClientTimeoutInMillis()))
117+
.timeout(Duration.ofMillis(configuration.getSubmissionClientTimeoutInMillis()))
112118
.build();
113119

114120
HttpResponse<String> response =
@@ -120,8 +126,8 @@ public void sendHeartBeat(String sessionIdOrUserId, boolean closeSession) {
120126
"Error in submitting heartbeat to the server for sessionOrUserId: %s",
121127
sessionIdOrUserId));
122128
}
123-
} catch (URISyntaxException | InterruptedException | IOException e) {
124-
throw new ClientException(e);
129+
} catch (Exception e) {
130+
throw new SubmissionException(e);
125131
}
126132
}
127133
}

src/main/java/com/exceptionless/exceptionlessclient/submission/SubmissionClientIF.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88

99
public interface SubmissionClientIF {
1010
SubmissionResponse postEvents(
11-
List<Event> events,
12-
boolean isAppExiting);
11+
List<Event> events);
1312

1413
SubmissionResponse postUserDescription(
1514
String referenceId,

src/test/java/com/exceptionless/exceptionlessclient/queue/DefaultEventQueueTest.java

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import com.exceptionless.exceptionlessclient.TestFixtures;
44
import com.exceptionless.exceptionlessclient.configuration.Configuration;
5-
import com.exceptionless.exceptionlessclient.exceptions.ClientException;
5+
import com.exceptionless.exceptionlessclient.exceptions.SubmissionException;
66
import com.exceptionless.exceptionlessclient.models.Event;
77
import com.exceptionless.exceptionlessclient.models.submission.SubmissionResponse;
88
import com.exceptionless.exceptionlessclient.storage.InMemoryStorage;
@@ -64,10 +64,10 @@ public void itCanProcessABatchSuccessfully() {
6464

6565
SubmissionResponse response =
6666
SubmissionResponse.builder().message("test-message").statusCode(200).build();
67-
doReturn(response).when(submissionClient).postEvents(List.of(event), false);
67+
doReturn(response).when(submissionClient).postEvents(List.of(event));
6868

6969
queue.onEventsPosted(testHandler);
70-
queue.process(false);
70+
queue.process();
7171

7272
assertThat(storage.peek()).isNull();
7373
verify(testHandler, times(1)).accept(List.of(event), response);
@@ -87,11 +87,11 @@ public void itShouldNotProcessIfCurrentlyProcessingEvents()
8787
return response;
8888
})
8989
.when(submissionClient)
90-
.postEvents(List.of(event), false);
90+
.postEvents(List.of(event));
9191

9292
queue.onEventsPosted(testHandler);
93-
Future<?> future = Executors.newSingleThreadExecutor().submit(() -> queue.process(false));
94-
queue.process(false);
93+
Future<?> future = Executors.newSingleThreadExecutor().submit(() -> queue.process());
94+
queue.process();
9595
future.get();
9696

9797
assertThat(storage.get(2)).hasSize(1); // Only one is processed
@@ -101,7 +101,7 @@ public void itShouldNotProcessIfCurrentlyProcessingEvents()
101101
@Test
102102
public void itShouldNotPostEmptyEvents() {
103103
queue.onEventsPosted(testHandler);
104-
queue.process(false);
104+
queue.process();
105105

106106
verifyZeroInteractions(submissionClient);
107107
verifyZeroInteractions(testHandler);
@@ -111,10 +111,10 @@ public void itShouldNotPostEmptyEvents() {
111111
public void itShouldSuspendProcessingOnClientException() {
112112
storage.save(event);
113113

114-
doThrow(new ClientException("test")).when(submissionClient).postEvents(List.of(event), false);
114+
doThrow(new SubmissionException("test")).when(submissionClient).postEvents(List.of(event));
115115

116116
queue.onEventsPosted(testHandler);
117-
queue.process(false);
117+
queue.process();
118118

119119
assertThat(queue.isProcessingCurrentlySuspended()).isTrue();
120120
assertThat(storage.peek().getValue()).isEqualTo(event);
@@ -127,10 +127,10 @@ public void itShouldSuspendProcessingIfServiceIsUnavailable() {
127127

128128
SubmissionResponse response =
129129
SubmissionResponse.builder().message("test-message").statusCode(503).build();
130-
doReturn(response).when(submissionClient).postEvents(List.of(event), false);
130+
doReturn(response).when(submissionClient).postEvents(List.of(event));
131131

132132
queue.onEventsPosted(testHandler);
133-
queue.process(false);
133+
queue.process();
134134

135135
assertThat(queue.isProcessingCurrentlySuspended()).isTrue();
136136
assertThat(storage.peek().getValue()).isEqualTo(event);
@@ -143,10 +143,10 @@ public void itShouldSuspendAndDiscardProcessingAndClearQueueIfNoPayment() {
143143

144144
SubmissionResponse response =
145145
SubmissionResponse.builder().message("test-message").statusCode(402).build();
146-
doReturn(response).when(submissionClient).postEvents(List.of(event), false);
146+
doReturn(response).when(submissionClient).postEvents(List.of(event));
147147

148148
queue.onEventsPosted(testHandler);
149-
queue.process(false);
149+
queue.process();
150150

151151
assertThat(queue.isProcessingCurrentlySuspended()).isTrue();
152152
assertThat(storage.peek()).isNull(); // queue is cleared
@@ -163,10 +163,10 @@ public void itShouldSuspendProcessingAndClearQueueIfUnableToAuthenticate() {
163163

164164
SubmissionResponse response =
165165
SubmissionResponse.builder().message("test-message").statusCode(401).build();
166-
doReturn(response).when(submissionClient).postEvents(List.of(event), false);
166+
doReturn(response).when(submissionClient).postEvents(List.of(event));
167167

168168
queue.onEventsPosted(testHandler);
169-
queue.process(false);
169+
queue.process();
170170

171171
assertThat(queue.isProcessingCurrentlySuspended()).isTrue();
172172
assertThat(storage.peek()).isNull(); // queue is cleared
@@ -179,10 +179,10 @@ public void itShouldSuspendProcessingAndClearQueueForNotFoundResponse() {
179179

180180
SubmissionResponse response =
181181
SubmissionResponse.builder().message("test-message").statusCode(404).build();
182-
doReturn(response).when(submissionClient).postEvents(List.of(event), false);
182+
doReturn(response).when(submissionClient).postEvents(List.of(event));
183183

184184
queue.onEventsPosted(testHandler);
185-
queue.process(false);
185+
queue.process();
186186

187187
assertThat(queue.isProcessingCurrentlySuspended()).isTrue();
188188
assertThat(storage.peek()).isNull(); // queue is cleared
@@ -195,10 +195,10 @@ public void itShouldSuspendProcessingAndClearQueueForBadRequest() {
195195

196196
SubmissionResponse response =
197197
SubmissionResponse.builder().message("test-message").statusCode(400).build();
198-
doReturn(response).when(submissionClient).postEvents(List.of(event), false);
198+
doReturn(response).when(submissionClient).postEvents(List.of(event));
199199

200200
queue.onEventsPosted(testHandler);
201-
queue.process(false);
201+
queue.process();
202202

203203
assertThat(queue.isProcessingCurrentlySuspended()).isTrue();
204204
assertThat(storage.peek()).isNull(); // queue is cleared
@@ -211,10 +211,10 @@ public void itShouldSuspendProcessingByDefault() {
211211

212212
SubmissionResponse response =
213213
SubmissionResponse.builder().message("test-message").statusCode(-1).build();
214-
doReturn(response).when(submissionClient).postEvents(List.of(event), false);
214+
doReturn(response).when(submissionClient).postEvents(List.of(event));
215215

216216
queue.onEventsPosted(testHandler);
217-
queue.process(false);
217+
queue.process();
218218

219219
assertThat(queue.isProcessingCurrentlySuspended()).isTrue();
220220
assertThat(storage.peek().getValue()).isEqualTo(event);
@@ -239,22 +239,22 @@ public void itShouldReduceSubmissionBatchSizeIfRequestEntitiesAreTooLarge() {
239239

240240
SubmissionResponse response =
241241
SubmissionResponse.builder().message("test-message").statusCode(413).build();
242-
doReturn(response).when(submissionClient).postEvents(anyList(), anyBoolean());
242+
doReturn(response).when(submissionClient).postEvents(anyList());
243243

244244
queue.onEventsPosted(testHandler);
245-
queue.process(false);
245+
queue.process();
246246

247247
assertThat(storage.get(10)).hasSize(10);
248248
verify(testHandler, times(1)).accept(anyList(), eq(response));
249249

250-
queue.process(false);
250+
queue.process();
251251

252252
// One invocation with full batch
253253
verify(submissionClient, times(1))
254-
.postEvents(argThat(argument -> argument.size() == 3), anyBoolean());
254+
.postEvents(argThat(argument -> argument.size() == 3));
255255
// One invocation with reduced batch
256256
verify(submissionClient, times(1))
257-
.postEvents(argThat(argument -> argument.size() == 2), anyBoolean());
257+
.postEvents(argThat(argument -> argument.size() == 2));
258258
}
259259

260260
@Test
@@ -265,10 +265,10 @@ public void itShouldDiscardEventsIfItCantReduceSubmissionSizeAndRequestEntitiesA
265265

266266
SubmissionResponse response =
267267
SubmissionResponse.builder().message("test-message").statusCode(413).build();
268-
doReturn(response).when(submissionClient).postEvents(anyList(), anyBoolean());
268+
doReturn(response).when(submissionClient).postEvents(anyList());
269269

270270
queue.onEventsPosted(testHandler);
271-
queue.process(false);
271+
queue.process();
272272

273273
assertThat(storage.get(10)).hasSize(9);
274274
verify(testHandler, times(1)).accept(anyList(), eq(response));
@@ -292,30 +292,30 @@ public void itShouldResetSubmissionBatchSizeOnNextSuccessfulResponse() {
292292

293293
doReturn(SubmissionResponse.builder().message("test-message").statusCode(413).build())
294294
.when(submissionClient)
295-
.postEvents(anyList(), anyBoolean());
296-
queue.process(false);
295+
.postEvents(anyList());
296+
queue.process();
297297

298298
doReturn(SubmissionResponse.builder().message("test-message").statusCode(200).build())
299299
.when(submissionClient)
300-
.postEvents(anyList(), anyBoolean());
301-
queue.process(false);
302-
queue.process(false);
300+
.postEvents(anyList());
301+
queue.process();
302+
queue.process();
303303

304304
// Two invocations with full batch; First with the default size and next after a successful
305305
// response
306306
verify(submissionClient, times(2))
307-
.postEvents(argThat(argument -> argument.size() == 3), anyBoolean());
307+
.postEvents(argThat(argument -> argument.size() == 3));
308308
// One invocation with reduced batch
309309
verify(submissionClient, times(1))
310-
.postEvents(argThat(argument -> argument.size() == 2), anyBoolean());
310+
.postEvents(argThat(argument -> argument.size() == 2));
311311
}
312312

313313
@Test
314314
public void itShouldProcessEventsUsingTimer() throws InterruptedException {
315315
storage.save(event);
316316
SubmissionResponse response =
317317
SubmissionResponse.builder().message("test-message").statusCode(200).build();
318-
doReturn(response).when(submissionClient).postEvents(List.of(event), false);
318+
doReturn(response).when(submissionClient).postEvents(List.of(event));
319319

320320
Configuration configuration =
321321
TestFixtures.aDefaultConfiguration().submissionBatchSize(1).build();

0 commit comments

Comments
 (0)