Skip to content

Commit

Permalink
fix getLastCommonTransactionTimestamp logic
Browse files Browse the repository at this point in the history
  • Loading branch information
fatbobman committed Jul 21, 2022
1 parent 16b9eb1 commit b89c5ef
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -88,7 +89,7 @@ class TimestampManagerTests: XCTestCase {
let lastTimestampe = manager.getLastCommonTransactionTimestamp(in: authors)

// then
XCTAssertNil(lastTimestampe)
XCTAssertNotNil(lastTimestampe)
}

// 部分author设置了时间戳,已触及阈值日期
Expand All @@ -115,7 +116,7 @@ class TimestampManagerTests: XCTestCase {
// then
XCTAssertNotNil(lastTimestamp)
if let lastTimestamp = lastTimestamp {
XCTAssertGreaterThan(lastTimestamp, date1)
XCTAssertLessThan(lastTimestamp, date1)
}
}

Expand All @@ -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)
Expand Down

0 comments on commit b89c5ef

Please sign in to comment.