Skip to content

Commit

Permalink
Merge pull request #2 from yoheimuta/tests-resume
Browse files Browse the repository at this point in the history
Add tests about suspend() and resume()
  • Loading branch information
yoheimuta authored Jul 27, 2018
2 parents f332067 + 64a4351 commit e00bfff
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 1 deletion.
2 changes: 1 addition & 1 deletion BufferedLogger/BufferedOutput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ final class BufferedOutput {
selector: #selector(tick(_:)),
userInfo: nil,
repeats: true)
DispatchQueue.main.sync {
DispatchQueue.main.async {
RunLoop.main.add(timer, forMode: .commonModes)
}
self.timer = timer
Expand Down
87 changes: 87 additions & 0 deletions BufferedLoggerTests/BufferedOutputTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,91 @@ class BufferedOutputTests: XCTestCase {
test.name)
}
}

func testSuspend() {
let tests: [(
name: String,
callSuspend: Bool,
wantCalledWriteCount: Int
)] = [
(
name: "expect a call to flush without suspension",
callSuspend: false,
wantCalledWriteCount: 1
),
(
name: "expect no call to flush after suspension",
callSuspend: true,
wantCalledWriteCount: 0
)
]

for test in tests {
let mwriter = MockWriter(shouldSuccess: true)
let output = BufferedOutput(writer: mwriter,
config: Config(flushEntryCount: 10,
flushInterval: 1,
retryRule: DefaultRetryRule(retryLimit: 1)))
output.start()

if test.callSuspend {
output.suspend()
}

output.emit(Entry("1".data(using: .utf8)!))

let afterExpectation = expectation(description: "after")
DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
afterExpectation.fulfill()
}
wait(for: [afterExpectation], timeout: 4.0)
XCTAssertEqual(mwriter.calledWriteCount,
test.wantCalledWriteCount,
test.name)
}
}

func testResume() {
let tests: [(
name: String,
callResume: Bool,
wantCalledWriteCount: Int
)] = [
(
name: "expect no call to flush without resumption",
callResume: false,
wantCalledWriteCount: 0
),
(
name: "expect a call to flush after resumption",
callResume: true,
wantCalledWriteCount: 1
)
]

for test in tests {
let mwriter = MockWriter(shouldSuccess: true)
let output = BufferedOutput(writer: mwriter,
config: Config(flushEntryCount: 10,
flushInterval: 1,
retryRule: DefaultRetryRule(retryLimit: 1)))
output.start()
output.suspend()

if test.callResume {
output.resume()
}

output.emit(Entry("1".data(using: .utf8)!))

let afterExpectation = expectation(description: "after")
DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
afterExpectation.fulfill()
}
wait(for: [afterExpectation], timeout: 4.0)
XCTAssertEqual(mwriter.calledWriteCount,
test.wantCalledWriteCount,
test.name)
}
}
}
8 changes: 8 additions & 0 deletions Demo/Demo/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
logger.post("6".data(using: .utf8)!)
return true
}

func applicationDidEnterBackground(_: UIApplication) {
logger.suspend()
}

func applicationWillEnterForeground(_: UIApplication) {
logger.resume()
}
}

0 comments on commit e00bfff

Please sign in to comment.