Skip to content

Commit

Permalink
Merge branch 'release/3.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
matus-tomlein committed Jun 28, 2022
2 parents 78fca9f + ed9b605 commit dc5ecb6
Show file tree
Hide file tree
Showing 32 changed files with 328 additions and 91 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Version 3.2.0 (2022-06-28)
--------------------------
Add eventIndex and firstEventTimestamp properties to client sessions (#682)
Add a customizable set of failure HTTP status codes for which collector requests should not be retried (#684)

Version 3.1.2 (2022-05-25)
--------------------------
Attach LifecycleEntity before the first Background event (#679)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ - (void)tearDown {
}

- (void)testPauseEmitter {
SPMockNetworkConnection *networkConnection = [[SPMockNetworkConnection alloc] initWithRequestOption:SPHttpMethodPost successfulConnection:YES];
SPMockNetworkConnection *networkConnection = [[SPMockNetworkConnection alloc] initWithRequestOption:SPHttpMethodPost statusCode:200];
SPEmitterConfiguration *emitterConfig = [[SPEmitterConfiguration alloc] init];
emitterConfig.eventStore = [SPMockEventStore new];
emitterConfig.bufferOption = SPBufferOptionSingle;
Expand Down
64 changes: 55 additions & 9 deletions Snowplow iOSTests/Legacy Tests/LegacyTestEmitter.m
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ - (void)testEmitEventWithBrokenNetworkConnectionDoesntFreezeEmitterStatus {
}

- (void)testEmitSingleGetEventWithSuccess {
SPMockNetworkConnection *networkConnection = [[SPMockNetworkConnection alloc] initWithRequestOption:SPHttpMethodGet successfulConnection:YES];
SPMockNetworkConnection *networkConnection = [[SPMockNetworkConnection alloc] initWithRequestOption:SPHttpMethodGet statusCode:200];
SPEmitter *emitter = [self emitterWithNetworkConnection:networkConnection bufferOption:SPBufferOptionSingle];
[emitter addPayloadToBuffer:[self generatePayloads:1].firstObject];

Expand All @@ -169,7 +169,7 @@ - (void)testEmitSingleGetEventWithSuccess {
}

- (void)testEmitSingleGetEventWithNoSuccess {
SPMockNetworkConnection *networkConnection = [[SPMockNetworkConnection alloc] initWithRequestOption:SPHttpMethodGet successfulConnection:NO];
SPMockNetworkConnection *networkConnection = [[SPMockNetworkConnection alloc] initWithRequestOption:SPHttpMethodGet statusCode:500];
SPEmitter *emitter = [self emitterWithNetworkConnection:networkConnection bufferOption:SPBufferOptionSingle];
[emitter addPayloadToBuffer:[self generatePayloads:1].firstObject];

Expand All @@ -186,7 +186,7 @@ - (void)testEmitSingleGetEventWithNoSuccess {
}

- (void)testEmitTwoGetEventsWithSuccess {
SPMockNetworkConnection *networkConnection = [[SPMockNetworkConnection alloc] initWithRequestOption:SPHttpMethodGet successfulConnection:YES];
SPMockNetworkConnection *networkConnection = [[SPMockNetworkConnection alloc] initWithRequestOption:SPHttpMethodGet statusCode:200];
SPEmitter *emitter = [self emitterWithNetworkConnection:networkConnection bufferOption:SPBufferOptionSingle];

for (SPPayload *payload in [self generatePayloads:2]) {
Expand All @@ -211,7 +211,7 @@ - (void)testEmitTwoGetEventsWithSuccess {
}

- (void)testEmitTwoGetEventsWithNoSuccess {
SPMockNetworkConnection *networkConnection = [[SPMockNetworkConnection alloc] initWithRequestOption:SPHttpMethodGet successfulConnection:NO];
SPMockNetworkConnection *networkConnection = [[SPMockNetworkConnection alloc] initWithRequestOption:SPHttpMethodGet statusCode:500];
SPEmitter *emitter = [self emitterWithNetworkConnection:networkConnection bufferOption:SPBufferOptionSingle];

for (SPPayload *payload in [self generatePayloads:2]) {
Expand All @@ -233,7 +233,7 @@ - (void)testEmitTwoGetEventsWithNoSuccess {
}

- (void)testEmitSinglePostEventWithSuccess {
SPMockNetworkConnection *networkConnection = [[SPMockNetworkConnection alloc] initWithRequestOption:SPHttpMethodPost successfulConnection:YES];
SPMockNetworkConnection *networkConnection = [[SPMockNetworkConnection alloc] initWithRequestOption:SPHttpMethodPost statusCode:200];
SPEmitter *emitter = [self emitterWithNetworkConnection:networkConnection bufferOption:SPBufferOptionSingle];

[emitter addPayloadToBuffer:[self generatePayloads:1].firstObject];
Expand All @@ -251,7 +251,7 @@ - (void)testEmitSinglePostEventWithSuccess {
}

- (void)testEmitEventsPostAsGroup {
SPMockNetworkConnection *networkConnection = [[SPMockNetworkConnection alloc] initWithRequestOption:SPHttpMethodPost successfulConnection:NO];
SPMockNetworkConnection *networkConnection = [[SPMockNetworkConnection alloc] initWithRequestOption:SPHttpMethodPost statusCode:500];
SPEmitter *emitter = [self emitterWithNetworkConnection:networkConnection bufferOption:SPBufferOptionDefaultGroup];

NSArray<SPPayload *> *payloads = [self generatePayloads:15];
Expand All @@ -264,7 +264,7 @@ - (void)testEmitEventsPostAsGroup {
}

XCTAssertEqual(14, [emitter getDbCount]);
networkConnection.successfulConnection = YES;
networkConnection.statusCode = 200;
NSUInteger prevSendingCount = [networkConnection sendingCount];
[emitter addPayloadToBuffer:payloads[14]];

Expand Down Expand Up @@ -292,7 +292,7 @@ - (void)testEmitEventsPostAsGroup {
}

- (void)testEmitOversizeEventsPostAsGroup {
SPMockNetworkConnection *networkConnection = [[SPMockNetworkConnection alloc] initWithRequestOption:SPHttpMethodPost successfulConnection:NO];
SPMockNetworkConnection *networkConnection = [[SPMockNetworkConnection alloc] initWithRequestOption:SPHttpMethodPost statusCode:500];
SPEmitter *emitter = [self emitterWithNetworkConnection:networkConnection build:^(id<SPEmitterBuilder> builder) {
[builder setBufferOption:SPBufferOptionDefaultGroup];
[builder setByteLimitPost:5];
Expand All @@ -308,7 +308,7 @@ - (void)testEmitOversizeEventsPostAsGroup {
}

XCTAssertEqual(0, [emitter getDbCount]);
networkConnection.successfulConnection = YES;
networkConnection.statusCode = 200;
NSUInteger prevSendingCount = [networkConnection sendingCount];
[emitter addPayloadToBuffer:payloads[14]];

Expand Down Expand Up @@ -341,6 +341,52 @@ - (SPEmitter *)emitterWithNetworkConnection:(id<SPNetworkConnection>)networkConn
}];
}

- (void)testRemovesEventsFromQueueOnNoRetryStatus {
SPMockNetworkConnection *networkConnection = [[SPMockNetworkConnection alloc] initWithRequestOption:SPHttpMethodGet statusCode:403];
SPEmitter *emitter = [self emitterWithNetworkConnection:networkConnection bufferOption:SPBufferOptionSingle];

[emitter addPayloadToBuffer:[self generatePayloads:1].firstObject];

[NSThread sleepForTimeInterval:1];

XCTAssertEqual(0, [emitter getDbCount]);
for (NSArray<SPRequestResult *> *results in networkConnection.previousResults) {
for (SPRequestResult *result in results) {
XCTAssertFalse(result.isSuccessful);
}
}

[emitter flush];
}

- (void)testFollowCustomRetryRules {
SPMockNetworkConnection *networkConnection = [[SPMockNetworkConnection alloc] initWithRequestOption:SPHttpMethodGet statusCode:500];
SPEmitter *emitter = [self emitterWithNetworkConnection:networkConnection bufferOption:SPBufferOptionSingle];

NSMutableDictionary *customRules = [[NSMutableDictionary alloc] init];
[customRules setObject:@YES forKey:@403];
[customRules setObject:@NO forKey:@500];
[emitter setCustomRetryForStatusCodes:customRules];

[emitter addPayloadToBuffer:[self generatePayloads:1].firstObject];

[NSThread sleepForTimeInterval:1];

// no events in queue since they were dropped because retrying is disabled for 500
XCTAssertEqual(0, [emitter getDbCount]);

networkConnection.statusCode = 403;

[emitter addPayloadToBuffer:[self generatePayloads:1].firstObject];

[NSThread sleepForTimeInterval:1];

// event still in queue because retrying is enabled for 403
XCTAssertEqual(1, [emitter getDbCount]);

[emitter flush];
}

// MARK: - Service methods

- (NSArray<SPPayload *> *)generatePayloads:(int)count {
Expand Down
2 changes: 1 addition & 1 deletion Snowplow iOSTests/TestGeneratedJsons.m
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ - (void)testScreenContextJson {

- (void)testClientSessionContextJson {
SPSession * session = [[SPSession alloc] init];
NSDictionary * data = [session getSessionDictWithEventId:@"first-event-id"];
NSDictionary * data = [session getSessionDictWithEventId:@"first-event-id" eventTimestamp:1654496481346];
NSDictionary * json = [[[SPSelfDescribingJson alloc] initWithSchema:kSPSessionContextSchema andData:data] getAsDictionary];
XCTAssertTrue([validator validateJson:json]);
}
Expand Down
3 changes: 1 addition & 2 deletions Snowplow iOSTests/TestRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,9 @@ - (instancetype)initWithResultCode:(NSInteger)resultCode method:(SPHttpMethod)ht
}

- (NSArray<SPRequestResult *> *)sendRequests:(NSArray<SPRequest *> *)requests {
BOOL isSuccess = self.resultCode == 200;
NSMutableArray<SPRequestResult *> *results = [NSMutableArray new];
for (SPRequest *request in requests) {
SPRequestResult *result = [[SPRequestResult alloc] initWithSuccess:isSuccess storeIds:request.emitterEventIds];
SPRequestResult *result = [[SPRequestResult alloc] initWithStatusCode:self.resultCode oversize:request.oversize storeIds:request.emitterEventIds];
[results addObject:result];
}
return results;
Expand Down
47 changes: 41 additions & 6 deletions Snowplow iOSTests/TestRequestResult.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,55 @@ - (void)tearDown {
[super tearDown];
}

- (void)testInit {
- (void)testSuccessfulRequest {
NSMutableArray<NSNumber *> *emitterEventIds = [NSMutableArray new];
[emitterEventIds addObject:@1];
SPRequestResult *result = [[SPRequestResult alloc] initWithSuccess:YES storeIds:emitterEventIds];
SPRequestResult *result = [[SPRequestResult alloc] initWithStatusCode:200 oversize:NO storeIds:emitterEventIds];

XCTAssertNotNil(result);
XCTAssertEqual(result.isSuccessful, YES);
XCTAssertEqual([result shouldRetry:@{}], NO);
XCTAssertEqual(result.storeIds, emitterEventIds);

result = [SPRequestResult new];

}

- (void)testFailedRequest {
NSMutableArray<NSNumber *> *emitterEventIds = [NSMutableArray new];
[emitterEventIds addObject:@1];
SPRequestResult *result = [[SPRequestResult alloc] initWithStatusCode:500 oversize:NO storeIds:emitterEventIds];
XCTAssertEqual(result.isSuccessful, NO);
XCTAssertEqual([result shouldRetry:@{}], YES);
}

- (void)testDefaultResult {
SPRequestResult *result = [SPRequestResult new];

XCTAssertNotNil(result);
XCTAssertEqual(result.isSuccessful, NO);
XCTAssertEqual(result.storeIds.count, 0);
}

- (void)testOversizedFailedRequest {
SPRequestResult *result = [[SPRequestResult alloc] initWithStatusCode:500 oversize:YES storeIds:@[]];
XCTAssertEqual(result.isSuccessful, NO);
XCTAssertEqual([result shouldRetry:@{}], NO);
}

- (void)testFailedRequestWithNoRetryStatus {
SPRequestResult *result = [[SPRequestResult alloc] initWithStatusCode:403 oversize:NO storeIds:@[]];
XCTAssertEqual(result.isSuccessful, NO);
XCTAssertEqual([result shouldRetry:@{}], NO);
}

- (void)testFailedRequestWithCustomNoRetryStatus {
NSMutableDictionary *customRetryRules = [[NSMutableDictionary alloc] init];
[customRetryRules setObject:@YES forKey:@403];
[customRetryRules setObject:@NO forKey:@500];

SPRequestResult *result = [[SPRequestResult alloc] initWithStatusCode:403 oversize:NO storeIds:@[]];
XCTAssertEqual([result shouldRetry:customRetryRules], YES);

result = [[SPRequestResult alloc] initWithStatusCode:500 oversize:NO storeIds:@[]];
XCTAssertEqual([result shouldRetry:customRetryRules], NO);
}

@end
Loading

0 comments on commit dc5ecb6

Please sign in to comment.