Skip to content

Fix retain-cycle bug. #5

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

Merged
merged 2 commits into from
Sep 11, 2014
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
8 changes: 6 additions & 2 deletions SwiftTask.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
1F6A8CA319A4E4F200369A5D /* SwiftTaskTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F46DEE3199EDF1000F97868 /* SwiftTaskTests.swift */; };
1F6A8CA619A5090E00369A5D /* AlamofireTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F5FA35619A374E600975FB9 /* AlamofireTests.swift */; };
1FA4634919A8D73300DD8729 /* Alamofire.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FA4631719A8D70A00DD8729 /* Alamofire.swift */; };
48511C5B19C17563002FE03C /* RetainCycleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48511C5A19C17563002FE03C /* RetainCycleTests.swift */; };
48797D6619B42CEF0085D80F /* SwiftState.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1FA4634319A8D70A00DD8729 /* SwiftState.framework */; };
48797D6719B42CF30085D80F /* SwiftState.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1FA4634319A8D70A00DD8729 /* SwiftState.framework */; };
48CD5A3C19AEEBDF0042B9F1 /* SwiftTask.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F46DEFA199EDF8100F97868 /* SwiftTask.swift */; };
Expand Down Expand Up @@ -60,6 +61,7 @@
1F5FA35619A374E600975FB9 /* AlamofireTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AlamofireTests.swift; sourceTree = "<group>"; };
1FA4631719A8D70A00DD8729 /* Alamofire.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Alamofire.swift; sourceTree = "<group>"; };
1FA4633019A8D70A00DD8729 /* SwiftState.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; path = SwiftState.xcodeproj; sourceTree = "<group>"; };
48511C5A19C17563002FE03C /* RetainCycleTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RetainCycleTests.swift; sourceTree = "<group>"; };
48CD5A0C19AEE3570042B9F1 /* SwiftTask.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SwiftTask.framework; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -136,6 +138,7 @@
1F20250119ADA8FD00DE0495 /* BasicTests.swift */,
1F46DEE3199EDF1000F97868 /* SwiftTaskTests.swift */,
1F2024FE19AD97A700DE0495 /* CustomOperatorTests.swift */,
48511C5A19C17563002FE03C /* RetainCycleTests.swift */,
1F5FA35619A374E600975FB9 /* AlamofireTests.swift */,
1F46DEE1199EDF1000F97868 /* Supporting Files */,
);
Expand Down Expand Up @@ -188,7 +191,7 @@
children = (
1FA4634319A8D70A00DD8729 /* SwiftState.framework */,
1FA4634519A8D70A00DD8729 /* SwiftStateTests.xctest */,
4872D5C819B423D600F326B5 /* SwiftState-iOS.framework */,
4872D5C819B423D600F326B5 /* SwiftState.framework */,
);
name = Products;
sourceTree = "<group>";
Expand Down Expand Up @@ -328,7 +331,7 @@
remoteRef = 1FA4634419A8D70A00DD8729 /* PBXContainerItemProxy */;
sourceTree = BUILT_PRODUCTS_DIR;
};
4872D5C819B423D600F326B5 /* SwiftState-iOS.framework */ = {
4872D5C819B423D600F326B5 /* SwiftState.framework */ = {
isa = PBXReferenceProxy;
fileType = wrapper.framework;
path = SwiftState.framework;
Expand Down Expand Up @@ -379,6 +382,7 @@
1F20250219ADA8FD00DE0495 /* BasicTests.swift in Sources */,
1F6A8CA619A5090E00369A5D /* AlamofireTests.swift in Sources */,
1F6A8CA319A4E4F200369A5D /* SwiftTaskTests.swift in Sources */,
48511C5B19C17563002FE03C /* RetainCycleTests.swift in Sources */,
1F46DEFD199EE2C200F97868 /* _TestCase.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
14 changes: 14 additions & 0 deletions SwiftTask/SwiftTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ public class TaskConfiguration
public var pause: (Void -> Void)?
public var resume: (Void -> Void)?
public var cancel: (Void -> Void)?

// deinit
// {
// println("deinit: TaskConfiguration")
// }

internal func clear()
{
self.pause = nil
self.resume = nil
self.cancel = nil
}
}

public class Task<Progress, Value, Error>
Expand Down Expand Up @@ -168,12 +180,14 @@ public class Task<Progress, Value, Error>
if let value = context.userInfo as? Value {
self!.value = value
}
configuration.clear()
}
self.machine.addEventHandler(.Reject, order: 90) { [weak self] context in
if let errorInfo = context.userInfo as? ErrorInfo {
self!.errorInfo = errorInfo
configuration.cancel?() // NOTE: call configured cancellation on reject as well
}
configuration.clear()
}

let progressHandler: ProgressHandler = { /*[weak self]*/ (progress: Progress) in
Expand Down
233 changes: 233 additions & 0 deletions SwiftTaskTests/RetainCycleTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
//
// RetainCycleTests.swift
// SwiftTask
//
// Created by Yasuhiro Inami on 2014/09/11.
// Copyright (c) 2014年 Yasuhiro Inami. All rights reserved.
//

import SwiftTask
import XCTest

class Player
{
var completionHandler: (Void -> Void)?

deinit
{
println("deinit: Player")
}

func doSomething(completion: (Void -> Void)? = nil)
{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 100_000_000), dispatch_get_main_queue()) { /*[weak self] in */

// NOTE: callback (either as argument or stored property) must be captured by dispatch_queue

if let completion = completion {
completion() // NOTE: interestingly, self is also captured by just calling completion() if [weak self] is not set
}
else {
self.completionHandler?()
}
}
}

func cancel()
{
// no operation, just for capturing test
}
}

