Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii committed Dec 13, 2024
1 parent 2e84e2d commit 30bcaf0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ let package = Package(
name: "Verge",
platforms: [
.macOS(.v11),
.iOS(.v16),
.iOS(.v13),
.tvOS(.v13),
.watchOS(.v6),
],
Expand Down
19 changes: 10 additions & 9 deletions Tests/VergeTests/ConcurrencyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,18 @@ final class ConcurrencyTests: XCTestCase {
let store1 = DemoStore()

let exp = expectation(description: "11")
let count = OSAllocatedUnfairLock<Int>.init(initialState: 0)

let count = VergeConcurrency.UnfairLockAtomic<Int>.init(0)

DispatchQueue.global().async {
let cancellable = store1.sinkState(queue: .startsFromCurrentThread(andUse: .mainIsolated())) {
state in

defer {
count.withLock { $0 += 1 }
count.modify { $0 += 1 }
}

if count.withLock({ $0 == 0 }) {
if count.modify({ $0 == 0 }) {
XCTAssertEqual(Thread.isMainThread, false)
} else {
XCTAssertEqual(Thread.isMainThread, true)
Expand All @@ -109,25 +110,25 @@ final class ConcurrencyTests: XCTestCase {

for i in 0..<100 {
do {
let version = OSAllocatedUnfairLock<UInt64>.init(initialState: 0)
let version = VergeConcurrency.UnfairLockAtomic<UInt64>.init(0)
store.sinkState(queue: .passthrough) { s in
if version.withLock({ $0 > s.version }) {
if version.modify({ $0 > s.version }) {
XCTFail()
}
version.withLock { $0 = s.version }
version.modify { $0 = s.version }
print("\(i)", s.version)
}
.store(in: &bag)
}
}

do {
let version = OSAllocatedUnfairLock<UInt64>.init(initialState: 0)
let version = VergeConcurrency.UnfairLockAtomic<UInt64>.init(0)
store.sinkState(queue: .passthrough) { s in
if version.withLock({ $0 > s.version }) {
if version.modify({ $0 > s.version }) {
XCTFail()
}
version.withLock { $0 = s.version }
version.modify { $0 = s.version }
print("x", s.version)
store.commit {
if s.count == 1 {
Expand Down
21 changes: 11 additions & 10 deletions Tests/VergeTests/PipelineTests.swift
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@

import XCTest
import Verge
import os.lock

final class PipelineTests: XCTestCase {

func test_MapPipeline() {

let mapCounter = OSAllocatedUnfairLock<Int>.init(initialState: 0)
let mapCounter = VergeConcurrency.UnfairLockAtomic<Int>.init(0)

let pipeline = Pipelines.ChangesMapPipeline<DemoState, _, _>(
intermediate: {
$0
},
transform: {
mapCounter.withLock { $0 += 1 }
mapCounter.modify {
$0 += 1
}
return $0.name.count
},
additionalDropCondition: nil
Expand All @@ -33,7 +34,7 @@ final class PipelineTests: XCTestCase {
.noUpdates
)

XCTAssertEqual(mapCounter.withLock { $0 }, 0)
XCTAssertEqual(mapCounter.value, 0)
}

do {
Expand All @@ -48,22 +49,22 @@ final class PipelineTests: XCTestCase {
.noUpdates
)

XCTAssertEqual(mapCounter.withLock { $0 }, 2)
XCTAssertEqual(mapCounter.value , 2)

}

}

func test_MapPipeline_Intermediate() {

let mapCounter = OSAllocatedUnfairLock<Int>.init(initialState: 0)
let mapCounter = VergeConcurrency.UnfairLockAtomic<Int>.init(0)

let pipeline = Pipelines.ChangesMapPipeline<DemoState, _, _>(
intermediate: {
$0.name
},
transform: {
mapCounter.withLock { $0 += 1 }
mapCounter.modify { $0 += 1 }
return $0.count
},
additionalDropCondition: nil
Expand All @@ -82,7 +83,7 @@ final class PipelineTests: XCTestCase {
.noUpdates
)

XCTAssertEqual(mapCounter.withLock { $0 }, 0)
XCTAssertEqual(mapCounter.value, 0)
}

do {
Expand All @@ -97,7 +98,7 @@ final class PipelineTests: XCTestCase {
.noUpdates
)

XCTAssertEqual(mapCounter.withLock { $0 }, 0)
XCTAssertEqual(mapCounter.value, 0)

}

Expand Down
12 changes: 6 additions & 6 deletions Tests/VergeTests/VergeStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,10 @@ final class VergeStoreTests: XCTestCase {

let store = DemoStore()

let count = OSAllocatedUnfairLock<Int>.init(initialState: 0)
let count = VergeConcurrency.UnfairLockAtomic<Int>.init(0)

let subs = store.sinkState(queue: .passthrough) { (_) in
count.withLock { $0 += 1 }
count.modify { $0 += 1 }
}

XCTAssertEqual(store.state.version, 0)
Expand Down Expand Up @@ -256,7 +256,7 @@ final class VergeStoreTests: XCTestCase {
}

XCTAssertEqual(store.state.version, 2)
XCTAssertEqual(count.withLock { $0 }, 3)
XCTAssertEqual(count.value, 3)

withExtendedLifetime(subs, {})

Expand Down Expand Up @@ -308,10 +308,10 @@ final class VergeStoreTests: XCTestCase {
func testSubscription() {

var subscriptions = Set<AnyCancellable>()
let count = OSAllocatedUnfairLock<Int>.init(initialState: 0)
let count = VergeConcurrency.UnfairLockAtomic<Int>.init(0)

store.sinkState(queue: .passthrough) { (changes) in
count.withLock {
count.modify {
$0 += 1
}
}
Expand All @@ -328,7 +328,7 @@ final class VergeStoreTests: XCTestCase {
$0.markAsModified()
}

XCTAssertEqual(count.withLock { $0 }, 2)
XCTAssertEqual(count.value, 2)

}

Expand Down

0 comments on commit 30bcaf0

Please sign in to comment.