Skip to content

Commit b1891d3

Browse files
committed
fixed tests and added more protocols
1 parent 193c27b commit b1891d3

File tree

5 files changed

+39
-25
lines changed

5 files changed

+39
-25
lines changed

Sources/Swift/Helper/SentryDispatchQueueWrapper.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
@_implementationOnly import _SentryPrivate
22

3+
protocol SentryDispatchQueueWrapperProtocol {
4+
func dispatchSync(_ block: @escaping () -> Void)
5+
func dispatchAsync(_ block: @escaping () -> Void)
6+
func dispatch(after interval: TimeInterval, block: @escaping () -> Void)
7+
func dispatchAsyncOnMainQueueIfNotMainThread(block: @escaping () -> Void)
8+
func dispatchSyncOnMainQueue(block: @escaping () -> Void)
9+
func dispatchSyncOnMainQueue(_ block: @escaping () -> Void, timeout: Double)
10+
func dispatchOnce(_ predicate: UnsafeMutablePointer<CLong>, block: @escaping () -> Void)
11+
func dispatch(after interval: TimeInterval, workItem: DispatchWorkItem)
12+
}
13+
314
// This is the Swift version of `_SentryDispatchQueueWrapperInternal`
415
// It exists to allow the implementation of `_SentryDispatchQueueWrapperInternal`
516
// to be accessible to Swift without making that header file public
@@ -76,3 +87,5 @@
7687
return true
7788
}
7889
}
90+
91+
extension SentryDispatchQueueWrapper: SentryDispatchQueueWrapperProtocol {}

Sources/Swift/Tools/SentryItemBatcher.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import Foundation
33

44
protocol SentryItemBatcherDelegate: AnyObject {
5-
func capture(itemsData: Data, count: Int)
5+
func capture(itemBatcherData: Data, count: Int)
66
}
77

