Skip to content

Commit

Permalink
Add isLocked property fix #3
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmassicotte committed Sep 30, 2024
1 parent ffb00a2 commit 9ba857c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
12 changes: 11 additions & 1 deletion Sources/Lock/AsyncLock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,15 @@ public final class AsyncLock {
throw error
}
}
}

public var isLocked: Bool {
get {
switch state {
case .unlocked:
false
case .locked:
true
}
}
}
}
22 changes: 22 additions & 0 deletions Tests/LockTests/LockTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,26 @@ struct LockTests {
try await task.value
}
}

@Test
func checkLock() async throws {
let lock = AsyncLock()

#expect(lock.isLocked == false)
await lock.lock()
#expect(lock.isLocked == true)
lock.unlock()
#expect(lock.isLocked == false)
}

@Test
func checkLockWithLocked() async throws {
let lock = AsyncLock()

#expect(lock.isLocked == false)
await lock.withLock {
#expect(lock.isLocked == true)
}
#expect(lock.isLocked == false)
}
}

0 comments on commit 9ba857c

Please sign in to comment.