Skip to content

Commit

Permalink
Fixed bug in ObservableValue
Browse files Browse the repository at this point in the history
  • Loading branch information
Yanko Dimitrov committed Jul 27, 2015
1 parent 38e5430 commit c14687e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
2 changes: 2 additions & 0 deletions SignalKit/Observables/ObservableValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public final class ObservableValue<T>: Observable {
self.internalValue = value
self.observable = observable
self.lock = lock

observable.dispatch(value)
}

public convenience init(_ value: T) {
Expand Down
20 changes: 18 additions & 2 deletions SignalKitTests/Observables/ObservableValueTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,30 @@ class ObservableValueTests: XCTestCase {
XCTAssertEqual(lock.syncCounter, 0, "Should perform balanced calls to lock() / unlock()")
}

func testDispatchTheCurrentValueOnAddObserver() {

let name = ObservableValue<String>("John")
var result = ""

name.addObserver { result = $0 }

XCTAssertEqual(result, "John", "Should dispatch the current value when adding a new observer")
}

/// MARK: ObservableValue Extensions Tests

func testDispatchOnMainQueue() {

let expectation = expectationWithDescription("Should dispatch on main queue")
let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
var dispatchCounter = 0

observe(userName)
.next { _ in

if NSThread.isMainThread() == true {
dispatchCounter += 1

if dispatchCounter == 2 && NSThread.isMainThread() == true {
expectation.fulfill()
}
}
Expand All @@ -150,11 +163,14 @@ class ObservableValueTests: XCTestCase {

let expectation = expectationWithDescription("Should dispatch on background queue")
let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
var dispatchCounter = 0

observe(userName)
.next { _ in

if NSThread.isMainThread() == false {
dispatchCounter += 1

if dispatchCounter == 2 && NSThread.isMainThread() == false {
expectation.fulfill()
}
}
Expand Down
29 changes: 15 additions & 14 deletions SignalKitTests/Observables/SignalTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import XCTest

class SignalTests: XCTestCase {

let initialUserName = "John"
var signal: Signal<Int>!
var lock: MockLock!
var userName: ObservableOf<String>!
var userName: ObservableValue<String>!
var signalContainer: SignalContainer!

override func setUp() {
Expand All @@ -23,7 +24,7 @@ class SignalTests: XCTestCase {

lock = MockLock()
signal = Signal<Int>(observable: observable, lock: lock)
userName = ObservableOf<String>()
userName = ObservableValue<String>(value: initialUserName, lock: lock)
signalContainer = SignalContainer()
}

Expand Down Expand Up @@ -127,9 +128,7 @@ class SignalTests: XCTestCase {

let signal = observe(userName).next { result = $0 }

userName.dispatch("John")

XCTAssertEqual(result, "John", "Should add a new observer to the signal")
XCTAssertEqual(result, initialUserName, "Should add a new observer to the signal")
}

func testAddToSignalContainer() {
Expand All @@ -140,10 +139,10 @@ class SignalTests: XCTestCase {
.next{ result = $0 }
.addTo(signalContainer)

userName.dispatch("John")
userName.dispatch("Jane")

XCTAssertEqual(signalContainer.signalsCount, 1, "Should add the signal chain to signal container")
XCTAssertEqual(result, "John", "Should retain the signal chain")
XCTAssertEqual(result, "Jane", "Should retain the signal chain")
}

func testMap() {
Expand All @@ -155,8 +154,6 @@ class SignalTests: XCTestCase {
.next { result = $0 }
.addTo(signalContainer)

userName.dispatch("John")

XCTAssertEqual(result, 4, "Should transform a signal of type T to signal of type U")
}

Expand Down Expand Up @@ -217,12 +214,15 @@ class SignalTests: XCTestCase {

let expectation = expectationWithDescription("Should deliver on main queue")
let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
var dispatchCounter = 0

observe(userName)
.deliverOn(.Main)
.next { _ in

if NSThread.isMainThread() == true {
dispatchCounter += 1

if dispatchCounter == 2 && NSThread.isMainThread() == true {

expectation.fulfill()
}
Expand All @@ -241,12 +241,15 @@ class SignalTests: XCTestCase {

let expectation = expectationWithDescription("Should deliver on background queue")
let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
var dispatchCounter = 0

observe(userName)
.deliverOn(.Background(queue))
.next { _ in

if NSThread.isMainThread() == false {
dispatchCounter += 1

if dispatchCounter == 2 && NSThread.isMainThread() == false {

expectation.fulfill()
}
Expand Down Expand Up @@ -278,12 +281,10 @@ class SignalTests: XCTestCase {

var result = ""

userName.dispatch("Jack")

observe(userName)
.bindTo { result = $0 }
.addTo(signalContainer)

XCTAssertEqual(result, "Jack", "Should bind the signal value")
XCTAssertEqual(result, initialUserName, "Should bind the signal value")
}
}

0 comments on commit c14687e

Please sign in to comment.