RxSwift/Tests/RxTest.swift

108 lines
2.9 KiB
Swift
Raw Normal View History

//
// RxTest.swift
// RxTests
//
// Created by Krunoslav Zaher on 2/8/15.
// Copyright (c) 2015 Krunoslav Zaher. All rights reserved.
//
import XCTest
2015-04-10 02:52:51 +03:00
import RxSwift
import RxTests
#if TRACE_RESOURCES
#elseif RELEASE
#else
//let a = unknown
#endif
2015-05-17 20:45:03 +03:00
// because otherwise OSX unit tests won't run
#if os(iOS)
import UIKit
#elseif os(OSX)
import AppKit
2015-05-17 20:45:03 +03:00
#endif
class RxTest: XCTestCase {
struct Defaults {
static let created = 100
static let subscribed = 200
static let disposed = 1000
}
2015-07-24 18:17:28 +03:00
func sleep(time: NSTimeInterval) {
2015-10-16 00:11:09 +03:00
NSRunLoop.currentRunLoop().runMode(NSDefaultRunLoopMode, beforeDate: NSDate(timeIntervalSinceNow: time))
2015-07-24 18:17:28 +03:00
}
private var startResourceCount: Int32 = 0
2015-09-27 03:25:41 +03:00
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
2015-12-25 23:48:18 +03:00
func setUpActions(){
#if TRACE_RESOURCES
self.startResourceCount = resourceCount
registerMallocHooks()
(startNumberOfAllocatedBytes, startNumberOfAllocations) = getMemoryInfo()
#endif
}
2015-10-16 00:11:09 +03:00
2015-12-25 23:48:18 +03:00
#if os(Linux)
func setUp() {
setUpActions()
}
#else
override func setUp() {
setUpActions()
2015-10-16 00:11:09 +03:00
}
2015-12-25 23:48:18 +03:00
#endif
func tearDownActions() {
#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
}
}
2015-10-16 00:11:09 +03:00
2015-12-25 23:48:18 +03:00
XCTAssertEqual(self.startResourceCount, resourceCount)
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-12-25 23:48:18 +03:00
#if os(Linux)
func tearDown() {
tearDownActions()
}
#else
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
tearDownActions()
}
#endif
}