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 memory leak when using async expectations. Closes #405 #449

Merged
merged 1 commit into from
Jul 18, 2017
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
6 changes: 5 additions & 1 deletion Sources/Nimble/Utils/Async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ internal class AwaitPromise<T> {
signal = DispatchSemaphore(value: 1)
}

deinit {
signal.signal()
}

/// Resolves the promise with the given result if it has not been resolved. Repeated calls to
/// this method will resolve in a no-op.
///
Expand Down Expand Up @@ -252,7 +256,7 @@ internal class AwaitPromiseBuilder<T> {
// Stopping the run loop does not work unless we run only 1 mode
_ = RunLoop.current.run(mode: .defaultRunLoopMode, before: .distantFuture)
}
self.trigger.timeoutSource.suspend()

self.trigger.timeoutSource.cancel()
if let asyncSource = self.trigger.actionSource {
asyncSource.cancel()
Expand Down
25 changes: 25 additions & 0 deletions Tests/NimbleTests/AsynchronousTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ final class AsyncTest: XCTestCase, XCTestCaseProvider {
("testWaitUntilErrorsIfDoneIsCalledMultipleTimes", testWaitUntilErrorsIfDoneIsCalledMultipleTimes),
("testWaitUntilMustBeInMainThread", testWaitUntilMustBeInMainThread),
("testToEventuallyMustBeInMainThread", testToEventuallyMustBeInMainThread),
("testSubjectUnderTestIsReleasedFromMemory", testSubjectUnderTestIsReleasedFromMemory),
]
}

Expand Down Expand Up @@ -217,4 +218,28 @@ final class AsyncTest: XCTestCase, XCTestCaseProvider {
expect(executedAsyncBlock).toEventually(beTruthy())
#endif
}

func testSubjectUnderTestIsReleasedFromMemory() {
final class ClassUnderTest {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nesting Violation: Types should be nested at most 1 level deep (nesting)

var deinitCalled: (() -> Void)?
var count = 0
deinit { deinitCalled?() }
}

var subject: ClassUnderTest? = ClassUnderTest()

if let sub = subject {
expect(sub.count).toEventually(equal(0), timeout: 0.1)
expect(sub.count).toEventuallyNot(equal(1), timeout: 0.1)
}

waitUntil(timeout: 0.5) { done in
subject?.deinitCalled = {
done()
}

deferToMainQueue { subject = nil }
}
}

}