Skip to content

Commit d19797d

Browse files
committed
Update tests
1 parent 76a5c9b commit d19797d

7 files changed

+473
-39
lines changed

Tests/SentryTests/Integrations/WatchdogTerminations/Processors/SentryWatchdogTerminationFieldsProcessorTests.swift

Lines changed: 139 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ class SentryWatchdogTerminationFieldsProcessorTests: XCTestCase {
4242
user.data = ["invalid_key": Double.infinity]
4343
return user
4444
}()
45+
46+
let dist: String = "1.0.0"
47+
48+
let env: String = "test"
4549

4650
init() throws {
4751
let options = Options()
@@ -278,6 +282,132 @@ class SentryWatchdogTerminationFieldsProcessorTests: XCTestCase {
278282
let writtenData = try Data(contentsOf: fixture.scopePersistentStore.currentFileURLFor(field: .user))
279283
XCTAssertEqual(writtenData, data)
280284
}
285+
286+
// MARK: - Dist Tests
287+
288+
func testSetDist_whenDistIsValid_shouldDispatchToQueue() {
289+
// -- Act --
290+
sut.setDist(fixture.dist)
291+
292+
// -- Assert --
293+
XCTAssertEqual(fixture.dispatchQueueWrapper.dispatchAsyncInvocations.count, 1)
294+
}
295+
296+
func testSetDist_whenProcessorIsDeallocatedWhileDispatching_shouldNotCauseRetainCycle() {
297+
// The processor is dispatching the file operation on a background queue.
298+
// This tests checks that the dispatch block is not keeping a strong reference to the
299+
// processor and causes a retain cycle.
300+
301+
// -- Arrange --
302+
// Configure the mock to not execute the block and only keep a reference to the block
303+
fixture.dispatchQueueWrapper.dispatchAsyncExecutesBlock = false
304+
305+
// Define a log mock to assert the execution path
306+
let logOutput = TestLogOutput()
307+
SentrySDKLog.setLogOutput(logOutput)
308+
SentrySDKLog.configureLog(true, diagnosticLevel: .debug)
309+
310+
// -- Act --
311+
sut.setDist(fixture.dist)
312+
sut = nil
313+
314+
// Execute the block after the processor is deallocated to have a weak reference
315+
// in the dispatch block
316+
fixture.dispatchQueueWrapper.invokeLastDispatchAsync()
317+
318+
// -- Assert --
319+
// This assertion is a best-effort check to see if the block was executed, as there is not other
320+
// mechanism to assert this case
321+
XCTAssertTrue(logOutput.loggedMessages.contains { line in
322+
line.contains("Can not set dist, reason: reference to processor is nil")
323+
})
324+
}
325+
326+
func testSetDist_whenDistIsNilAndActiveFileExists_shouldDeleteActiveFile() {
327+
// -- Arrange --
328+
createPersistedFile(field: .dist)
329+
assertPersistedFileExists(field: .dist)
330+
331+
// -- Act --
332+
sut.setDist(nil)
333+
334+
// -- Assert --
335+
assertPersistedFileNotExists(field: .dist)
336+
}
337+
338+
func testSetDist_whenDistIsNilAndActiveFileNotExists_shouldNotThrow() {
339+
// -- Arrange --
340+
assertPersistedFileNotExists(field: .dist)
341+
342+
// -- Act --
343+
sut.setDist(nil)
344+
345+
// -- Assert --
346+
assertPersistedFileNotExists(field: .dist)
347+
}
348+
349+
// MARK: - Environment Tests
350+
351+
func testSetEnvironment_whenEnvironmentIsValid_shouldDispatchToQueue() {
352+
// -- Act --
353+
sut.setEnvironment(fixture.env)
354+
355+
// -- Assert --
356+
XCTAssertEqual(fixture.dispatchQueueWrapper.dispatchAsyncInvocations.count, 1)
357+
}
358+
359+
func testSetEnvironment_whenProcessorIsDeallocatedWhileDispatching_shouldNotCauseRetainCycle() {
360+
// The processor is dispatching the file operation on a background queue.
361+
// This tests checks that the dispatch block is not keeping a strong reference to the
362+
// processor and causes a retain cycle.
363+
364+
// -- Arrange --
365+
// Configure the mock to not execute the block and only keep a reference to the block
366+
fixture.dispatchQueueWrapper.dispatchAsyncExecutesBlock = false
367+
368+
// Define a log mock to assert the execution path
369+
let logOutput = TestLogOutput()
370+
SentrySDKLog.setLogOutput(logOutput)
371+
SentrySDKLog.configureLog(true, diagnosticLevel: .debug)
372+
373+
// -- Act --
374+
sut.setEnvironment(fixture.env)
375+
sut = nil
376+
377+
// Execute the block after the processor is deallocated to have a weak reference
378+
// in the dispatch block
379+
fixture.dispatchQueueWrapper.invokeLastDispatchAsync()
380+
381+
// -- Assert --
382+
// This assertion is a best-effort check to see if the block was executed, as there is not other
383+
// mechanism to assert this case
384+
XCTAssertTrue(logOutput.loggedMessages.contains { line in
385+
line.contains("Can not set environment, reason: reference to processor is nil")
386+
})
387+
}
388+
389+
func testSetEnvironment_whenEnvironmentIsNilAndActiveFileExists_shouldDeleteActiveFile() {
390+
// -- Arrange --
391+
createPersistedFile(field: .environment)
392+
assertPersistedFileExists(field: .environment)
393+
394+
// -- Act --
395+
sut.setEnvironment(nil)
396+
397+
// -- Assert --
398+
assertPersistedFileNotExists(field: .environment)
399+
}
400+
401+
func testSetEnvironment_whenEnvironmentIsNilAndActiveFileNotExists_shouldNotThrow() {
402+
// -- Arrange --
403+
assertPersistedFileNotExists(field: .environment)
404+
405+
// -- Act --
406+
sut.setEnvironment(nil)
407+
408+
// -- Assert --
409+
assertPersistedFileNotExists(field: .environment)
410+
}
281411

282412
// MARK: - Clear Tests
283413

@@ -337,19 +467,27 @@ class SentryWatchdogTerminationFieldsProcessorTests: XCTestCase {
337467
// -- Arrange --
338468
let contextData = Data("Context content".utf8)
339469
let userData = Data("User content".utf8)
470+
let distData = Data("Dist content".utf8)
471+
let envData = Data("Environment content".utf8)
340472

341473
createPersistedFile(field: .context, data: contextData)
342474
createPersistedFile(field: .user, data: userData)
343-
475+
createPersistedFile(field: .dist, data: distData)
476+
createPersistedFile(field: .environment, data: envData)
477+
344478
assertPersistedFileExists(field: .context)
345479
assertPersistedFileExists(field: .user)
480+
assertPersistedFileExists(field: .dist)
481+
assertPersistedFileExists(field: .environment)
346482

347483
// -- Act --
348484
sut.clear()
349485

350486
// -- Assert --
351487
assertPersistedFileNotExists(field: .context)
352488
assertPersistedFileNotExists(field: .user)
489+
assertPersistedFileNotExists(field: .dist)
490+
assertPersistedFileNotExists(field: .environment)
353491
}
354492

355493
// MARK: - Assertion Helpers

Tests/SentryTests/Integrations/WatchdogTerminations/SentryWatchdogTerminationScopeObserverTests.swift

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class SentryWatchdogTerminationScopeObserverTests: XCTestCase {
2424
]
2525
]
2626
let user: User = User(userId: "123")
27+
let dist = "1.0.0"
28+
let env = "prod"
2729

