RxSwift/Tests/RxTest.swift

128 lines
3.3 KiB
Swift
Raw Normal View History

//
// RxTest.swift
2016-10-15 15:38:22 +03:00
// Tests
//
// Created by Krunoslav Zaher on 2/8/15.
2015-12-29 18:56:21 +03:00
// Copyright © 2015 Krunoslav Zaher. All rights reserved.
//
import XCTest
2015-04-10 02:52:51 +03:00
import RxSwift
import RxTest
import struct Foundation.TimeInterval
import struct Foundation.Date
import class Foundation.RunLoop
#if os(Linux)
import Foundation
#endif
#if TRACE_RESOURCES
#elseif RELEASE
2016-10-20 01:03:41 +03:00
#elseif os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#elseif os(Linux)
#else
let failure = unhandled_case()
#endif
2016-10-20 01:03:41 +03:00
// because otherwise macOS unit tests won't run
#if os(iOS)
import UIKit
2016-10-20 01:03:41 +03:00
#elseif os(macOS)
import AppKit
2015-05-17 20:45:03 +03:00
#endif
#if os(Linux)
// TODO: Implement PerformanceTests.swift for Linux
func getMemoryInfo() -> (bytes: Int64, allocations: Int64) {
return (0, 0)
}
#endif
2017-01-06 13:39:18 +03:00
class RxTest
: XCTestCase {
2016-10-15 18:17:38 +03:00
#if TRACE_RESOURCES
fileprivate var startResourceCount: Int32 = 0
2016-10-15 18:17:38 +03:00
#endif
2015-09-27 03:25:41 +03:00
var accumulateStatistics: Bool {
2016-03-07 20:31:00 +03:00
return true
2015-09-27 03:25:41 +03:00
}
#if TRACE_RESOURCES
static var totalNumberOfAllocations: Int64 = 0
static var totalNumberOfAllocatedBytes: Int64 = 0
2015-09-27 03:25:41 +03:00
var startNumberOfAllocations: Int64 = 0
var startNumberOfAllocatedBytes: Int64 = 0
#endif
2015-10-16 00:11:09 +03:00
override func setUp() {
super.setUp()
setUpActions()
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
tearDownActions()
}
}
extension RxTest {
struct Defaults {
static let created = 100
static let subscribed = 200
static let disposed = 1000
}
func sleep(_ time: TimeInterval) {
2017-07-17 01:39:40 +03:00
Thread.sleep(forTimeInterval: time)
}
func setUpActions(){
_ = Hooks.defaultErrorHandler // lazy load resource so resource count matches
_ = Hooks.customCaptureSubscriptionCallstack // lazy load resource so resource count matches
#if TRACE_RESOURCES
self.startResourceCount = Resources.total
2016-08-04 12:12:53 +03:00
//registerMallocHooks()
(startNumberOfAllocatedBytes, startNumberOfAllocations) = getMemoryInfo()
#endif
}
2015-12-25 23:48:18 +03:00
func tearDownActions() {
#if TRACE_RESOURCES
// give 5 sec to clean up resources
for _ in 0..<30 {
if self.startResourceCount < Resources.total {
2015-12-25 23:48:18 +03:00
// main schedulers need to finish work
print("Waiting for resource cleanup ...")
2019-04-06 22:16:49 +03:00
let mode = RunLoop.Mode.default
2018-09-19 10:17:55 +03:00
RunLoop.current.run(mode: mode, before: Date(timeIntervalSinceNow: 0.05))
2015-12-25 23:48:18 +03:00
}
else {
break
}
}
2015-10-16 00:11:09 +03:00
XCTAssertEqual(self.startResourceCount, Resources.total)
2015-12-25 23:48:18 +03:00
let (endNumberOfAllocatedBytes, endNumberOfAllocations) = getMemoryInfo()
2015-09-27 03:25:41 +03:00
2015-12-25 23:48:18 +03:00
let (newBytes, newAllocations) = (endNumberOfAllocatedBytes - startNumberOfAllocatedBytes, endNumberOfAllocations - startNumberOfAllocations)
2015-09-27 03:25:41 +03:00
2015-12-25 23:48:18 +03:00
if accumulateStatistics {
RxTest.totalNumberOfAllocations += newAllocations
RxTest.totalNumberOfAllocatedBytes += newBytes
}
print("allocatedBytes = \(newBytes), allocations = \(newAllocations) (totalBytes = \(RxTest.totalNumberOfAllocatedBytes), totalAllocations = \(RxTest.totalNumberOfAllocations))")
#endif
}
}