@@ -1106,13 +1106,13 @@ final class StoreContextTests: XCTestCase {
11061106
11071107 struct TestAtom2 : ValueAtom , Hashable {
11081108 func value( context: Context ) -> Int {
1109- context. watch ( TestAtom1 ( ) )
1109+ context. watch ( TestAtom1 ( ) ) * 2
11101110 }
11111111 }
11121112
11131113 struct TestAtom3 : ValueAtom , Hashable {
11141114 func value( context: Context ) -> Int {
1115- context. watch ( TestAtom1 ( ) )
1115+ context. watch ( TestAtom1 ( ) ) * 3
11161116 }
11171117 }
11181118
@@ -1145,7 +1145,7 @@ final class StoreContextTests: XCTestCase {
11451145 context. set ( 1 , for: TestAtom1 ( ) )
11461146 let value1 = context. read ( TestAtom4 ( ) )
11471147
1148- XCTAssertEqual ( value1, 3 )
1148+ XCTAssertEqual ( value1, 6 )
11491149 XCTAssertEqual ( updateCount, 1 )
11501150 }
11511151
@@ -1207,7 +1207,16 @@ final class StoreContextTests: XCTestCase {
12071207 }
12081208 }
12091209
1210- // Could be flaky.
1210+ struct TestAtom3 : ValueAtom , Hashable {
1211+ func value( context: Context ) -> Int {
1212+ context. watch ( TestAtom1 ( ) )
1213+ }
1214+ }
1215+
1216+ // Scenario:
1217+ // If two atoms with the same update source are subscribed to and
1218+ // a transitive update from one is skipped, from other will take effect.
1219+ // Flaky.
12111220 for _ in 0 ..< 100 {
12121221 let store = AtomStore ( )
12131222 let context = StoreContext ( store: store)
@@ -1216,14 +1225,14 @@ final class StoreContextTests: XCTestCase {
12161225
12171226 var updateCount = 0
12181227 let value0 = context. watch (
1219- TestAtom1 ( ) . changes,
1228+ TestAtom2 ( ) . changes,
12201229 subscriber: subscriber,
12211230 subscription: Subscription {
12221231 updateCount += 1
12231232 }
12241233 )
12251234 let value1 = context. watch (
1226- TestAtom2 ( ) ,
1235+ TestAtom3 ( ) ,
12271236 subscriber: subscriber,
12281237 subscription: Subscription {
12291238 updateCount += 1
@@ -1235,16 +1244,16 @@ final class StoreContextTests: XCTestCase {
12351244 XCTAssertEqual ( updateCount, 0 )
12361245
12371246 context. set ( 1 , for: TestAtom1 ( ) )
1238- let value2 = context. read ( TestAtom1 ( ) . changes)
1239- let value3 = context. read ( TestAtom2 ( ) )
1247+ let value2 = context. read ( TestAtom2 ( ) . changes)
1248+ let value3 = context. read ( TestAtom3 ( ) )
12401249
12411250 XCTAssertEqual ( value2, 1 )
12421251 XCTAssertEqual ( value3, 1 )
12431252 XCTAssertEqual ( updateCount, 1 )
12441253
12451254 context. set ( 1 , for: TestAtom1 ( ) )
1246- let value4 = context. read ( TestAtom1 ( ) . changes)
1247- let value5 = context. read ( TestAtom2 ( ) )
1255+ let value4 = context. read ( TestAtom2 ( ) . changes)
1256+ let value5 = context. read ( TestAtom3 ( ) )
12481257
12491258 XCTAssertEqual ( value4, 1 )
12501259 XCTAssertEqual ( value5, 1 )
@@ -1272,7 +1281,16 @@ final class StoreContextTests: XCTestCase {
12721281 }
12731282 }
12741283
1275- // Could be flaky.
1284+ struct TestAtom4 : ValueAtom , Hashable {
1285+ func value( context: Context ) -> Int {
1286+ context. watch ( TestAtom3 ( ) . changes) * 2
1287+ }
1288+ }
1289+
1290+ // Scenario:
1291+ // When all subscribing atoms skip transitive updates somewhere in their
1292+ // dependencies, the subscriber will not receive updates.
1293+ // Flaky.
12761294 for _ in 0 ..< 100 {
12771295 let store = AtomStore ( )
12781296 let context = StoreContext ( store: store)
@@ -1294,25 +1312,37 @@ final class StoreContextTests: XCTestCase {
12941312 updateCount += 1
12951313 }
12961314 )
1315+ let value2 = context. watch (
1316+ TestAtom4 ( ) ,
1317+ subscriber: subscriber,
1318+ subscription: Subscription {
1319+ updateCount += 1
1320+ }
1321+ )
12971322
12981323 XCTAssertEqual ( value0, 0 )
12991324 XCTAssertEqual ( value1, 0 )
1325+ XCTAssertEqual ( value2, 0 )
13001326 XCTAssertEqual ( updateCount, 0 )
13011327
13021328 context. set ( 1 , for: TestAtom1 ( ) )
1303- let value2 = context. read ( TestAtom2 ( ) . changes)
1304- let value3 = context. read ( TestAtom3 ( ) . changes)
1329+ let value3 = context. read ( TestAtom2 ( ) . changes)
1330+ let value4 = context. read ( TestAtom3 ( ) . changes)
1331+ let value5 = context. read ( TestAtom4 ( ) )
13051332
1306- XCTAssertEqual ( value2, 1 )
13071333 XCTAssertEqual ( value3, 1 )
1334+ XCTAssertEqual ( value4, 1 )
1335+ XCTAssertEqual ( value5, 2 )
13081336 XCTAssertEqual ( updateCount, 1 )
13091337
13101338 context. set ( 1 , for: TestAtom1 ( ) )
1311- let value4 = context. read ( TestAtom2 ( ) . changes)
1312- let value5 = context. read ( TestAtom3 ( ) . changes)
1339+ let value6 = context. read ( TestAtom2 ( ) . changes)
1340+ let value7 = context. read ( TestAtom3 ( ) . changes)
1341+ let value8 = context. read ( TestAtom4 ( ) )
13131342
1314- XCTAssertEqual ( value4, 1 )
1315- XCTAssertEqual ( value5, 1 )
1343+ XCTAssertEqual ( value6, 1 )
1344+ XCTAssertEqual ( value7, 1 )
1345+ XCTAssertEqual ( value8, 2 )
13161346 XCTAssertEqual ( updateCount, 1 )
13171347 }
13181348 }
0 commit comments