-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #195 from davidstump/dr/heartbeat-timer-crash
Refactor HeartbeatTimer to guarantee thread safety
- Loading branch information
Showing
7 changed files
with
197 additions
and
102 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// | ||
// HeartbeatTimerSpec.swift | ||
// SwiftPhoenixClientTests | ||
// | ||
// Created by Daniel Rees on 8/24/21. | ||
// Copyright © 2021 SwiftPhoenixClient. All rights reserved. | ||
// | ||
|
||
import Quick | ||
import Nimble | ||
@testable import SwiftPhoenixClient | ||
|
||
class HeartbeatTimerSpec: QuickSpec { | ||
|
||
override func spec() { | ||
|
||
let queue = DispatchQueue(label: "heartbeat.timer.spec") | ||
var timer: HeartbeatTimer! | ||
|
||
|
||
beforeEach { | ||
timer = HeartbeatTimer(timeInterval: 10, queue: queue) | ||
} | ||
|
||
describe("isValid") { | ||
it("returns false if is not started") { | ||
expect(timer.isValid).to(beFalse()) | ||
} | ||
|
||
it("returns true if the timer has started") { | ||
timer.start { /* no-op */ } | ||
expect(timer.isValid).to(beTrue()) | ||
} | ||
|
||
it("returns false if timer has been stopped") { | ||
timer.start { /* no-op */ } | ||
timer.stop() | ||
expect(timer.isValid).to(beFalse()) | ||
} | ||
} | ||
|
||
describe("fire") { | ||
it("calls the event handler") { | ||
var timerCalled = 0 | ||
timer.start { timerCalled += 1 } | ||
expect(timerCalled).to(equal(0)) | ||
|
||
timer.fire() | ||
expect(timerCalled).to(equal(1)) | ||
} | ||
|
||
it("does not call event handler if stopped") { | ||
var timerCalled = 0 | ||
timer.start { timerCalled += 1 } | ||
expect(timerCalled).to(equal(0)) | ||
|
||
timer.stop() | ||
timer.fire() | ||
expect(timerCalled).to(equal(0)) | ||
} | ||
} | ||
|
||
describe("equatable") { | ||
it("equates different timers correctly", closure: { | ||
let timerA = HeartbeatTimer(timeInterval: 10, queue: queue) | ||
let timerB = HeartbeatTimer(timeInterval: 10, queue: queue) | ||
|
||
expect(timerA).to(equal(timerA)) | ||
expect(timerA).toNot(equal(timerB)) | ||
|
||
}) | ||
} | ||
} | ||
} |
This file contains 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