class RetainCycleTests: _TestCase
{
typealias Task = SwiftTask.Task<Float, String, ErrorString>

// weak properties for inspection
weak var task: Task?
weak var player: Player?

func _testPlayer_completionAsArgument_notConfigured()
{
var expect = self.expectationWithDescription(__FUNCTION__)

//
// retain cycle:
// ("x->" = will be released shortly)
//
// 1. dispatch_queue x-> task
// dispatch_queue (via player impl) x-> completion -> fulfill -> task
//
// 2. dispatch_queue x-> player
// dispatch_queue (via player impl) x-> player (via completion capturing)
//
self.task = Task { (progress, fulfill, reject, configure) in

let player = Player()
self.player = player

// comment-out: no configuration test
// configure.cancel = { player.cancel() }

player.doSomething {
fulfill("OK")
}

}

XCTAssertNotNil(self.task, "self.task (weak) should NOT be nil because of retain cycle: task <- dispatch_queue.")
XCTAssertNotNil(self.player, "self.player (weak) should NOT nil because player is not retained by dispatch_queue.")

println("then")

self.task!.then { (value: String) -> Void in

XCTAssertEqual(value, "OK")
expect.fulfill()

}

self.wait {
XCTAssertNil(self.task)
XCTAssertNil(self.player)
}
}

func _testPlayer_completionAsArgument_configured()
{
var expect = self.expectationWithDescription(__FUNCTION__)

//
// retain cycle:
// ("x->" = will be released shortly)
//
// 1. dispatch_queue x-> task
// dispatch_queue (via player impl) x-> completion -> fulfill -> task
//
// 2. dispatch_queue x-> player
// dispatch_queue (via player impl) x-> player (via completion capturing)
//
// 3. task -> player
// task -> task.machine -> configure (via pause/resume addEventHandler) -> configure.cancel -> player
//
self.task = Task { (progress, fulfill, reject, configure) in

let player = Player()
self.player = player

configure.cancel = { player.cancel() }

player.doSomething {
fulfill("OK")
}

}

XCTAssertNotNil(self.task, "self.task (weak) should NOT be nil because of retain cycle: task <- dispatch_queue.")
XCTAssertNotNil(self.player, "self.player (weak) should NOT be nil because of retain cycle: player <- configure <- task.")

println("then")

self.task!.then { (value: String) -> Void in

XCTAssertEqual(value, "OK")
expect.fulfill()

}

self.wait {
XCTAssertNil(self.task)
XCTAssertNil(self.player)
}
}

func _testPlayer_completionAsStoredProperty_notConfigured()
{
var expect = self.expectationWithDescription(__FUNCTION__)

//
// retain cycle:
// ("x->" = will be released shortly)
//
// 1. dispatch_queue x-> player -> task
// dispatch_queue (via player impl) x-> player -> player.completionHandler -> fulfill -> task
//
self.task = Task { (progress, fulfill, reject, configure) in

let player = Player()
self.player = player

// comment-out: no configuration test
// configure.cancel = { player.cancel() }

player.completionHandler = {
fulfill("OK")
}
player.doSomething()

}

XCTAssertNotNil(self.task, "self.task (weak) should not be nil because of retain cycle: task <- player <- dispatch_queue.")
XCTAssertNotNil(self.player, "self.player (weak) should not be nil because of retain cycle: player <- configure <- task.")

println("then")

self.task!.then { (value: String) -> Void in

XCTAssertEqual(value, "OK")
expect.fulfill()

}

self.wait {
XCTAssertNil(self.task)
XCTAssertNil(self.player)
}
}

func testPlayer_completionAsStoredProperty_configured()
{
var expect = self.expectationWithDescription(__FUNCTION__)

//
// retain cycle:
// ("x->" = will be released shortly)
//
// 1. dispatch_queue x-> player -> task
// dispatch_queue (via player impl) -> player -> player.completionHandler -> fulfill -> task
//
// 2. task -> player
// task -> task.machine -> configure (via pause/resume addEventHandler) -> configure.pause/resume/cancel -> player
//
self.task = Task { (progress, fulfill, reject, configure) in

let player = Player()
self.player = player

configure.cancel = { player.cancel() }

player.completionHandler = {
fulfill("OK")
}
player.doSomething()

}

XCTAssertNotNil(self.task, "self.task (weak) should not be nil because of retain cycle: task <- player <- dispatch_queue.")
XCTAssertNotNil(self.player, "self.player (weak) should not be nil because of retain cycle: player <- configure <- task.")

println("then")

self.task!.then { (value: String) -> Void in

XCTAssertEqual(value, "OK")
expect.fulfill()

}

self.wait {
XCTAssertNil(self.task)
XCTAssertNil(self.player)
}
}
}
11 changes: 9 additions & 2 deletions SwiftTaskTests/_TestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,16 @@ class _TestCase: XCTestCase
super.tearDown()
}

func wait()
func wait(handler: (Void -> Void)? = nil)
{
self.waitForExpectationsWithTimeout(3) { error in println("wait error = \(error)") }
self.waitForExpectationsWithTimeout(3) { error in

println("wait error = \(error)")

if let handler = handler {
handler()
}
}
}

var isAsync: Bool { return false }
Expand Down