2830
init() throws {
2931
let fileManager = try TestFileManager(options: Options())
@@ -159,6 +161,54 @@ class SentryWatchdogTerminationScopeObserverTests: XCTestCase {
159161
let invocationContext = try XCTUnwrap(invocation)
160162
XCTAssertEqual(invocationContext, user)
161163
}
164+
165+
func testSetDist_whenDistIsNil_shouldCallFieldsProcessorSetDist() throws {
166+
// -- Act --
167+
sut.setDist(nil)
168+
169+
// -- Assert --
170+
XCTAssertEqual(fixture.fieldsProcessor.setDistInvocations.count, 1)
171+
let invocation = try XCTUnwrap(fixture.fieldsProcessor.setDistInvocations.first)
172+
XCTAssertNil(invocation)
173+
}
174+
175+
func testSetDist_whenDistIsDefined_shouldCallFieldsProcessorSetDist() throws {
176+
// -- Arrange --
177+
let dist = fixture.dist
178+
179+
// -- Act --
180+
sut.setDist(dist)
181+
182+
// -- Assert --
183+
XCTAssertEqual(fixture.fieldsProcessor.setDistInvocations.count, 1)
184+
let invocation = try XCTUnwrap(fixture.fieldsProcessor.setDistInvocations.first)
185+
let invocationContext = try XCTUnwrap(invocation)
186+
XCTAssertEqual(invocationContext, dist)
187+
}
188+
189+
func testSetEnvironment_whenEnvironmentIsNil_shouldCallFieldsProcessorSetEnvironment() throws {
190+
// -- Act --
191+
sut.setEnvironment(nil)
192+
193+
// -- Assert --
194+
XCTAssertEqual(fixture.fieldsProcessor.setEnvironmentInvocations.count, 1)
195+
let invocation = try XCTUnwrap(fixture.fieldsProcessor.setEnvironmentInvocations.first)
196+
XCTAssertNil(invocation)
197+
}
198+
199+
func testSetEnvironment_whenEnvironmentIsDefined_shouldCallFieldsProcessorSetEnvironment() throws {
200+
// -- Arrange --
201+
let env = fixture.env
202+
203+
// -- Act --
204+
sut.setEnvironment(env)
205+
206+
// -- Assert --
207+
XCTAssertEqual(fixture.fieldsProcessor.setEnvironmentInvocations.count, 1)
208+
let invocation = try XCTUnwrap(fixture.fieldsProcessor.setEnvironmentInvocations.first)
209+
let invocationContext = try XCTUnwrap(invocation)
210+
XCTAssertEqual(invocationContext, env)
211+
}
162212
}
163213

