2015-04-08 01:28:38 +03:00
|
|
|
//
|
|
|
|
// RxTest.swift
|
2016-10-15 15:38:22 +03:00
|
|
|
// Tests
|
2015-04-08 01:28:38 +03:00
|
|
|
//
|
|
|
|
// Created by Krunoslav Zaher on 2/8/15.
|
2015-12-29 18:56:21 +03:00
|
|
|
// Copyright © 2015 Krunoslav Zaher. All rights reserved.
|
2015-04-08 01:28:38 +03:00
|
|
|
//
|
|
|
|
|
|
|
|
import XCTest
|
2015-04-10 02:52:51 +03:00
|
|
|
import RxSwift
|
2016-10-09 23:06:36 +03:00
|
|
|
import RxTest
|
2017-01-29 03:11:43 +03:00
|
|
|
|
|
|
|
import struct Foundation.TimeInterval
|
|
|
|
import struct Foundation.Date
|
|
|
|
|
|
|
|
import class Foundation.RunLoop
|
|
|
|
|
|
|
|
#if os(Linux)
|
|
|
|
import Foundation
|
|
|
|
#endif
|
2015-05-17 21:39:15 +03:00
|
|
|
|
2015-06-08 01:09:49 +03:00
|
|
|
#if TRACE_RESOURCES
|
|
|
|
#elseif RELEASE
|
2016-10-20 01:03:41 +03:00
|
|
|
#elseif os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
|
2015-12-27 03:12:36 +03:00
|
|
|
#elseif os(Linux)
|
2015-06-08 01:09:49 +03:00
|
|
|
#else
|
2015-12-27 03:12:36 +03:00
|
|
|
let failure = unhandled_case()
|
2015-06-08 01:09:49 +03:00
|
|
|
#endif
|
|
|
|
|
2016-10-20 01:03:41 +03:00
|
|
|
// because otherwise macOS unit tests won't run
|
2015-05-17 21:39:15 +03:00
|
|
|
#if os(iOS)
|
|
|
|
import UIKit
|
2016-10-20 01:03:41 +03:00
|
|
|
#elseif os(macOS)
|
2015-05-17 21:39:15 +03:00
|
|
|
import AppKit
|
2015-05-17 20:45:03 +03:00
|
|
|
#endif
|
2015-12-26 06:58:58 +03:00
|
|
|
|
2015-12-27 03:12:36 +03:00
|
|
|
|
2016-09-21 11:22:31 +03:00
|
|
|
|
|
|
|
#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
|
2015-12-27 03:12:36 +03:00
|
|
|
: XCTestCase {
|
|
|
|
|
2016-10-15 18:17:38 +03:00
|
|
|
#if TRACE_RESOURCES
|
2016-10-15 23:10:40 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-12-27 03:12:36 +03:00
|
|
|
#if TRACE_RESOURCES
|
|
|
|
static var totalNumberOfAllocations: Int64 = 0
|
|
|
|
static var totalNumberOfAllocatedBytes: Int64 = 0
|
2015-09-27 03:25:41 +03:00
|
|
|
|
2015-12-27 03:12:36 +03:00
|
|
|
var startNumberOfAllocations: Int64 = 0
|
|
|
|
var startNumberOfAllocatedBytes: Int64 = 0
|
|
|
|
#endif
|
2015-10-16 00:11:09 +03:00
|
|
|
|
2016-09-21 11:22:31 +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()
|
|
|
|
}
|
2015-12-27 03:12:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
extension RxTest {
|
|
|
|
struct Defaults {
|
|
|
|
static let created = 100
|
|
|
|
static let subscribed = 200
|
|
|
|
static let disposed = 1000
|
|
|
|
}
|
|
|
|
|
2016-06-18 09:19:16 +03:00
|
|
|
func sleep(_ time: TimeInterval) {
|
2017-07-17 01:39:40 +03:00
|
|
|
Thread.sleep(forTimeInterval: time)
|
2015-12-27 03:12:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func setUpActions(){
|
2017-10-15 21:27:33 +03:00
|
|
|
_ = Hooks.defaultErrorHandler // lazy load resource so resource count matches
|
2018-07-20 23:14:37 +03:00
|
|
|
_ = Hooks.customCaptureSubscriptionCallstack // lazy load resource so resource count matches
|
2015-12-27 03:12:36 +03:00
|
|
|
#if TRACE_RESOURCES
|
2016-10-15 23:10:40 +03:00
|
|
|
self.startResourceCount = Resources.total
|
2016-08-04 12:12:53 +03:00
|
|
|
//registerMallocHooks()
|
2015-12-27 03:12:36 +03:00
|
|
|
(startNumberOfAllocatedBytes, startNumberOfAllocations) = getMemoryInfo()
|
|
|
|
#endif
|
|
|
|
}
|
2015-12-25 23:48:18 +03:00
|
|
|
|
|
|
|
func tearDownActions() {
|
|
|
|
#if TRACE_RESOURCES
|
|
|
|
// give 5 sec to clean up resources
|
2016-03-17 12:40:24 +03:00
|
|
|
for _ in 0..<30 {
|
2016-10-15 23:10:40 +03:00
|
|
|
if self.startResourceCount < Resources.total {
|
2015-12-25 23:48:18 +03:00
|
|
|
// main schedulers need to finish work
|
2016-03-17 12:40:24 +03:00
|
|
|
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
|
|
|
|
2016-10-15 23:10:40 +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
|
2015-04-08 01:28:38 +03:00
|
|
|
}
|
|
|
|
}
|