<commit_before>//
//  Anomalies.swift
//  Tests
//
//  Created by <NAME> on 10/22/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation
import RxSwift
import RxCocoa
import RxTest
import XCTest
import Dispatch

/**
 Makes sure github anomalies and edge cases don't surface up again.
 */
class AnomaliesTest: RxTest {
}

extension AnomaliesTest {
    func test936() {
        func testSharingOperators(share: (Observable<Int>) -> Observable<Int>) {
            let expectation = self.expectation(description: "wait until sequence completes")

            let queue = DispatchQueue(
                label: "Test",
                attributes: .concurrent // commenting this to use a serial queue remove the issue
            )

            let scheduler: SchedulerType = ConcurrentDispatchQueueScheduler(queue: queue)

            func makeSequence(label: String, period: RxTimeInterval) -> Observable<Int> {
                return Observable<Int>
                    .interval(period, scheduler: scheduler)
                    .shareReplay(1)
            }

            let _ = makeSequence(label: "main", period: 0.04)
                .flatMapLatest { (index: Int) -> Observable<(Int, Int)> in
                    return makeSequence(label: "nested", period: 0.02).map { (index, $0) }
                }
                .take(50)
                .mapWithIndex { ($1, $0.0, $0.1) }
                .subscribe(
                    onNext: { _ in },
                    onCompleted: {
                        expectation.fulfill()
                    } 
            )
            
            waitForExpectations(timeout: 20.0) { (e) in
                XCTAssertNil(e)
            }
        }

        for op in [
                { $0.shareReplay(1) },
                { $0.replay(1).refCount() },
                { $0.shareReplayLatestWhileConnected() }
            ] as [(Observable<Int>) -> Observable<Int>] {
            testSharingOperators(share: op)
        }
    }<commit_msg>Adds additional deadlock tests.<commit_after>//
//  Anomalies.swift
//  Tests
//
//  Created by <NAME> on 10/22/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation
import RxSwift
import RxCocoa
import RxTest
import XCTest
import Dispatch

/**
 Makes sure github anomalies and edge cases don't surface up again.
 */
class AnomaliesTest: RxTest {
}

extension AnomaliesTest {
    func test936() {
        func performSharingOperatorsTest(share: @escaping (Observable<Int>) -> Observable<Int>) {
            let queue = DispatchQueue(
                label: "Test",
                attributes: .concurrent // commenting this to use a serial queue remove the issue
            )

            for i in 0 ..< 10 {
                let expectation = self.expectation(description: "wait until sequence completes")

                queue.async {
                    let scheduler: SchedulerType = ConcurrentDispatchQueueScheduler(queue: queue, leeway: .milliseconds(5))

                    func makeSequence(label: String, period: RxTimeInterval) -> Observable<Int> {
                        return share(Observable<Int>.interval(period, scheduler: scheduler))
                    }

                    let _ = makeSequence(label: "main", period: 0.1)
                        .flatMapLatest { (index: Int) -> Observable<(Int, Int)> in
                            return makeSequence(label: "nested", period: 0.02).map { (index, $0) }
                        }
                        .take(10)
                        .mapWithIndex { ($1, $0.0, $0.1) }
                        .subscribe(
                            onNext: { _ in },
                            onCompleted: {
                                expectation.fulfill()
                            } 
                    )
                }
            }

            waitForExpectations(timeout: 10.0) { (e) in
                XCTAssertNil(e)
            }
        }

        for op in [
                { $0.shareReplay(1) },
                { $0.replay(1).refCount() },
                { $0.publish().refCount() },
                { $0.shareReplayLatestWhileConnected() }
            ] as [(Observable<Int>) -> Observable<Int>] {
            performSharingOperatorsTest(share: op)
        }
    }

    func testSeparationBetweenOnAndSubscriptionLocks() {
        func performSharingOperatorsTest(share: @escaping (Observable<Int>) -> Observable<Int>) {
            for i in 0 ..< 1 {
                let expectation = self.expectation(description: "wait until sequence completes")

                let queue = DispatchQueue(
                            label: "off main thread",
                            attributes: .concurrent
                        )

                queue.async {
                    func makeSequence(label: String, period: RxTimeInterval) -> Observable<Int> {
                        let schedulerQueue = DispatchQueue(
                            label: "Test",
                            attributes: .concurrent
                        )

                        let scheduler: SchedulerType = ConcurrentDispatchQueueScheduler(queue: schedulerQueue, leeway: .milliseconds(0))

                        return share(Observable<Int>.interval(period, scheduler: scheduler))
                    }

                    let _ = Observable.of(
                            makeSequence(label: "main", period: 0.2),
                            makeSequence(label: "nested", period: 0.3)
                        ).merge()
                        .take(1)
                        .subscribe(
                            onNext: { _ in
                                Thread.sleep(forTimeInterval: 0.4)
                            },
                            onCompleted: {
                                expectation.fulfill()
                        }
                    )
                }
            }

            waitForExpectations(timeout: 2.0) { (e) in
                XCTAssertNil(e)
            }
        }

        for op in [
            { $0.shareReplay(1) },
            { $0.replay(1).refCount() },
            { $0.publish().refCount() },
            { $0.shareReplayLatestWhileConnected() }
            ] as [(Observable<Int>) -> Observable<Int>] {
            performSharingOperatorsTest(share: op)
        }
    }
{'fraction_non_alphanumeric': 0.10101010101010101, 'fraction_numerical': 0.014678030303030304, 'mean_word_length': 1.298512876314835, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 38, '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': '227407', 'n_tokens_mistral': 1728, 'n_tokens_neox': 1565, 'n_words': 368}