Skip to content

Commit

Permalink
Make it commit order and receive order are same in concurrent #101
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii committed Apr 30, 2020
1 parent 2278973 commit 0f9482c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
8 changes: 6 additions & 2 deletions Sources/VergeCore/Storage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,17 @@ open class Storage<Value>: ReadonlyStorage<Value> {

lock()
do {

let r = try update(&nonatomicValue)
let notifyValue = nonatomicValue
unlock()
// TODO: cause cracking the order of event

if notificationFilter(notifyValue) {
// notify the event with locking
// it may cause dead-lock in somecase
notifyDidUpdate(value: notifyValue)
}

unlock()
return r
} catch {
unlock()
Expand Down
10 changes: 4 additions & 6 deletions Tests/VergeStoreTests/VergeStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -305,15 +305,13 @@ final class VergeStoreTests: XCTestCase {
}

func testOrderOfEvents() {

// Currently, it's collapsed because Storage emits event without locking.


let store = VergeStore.Store<DemoState, Never>(initialState: .init(), logger: nil)

let exp = expectation(description: "")
let counter = expectation(description: "update count")
counter.assertForOverFulfill = true
counter.expectedFulfillmentCount = 1001
counter.expectedFulfillmentCount = 10001

let results: VergeConcurrency.Atomic<[Int]> = .init([])

Expand All @@ -325,7 +323,7 @@ final class VergeStoreTests: XCTestCase {
}

DispatchQueue.global().async {
DispatchQueue.concurrentPerform(iterations: 1000) { (i) in
DispatchQueue.concurrentPerform(iterations: 10000) { (i) in
store.commit {
$0.count += 1
}
Expand All @@ -335,7 +333,7 @@ final class VergeStoreTests: XCTestCase {
}

wait(for: [exp, counter], timeout: 10)
XCTAssertEqual(Array((0...1000).map { $0 }), results.value)
XCTAssertEqual(Array((0...10000).map { $0 }), results.value)
withExtendedLifetime(sub) {}
}

Expand Down
3 changes: 0 additions & 3 deletions Verge.xcodeproj/xcshareddata/xcschemes/LightAll.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,6 @@
<Test
Identifier = "VergeStoreTests/testDeadlock()">
</Test>
<Test
Identifier = "VergeStoreTests/testOrderOfEvents()">
</Test>
</SkippedTests>
</TestableReference>
<TestableReference
Expand Down

0 comments on commit 0f9482c

Please sign in to comment.