From f10f046660350b9bc9fd0393251fb613ac8974e9 Mon Sep 17 00:00:00 2001 From: yoheimuta Date: Fri, 27 Jul 2018 18:12:43 +0900 Subject: [PATCH 1/5] fix: main thread deadlock --- BufferedLogger/BufferedOutput.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BufferedLogger/BufferedOutput.swift b/BufferedLogger/BufferedOutput.swift index f72fb66..1e82611 100644 --- a/BufferedLogger/BufferedOutput.swift +++ b/BufferedLogger/BufferedOutput.swift @@ -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 From bb13281bd7080c6eed4e23335e9f17692c839a73 Mon Sep 17 00:00:00 2001 From: yoheimuta Date: Fri, 27 Jul 2018 18:30:00 +0900 Subject: [PATCH 2/5] tests: testSuspend --- BufferedLoggerTests/BufferedOutputTests.swift | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/BufferedLoggerTests/BufferedOutputTests.swift b/BufferedLoggerTests/BufferedOutputTests.swift index fe4e566..f5842d9 100644 --- a/BufferedLoggerTests/BufferedOutputTests.swift +++ b/BufferedLoggerTests/BufferedOutputTests.swift @@ -225,4 +225,47 @@ class BufferedOutputTests: XCTestCase { test.name) } } + + func testSuspend() { + let tests: [( + name: String, + isCallSuspend: Bool, + wantCalledWriteCount: Int + )] = [ + ( + name: "expect a call to flush without suspension", + isCallSuspend: false, + wantCalledWriteCount: 1 + ), + ( + name: "expect no call to flush after suspension", + isCallSuspend: 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.isCallSuspend { + 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) + } + } } From 60575c2d5cbdcf7898601ec433e9f7c4c1a6e30d Mon Sep 17 00:00:00 2001 From: yoheimuta Date: Fri, 27 Jul 2018 18:32:53 +0900 Subject: [PATCH 3/5] tests: testResume --- BufferedLoggerTests/BufferedOutputTests.swift | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/BufferedLoggerTests/BufferedOutputTests.swift b/BufferedLoggerTests/BufferedOutputTests.swift index f5842d9..8c17133 100644 --- a/BufferedLoggerTests/BufferedOutputTests.swift +++ b/BufferedLoggerTests/BufferedOutputTests.swift @@ -268,4 +268,48 @@ class BufferedOutputTests: XCTestCase { test.name) } } + + func testResume() { + let tests: [( + name: String, + isCallResume: Bool, + wantCalledWriteCount: Int + )] = [ + ( + name: "expect no call to flush without resumption", + isCallResume: false, + wantCalledWriteCount: 0 + ), + ( + name: "expect a call to flush after resumption", + isCallResume: 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.isCallResume { + 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) + } + } } From 4cfb860f97a9ccdeca8d6ea213cdb836dc0c32f8 Mon Sep 17 00:00:00 2001 From: yoheimuta Date: Fri, 27 Jul 2018 18:34:48 +0900 Subject: [PATCH 4/5] chore: Added suspend and resume calls to Demo --- Demo/Demo/AppDelegate.swift | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Demo/Demo/AppDelegate.swift b/Demo/Demo/AppDelegate.swift index aa7b174..739f440 100644 --- a/Demo/Demo/AppDelegate.swift +++ b/Demo/Demo/AppDelegate.swift @@ -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() + } } From 64a435112ecd9f66c260555f82744ca76467cf4c Mon Sep 17 00:00:00 2001 From: yoheimuta Date: Fri, 27 Jul 2018 18:37:48 +0900 Subject: [PATCH 5/5] refactor: Fixed var names --- BufferedLoggerTests/BufferedOutputTests.swift | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/BufferedLoggerTests/BufferedOutputTests.swift b/BufferedLoggerTests/BufferedOutputTests.swift index 8c17133..1276756 100644 --- a/BufferedLoggerTests/BufferedOutputTests.swift +++ b/BufferedLoggerTests/BufferedOutputTests.swift @@ -229,17 +229,17 @@ class BufferedOutputTests: XCTestCase { func testSuspend() { let tests: [( name: String, - isCallSuspend: Bool, + callSuspend: Bool, wantCalledWriteCount: Int )] = [ ( name: "expect a call to flush without suspension", - isCallSuspend: false, + callSuspend: false, wantCalledWriteCount: 1 ), ( name: "expect no call to flush after suspension", - isCallSuspend: true, + callSuspend: true, wantCalledWriteCount: 0 ) ] @@ -252,7 +252,7 @@ class BufferedOutputTests: XCTestCase { retryRule: DefaultRetryRule(retryLimit: 1))) output.start() - if test.isCallSuspend { + if test.callSuspend { output.suspend() } @@ -272,17 +272,17 @@ class BufferedOutputTests: XCTestCase { func testResume() { let tests: [( name: String, - isCallResume: Bool, + callResume: Bool, wantCalledWriteCount: Int )] = [ ( name: "expect no call to flush without resumption", - isCallResume: false, + callResume: false, wantCalledWriteCount: 0 ), ( name: "expect a call to flush after resumption", - isCallResume: true, + callResume: true, wantCalledWriteCount: 1 ) ] @@ -296,7 +296,7 @@ class BufferedOutputTests: XCTestCase { output.start() output.suspend() - if test.isCallResume { + if test.callResume { output.resume() }