Skip to content

Commit a502bee

Browse files
authored
[PTECH-7523] Fix that log drops new entries (#85)
* Fix that log drops new entries * Bump patch version
1 parent acfe8a0 commit a502bee

File tree

3 files changed

+72
-5
lines changed

3 files changed

+72
-5
lines changed

Cilicon.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@
386386
"$(inherited)",
387387
"@executable_path/../Frameworks",
388388
);
389-
MARKETING_VERSION = 2.4.1;
389+
MARKETING_VERSION = 2.4.2;
390390
PRODUCT_BUNDLE_IDENTIFIER = com.traderepublic.cilicon;
391391
PRODUCT_NAME = "$(TARGET_NAME)";
392392
PROVISIONING_PROFILE_SPECIFIER = "";
@@ -420,7 +420,7 @@
420420
"$(inherited)",
421421
"@executable_path/../Frameworks",
422422
);
423-
MARKETING_VERSION = 2.4.1;
423+
MARKETING_VERSION = 2.4.2;
424424
PRODUCT_BUNDLE_IDENTIFIER = com.traderepublic.cilicon;
425425
PRODUCT_NAME = "$(TARGET_NAME)";
426426
PROVISIONING_PROFILE_SPECIFIER = "";

Cilicon/SSHLogger.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ import Foundation
44
final class SSHLogger: ObservableObject {
55
static let shared = SSHLogger()
66

7-
private static let maxLogChunks = 500
7+
static let maxLogChunks = 500
88
private static let dateFormatter: DateFormatter = {
99
let formatter = DateFormatter()
1010
formatter.dateStyle = .short
1111
formatter.timeStyle = .medium
1212
return formatter
1313
}()
1414

15-
private init() { }
15+
init() { }
1616

1717
@Published
1818
var log: [LogChunk] = []
@@ -39,7 +39,8 @@ final class SSHLogger: ObservableObject {
3939
log.append(LogChunk(text: String(line)))
4040
}
4141
if log.count > Self.maxLogChunks {
42-
log.removeLast(log.count - Self.maxLogChunks)
42+
// Drop the oldest log entries to keep only the most recent ones
43+
log.removeFirst(log.count - Self.maxLogChunks)
4344
}
4445
}
4546

CiliconTests/SSHLoggerTests.swift

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
@testable import Cilicon
2+
import XCTest
3+
4+
@MainActor
5+
final class SSHLoggerTests: XCTestCase {
6+
private var sut: SSHLogger!
7+
8+
override func setUp() async throws {
9+
sut = SSHLogger()
10+
}
11+
12+
override func tearDown() async throws {
13+
sut = nil
14+
}
15+
16+
func test_log_trimsWhitespaceAndStoresLine() {
17+
// When
18+
sut.log(string: " hello world \n")
19+
20+
// Then
21+
XCTAssertEqual(sut.log.map(\.text), ["hello world"])
22+
}
23+
24+
func test_log_ignoresEmptyOrWhitespaceOnlyInput() {
25+
// When
26+
sut.log(string: " \n\t ")
27+
28+
// Then
29+
XCTAssertTrue(sut.log.isEmpty)
30+
}
31+
32+
func test_log_splitsOnNewlines_preservingEmptyLines() {
33+
// When
34+
sut.log(string: "line1\n\nline3\n")
35+
36+
// Then
37+
XCTAssertEqual(sut.log.map(\.text), ["line1", "", "line3"])
38+
}
39+
40+
func test_log_respectsMaxLogChunksAndDropsOldestEntries() {
41+
// When Log more than the max number of chunks
42+
let maxLimit = SSHLogger.maxLogChunks
43+
let overLimit = maxLimit + 10
44+
for i in 0 ..< overLimit {
45+
sut.log(string: "line_\(i)")
46+
}
47+
48+
// Then Oldest entries should be dropped; keep the most recent SSHLogger.maxLogChunks.
49+
XCTAssertEqual(sut.log.count, maxLimit)
50+
let firstKeptIndex = overLimit - maxLimit
51+
XCTAssertEqual(sut.log.first?.text, "line_\(firstKeptIndex)")
52+
XCTAssertEqual(sut.log.last?.text, "line_\(overLimit - 1)")
53+
}
54+
55+
func test_log_timestampsAreNonDecreasing() {
56+
// When
57+
sut.log(string: "first")
58+
sut.log(string: "second")
59+
60+
// Then
61+
XCTAssertEqual(sut.log.count, 2)
62+
let firstTimestamp = sut.log[0].timestamp
63+
let secondTimestamp = sut.log[1].timestamp
64+
XCTAssertLessThanOrEqual(firstTimestamp, secondTimestamp)
65+
}
66+
}

0 commit comments

Comments
 (0)