From b89c5efbb9c500bfbb44786724ddfc4f75a0a2aa Mon Sep 17 00:00:00 2001 From: Yang Xu Date: Thu, 21 Jul 2022 16:02:17 +0800 Subject: [PATCH] fix getLastCommonTransactionTimestamp logic --- .../TransactionTimestampManager.swift | 19 ++----------------- .../TimestampManagerTests.swift | 11 ++++++----- 2 files changed, 8 insertions(+), 22 deletions(-) diff --git a/Sources/PersistentHistoryTrackingKit/TransactionTimestampManager.swift b/Sources/PersistentHistoryTrackingKit/TransactionTimestampManager.swift index f8f52e2..dd08cfc 100644 --- a/Sources/PersistentHistoryTrackingKit/TransactionTimestampManager.swift +++ b/Sources/PersistentHistoryTrackingKit/TransactionTimestampManager.swift @@ -32,23 +32,8 @@ struct TransactionTimestampManager: TransactionTimestampManagerProtocol { getLastHistoryTransactionTimestamp(for: author) } // 没有任何author记录时间的情况下,直接返回nil - guard let lastTimestamp = lastTimestamps.min() else { return nil } - - // 如果全部的author都记录了时间戳,则返回最早的日期。 - if lastTimestamps.count == shouldCheckAuthors.count { - // 返回所有auhtor时间戳中最早的日期) - return lastTimestamp - } else { - // 阈值日期 - let thresholdDate = Date().addingTimeInterval(-1 * abs(maximumDuration)) - if lastTimestamp < thresholdDate { - // 在最长持续时间之内仍未收集到全部author的时间戳,则返回阈值日期 - return thresholdDate - } else { - // 继续等待其他的author时间戳 - return nil - } - } + let lastTimestamp = lastTimestamps.min() ?? Date().addingTimeInterval(-1 * abs(maximumDuration)) + return lastTimestamp } func updateLastHistoryTransactionTimestamp(for author: String, to newDate: Date?) { diff --git a/Tests/PersistentHistoryTrackingKitTests/TimestampManagerTests.swift b/Tests/PersistentHistoryTrackingKitTests/TimestampManagerTests.swift index 9d4ef0f..92acdf8 100644 --- a/Tests/PersistentHistoryTrackingKitTests/TimestampManagerTests.swift +++ b/Tests/PersistentHistoryTrackingKitTests/TimestampManagerTests.swift @@ -40,14 +40,15 @@ class TimestampManagerTests: XCTestCase { func testNoAuthorUpdateTimestamp() { // given - let manager = TransactionTimestampManager(userDefaults: userDefaults, uniqueString: uniqueString) + let max:TimeInterval = 100 + let manager = TransactionTimestampManager(userDefaults: userDefaults, maximumDuration: max, uniqueString: uniqueString) let authors = AppActor.allCases.map { $0.rawValue } // when let lastTimestamp = manager.getLastCommonTransactionTimestamp(in: authors) // then - XCTAssertNil(lastTimestamp) + XCTAssertNotNil(lastTimestamp) } func testAllAuthorsHaveUpdatedTimestamp() { @@ -88,7 +89,7 @@ class TimestampManagerTests: XCTestCase { let lastTimestampe = manager.getLastCommonTransactionTimestamp(in: authors) // then - XCTAssertNil(lastTimestampe) + XCTAssertNotNil(lastTimestampe) } // 部分author设置了时间戳,已触及阈值日期 @@ -115,7 +116,7 @@ class TimestampManagerTests: XCTestCase { // then XCTAssertNotNil(lastTimestamp) if let lastTimestamp = lastTimestamp { - XCTAssertGreaterThan(lastTimestamp, date1) + XCTAssertLessThan(lastTimestamp, date1) } } @@ -135,7 +136,7 @@ class TimestampManagerTests: XCTestCase { let lastDate1 = manager.getLastCommonTransactionTimestamp(in: authors) // then - XCTAssertNil(lastDate1) + XCTAssertNotNil(lastDate1) // when let lastDate2 = manager.getLastCommonTransactionTimestamp(in: authors, exclude: batchAuthors)