<commit_before>//
//  SchedulerSpec.swift
//  ReactiveCocoa
//
//  Created by <NAME> on 2014-07-13.
//  Copyright (c) 2014 GitHub. All rights reserved.
//

import Foundation
import Nimble
import Quick
import ReactiveCocoa

class SchedulerSpec: QuickSpec {
	override func spec() {
		describe("ImmediateScheduler") {
			it("should run enqueued actions immediately") {
				var didRun = false
				ImmediateScheduler().schedule {
					didRun = true
				}

				expect(didRun).to(beTruthy())
			}
		}

		describe("MainScheduler") {
			it("should run enqueued actions on the main thread") {
				var didRun = false
				MainScheduler().schedule {
					didRun = true
					expect(NSThread.isMainThread()).to(beTruthy())
				}

				expect(didRun).to(beFalsy())
				expect{didRun}.toEventually(beTruthy())
			}<commit_msg>Port -deliverOnMainThread tests to Swift<commit_after>//
//  SchedulerSpec.swift
//  ReactiveCocoa
//
//  Created by <NAME> on 2014-07-13.
//  Copyright (c) 2014 GitHub. All rights reserved.
//

import Foundation
import Nimble
import Quick
import ReactiveCocoa

class SchedulerSpec: QuickSpec {
	override func spec() {
		describe("ImmediateScheduler") {
			it("should run enqueued actions immediately") {
				var didRun = false
				ImmediateScheduler().schedule {
					didRun = true
				}

				expect(didRun).to(beTruthy())
			}
		}

		describe("MainScheduler") {
			func dispatchSyncInBackground(action: () -> ()) {
				let group = dispatch_group_create()
				dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), action)
				dispatch_group_wait(group, DISPATCH_TIME_FOREVER)
			}

			it("should run actions immediately when on the main thread") {
				let scheduler = MainScheduler()
				var values: [Int] = []
				expect(NSThread.isMainThread()).to(beTruthy())

				scheduler.schedule {
					values.append(0)
				}

				expect(values).to(equal([ 0 ]))

				scheduler.schedule {
					values.append(1)
				}

				scheduler.schedule {
					values.append(2)
				}

				expect(values).to(equal([ 0, 1, 2 ]))
			}

			it("should enqueue actions scheduled from the background") {
				let scheduler = MainScheduler()
				var values: [Int] = []

				dispatchSyncInBackground {
					scheduler.schedule {
						expect(NSThread.isMainThread()).to(beTruthy())
						values.append(0)
					}

					return
				}

				expect(values).to(equal([]))
				expect(values).toEventually(equal([ 0 ]))

				dispatchSyncInBackground {
					scheduler.schedule {
						expect(NSThread.isMainThread()).to(beTruthy())
						values.append(1)
					}

					scheduler.schedule {
						expect(NSThread.isMainThread()).to(beTruthy())
						values.append(2)
					}

					return
				}

				expect(values).to(equal([ 0 ]))
				expect(values).toEventually(equal([ 0, 1, 2 ]))
			}

			it("should run actions enqueued from the main thread after those from the background") {
				let scheduler = MainScheduler()
				var values: [Int] = []

				dispatchSyncInBackground {
					scheduler.schedule {
						expect(NSThread.isMainThread()).to(beTruthy())
						values.append(0)
					}

					return
				}

				scheduler.schedule {
					expect(NSThread.isMainThread()).to(beTruthy())
					values.append(1)
				}

				scheduler.schedule {
					expect(NSThread.isMainThread()).to(beTruthy())
					values.append(2)
				}

				expect(values).to(equal([]))
				expect(values).toEventually(equal([ 0, 1, 2 ]))
			}
{'fraction_non_alphanumeric': 0.12962962962962962, 'fraction_numerical': 0.01352145796590241, 'mean_word_length': 3.0705741626794256, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '403363', 'n_tokens_mistral': 1476, 'n_tokens_neox': 1083, 'n_words': 216}