164214
#endif // os(iOS) || os(tvOS) || targetEnvironment(macCatalyst)

Tests/SentryTests/Integrations/WatchdogTerminations/SentryWatchdogTerminationTrackerTests.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,12 @@ class SentryWatchdogTerminationTrackerTests: NotificationCenterTestCase {
318318

319319
let testUser = TestData.user
320320
let testContext = ["device": ["name": "iPhone"], "appData": ["version": "1.0.0"]] as [String: [String: Any]]
321+
let dist = "1.0.0"
322+
let env = "development"
321323
sentryWatchdogTerminationScopeObserver.setUser(testUser)
322324
sentryWatchdogTerminationScopeObserver.setContext(testContext)
325+
sentryWatchdogTerminationScopeObserver.setDist(dist)
326+
sentryWatchdogTerminationScopeObserver.setEnvironment(env)
323327

324328
sut.start()
325329
goToForeground()
@@ -335,6 +339,9 @@ class SentryWatchdogTerminationTrackerTests: NotificationCenterTestCase {
335339
XCTAssertEqual(fatalEvent?.user?.email, testUser.email)
336340
XCTAssertEqual(fatalEvent?.user?.username, testUser.username)
337341
XCTAssertEqual(fatalEvent?.user?.name, testUser.name)
342+
343+
XCTAssertEqual(fatalEvent?.dist, dist)
344+
XCTAssertEqual(fatalEvent?.environment, env)
338345

339346
// Verify context is properly set (including the app.in_foreground = true that's added by the tracker)
340347
let eventContext = fatalEvent?.context

Tests/SentryTests/Integrations/WatchdogTerminations/SentryWatchdogTerminationTrackingIntegrationTests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ class SentryWatchdogTerminationIntegrationTests: XCTestCase {
175175
XCTAssertEqual(fixture.watchdogTerminationFieldsProcessor.setUserInvocations.count, 1)
176176
fixture.scope.setContext(value: ["key": "value"], key: "foo")
177177
fixture.scope.setUser(User(userId: "user1234"))
178+
fixture.scope.setDist("dist-124")
179+
fixture.scope.setEnvironment("test")
178180

179181
// -- Assert --
180182
// As the instance of the scope observer is dynamically created by the dependency container,
@@ -186,17 +188,27 @@ class SentryWatchdogTerminationIntegrationTests: XCTestCase {
186188
XCTAssertEqual(fixture.watchdogTerminationFieldsProcessor.setUserInvocations.count, 2)
187189
let userInvocation = try XCTUnwrap(fixture.watchdogTerminationFieldsProcessor.setUserInvocations.last)
188190
XCTAssertEqual(userInvocation?.userId, "user1234")
191+
XCTAssertEqual(fixture.watchdogTerminationFieldsProcessor.setDistInvocations.count, 2)
192+
let distInvocation = try XCTUnwrap(fixture.watchdogTerminationFieldsProcessor.setDistInvocations.last)
193+
XCTAssertEqual(distInvocation, "dist-124")
194+
XCTAssertEqual(fixture.watchdogTerminationFieldsProcessor.setEnvironmentInvocations.count, 2)
195+
let envInvocation = try XCTUnwrap(fixture.watchdogTerminationFieldsProcessor.setEnvironmentInvocations.last)
196+
XCTAssertEqual(envInvocation, "test")
189197
}
190198

191199
func testInstallWithOptions_shouldSetCurrentContextOnScopeObserver() throws {
192200
// -- Arrange --
193201
let sut = fixture.getSut()
194202
fixture.scope.contextDictionary = ["foo": ["key": "value"]]
195203
fixture.scope.userObject = User(userId: "user1234")
204+
fixture.scope.distString = "dist-124"
205+
fixture.scope.environmentString = "test"
196206

197207
// Check pre-condition
198208
XCTAssertEqual(fixture.watchdogTerminationFieldsProcessor.setContextInvocations.count, 0)
199209
XCTAssertEqual(fixture.watchdogTerminationFieldsProcessor.setUserInvocations.count, 0)
210+
XCTAssertEqual(fixture.watchdogTerminationFieldsProcessor.setDistInvocations.count, 0)
211+
XCTAssertEqual(fixture.watchdogTerminationFieldsProcessor.setEnvironmentInvocations.count, 0)
200212

201213
// -- Act --
202214
sut.install(with: fixture.options)
@@ -211,6 +223,12 @@ class SentryWatchdogTerminationIntegrationTests: XCTestCase {
211223
XCTAssertEqual(fixture.watchdogTerminationFieldsProcessor.setUserInvocations.count, 1)
212224
let userInvocation = try XCTUnwrap(fixture.watchdogTerminationFieldsProcessor.setUserInvocations.last)
213225
XCTAssertEqual(userInvocation?.userId, "user1234")
226+
XCTAssertEqual(fixture.watchdogTerminationFieldsProcessor.setDistInvocations.count, 1)
227+
let distInvocation = try XCTUnwrap(fixture.watchdogTerminationFieldsProcessor.setDistInvocations.last)
228+
XCTAssertEqual(distInvocation, "dist-124")
229+
XCTAssertEqual(fixture.watchdogTerminationFieldsProcessor.setEnvironmentInvocations.count, 1)
230+
let envInvocation = try XCTUnwrap(fixture.watchdogTerminationFieldsProcessor.setEnvironmentInvocations.last)
231+
XCTAssertEqual(envInvocation, "test")
214232
}
215233

216234
func testANRDetected_UpdatesAppStateToTrue() throws {

Tests/SentryTests/Integrations/WatchdogTerminations/TestSentryWatchdogTerminationFieldsProcessor.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
class TestSentryWatchdogTerminationFieldsProcessor: SentryWatchdogTerminationFieldsProcessor {
88
var setContextInvocations = Invocations<[String: [String: Any]]?>()
99
var setUserInvocations = Invocations<User?>()
10+
var setDistInvocations = Invocations<String?>()
11+
var setEnvironmentInvocations = Invocations<String?>()
1012
var clearInvocations = Invocations<Void>()
1113

1214
override func setContext(_ context: [String: [String: Any]]?) {
@@ -16,6 +18,14 @@ class TestSentryWatchdogTerminationFieldsProcessor: SentryWatchdogTerminationFie
1618
override func setUser(_ user: User?) {
1719
setUserInvocations.record(user)
1820
}
21+
22+
override func setDist(_ dist: String?) {
23+
setDistInvocations.record(dist)
24+
}
25+
26+
override func setEnvironment(_ environment: String?) {
27+
setEnvironmentInvocations.record(environment)
28+
}
1929

2030
override func clear() {
2131
clearInvocations.record(())

Tests/SentryTests/Integrations/WatchdogTerminations/TestSentryWatchdogTerminationFieldsProcessorTests.swift

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ class TestSentryWatchdogTerminationFieldsProcessorTests: XCTestCase {
7272
func testSetUser_shouldRecordInvocations() throws {
7373
// -- Arrange --
7474
// Clean the invocations to ensure a clean state
75-
sut.setContextInvocations.removeAll()
76-
XCTAssertEqual(sut.setContextInvocations.count, 0)
75+
sut.setUserInvocations.removeAll()
76+
XCTAssertEqual(sut.setUserInvocations.count, 0)
7777

7878
// -- Act --
7979
sut.setUser(User(userId: "user1234"))
@@ -90,6 +90,47 @@ class TestSentryWatchdogTerminationFieldsProcessorTests: XCTestCase {
9090
let thirdInvocation = try XCTUnwrap(sut.setUserInvocations.invocations.element(at: 2))
9191
XCTAssertNil(thirdInvocation)
9292
}
93+
94+
func testSetDist_shouldRecordInvocations() throws {
95+
// -- Arrange --
96+
// Clean the invocations to ensure a clean state
97+
sut.setDistInvocations.removeAll()
98+
XCTAssertEqual(sut.setDistInvocations.count, 0)
99+
100+
// -- Act --
101+
sut.setDist("1.0.0")
102+
sut.setDist("2.0.0")
103+
sut.setDist(nil)
104+
105+
// -- Assert --
106+
XCTAssertEqual(sut.setDistInvocations.count, 3)
107+
XCTAssertEqual(
108+
sut.setDistInvocations.invocations.element(at: 0),
109+
"1.0.0"
110+
)
111+
XCTAssertEqual(sut.setDistInvocations.invocations.element(at: 1), "2.0.0")
112+
let thirdInvocation = try XCTUnwrap(sut.setDistInvocations.invocations.element(at: 2))
113+
XCTAssertNil(thirdInvocation)
114+
}
115+
116+
func testSetEnvironment_shouldRecordInvocations() throws {
117+
// -- Arrange --
118+
// Clean the invocations to ensure a clean state
119+
sut.setEnvironmentInvocations.removeAll()
120+
XCTAssertEqual(sut.setEnvironmentInvocations.count, 0)
121+
122+
// -- Act --
123+
sut.setEnvironment("dev")
124+
sut.setEnvironment("prod")
125+
sut.setEnvironment(nil)
126+
127+
// -- Assert --
128+
XCTAssertEqual(sut.setEnvironmentInvocations.count, 3)
129+
XCTAssertEqual(sut.setEnvironmentInvocations.invocations.element(at: 0), "dev")
130+
XCTAssertEqual(sut.setEnvironmentInvocations.invocations.element(at: 1), "prod")
131+
let thirdInvocation = try XCTUnwrap(sut.setEnvironmentInvocations.invocations.element(at: 2))
132+
XCTAssertNil(thirdInvocation)
133+
}
93134

94135
func testClear_shouldRecordInvocations() throws {
95136
// -- Arrange --

0 commit comments

Comments
 (0)