SessionCoordinatorTests.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // Copyright 2022 Google LLC
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. import XCTest
  16. @testable import FirebaseSessions
  17. class SessionCoordinatorTests: XCTestCase {
  18. var identifiers = MockIdentifierProvider()
  19. var installations = MockInstallationsProtocol()
  20. var time = MockTimeProvider()
  21. var fireLogger = MockGDTLogger()
  22. var appInfo = MockApplicationInfo()
  23. var sampler = SessionSampler()
  24. var coordinator: SessionCoordinator!
  25. override func setUp() {
  26. super.setUp()
  27. coordinator = SessionCoordinator(
  28. identifiers: identifiers,
  29. installations: installations,
  30. fireLogger: fireLogger,
  31. sampler: sampler
  32. )
  33. sampler.sessionSamplingRate = 1.0
  34. }
  35. func test_attemptLoggingSessionStart_logsToGDT() throws {
  36. identifiers.mockAllValidIDs()
  37. let event = SessionStartEvent(identifiers: identifiers, appInfo: appInfo, time: time)
  38. var resultSuccess = false
  39. coordinator.attemptLoggingSessionStart(event: event) { result in
  40. switch result {
  41. case .success(()):
  42. resultSuccess = true
  43. case .failure:
  44. resultSuccess = false
  45. }
  46. }
  47. // Make sure we've set the Installation ID
  48. assertEqualProtoString(
  49. event.proto.session_data.firebase_installation_id,
  50. expected: MockInstallationsProtocol.testInstallationId,
  51. fieldName: "installation_id"
  52. )
  53. // We should have logged successfully
  54. XCTAssertEqual(fireLogger.loggedEvent, event)
  55. XCTAssert(resultSuccess)
  56. }
  57. func test_attemptLoggingSessionStart_handlesGDTError() throws {
  58. identifiers.mockAllValidIDs()
  59. fireLogger.result = .failure(NSError(domain: "TestError", code: -1))
  60. let event = SessionStartEvent(identifiers: identifiers, appInfo: appInfo, time: time)
  61. // Start success so it must be set to false
  62. var resultSuccess = true
  63. coordinator.attemptLoggingSessionStart(event: event) { result in
  64. switch result {
  65. case .success(()):
  66. resultSuccess = true
  67. case .failure:
  68. resultSuccess = false
  69. }
  70. }
  71. // Make sure we've set the Installation ID
  72. assertEqualProtoString(
  73. event.proto.session_data.firebase_installation_id,
  74. expected: MockInstallationsProtocol.testInstallationId,
  75. fieldName: "installation_id"
  76. )
  77. // We should have logged the event, but with a failed result
  78. XCTAssertEqual(fireLogger.loggedEvent, event)
  79. XCTAssertFalse(resultSuccess)
  80. }
  81. func test_eventNotDropped_handlesAllEventsAllowed() throws {
  82. identifiers.mockAllValidIDs()
  83. let event = SessionStartEvent(identifiers: identifiers, appInfo: appInfo, time: time)
  84. sampler.sessionSamplingRate = 1.0
  85. var resultSuccess = true
  86. coordinator.attemptLoggingSessionStart(event: event) { result in
  87. switch result {
  88. case .success(()):
  89. resultSuccess = true
  90. case .failure:
  91. resultSuccess = false
  92. }
  93. }
  94. XCTAssertTrue(resultSuccess)
  95. }
  96. func test_eventDropped_handlesZeroSamplingRate() throws {
  97. identifiers.mockAllValidIDs()
  98. let event = SessionStartEvent(identifiers: identifiers, appInfo: appInfo, time: time)
  99. sampler.sessionSamplingRate = 0.0
  100. var resultSuccess = true
  101. coordinator.attemptLoggingSessionStart(event: event) { result in
  102. switch result {
  103. case .success(()):
  104. resultSuccess = true
  105. case .failure:
  106. resultSuccess = false
  107. }
  108. }
  109. XCTAssertFalse(resultSuccess)
  110. }
  111. }