88
protocol SentryItemBatcherItem: Encodable {
@@ -11,7 +11,7 @@ protocol SentryItemBatcherItem: Encodable {
1111
var body: String { get }
1212
}
1313

14-
class SentryItemBatcher<Item: SentryItemBatcherItem> {
14+
final class SentryItemBatcher<Item: SentryItemBatcherItem> {
1515
struct Config {
1616
let beforeSendItem: ((Item) -> Item?)?
1717
let environment: String
@@ -25,7 +25,7 @@ class SentryItemBatcher<Item: SentryItemBatcherItem> {
2525
}
2626

2727
private let config: Config
28-
private let dispatchQueue: SentryDispatchQueueWrapper
28+
private let dispatchQueue: SentryDispatchQueueWrapperProtocol
2929

3030
// Every items data is added sepratley. They are flushed together in an envelope.
3131
private var encodedItems: [Data] = []
@@ -49,7 +49,7 @@ class SentryItemBatcher<Item: SentryItemBatcherItem> {
4949
/// - Note: Items are flushed when either `maxItemCount` or `maxBufferSizeBytes` limit is reached.
5050
@_spi(Private) public init(
5151
config: Config,
52-
dispatchQueue: SentryDispatchQueueWrapper
52+
dispatchQueue: SentryDispatchQueueWrapperProtocol
5353
) {
5454
self.config = config
5555
self.dispatchQueue = dispatchQueue
@@ -236,6 +236,6 @@ class SentryItemBatcher<Item: SentryItemBatcherItem> {
236236
SentrySDKLog.debug("Delegate not set, not capturing items data.")
237237
return
238238
}
239-
delegate.capture(itemsData: payloadData, count: encodedItems.count)
239+
delegate.capture(itemBatcherData: payloadData, count: encodedItems.count)
240240
}
241241
}

Sources/Swift/Tools/SentryLogBatcher.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,11 @@ extension SentryLog: SentryItemBatcherItem {}
100100
}
101101

102102
extension SentryLogBatcher: SentryItemBatcherDelegate {
103-
func capture(itemsData: Data, count: Int) {
103+
func capture(itemBatcherData: Data, count: Int) {
104104
guard let delegate else {
105105
SentrySDKLog.debug("SentryLogBatcher: Delegate not set, not capturing logs data.")
106106
return
107107
}
108-
delegate.capture(logsData: itemsData as NSData, count: NSNumber(value: count))
108+
delegate.capture(logsData: itemBatcherData as NSData, count: NSNumber(value: count))
109109
}
110110
}

Tests/SentryTests/SentryItemBatcherTests.swift

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ final class SentryItemBatcherTests: XCTestCase {
6363
sut.captureItems()
6464

6565
// -- Assert --
66-
XCTAssertEqual(testDelegate.captureItemsDataInvocations.count, 1)
66+
XCTAssertEqual(testDelegate.captureItemsBatcherDataInvocations.count, 1)
6767

6868
let capturedItems = testDelegate.getCapturedItems()
6969
XCTAssertEqual(capturedItems.count, 2)
@@ -83,7 +83,7 @@ final class SentryItemBatcherTests: XCTestCase {
8383
sut.addItem(largeItem, scope: scope)
8484

8585
// -- Assert --
86-
XCTAssertEqual(testDelegate.captureItemsDataInvocations.count, 1)
86+
XCTAssertEqual(testDelegate.captureItemsBatcherDataInvocations.count, 1)
8787

8888
// Verify the large item is sent
8989
let capturedItems = testDelegate.getCapturedItems()
@@ -103,13 +103,13 @@ final class SentryItemBatcherTests: XCTestCase {
103103
sut.addItem(item, scope: scope)
104104
}
105105

106-
XCTAssertEqual(testDelegate.captureItemsDataInvocations.count, 0)
106+
XCTAssertEqual(testDelegate.captureItemsBatcherDataInvocations.count, 0)
107107

108108
let item = createTestItem(body: "Item \(10)") // Reached 10 max items limit
109109
sut.addItem(item, scope: scope)
110110

111111
// -- Assert --
112-
XCTAssertEqual(testDelegate.captureItemsDataInvocations.count, 1)
112+
XCTAssertEqual(testDelegate.captureItemsBatcherDataInvocations.count, 1)
113113

114114
let capturedItems = testDelegate.getCapturedItems()
115115
XCTAssertEqual(capturedItems.count, 10, "Should have captured exactly \(10) items")
@@ -129,7 +129,7 @@ final class SentryItemBatcherTests: XCTestCase {
129129
// -- Assert --
130130
XCTAssertEqual(testDispatchQueue.dispatchAfterWorkItemInvocations.count, 1)
131131
XCTAssertEqual(testDispatchQueue.dispatchAfterWorkItemInvocations.first?.interval, 0.1)
132-
XCTAssertEqual(testDelegate.captureItemsDataInvocations.count, 1)
132+
XCTAssertEqual(testDelegate.captureItemsBatcherDataInvocations.count, 1)
133133

134134
let capturedItems = testDelegate.getCapturedItems()
135135
XCTAssertEqual(capturedItems.count, 1)
@@ -149,7 +149,7 @@ final class SentryItemBatcherTests: XCTestCase {
149149
// -- Assert --
150150
XCTAssertEqual(testDispatchQueue.dispatchAfterWorkItemInvocations.count, 1)
151151
XCTAssertEqual(testDispatchQueue.dispatchAfterWorkItemInvocations.first?.interval, 0.1)
152-
XCTAssertEqual(testDelegate.captureItemsDataInvocations.count, 1)
152+
XCTAssertEqual(testDelegate.captureItemsBatcherDataInvocations.count, 1)
153153

154154
let capturedItems = testDelegate.getCapturedItems()
155155
XCTAssertEqual(capturedItems.count, 2)
@@ -169,7 +169,7 @@ final class SentryItemBatcherTests: XCTestCase {
169169
sut.captureItems()
170170

171171
// -- Assert --
172-
XCTAssertEqual(testDelegate.captureItemsDataInvocations.count, 1)
172+
XCTAssertEqual(testDelegate.captureItemsBatcherDataInvocations.count, 1)
173173

174174
let capturedItems = testDelegate.getCapturedItems()
175175
XCTAssertEqual(capturedItems.count, 2)
@@ -187,7 +187,7 @@ final class SentryItemBatcherTests: XCTestCase {
187187
timerWorkItem.perform()
188188

189189
// -- Assert --
190-
XCTAssertEqual(testDelegate.captureItemsDataInvocations.count, 1, "Manual flush should work and timer should be cancelled")
190+
XCTAssertEqual(testDelegate.captureItemsBatcherDataInvocations.count, 1, "Manual flush should work and timer should be cancelled")
191191
}
192192

193193
func testManualCaptureItems_WithEmptyBuffer_DoesNothing() {
@@ -198,7 +198,7 @@ final class SentryItemBatcherTests: XCTestCase {
198198
sut.captureItems()
199199

200200
// -- Assert --
201-
XCTAssertEqual(testDelegate.captureItemsDataInvocations.count, 0)
201+
XCTAssertEqual(testDelegate.captureItemsBatcherDataInvocations.count, 0)
202202
}
203203

204204
// MARK: - Edge Cases Tests
@@ -217,7 +217,7 @@ final class SentryItemBatcherTests: XCTestCase {
217217
timerWorkItem.perform()
218218

219219
// -- Assert --
220-
XCTAssertEqual(testDelegate.captureItemsDataInvocations.count, 1)
220+
XCTAssertEqual(testDelegate.captureItemsBatcherDataInvocations.count, 1)
221221
}
222222

223223
func testAddItemAfterFlush_StartsNewBatch() throws {
@@ -233,7 +233,7 @@ final class SentryItemBatcherTests: XCTestCase {
233233
sut.captureItems()
234234

235235
// -- Assert --
236-
XCTAssertEqual(testDelegate.captureItemsDataInvocations.count, 2)
236+
XCTAssertEqual(testDelegate.captureItemsBatcherDataInvocations.count, 2)
237237

238238
let allCapturedItems = testDelegate.getCapturedItems()
239239
XCTAssertEqual(allCapturedItems.count, 2)
@@ -303,15 +303,15 @@ final class SentryItemBatcherTests: XCTestCase {
303303

304304
// -- Act --
305305
sutWithRealQueue.addItem(item, scope: scope)
306-
XCTAssertEqual(testDelegate.captureItemsDataInvocations.count, 0)
306+
XCTAssertEqual(testDelegate.captureItemsBatcherDataInvocations.count, 0)
307307

308308
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
309309
expectation.fulfill()
310310
}
311311
wait(for: [expectation], timeout: 1.0)
312312

313313
// -- Assert --
314-
XCTAssertEqual(testDelegate.captureItemsDataInvocations.count, 1, "Timeout should trigger flush")
314+
XCTAssertEqual(testDelegate.captureItemsBatcherDataInvocations.count, 1, "Timeout should trigger flush")
315315

316316
let capturedItems = self.testDelegate.getCapturedItems()
317317
XCTAssertEqual(capturedItems.count, 1, "Should contain exactly one item")
@@ -711,7 +711,7 @@ final class SentryItemBatcherTests: XCTestCase {
711711

712712
// -- Assert --
713713
XCTAssertTrue(beforeSendCalled)
714-
XCTAssertEqual(testDelegate.captureItemsDataInvocations.count, 0)
714+
XCTAssertEqual(testDelegate.captureItemsBatcherDataInvocations.count, 0)
715715
}
716716

717717
func testBeforeSendItem_NotSet_ItemCapturedUnmodified() throws {
@@ -813,17 +813,17 @@ struct TestItem: SentryItemBatcherItem {
813813
// MARK: - Test Helpers
814814

815815
final class TestItemBatcherDelegate: NSObject, SentryItemBatcherDelegate {
816-
var captureItemsDataInvocations = Invocations<(data: Data, count: Int)>()
816+
var captureItemsBatcherDataInvocations = Invocations<(data: Data, count: Int)>()
817817

818-
func capture(itemsData: Data, count: Int) {
819-
captureItemsDataInvocations.record((itemsData, count))
818+
func capture(itemBatcherData: Data, count: Int) {
819+
captureItemsBatcherDataInvocations.record((itemBatcherData, count))
820820
}
821821

822822
// Helper to get captured items
823823
func getCapturedItems() -> [TestItem] {
824824
var allItems: [TestItem] = []
825825

826-
for invocation in captureItemsDataInvocations.invocations {
826+
for invocation in captureItemsBatcherDataInvocations.invocations {
827827
if let jsonObject = try? JSONSerialization.jsonObject(with: invocation.data) as? [String: Any],
828828
let items = jsonObject["items"] as? [[String: Any]] {
829829
for item in items {

Tests/SentryTests/SentryLogBatcherTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ final class SentryLogBatcherTests: XCTestCase {
297297

298298
func testAddLog_AddsDefaultAttributes() throws {
299299
// -- Arrange --
300+
options.environment = "test-environment"
300301
options.releaseName = "1.0.0"
301302
let sut = getSut()
302303

0 commit comments

Comments
 (0)