@@ -46,7 +46,7 @@ class FilesOrchestrator_MetricsTests: XCTestCase {
4646
4747 // MARK: - "Batch Deleted" Metric
4848
49- func testWhenReadableFileIsDeleted_itSendsBatchDeletedMetric ( ) throws {
49+ func testWhenReadableFileIsDeleted_itSendsTelemetryMetric ( ) throws {
5050 // Given
5151 let orchestrator = createOrchestrator ( )
5252 let expectedBatchAge = storage. minFileAgeForRead + 1
@@ -63,8 +63,8 @@ class FilesOrchestrator_MetricsTests: XCTestCase {
6363 orchestrator. delete ( readableFile: file, deletionReason: . intakeCode( responseCode: 202 ) )
6464
6565 // Then
66- let metric = try XCTUnwrap ( telemetry. messages. firstMetricReport ( named: " Batch Deleted " ) )
67- DDAssertJSONEqual ( metric . attributes, [
66+ let batchDeleted = try XCTUnwrap ( telemetry. messages. firstMetricReport ( named: " Batch Deleted " ) )
67+ DDAssertJSONEqual ( batchDeleted . attributes, [
6868 " metric_type " : " batch deleted " ,
6969 " track " : " track name " ,
7070 " consent " : " consent value " ,
@@ -76,13 +76,20 @@ class FilesOrchestrator_MetricsTests: XCTestCase {
7676 " in_background " : false ,
7777 " background_tasks_enabled " : false ,
7878 " batch_age " : expectedBatchAge. toMilliseconds,
79- " batch_removal_reason " : " intake-code-202 " ,
80- " pending_batches " : 1
79+ " batch_removal_reason " : " intake-code-202 "
8180 ] )
82- XCTAssertEqual ( metric. sampleRate, BatchDeletedMetric . sampleRate)
81+ XCTAssertEqual ( batchDeleted. sampleRate, BatchDeletedMetric . sampleRate)
82+
83+ let pendingBatches = telemetry. messages. compactMap { $0. asMetricIncrement } . reduce ( 0 ) { count, metric in
84+ XCTAssertEqual ( metric. metric, " pending batches " )
85+ XCTAssertEqual ( metric. cardinalities [ " track " ] , . string( " track name " ) )
86+ return count + metric. increment
87+ }
88+
89+ XCTAssertEqual ( pendingBatches, 1 )
8390 }
8491
85- func testWhenObsoleteFileIsDeleted_itSendsBatchDeletedMetric ( ) throws {
92+ func testWhenObsoleteFileIsDeleted_itSendsTelemetryMetric ( ) throws {
8693 // Given:
8794 // - request some batch to be created
8895 let orchestrator = createOrchestrator ( )
@@ -95,8 +102,8 @@ class FilesOrchestrator_MetricsTests: XCTestCase {
95102 _ = orchestrator. getReadableFiles ( )
96103
97104 // Then
98- let metric = try XCTUnwrap ( telemetry. messages. firstMetricReport ( named: " Batch Deleted " ) )
99- DDAssertJSONEqual ( metric . attributes, [
105+ let batchDeleted = try XCTUnwrap ( telemetry. messages. firstMetricReport ( named: " Batch Deleted " ) )
106+ DDAssertJSONEqual ( batchDeleted . attributes, [
100107 " metric_type " : " batch deleted " ,
101108 " track " : " track name " ,
102109 " consent " : " consent value " ,
@@ -108,13 +115,29 @@ class FilesOrchestrator_MetricsTests: XCTestCase {
108115 " in_background " : false ,
109116 " background_tasks_enabled " : false ,
110117 " batch_age " : ( storage. maxFileAgeForRead + 1 ) . toMilliseconds,
111- " batch_removal_reason " : " obsolete " ,
112- " pending_batches " : 0
118+ " batch_removal_reason " : " obsolete "
113119 ] )
114- XCTAssertEqual ( metric. sampleRate, BatchDeletedMetric . sampleRate)
120+ XCTAssertEqual ( batchDeleted. sampleRate, BatchDeletedMetric . sampleRate)
121+
122+ let pendingBatches = telemetry. messages. reduce ( 0 ) { count, message in
123+ switch message {
124+ case let . metric( . record( metric, value, cardinalities) ) :
125+ XCTAssertEqual ( metric, " pending batches " )
126+ XCTAssertEqual ( cardinalities [ " track " ] , . string( " track name " ) )
127+ return value
128+ case let . metric( . increment( metric, value, cardinalities) ) :
129+ XCTAssertEqual ( metric, " pending batches " )
130+ XCTAssertEqual ( cardinalities [ " track " ] , . string( " track name " ) )
131+ return count + value
132+ default :
133+ return count
134+ }
135+ }
136+
137+ XCTAssertEqual ( pendingBatches, 0 )
115138 }
116139
117- func testWhenDirectoryIsPurged_itSendsBatchDeletedMetrics ( ) throws {
140+ func testWhenDirectoryIsPurged_itSendsTelemetryMetrics ( ) throws {
118141 // Given: some batch
119142 // - request batch to be created
120143 // - write more data than allowed directory size limit
@@ -130,8 +153,8 @@ class FilesOrchestrator_MetricsTests: XCTestCase {
130153 _ = try orchestrator. getWritableFile ( writeSize: 1 )
131154
132155 // Then
133- let metric = try XCTUnwrap ( telemetry. messages. firstMetricReport ( named: " Batch Deleted " ) )
134- DDAssertJSONEqual ( metric . attributes, [
156+ let batchDeleted = try XCTUnwrap ( telemetry. messages. firstMetricReport ( named: " Batch Deleted " ) )
157+ DDAssertJSONEqual ( batchDeleted . attributes, [
135158 " metric_type " : " batch deleted " ,
136159 " track " : " track name " ,
137160 " consent " : " consent value " ,
@@ -143,10 +166,17 @@ class FilesOrchestrator_MetricsTests: XCTestCase {
143166 " in_background " : false ,
144167 " background_tasks_enabled " : false ,
145168 " batch_age " : expectedBatchAge. toMilliseconds,
146- " batch_removal_reason " : " purged " ,
147- " pending_batches " : 0
169+ " batch_removal_reason " : " purged "
148170 ] )
149- XCTAssertEqual ( metric. sampleRate, BatchDeletedMetric . sampleRate)
171+ XCTAssertEqual ( batchDeleted. sampleRate, BatchDeletedMetric . sampleRate)
172+
173+ let pendingBatches = telemetry. messages. compactMap { $0. asMetricIncrement } . reduce ( 0 ) { count, metric in
174+ XCTAssertEqual ( metric. metric, " pending batches " )
175+ XCTAssertEqual ( metric. cardinalities [ " track " ] , . string( " track name " ) )
176+ return count + metric. increment
177+ }
178+
179+ XCTAssertEqual ( pendingBatches, 1 )
150180 }
151181
152182 // MARK: - "Batch Closed" Metric
0 commit comments