2015-04-08 01:28:38 +03:00
|
|
|
//
|
|
|
|
// 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
|
2015-12-20 02:44:51 +03:00
|
|
|
import RxTests
|
2015-05-17 21:39:15 +03:00
|
|
|
|
2015-06-08 01:09:49 +03:00
|
|
|
#if TRACE_RESOURCES
|
|
|
|
#elseif RELEASE
|
|
|
|
#else
|
2015-07-04 15:58:37 +03:00
|
|
|
//let a = unknown
|
2015-06-08 01:09:49 +03:00
|
|
|
#endif
|
|
|
|
|
2015-05-17 20:45:03 +03:00
|
|
|
// because otherwise OSX unit tests won't run
|
2015-05-17 21:39:15 +03:00
|
|
|
#if os(iOS)
|
|
|
|
import UIKit
|
|
|
|
#elseif os(OSX)
|
|
|
|
import AppKit
|
2015-05-17 20:45:03 +03:00
|
|
|
#endif
|
2015-04-08 01:28:38 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-04-08 01:28:38 +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-04-08 01:28:38 +03:00
|
|
|
}
|
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-04-08 01:28:38 +03:00
|
|
|
}
|
2015-12-06 03:13:47 +03:00
|
|
|
|
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
|
2015-04-08 01:28:38 +03:00
|
|
|
}
|