Skip to content

Commit

Permalink
Tests: adjust synchronization tests for Windows (#615)
Browse files Browse the repository at this point in the history
* Tests: adjust synchronization tests for Windows

Windows does not support pthreads.  Adjust the tests to use the Windows
APIs for locking where the primitive being used is the `SRWLOCK`.

* SwiftDocCTestes: extract a helper to check the lock state

Address feedback from @Kyle-Ye in #615.  This avoids replicating the
logic for checking the lock state which is already subtle due to the
checking of the return value.
  • Loading branch information
compnerd authored Jun 7, 2023
1 parent 80808a8 commit 4b92390
Showing 1 changed file with 28 additions and 25 deletions.
53 changes: 28 additions & 25 deletions Tests/SwiftDocCTests/Utility/SynchronizationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,29 @@

import XCTest
@testable import SwiftDocC
#if os(Windows)
import func WinSDK.TryAcquireSRWLockExclusive
#endif

private func XCTAssertLockIsUnlocked<T>(_ synced: Synchronized<T>) {
#if os(macOS) || os(iOS)
XCTAssertTrue(os_unfair_lock_trylock(synced.lock))
#elseif os(Windows)
XCTAssertNotEqual(TryAcquireSRWLockExclusive(synced.lock), 0)
#else
XCTAssertEqual(pthread_mutex_trylock(synced.lock), 0)
#endif
}

private func XCTAssertLockIsLocked<T>(_ synced: Synchronized<T>) {
#if os(macOS) || os(iOS)
XCTAssertFalse(os_unfair_lock_trylock(synced.lock))
#elseif os(Windows)
XCTAssertEqual(TryAcquireSRWLockExclusive(synced.lock), 0)
#else
XCTAssertNotEqual(pthread_mutex_trylock(synced.lock), 0)
#endif
}

class SynchronizationTests: XCTestCase {
func testInitialState() {
Expand All @@ -19,11 +42,7 @@ class SynchronizationTests: XCTestCase {
XCTAssertEqual(synced.sync({ $0 }), false)

// Verify the lock is unlocked
#if os(macOS) || os(iOS)
XCTAssertTrue(os_unfair_lock_trylock(synced.lock))
#else
XCTAssertEqual(pthread_mutex_trylock(synced.lock),0)
#endif
XCTAssertLockIsUnlocked(synced)
}

func testUpdatesWrappedValue() {
Expand All @@ -42,11 +61,7 @@ class SynchronizationTests: XCTestCase {
XCTAssertEqual(synced.sync({ $0 }), true)

// Verify the lock is unlocked after running the block
#if os(macOS) || os(iOS)
XCTAssertTrue(os_unfair_lock_trylock(synced.lock))
#else
XCTAssertEqual(pthread_mutex_trylock(synced.lock),0)
#endif
XCTAssertLockIsUnlocked(synced)
}

func testLocksLockDuringPerformingWork() {
Expand All @@ -64,11 +79,7 @@ class SynchronizationTests: XCTestCase {
// Asynchronously perform a check after 0.25 secs that the lock is locked
testQueue.asyncAfter(deadline: .now() + 0.25) {
// Verify the used lock is lock in here
#if os(macOS) || os(iOS)
XCTAssertFalse(os_unfair_lock_trylock(synced.lock))
#else
XCTAssertNotEqual(pthread_mutex_trylock(synced.lock),0)
#endif
XCTAssertLockIsLocked(synced)
didTest.fulfill()
}

Expand All @@ -88,11 +99,7 @@ class SynchronizationTests: XCTestCase {
}

// Verify that the lock is unlocked after re-throwing
#if os(macOS) || os(iOS)
XCTAssertTrue(os_unfair_lock_trylock(synced.lock))
#else
XCTAssertEqual(pthread_mutex_trylock(synced.lock),0)
#endif
XCTAssertLockIsUnlocked(synced)
}

func testBlocking() {
Expand Down Expand Up @@ -126,11 +133,7 @@ class SynchronizationTests: XCTestCase {
let synced = Lock()

// Verify the lock is unlocked
#if os(macOS) || os(iOS)
XCTAssertTrue(os_unfair_lock_trylock(synced.lock))
#else
XCTAssertEqual(pthread_mutex_trylock(synced.lock), 0)
#endif
XCTAssertLockIsUnlocked(synced)
}

func testBlockingWithLock() {
Expand Down

0 comments on commit 4b92390

Please sign in to comment.