Last active
December 17, 2019 15:20
-
-
Save Edudjr/a34f9995dad5b2aa8c759db193058f21 to your computer and use it in GitHub Desktop.
Swift observable hanging example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ObservableTestTests.swift | |
// ObservableTestTests | |
// | |
// Created by Eduardo Domene Junior on 01/12/19. | |
// Copyright © 2019 Eduardo Domene Junior. All rights reserved. | |
// | |
import XCTest | |
import Observable | |
@testable import ObservableTest | |
class ObservableTestTests: XCTestCase { | |
var disposal = Disposal() | |
/// Succeeds with current implementation | |
/// Succeeds with new implementation | |
func test_whenUsingDispatchMain_shouldSucceed() { | |
let exp = expectation(description: "") | |
let singleton = Singleton.shared | |
let callback: (() -> Void) = { | |
exp.fulfill() | |
} | |
var aClass: AClass? | |
singleton.observable.observe { _, _ in | |
singleton.observable.removeAllObservers() | |
DispatchQueue.main.async { | |
aClass = AClass(callback: callback) | |
} | |
}.add(to: &disposal) | |
print(aClass.debugDescription) | |
wait(for: [exp], timeout: 1.0) | |
XCTAssert(true) | |
} | |
/// Hangs with current implementation | |
/// Succeeds with new implementation | |
func test_whenPassingDispatchMain_shouldSucceed() { | |
let exp = expectation(description: "") | |
let singleton = Singleton.shared | |
let callback: (() -> Void) = { | |
exp.fulfill() | |
} | |
var aClass: AClass? | |
singleton.observable.observe(DispatchQueue.main) { _, _ in | |
singleton.observable.removeAllObservers() | |
aClass = AClass(callback: callback) | |
}.add(to: &disposal) | |
print(aClass.debugDescription) | |
wait(for: [exp], timeout: 2.0) | |
XCTAssert(true) | |
} | |
/// Hangs with current implementation | |
/// Succeeds with new implementation | |
func test_whenUsingDispatchGlobal_shouldSucceed() { | |
let exp = expectation(description: "") | |
let singleton = Singleton.shared | |
let callback: (() -> Void) = { | |
exp.fulfill() | |
} | |
var aClass: AClass? | |
singleton.observable.observe(DispatchQueue.global()) { _, _ in | |
singleton.observable.removeAllObservers() | |
aClass = AClass(callback: callback) | |
}.add(to: &disposal) | |
print(aClass.debugDescription) | |
wait(for: [exp], timeout: 1.0) | |
XCTAssert(true) | |
} | |
/// Hangs with current implementation | |
/// Hangs with new implementation | |
func test_whenNotUsingDispatch_shouldHang() { | |
let exp = expectation(description: "") | |
let singleton = Singleton.shared | |
let callback: (() -> Void) = { | |
exp.fulfill() | |
} | |
var aClass: AClass? | |
singleton.observable.observe { _, _ in | |
singleton.observable.removeAllObservers() | |
aClass = AClass(callback: callback) | |
}.add(to: &disposal) | |
print(aClass.debugDescription) | |
wait(for: [exp], timeout: 1.0) | |
XCTAssert(true) | |
} | |
class AClass { | |
var disposal = Disposal() | |
init(callback: @escaping () -> Void) { | |
let singleton = Singleton.shared | |
singleton.observable.observe { _, _ in | |
callback() | |
}.add(to: &disposal) | |
} | |
} | |
class Singleton { | |
static let shared = Singleton() | |
var observable = Observable(0) | |
private init() {} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment