Skip to content

Commit 2533a33

Browse files
committed
fix: Refactored failing test.
Signed-off-by: Iva Horn <iva.horn@nextcloud.com>
1 parent f89122a commit 2533a33

File tree

1 file changed

+22
-35
lines changed

1 file changed

+22
-35
lines changed

Tests/NextcloudFileProviderKitTests/RemoteChangeObserverTests.swift

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import NextcloudCapabilitiesKit
77
@testable import NextcloudFileProviderKit
88
import NextcloudFileProviderKitMocks
99
import RealmSwift
10+
import Testing
1011
import TestInterface
1112
import XCTest
1213

@@ -50,7 +51,8 @@ final class RemoteChangeObserverTests: NextcloudFileProviderKitTestCase {
5051

5152
/// Helper to wait for an expectation with a standard timeout.
5253
private func wait(for expectation: XCTestExpectation, description: String) async {
53-
let result = await XCTWaiter.fulfillment(of: [expectation], timeout: 5.0)
54+
let result = await XCTWaiter.fulfillment(of: [expectation], timeout: 10.0)
55+
5456
if result != .completed {
5557
XCTFail("Timeout waiting for \(description)")
5658
}
@@ -402,27 +404,14 @@ final class RemoteChangeObserverTests: NextcloudFileProviderKitTestCase {
402404
}
403405

404406
func testPinging() async throws {
407+
let authentication = expectation(description: "authentication")
408+
authentication.assertForOverFulfill = false
409+
405410
let remoteInterface = MockRemoteInterface(account: Self.account)
406411
remoteInterface.capabilities = mockCapabilities
407412

408-
actor AuthFlag {
409-
var value = false
410-
func set() {
411-
value = true
412-
}
413-
414-
func get() -> Bool {
415-
value
416-
}
417-
}
418-
let authenticated = AuthFlag()
419-
420-
NotificationCenter.default.addObserver(
421-
forName: NotifyPushAuthenticatedNotificationName, object: nil, queue: nil
422-
) { _ in
423-
Task {
424-
await authenticated.set()
425-
}
413+
NotificationCenter.default.addObserver(forName: NotifyPushAuthenticatedNotificationName, object: nil, queue: nil) { _ in
414+
authentication.fulfill()
426415
}
427416

428417
remoteChangeObserver = RemoteChangeObserver(
@@ -436,27 +425,25 @@ final class RemoteChangeObserverTests: NextcloudFileProviderKitTestCase {
436425

437426
let pingIntervalNsecs = 500_000_000
438427
remoteChangeObserver?.setWebSocketPingInterval(to: UInt64(pingIntervalNsecs))
428+
await wait(for: authentication, description: "authentication")
439429

440-
for _ in 0 ... Self.timeout {
441-
try await Task.sleep(nanoseconds: 1_000_000)
442-
if await authenticated.get() {
443-
break
444-
}
445-
}
446-
let isAuthenticated = await authenticated.get()
447-
XCTAssertTrue(isAuthenticated)
448-
449-
let intendedPings = 3
450-
// Add a bit of buffer to the wait time
451-
let intendedPingsWait = (intendedPings + 1) * pingIntervalNsecs
430+
let measurementStart = Date()
431+
let firstPing = expectation(description: "First Ping")
432+
let secondPing = expectation(description: "Second Ping")
433+
let thirdPing = expectation(description: "Third Ping")
434+
let pings = ExpectationFulfillmentCounter(firstPing, secondPing, thirdPing)
452435

453-
var pings = 0
454436
Self.notifyPushServer.pingHandler = {
455-
pings += 1
437+
Task {
438+
await pings.next()
439+
}
456440
}
457441

458-
try await Task.sleep(nanoseconds: UInt64(intendedPingsWait))
459-
XCTAssertEqual(pings, intendedPings)
442+
await fulfillment(of: [firstPing, secondPing, thirdPing])
443+
let measurementEnd = Date()
444+
let pingTimeInterval = measurementEnd.timeIntervalSince(measurementStart)
445+
446+
XCTAssertGreaterThan(pingTimeInterval, Double(pingIntervalNsecs / 1_000_000_000) * 3)
460447
}
461448

462449
func testRetryOnConnectionLoss() async throws {

0 commit comments

Comments
 (0)