Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion SentryTestUtils/TestClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@ import Foundation

public class TestClient: SentryClient {
public override init?(options: Options) {
super.init(options: options, fileManager: try! TestFileManager(options: options), deleteOldEnvelopeItems: false, transportAdapter: TestTransportAdapter(transports: [TestTransport()], options: options))
super.init(
options: options,
fileManager: try! TestFileManager(
options: options,
dateProvider: TestCurrentDateProvider(),
dispatchQueueWrapper: TestSentryDispatchQueueWrapper()
),
deleteOldEnvelopeItems: false,
transportAdapter: TestTransportAdapter(transports: [TestTransport()], options: options)
)
}

public override init?(options: Options, fileManager: SentryFileManager, deleteOldEnvelopeItems: Bool) {
Expand Down
4 changes: 0 additions & 4 deletions SentryTestUtils/TestFileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ public class TestFileManager: SentryFileManager {
public var storeEnvelopePath: String?
public var storeEnvelopePathNil: Bool = false

public init(options: Options) throws {
try super.init(options: options, dispatchQueueWrapper: TestSentryDispatchQueueWrapper())
}

public override func store(_ envelope: SentryEnvelope) -> String? {
storeEnvelopeInvocations.record(envelope)
if storeEnvelopePathNil {
Expand Down
17 changes: 13 additions & 4 deletions SentryTestUtilsTests/TestFileManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class TestFileManagerTests: XCTestCase {

private class Fixture {
fileprivate var dateProvider = TestCurrentDateProvider()
fileprivate var dispatchQueueWrapper = TestSentryDispatchQueueWrapper()
fileprivate var options: Options!

init(testName: String) {
Expand All @@ -18,11 +19,19 @@ class TestFileManagerTests: XCTestCase {
}

func getSut() throws -> TestFileManager {
return try TestFileManager(options: options)
return try TestFileManager(
options: options,
dateProvider: dateProvider,
dispatchQueueWrapper: dispatchQueueWrapper
)
}

func getActualFileManagerSut() throws -> SentryFileManager {
return try SentryFileManager(options: options)
return try SentryFileManager(
options: options,
dateProvider: dateProvider,
dispatchQueueWrapper: dispatchQueueWrapper
)
}
}

Expand Down Expand Up @@ -89,8 +98,8 @@ class TestFileManagerTests: XCTestCase {

// -- Assert --
// The paths are ending in a unique UUID, so we can only compare the prefix excluding the last 32 random characters and the `.json` extension
XCTAssertTrue(result1.hasPrefix(trimmedActualPath1) == true)
XCTAssertTrue(result2.hasPrefix(trimmedActualPath2) == true)
XCTAssertTrue(result1.hasPrefix(trimmedActualPath1) == true, "'\(result1)' does not have prefix '\(trimmedActualPath1)'")
XCTAssertTrue(result2.hasPrefix(trimmedActualPath2) == true, "'\(result2)' does not have prefix '\(trimmedActualPath2)'")
XCTAssertEqual(sut.storeEnvelopeInvocations.count, 2)
XCTAssertEqual(sut.storeEnvelopeInvocations.invocations.element(at: 0), envelope)
XCTAssertEqual(sut.storeEnvelopeInvocations.invocations.element(at: 1), envelope)
Expand Down
6 changes: 4 additions & 2 deletions Sources/Sentry/SentryClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,19 @@ @implementation SentryClient
- (_Nullable instancetype)initWithOptions:(SentryOptions *)options
{
return [self initWithOptions:options
dispatchQueue:[[SentryDispatchQueueWrapper alloc] init]
dateProvider:SentryDependencyContainer.sharedInstance.dateProvider
dispatchQueue:SentryDependencyContainer.sharedInstance.dispatchQueueWrapper
deleteOldEnvelopeItems:YES];
}

/** Internal constructor for testing purposes. */
- (nullable instancetype)initWithOptions:(SentryOptions *)options
dateProvider:(id<SentryCurrentDateProvider>)dateProvider
dispatchQueue:(SentryDispatchQueueWrapper *)dispatchQueue
deleteOldEnvelopeItems:(BOOL)deleteOldEnvelopeItems
{
NSError *error;
SentryFileManager *fileManager = [[SentryFileManager alloc] initWithOptions:options
dateProvider:dateProvider
dispatchQueueWrapper:dispatchQueue
error:&error];
if (error != nil) {
Expand Down
5 changes: 4 additions & 1 deletion Sources/Sentry/SentryDependencyContainer.m
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,10 @@ - (nullable SentryFileManager *)fileManager SENTRY_THREAD_SANITIZER_DOUBLE_CHECK
SENTRY_LAZY_INIT(_fileManager, ({
NSError *error;
SentryFileManager *manager =
[[SentryFileManager alloc] initWithOptions:SentrySDKInternal.options error:&error];
[[SentryFileManager alloc] initWithOptions:SentrySDKInternal.options
dateProvider:self.dateProvider
dispatchQueueWrapper:self.dispatchQueueWrapper
error:&error];
if (manager == nil) {
SENTRY_LOG_DEBUG(@"Could not create file manager - %@", error);
}
Expand Down
22 changes: 11 additions & 11 deletions Sources/Sentry/SentryFileManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@

@interface SentryFileManager ()

@property (nonatomic, strong) id<SentryCurrentDateProvider> dateProvider;
@property (nonatomic, strong) SentryDispatchQueueWrapper *dispatchQueue;

@property (nonatomic, copy) NSString *basePath;
@property (nonatomic, copy) NSString *sentryPath;
@property (nonatomic, copy) NSString *eventsPath;
Expand All @@ -116,19 +118,15 @@ @interface SentryFileManager ()

@implementation SentryFileManager

- (nullable instancetype)initWithOptions:(SentryOptions *)options error:(NSError **)error
{
return [self initWithOptions:options
dispatchQueueWrapper:SentryDependencyContainer.sharedInstance.dispatchQueueWrapper
error:error];
}

- (nullable instancetype)initWithOptions:(SentryOptions *)options
dateProvider:(id<SentryCurrentDateProvider>)dateProvider
dispatchQueueWrapper:(SentryDispatchQueueWrapper *)dispatchQueueWrapper
error:(NSError **)error
{
if (self = [super init]) {
self.dateProvider = dateProvider;
self.dispatchQueue = dispatchQueueWrapper;

[self createPathsWithOptions:options];

// Remove old cached events for versions before 6.0.0
Expand Down Expand Up @@ -160,9 +158,12 @@ - (void)createPathsWithOptions:(SentryOptions *)options
self.basePath = [cachePath stringByAppendingPathComponent:@"io.sentry"];

NSString *_Nullable nullableDsnHash = [options.parsedDsn getHash];
#if !SENTRY_TEST && !SENTRY_TEST_CI
if (nullableDsnHash == nil) {
SENTRY_LOG_FATAL(@"No DSN provided, using base path for envelopes: %@", self.basePath);
}
#endif // !SENTRY_TEST && !SENTRY_TEST_CI

// We decided against changing the `sentryPath` and use a null fallback instead, because this
// has been broken for a long time and the impact of changing the base path can result in
// critical issues.
Expand Down Expand Up @@ -1033,8 +1034,7 @@ - (void)deleteOldEnvelopesFromAllSentryPaths

- (void)deleteOldEnvelopesFromPath:(NSString *)envelopesPath
{
NSTimeInterval now =
[[SentryDependencyContainer.sharedInstance.dateProvider date] timeIntervalSince1970];
NSTimeInterval now = [[self.dateProvider date] timeIntervalSince1970];

for (NSString *path in [self allFilesInFolder:envelopesPath]) {
NSString *fullPath = [envelopesPath stringByAppendingPathComponent:path];
Expand Down Expand Up @@ -1110,8 +1110,8 @@ - (NSString *)uniqueAscendingJsonName
// %@ = NSString
// For example 978307200.000000-00001-3FE8C3AE-EB9C-4BEB-868C-14B8D47C33DD.json
return [NSString stringWithFormat:@"%f-%05lu-%@.json",
[[SentryDependencyContainer.sharedInstance.dateProvider date] timeIntervalSince1970],
(unsigned long)self.currentFileCounter++, [NSUUID UUID].UUIDString];
[[self.dateProvider date] timeIntervalSince1970], (unsigned long)self.currentFileCounter++,
[NSUUID UUID].UUIDString];
}

- (SentryFileContents *_Nullable)getFileContents:(NSString *)folderPath
Expand Down
4 changes: 2 additions & 2 deletions Sources/Sentry/include/SentryFileManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ NS_ASSUME_NONNULL_BEGIN
@class SentryOptions;
@class SentrySession;

@protocol SentryCurrentDateProvider;
@protocol SentryFileManagerDelegate <NSObject>

- (void)envelopeItemDeleted:(SentryEnvelopeItem *)envelopeItem
Expand All @@ -34,9 +35,8 @@ SENTRY_NO_INIT
@property (nonatomic, readonly) NSString *previousBreadcrumbsFilePathOne;
@property (nonatomic, readonly) NSString *previousBreadcrumbsFilePathTwo;

- (nullable instancetype)initWithOptions:(SentryOptions *)options error:(NSError **)error;

- (nullable instancetype)initWithOptions:(SentryOptions *)options
dateProvider:(id<SentryCurrentDateProvider>)dateProvider
dispatchQueueWrapper:(SentryDispatchQueueWrapper *)dispatchQueueWrapper
error:(NSError **)error NS_DESIGNATED_INITIALIZER;

Expand Down
8 changes: 6 additions & 2 deletions Tests/SentryTests/Helper/SentryAppStateManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ class SentryAppStateManagerTests: XCTestCase {
options = Options()
options.dsn = SentryAppStateManagerTests.dsnAsString
options.releaseName = TestData.appState.releaseName

fileManager = try! SentryFileManager(options: options, dispatchQueueWrapper: dispatchQueue)

fileManager = try! SentryFileManager(
options: options,
dateProvider: currentDate,
dispatchQueueWrapper: dispatchQueue
)
}

func getSut() -> SentryAppStateManager {
Expand Down
34 changes: 27 additions & 7 deletions Tests/SentryTests/Helper/SentryFileManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,22 @@ class SentryFileManagerTests: XCTestCase {
}

func getSut() -> SentryFileManager {
let sut = try! SentryFileManager(options: options, dispatchQueueWrapper: dispatchQueueWrapper)
let sut = try! SentryFileManager(
options: options,
dateProvider: currentDateProvider,
dispatchQueueWrapper: dispatchQueueWrapper
)
sut.setDelegate(delegate)
return sut
}

func getSut(maxCacheItems: UInt) -> SentryFileManager {
options.maxCacheItems = maxCacheItems
let sut = try! SentryFileManager(options: options, dispatchQueueWrapper: dispatchQueueWrapper)
let sut = try! SentryFileManager(
options: options,
dateProvider: currentDateProvider,
dispatchQueueWrapper: dispatchQueueWrapper
)
sut.setDelegate(delegate)
return sut
}
Expand Down Expand Up @@ -124,9 +132,17 @@ class SentryFileManagerTests: XCTestCase {
sut.storeCurrentSession(SentrySession(releaseName: "1.0.0", distinctId: "some-id"))
sut.storeTimestampLast(inForeground: Date())

_ = try! SentryFileManager(options: fixture.options, dispatchQueueWrapper: TestSentryDispatchQueueWrapper())
let fileManager = try! SentryFileManager(options: fixture.options, dispatchQueueWrapper: TestSentryDispatchQueueWrapper())

_ = try! SentryFileManager(
options: fixture.options,
dateProvider: fixture.currentDateProvider,
dispatchQueueWrapper: TestSentryDispatchQueueWrapper()
)
let fileManager = try! SentryFileManager(
options: fixture.options,
dateProvider: fixture.currentDateProvider,
dispatchQueueWrapper: TestSentryDispatchQueueWrapper()
)

XCTAssertEqual(1, fileManager.getAllEnvelopes().count)
XCTAssertNotNil(fileManager.readCurrentSession())
XCTAssertNotNil(fileManager.readTimestampLastInForeground())
Expand All @@ -135,8 +151,12 @@ class SentryFileManagerTests: XCTestCase {
func testInitDeletesEventsFolder() {
storeEvent()

_ = try! SentryFileManager(options: fixture.options, dispatchQueueWrapper: TestSentryDispatchQueueWrapper())

_ = try! SentryFileManager(
options: fixture.options,
dateProvider: fixture.currentDateProvider,
dispatchQueueWrapper: TestSentryDispatchQueueWrapper()
)

assertEventFolderDoesntExist()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
import Sentry
import SentryTestUtils
@testable import Sentry
@_spi(Private) import SentryTestUtils
import XCTest

class SentryAutoBreadcrumbTrackingIntegrationTests: XCTestCase {

private class Fixture {
private let dateProvider = TestCurrentDateProvider()
private let dispatchQueueWrapper = TestSentryDispatchQueueWrapper()

let fileManager: TestFileManager

let breadcrumbTracker = SentryTestBreadcrumbTracker()

#if os(iOS)
var systemEventBreadcrumbTracker: SentryTestSystemEventBreadcrumbs?
#endif // os(iOS)


init() throws {
let options = Options()
options.dsn = TestConstants.dsnForTestCase(type: SentryAutoBreadcrumbTrackingIntegrationTests.self)

fileManager = try TestFileManager(
options: options,
dateProvider: dateProvider,
dispatchQueueWrapper: dispatchQueueWrapper
)
}

var sut: SentryAutoBreadcrumbTrackingIntegration {
return SentryAutoBreadcrumbTrackingIntegration()
}
}

private var fixture: Fixture!
override func setUp() {
super.setUp()
fixture = Fixture()

override func setUpWithError() throws {
try super.setUpWithError()
fixture = try Fixture()
}

override func tearDown() {
Expand Down Expand Up @@ -92,7 +108,7 @@ class SentryAutoBreadcrumbTrackingIntegrationTests: XCTestCase {
private func install(sut: SentryAutoBreadcrumbTrackingIntegration, options: Options = Options()) throws {

#if os(iOS)
fixture.systemEventBreadcrumbTracker = SentryTestSystemEventBreadcrumbs(fileManager: try TestFileManager(options: options), andNotificationCenterWrapper: TestNSNotificationCenterWrapper())
fixture.systemEventBreadcrumbTracker = SentryTestSystemEventBreadcrumbs(fileManager: fixture.fileManager, andNotificationCenterWrapper: TestNSNotificationCenterWrapper())
sut.install(with: options, breadcrumbTracker: fixture.breadcrumbTracker, systemEventBreadcrumbs: fixture.systemEventBreadcrumbTracker!)
#else
sut.install(with: options, breadcrumbTracker: fixture.breadcrumbTracker)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@ class SentrySystemEventBreadcrumbsTest: XCTestCase {
var currentDateProvider = TestCurrentDateProvider()
let notificationCenterWrapper = TestNSNotificationCenterWrapper()

init() {
init() throws {
options = Options()
options.dsn = TestConstants.dsnAsString(username: "SentrySystemEventBreadcrumbsTest")
options.releaseName = "SentrySessionTrackerIntegrationTests"
options.sessionTrackingIntervalMillis = 10_000
options.environment = "debug"
SentryDependencyContainer.sharedInstance().dateProvider = currentDateProvider

fileManager = try! TestFileManager(options: options)
fileManager = try TestFileManager(
options: options,
dateProvider: currentDateProvider,
dispatchQueueWrapper: TestSentryDispatchQueueWrapper()
)
}

func getSut(currentDevice: UIDevice? = UIDevice.current) -> SentrySystemEventBreadcrumbs {
Expand Down Expand Up @@ -61,9 +65,9 @@ class SentrySystemEventBreadcrumbsTest: XCTestCase {
private var fixture: Fixture!
private var sut: SentrySystemEventBreadcrumbs!

override func setUp() {
super.setUp()
fixture = Fixture()
override func setUpWithError() throws {
try super.setUpWithError()
fixture = try Fixture()
}

override func tearDown() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,11 @@ class SentryFeedbackTests: XCTestCase {
let client = SentryClient(
options: options,
transportAdapter: transportAdapter,
fileManager: try XCTUnwrap(SentryFileManager(options: options, dispatchQueueWrapper: TestSentryDispatchQueueWrapper())),
fileManager: try XCTUnwrap(SentryFileManager(
options: options,
dateProvider: TestCurrentDateProvider(),
dispatchQueueWrapper: TestSentryDispatchQueueWrapper()
)),
deleteOldEnvelopeItems: false,
threadInspector: TestThreadInspector.instance,
debugImageProvider: TestDebugImageProvider(),
Expand Down Expand Up @@ -241,7 +245,11 @@ class SentryFeedbackTests: XCTestCase {
let client = SentryClient(
options: options,
transportAdapter: transportAdapter,
fileManager: try XCTUnwrap(SentryFileManager(options: options, dispatchQueueWrapper: TestSentryDispatchQueueWrapper())),
fileManager: try XCTUnwrap(SentryFileManager(
options: options,
dateProvider: TestCurrentDateProvider(),
dispatchQueueWrapper: TestSentryDispatchQueueWrapper()
)),
deleteOldEnvelopeItems: false,
threadInspector: TestThreadInspector.instance,
debugImageProvider: TestDebugImageProvider(),
Expand Down Expand Up @@ -280,7 +288,11 @@ class SentryFeedbackTests: XCTestCase {
let client = SentryClient(
options: options,
transportAdapter: transportAdapter,
fileManager: try XCTUnwrap(SentryFileManager(options: options, dispatchQueueWrapper: TestSentryDispatchQueueWrapper())),
fileManager: try XCTUnwrap(SentryFileManager(
options: options,
dateProvider: TestCurrentDateProvider(),
dispatchQueueWrapper: TestSentryDispatchQueueWrapper()
)),
deleteOldEnvelopeItems: false,
threadInspector: TestThreadInspector.instance,
debugImageProvider: TestDebugImageProvider(),
Expand Down
Loading
Loading