<commit_before>//
//  ManagerTests.swift
//  ImageLoader
//
//  Created by <NAME> on 12/1/15.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import XCTest

class ManagerTests: ImageLoaderTests {

    var manager: Manager!

    override func setUp() {
        super.setUp()
        manager = Manager()
    }

    override func tearDown() {
        waitForAsyncTask(5)
        super.tearDown()
    }

    func testLoad() {
        let URL = NSURL(string: "http://manager/test/load")!

        manager.load(URL)
        XCTAssert(manager.state == .Running, manager.state.toString())

        waitForAsyncTask(1.1) // wait for loading

        XCTAssert(manager.state == .Ready, manager.state.toString())
    }

    func testSuspend() {
        let URL = NSURL(string: "http://manager/test/suspend")!

        manager.suspend(URL)
        XCTAssert(manager.state == .Ready, manager.state.toString())

        let loader = manager.load(URL)

        XCTAssert(manager.state == .Running, manager.state.toString())
        XCTAssert(loader.state == .Running, loader.state.toString())

        manager.suspend(URL)

        XCTAssert(manager.state == .Running, manager.state.toString())
        XCTAssert(loader.state == .Suspended, loader.state.toString())
    }

    func testCancel() {
        let URL = NSURL(string: "http://manager/test/cancel")!

        manager.cancel(URL)
        XCTAssert(manager.state == .Ready, manager.state.toString())

        let loader = manager.load(URL)
        XCTAssert(loader.state == .Running, loader.state.toString())

        manager.cancel(URL)
        waitForAsyncTask()

        XCTAssert(manager.state == .Ready, manager.state.toString())
        XCTAssert(loader.state == .Completed, loader.state.toString())
    }

    func testCancelWhenHasBlock() {
        let URL = NSURL(string: "http://manager/test_when_has_block/cancel")!

        let block = Block(identifier: 1) { (URL, _, error, _) -> Void in
            XCTAssertTrue(false, "dont call this completion handler")
        }

        manager.cancel(URL)
        XCTAssert(manager.state == .Ready, manager.state.toString())

        let loader = manager.load(URL)
        loader.appendBlock(block)

        XCTAssert(manager.state == .Running, manager.state.toString())
        XCTAssert(loader.state == .Running, loader.state.toString())

        manager.cancel(URL, block: block)
        XCTAssert(loader.state == .Canceling, loader.state.toString())
        XCTAssertTrue(loader.blocks.isEmpty)
        waitForAsyncTask()

        XCTAssert(manager.state == .Ready, manager.state.toString())
        XCTAssert(loader.state == .Completed, loader.state.toString())
    }

    func testCancelWhenHasTwoBlocks() {
        let expectation = expectationWithDescription("wait until loader complete")
        let URL = NSURL(string: "http://manager/test/cancel")!<commit_msg>Loader already die when manager.state is `.Ready`<commit_after>//
//  ManagerTests.swift
//  ImageLoader
//
//  Created by <NAME> on 12/1/15.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import XCTest

class ManagerTests: ImageLoaderTests {

    var manager: Manager!

    override func setUp() {
        super.setUp()
        manager = Manager()
    }

    override func tearDown() {
        waitForAsyncTask(5)
        super.tearDown()
    }

    func testLoad() {
        let URL = NSURL(string: "http://manager/test/load")!

        manager.load(URL)
        XCTAssert(manager.state == .Running, manager.state.toString())

        waitForAsyncTask(1.1) // wait for loading

        XCTAssert(manager.state == .Ready, manager.state.toString())
    }

    func testSuspend() {
        let URL = NSURL(string: "http://manager/test/suspend")!

        manager.suspend(URL)
        XCTAssert(manager.state == .Ready, manager.state.toString())

        let loader = manager.load(URL)

        XCTAssert(manager.state == .Running, manager.state.toString())
        XCTAssert(loader.state == .Running, loader.state.toString())

        manager.suspend(URL)

        XCTAssert(manager.state == .Running, manager.state.toString())
        XCTAssert(loader.state == .Suspended, loader.state.toString())
    }

    func testCancel() {
        let URL = NSURL(string: "http://manager/test/cancel")!

        manager.cancel(URL)
        XCTAssert(manager.state == .Ready, manager.state.toString())

        let loader = manager.load(URL)
        XCTAssert(loader.state == .Running, loader.state.toString())

        manager.cancel(URL)
        waitForAsyncTask()

        XCTAssert(manager.state == .Ready, manager.state.toString())
        XCTAssert(loader.state == .Completed, loader.state.toString())
    }

    func testCancelWhenHasBlock() {
        let URL = NSURL(string: "http://manager/test_when_has_block/cancel")!

        let block = Block(identifier: 1) { (URL, _, error, _) -> Void in
            XCTAssertTrue(false, "dont call this completion handler")
        }

        manager.cancel(URL)
        XCTAssert(manager.state == .Ready, manager.state.toString())

        let loader = manager.load(URL)
        loader.appendBlock(block)

        XCTAssert(manager.state == .Running, manager.state.toString())
        XCTAssert(loader.state == .Running, loader.state.toString())

        manager.cancel(URL, block: block)
        XCTAssert(loader.state == .Canceling, loader.state.toString())
        XCTAssertTrue(loader.blocks.isEmpty)
        waitForAsyncTask()

        XCTAssert(manager.state == .Ready, manager.state.toString())
    }

    func testCancelWhenHasTwoBlocks() {
        let expectation = expectationWithDescription("wait until loader complete")
        let URL = NSURL(string: "http://manager/test/cancel")!
{'fraction_non_alphanumeric': 0.13565552249256083, 'fraction_numerical': 0.004551023980395589, 'mean_word_length': 2.6187460417986066, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '651415', 'n_tokens_mistral': 1697, 'n_tokens_neox': 1632, 'n_words': 324}