@@ -609,13 +609,8 @@ class DataUploadWorkerTests: XCTestCase {
609609 worker. cancelSynchronously ( )
610610
611611 // Then
612- XCTAssertEqual ( telemetry. messages. count, 2 )
612+ XCTAssertEqual ( telemetry. messages. count, 1 )
613613 XCTAssertNotNil ( telemetry. messages. firstMetric ( named: " upload_cycle " ) , " An upload cycle metric should be send to `telemetry`. " )
614-
615- let metric = try XCTUnwrap ( telemetry. messages. firstMetric ( named: " Batch Blocked " ) , " A Batch Blocked metric should be send to `telemetry`. " )
616- XCTAssertEqual ( metric. attributes [ " failure " ] as? String , " intake-code- \( randomStatusCode. rawValue) " )
617- XCTAssertNil ( metric. attributes [ " blockers " ] )
618- XCTAssertEqual ( metric. attributes [ " track " ] as? String , featureName)
619614 }
620615
621616 func testWhenDataIsUploadedWithAlertingStatusCode_itSendsErrorTelemetry( ) throws {
@@ -659,17 +654,9 @@ class DataUploadWorkerTests: XCTestCase {
659654 worker. cancelSynchronously ( )
660655
661656 // Then
662- XCTAssertEqual ( telemetry. messages. count, 3 )
663-
664- XCTAssertNotNil ( telemetry. messages. firstMetric ( named: " upload_cycle " ) , " An upload cycle metric should be send to `telemetry`. " )
665657
666658 let error = try XCTUnwrap ( telemetry. messages. firstError ( ) , " An error should be send to `telemetry`. " )
667659 XCTAssertEqual ( error. message, " Data upload finished with status code: \( randomStatusCode. rawValue) " )
668-
669- let metric = try XCTUnwrap ( telemetry. messages. firstMetric ( named: " Batch Blocked " ) , " A Batch Blocked metric should be send to `telemetry`. " )
670- XCTAssertEqual ( metric. attributes [ " failure " ] as? String , " intake-code- \( randomStatusCode. rawValue) " )
671- XCTAssertNil ( metric. attributes [ " blockers " ] )
672- XCTAssertEqual ( metric. attributes [ " track " ] as? String , featureName)
673660 }
674661
675662 func testWhenDataCannotBeUploadedDueToNetworkError_itSendsErrorTelemetry( ) throws {
@@ -707,15 +694,52 @@ class DataUploadWorkerTests: XCTestCase {
707694 worker. cancelSynchronously ( )
708695
709696 // Then
710- XCTAssertEqual ( telemetry. messages. count, 3 )
711-
712- XCTAssertNotNil ( telemetry. messages. firstMetric ( named: " upload_cycle " ) , " An upload cycle metric should be send to `telemetry`. " )
713-
714697 let error = try XCTUnwrap ( telemetry. messages. firstError ( ) , " An error should be send to `telemetry`. " )
715698 XCTAssertEqual ( error. message, #"Data upload finished with error - Error Domain=abc Code=0 "(null)""# )
699+ }
700+
701+ func testWhenDataIsUploadedWithRetryableStatusCode_itSendsBatchBlockedTelemetry( ) throws {
702+ // Given
703+ let telemetry = TelemetryMock ( )
704+
705+ writer. write ( value: [ " key " : " value " ] )
706+ let randomStatusCode : HTTPResponseStatusCode = [
707+ . requestTimeout,
708+ . tooManyRequests,
709+ . internalServerError,
710+ . serviceUnavailable
711+ ] . randomElement ( ) !
712+
713+ // When
714+ let startUploadExpectation = self . expectation ( description: " Upload has started " )
715+ let mockDataUploader = DataUploaderMock (
716+ uploadStatus: . mockWith( needsRetry: true , error: . httpError( statusCode: randomStatusCode) )
717+ )
716718
719+ mockDataUploader. onUpload = { previousUploadStatus in
720+ XCTAssertNil ( previousUploadStatus)
721+ startUploadExpectation. fulfill ( )
722+ }
723+
724+ let featureName : String = . mockRandom( )
725+ let worker = DataUploadWorker (
726+ queue: uploaderQueue,
727+ fileReader: reader,
728+ dataUploader: mockDataUploader,
729+ contextProvider: . mockAny( ) ,
730+ uploadConditions: . alwaysUpload( ) ,
731+ delay: DataUploadDelay ( performance: UploadPerformanceMock . veryQuickInitialUpload) ,
732+ featureName: featureName,
733+ telemetry: telemetry,
734+ maxBatchesPerUpload: . mockRandom( min: 1 , max: 100 )
735+ )
736+
737+ wait ( for: [ startUploadExpectation] , timeout: 0.5 )
738+ worker. cancelSynchronously ( )
739+
740+ // Then
717741 let metric = try XCTUnwrap ( telemetry. messages. firstMetric ( named: " Batch Blocked " ) , " A Batch Blocked metric should be send to `telemetry`. " )
718- XCTAssertEqual ( metric. attributes [ " failure " ] as? String , " network -code-\( nserror . code ) " )
742+ XCTAssertEqual ( metric. attributes [ " failure " ] as? String , " intake -code-\( randomStatusCode . rawValue ) " )
719743 XCTAssertNil ( metric. attributes [ " blockers " ] )
720744 XCTAssertEqual ( metric. attributes [ " track " ] as? String , featureName)
721745 }
0 commit comments