-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathRemoveHandlerTests.swift
More file actions
106 lines (74 loc) · 3.05 KB
/
RemoveHandlerTests.swift
File metadata and controls
106 lines (74 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//
// RemoveHandlerTests.swift
// SwiftTask
//
// Created by Yasuhiro Inami on 2015/05/28.
// Copyright (c) 2015年 Yasuhiro Inami. All rights reserved.
//
import SwiftTask
import Async
import XCTest
class RemoveHandlerTests: _TestCase
{
func testRemoveProgress()
{
typealias Task = SwiftTask.Task<Float, String, ErrorString>
let expect = self.expectation(description: #function)
var latestProgressValue: Float?
var canceller: AutoCanceller? = nil
// define task
Task { progress, fulfill, reject, configure in
progress(0.0)
Async.main(after: 0.1) {
progress(1.0)
fulfill("OK")
}
}.progress { oldProgress, newProgress in
print("progress1 = \(newProgress)")
latestProgressValue = newProgress
}.progress(&canceller) { oldProgress, newProgress in
print("progress2 = \(newProgress)")
XCTFail("Should never reach here because this progress-handler will be removed soon.")
}.then { value, errorInfo -> Void in
XCTAssertTrue(value == "OK")
XCTAssertTrue(errorInfo == nil)
expect.fulfill()
}
XCTAssertTrue(canceller != nil, "Async `task` will return non-nil `progressToken`.")
// remove progress-handler
canceller = nil
self.wait()
XCTAssertTrue(latestProgressValue == 1.0)
}
func testRemoveThen()
{
typealias Task = SwiftTask.Task<Float, String, ErrorString>
let expect = self.expectation(description: #function)
var canceller: AutoCanceller? = nil
// define task
Task { progress, fulfill, reject, configure in
Async.main(after: 0.1) {
fulfill("OK")
}
return
}.success { value -> String in
XCTAssertEqual(value, "OK")
return "Now OK"
}.then(&canceller) { value, errorInfo -> String in
print("Should never reach here")
XCTFail("Should never reach here because this then-handler will be removed soon.")
return "Never reaches here"
}.then { value, errorInfo -> Void in
print("value = \(value ?? "")")
print("errorInfo = \(errorInfo ?? "")")
XCTAssertTrue(value == nil)
XCTAssertTrue(errorInfo != nil)
XCTAssertTrue(errorInfo!.error == nil)
XCTAssertTrue(errorInfo!.isCancelled, "Deallocation of `canceller` will force `task2` (where `task2 = task.then(&canceller)`) to deinit immediately and tries cancellation if it is still running.")
expect.fulfill()
}
// remove then-handler
canceller = nil
self.wait()
}
}