Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ARC issue for FileManager #2525

Merged
merged 2 commits into from
Dec 14, 2022
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ This version adds a dependency on Swift.
- Don't capture zero size screenshots ([#2459](https://github.com/getsentry/sentry-cocoa/pull/2459))
- Use the preexisting app release version format for profiles (#2470)
- Don't add out of date context for crashes (#2523)
- Fix ARC issue for FileManager (#2525)

### Breaking Changes

Expand Down
8 changes: 6 additions & 2 deletions Sources/Sentry/SentryFileManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,15 @@ - (nullable instancetype)initWithOptions:(SentryOptions *)options
self.currentFileCounter = 0;
self.maxEnvelopes = options.maxCacheItems;

__weak SentryFileManager *weakSelf = self;
[dispatchQueueWrapper
dispatchAfter:10
block:^{
SENTRY_LOG_DEBUG(@"Dispatched deletion of old envelopes from %@", self);
[self deleteOldEnvelopesFromAllSentryPaths];
if (weakSelf == nil) {
return;
}
SENTRY_LOG_DEBUG(@"Dispatched deletion of old envelopes from %@", weakSelf);
[weakSelf deleteOldEnvelopesFromAllSentryPaths];
}];
}
return self;
Expand Down
36 changes: 28 additions & 8 deletions Tests/SentryTests/Helper/SentryFileManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,7 @@ class SentryFileManagerTests: XCTestCase {
func testDeleteOldEnvelopes() throws {
fixture.dispatchQueueWrapper.dispatchAfterExecutesBlock = true

let envelope = TestConstants.envelope
let path = sut.store(envelope)

let timeIntervalSince1970 = fixture.currentDateProvider.date().timeIntervalSince1970 - (90 * 24 * 60 * 60)
let date = Date(timeIntervalSince1970: timeIntervalSince1970 - 1)
try FileManager.default.setAttributes([FileAttributeKey.creationDate: date], ofItemAtPath: path)

XCTAssertEqual(sut.getAllEnvelopes().count, 1)
try givenOldEnvelopes()

sut = fixture.getSut()

Expand Down Expand Up @@ -181,6 +174,22 @@ class SentryFileManagerTests: XCTestCase {

XCTAssertEqual(sut.getAllEnvelopes().count, 1)
}

func testFileManagerDeallocated_OldEnvelopesNotDeleted() throws {
try givenOldEnvelopes()

fixture.dispatchQueueWrapper.dispatchAfterExecutesBlock = false

// Initialize sut in extra function so ARC deallocates it
func getSut() {
_ = fixture.getSut()
}
getSut()

fixture.dispatchQueueWrapper.invokeLastDispatchAfter()

XCTAssertEqual(sut.getAllEnvelopes().count, 1)
}

func testCreateDirDoesNotThrow() throws {
try SentryFileManager.createDirectory(atPath: "a")
Expand Down Expand Up @@ -634,6 +643,17 @@ class SentryFileManagerTests: XCTestCase {
XCTFail("Failed to store garbage in Envelopes folder.")
}
}

private func givenOldEnvelopes() throws {
let envelope = TestConstants.envelope
let path = sut.store(envelope)

let timeIntervalSince1970 = fixture.currentDateProvider.date().timeIntervalSince1970 - (90 * 24 * 60 * 60)
let date = Date(timeIntervalSince1970: timeIntervalSince1970 - 1)
try FileManager.default.setAttributes([FileAttributeKey.creationDate: date], ofItemAtPath: path)

XCTAssertEqual(sut.getAllEnvelopes().count, 1)
}

private func storeEvent() {
do {
Expand Down