forked from ReactiveX/RxSwift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RxTest.swift
137 lines (108 loc) · 3.92 KB
/
RxTest.swift
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//
// RxTest.swift
// RxTests
//
// Created by Krunoslav Zaher on 2/8/15.
// Copyright (c) 2015 Krunoslav Zaher. All rights reserved.
//
import XCTest
import RxSwift
import CoreLocation
#if TRACE_RESOURCES
#elseif RELEASE
#else
//let a = unknown
#endif
// because otherwise OSX unit tests won't run
#if os(iOS)
import UIKit
#elseif os(OSX)
import AppKit
#endif
typealias Time = Int
func XCTAssertErrorEqual(lhs: ErrorType, _ rhs: ErrorType) {
XCTAssertTrue(lhs as NSError === rhs as NSError)
}
func XCTAssertEqual<T>(lhs: [T], _ rhs: [T], _ comparison: (T, T) -> Bool) {
XCTAssertEqual(lhs.count, rhs.count)
XCTAssertTrue(zip(lhs, rhs).reduce(true) { (a: Bool, z: (T, T)) in a && comparison(z.0, z.1) })
}
let testError = NSError(domain: "dummyError", code: -232, userInfo: nil)
let testError1 = NSError(domain: "dummyError1", code: -233, userInfo: nil)
let testError2 = NSError(domain: "dummyError2", code: -234, userInfo: nil)
func next<T>(value: T) -> Recorded<T> {
return Recorded(time: 0, event: .Next(value))
}
func completed<T>() -> Recorded<T> {
return Recorded(time: 0, event: .Completed)
}
func error<T>(error: NSError) -> Recorded<T> {
return Recorded(time: 0, event: .Error(error))
}
func next<T>(time: Time, _ value: T) -> Recorded<T> {
return Recorded(time: time, event: .Next(value))
}
func completed<T>(time: Time) -> Recorded<T> {
return Recorded(time: time, event: .Completed)
}
func error<T>(time: Time, _ error: ErrorType) -> Recorded<T> {
return Recorded(time: time, event: .Error(error))
}
class RxTest: XCTestCase {
struct Defaults {
static let created = 100
static let subscribed = 200
static let disposed = 1000
}
func sleep(time: NSTimeInterval) {
NSRunLoop.currentRunLoop().runMode(NSDefaultRunLoopMode, beforeDate: NSDate(timeIntervalSinceNow: time))
}
private var startResourceCount: Int32 = 0
var accumulateStatistics: Bool {
get {
return true
}
}
#if TRACE_RESOURCES
static var totalNumberOfAllocations: Int64 = 0
static var totalNumberOfAllocatedBytes: Int64 = 0
var startNumberOfAllocations: Int64 = 0
var startNumberOfAllocatedBytes: Int64 = 0
#endif
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
#if TRACE_RESOURCES
self.startResourceCount = resourceCount
registerMallocHooks()
(startNumberOfAllocatedBytes, startNumberOfAllocations) = getMemoryInfo()
#endif
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
#if TRACE_RESOURCES
// give 5 sec to clean up resources
for var i = 0; i < 10; ++i {
if self.startResourceCount < resourceCount {
// main schedulers need to finish work
NSRunLoop.currentRunLoop().runMode(NSDefaultRunLoopMode, beforeDate: NSDate(timeIntervalSinceNow: 0.05))
}
else {
break
}
}
XCTAssertEqual(self.startResourceCount, resourceCount)
let (endNumberOfAllocatedBytes, endNumberOfAllocations) = getMemoryInfo()
let (newBytes, newAllocations) = (endNumberOfAllocatedBytes - startNumberOfAllocatedBytes, endNumberOfAllocations - startNumberOfAllocations)
if accumulateStatistics {
RxTest.totalNumberOfAllocations += newAllocations
RxTest.totalNumberOfAllocatedBytes += newBytes
}
print("allocatedBytes = \(newBytes), allocations = \(newAllocations) (totalBytes = \(RxTest.totalNumberOfAllocatedBytes), totalAllocations = \(RxTest.totalNumberOfAllocations))")
#endif
}
func on<T>(time: Time, _ event: Event<T>) -> Recorded<T> {
return Recorded(time: time, event: event)
}
}