Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests: adjust synchronization tests for Windows #615

Merged
merged 2 commits into from
Jun 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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