@@ -51,73 +51,64 @@ final class AsyncSequenceHookTests: XCTestCase {
5151 XCTAssertEqual ( hook. value ( context: context) . value, 100 )
5252 }
5353
54- func testUpdate( ) {
54+ func testUpdate( ) async {
5555 let pipe = AsyncThrowingStreamPipe < Int > ( )
5656 let hook = AsyncSequenceHook { _ in pipe. stream }
5757 let atom = TestAtom ( key: 0 , hook: hook)
5858 let context = AtomTestContext ( )
5959
60- XCTContext . runActivity ( named : " Initially suspending " ) { _ in
60+ do {
6161 XCTAssertTrue ( context. watch ( atom) . isSuspending)
6262 }
6363
64- XCTContext . runActivity ( named: " Value " ) { _ in
65- let expectation = expectation ( description: " Update " )
66- context. onUpdate = expectation. fulfill
64+ do {
65+ // Value
6766 pipe. continuation. yield ( 0 )
67+ await context. waitUntilNextUpdate ( )
6868
69- wait ( for: [ expectation] , timeout: 1 )
7069 XCTAssertEqual ( context. watch ( atom) . value, 0 )
7170 }
7271
73- XCTContext . runActivity ( named: " Error " ) { _ in
74- let expectation = expectation ( description: " Update " )
75- context. onUpdate = expectation. fulfill
72+ do {
73+ // Failure
7674 pipe. continuation. finish ( throwing: URLError ( . badURL) )
75+ await context. waitUntilNextUpdate ( )
7776
78- wait ( for: [ expectation] , timeout: 1 )
7977 XCTAssertEqual ( context. watch ( atom) . error as? URLError , URLError ( . badURL) )
8078 }
8179
82- XCTContext . runActivity ( named: " Value after finished " ) { _ in
83- let expectation = expectation ( description: " Update " )
84- expectation. isInverted = true
85- context. onUpdate = expectation. fulfill
80+ do {
81+ // Yield value after finish
8682 pipe. continuation. yield ( 1 )
83+ let didUpdate = await context. waitUntilNextUpdate ( timeout: 1 )
8784
88- wait ( for: [ expectation] , timeout: 1 )
89- XCTAssertEqual ( context. watch ( atom) . error as? URLError , URLError ( . badURL) )
85+ XCTAssertFalse ( didUpdate)
9086 }
9187
92- XCTContext . runActivity ( named : " Value after termination " ) { _ in
93- context . unwatch ( atom )
88+ do {
89+ // Yield value after termination
9490 pipe. reset ( )
95- context. watch ( atom)
9691 context. unwatch ( atom)
9792
98- let expectation = expectation ( description: " Update " )
99- expectation. isInverted = true
100- context. onUpdate = expectation. fulfill
10193 pipe. continuation. yield ( 0 )
94+ let didUpdate = await context. waitUntilNextUpdate ( timeout: 1 )
10295
103- wait ( for : [ expectation ] , timeout : 1 )
96+ XCTAssertFalse ( didUpdate )
10497 }
10598
106- XCTContext . runActivity ( named : " Error after termination " ) { _ in
107- context . unwatch ( atom )
99+ do {
100+ // Yield error after termination
108101 pipe. reset ( )
109- context. watch ( atom)
110102 context. unwatch ( atom)
111103
112- let expectation = expectation ( description: " Update " )
113- expectation. isInverted = true
114- context. onUpdate = expectation. fulfill
115104 pipe. continuation. finish ( throwing: URLError ( . badURL) )
105+ let didUpdate = await context. waitUntilNextUpdate ( timeout: 1 )
116106
117- wait ( for : [ expectation ] , timeout : 1 )
107+ XCTAssertFalse ( didUpdate )
118108 }
119109
120- XCTContext . runActivity ( named: " Override " ) { _ in
110+ do {
111+ // Override
121112 context. override ( atom) { _ in . success( 100 ) }
122113
123114 XCTAssertEqual ( context. watch ( atom) . value, 100 )
@@ -129,50 +120,60 @@ final class AsyncSequenceHookTests: XCTestCase {
129120 let hook = AsyncSequenceHook { _ in pipe. stream }
130121 let atom = TestAtom ( key: 0 , hook: hook)
131122 let context = AtomTestContext ( )
132- var updateCount = 0
133-
134- context. onUpdate = { updateCount += 1 }
135123
136- // Refresh
137-
138- XCTAssertTrue ( context. watch ( atom) . isSuspending)
139-
140- Task {
141- pipe. continuation. yield ( 0 )
142- pipe. continuation. finish ( throwing: nil )
124+ do {
125+ XCTAssertTrue ( context. watch ( atom) . isSuspending)
143126 }
144127
145- pipe. reset ( )
146- let phase0 = await context. refresh ( atom)
128+ do {
129+ // Refresh
130+ var updateCount = 0
131+ context. onUpdate = { updateCount += 1 }
132+ pipe. reset ( )
147133
148- XCTAssertEqual ( phase0. value, 0 )
149- XCTAssertEqual ( updateCount, 1 )
134+ Task {
135+ pipe. continuation. yield ( 0 )
136+ pipe. continuation. finish ( throwing: nil )
137+ }
150138
151- // Cancellation
139+ let phase = await context . refresh ( atom )
152140
153- let refreshTask = Task {
154- await context . refresh ( atom )
141+ XCTAssertEqual ( phase . value , 0 )
142+ XCTAssertEqual ( updateCount , 1 )
155143 }
156144
157- Task {
158- pipe. continuation. yield ( 1 )
159- refreshTask. cancel ( )
160- }
145+ do {
146+ // Cancellation
147+ var updateCount = 0
148+ context. onUpdate = { updateCount += 1 }
149+ pipe. reset ( )
161150
162- pipe. reset ( )
163- let phase1 = await refreshTask. value
151+ let refreshTask = Task {
152+ await context. refresh ( atom)
153+ }
164154
165- XCTAssertEqual ( phase1. value, 1 )
166- XCTAssertEqual ( updateCount, 2 )
155+ Task {
156+ pipe. continuation. yield ( 1 )
157+ refreshTask. cancel ( )
158+ }
167159
168- // Override
160+ let phase = await refreshTask . value
169161
170- context. override ( atom) { _ in . success( 200 ) }
162+ XCTAssertEqual ( phase. value, 1 )
163+ XCTAssertEqual ( updateCount, 1 )
164+ }
165+
166+ do {
167+ // Override
168+ var updateCount = 0
169+ context. onUpdate = { updateCount += 1 }
170+ context. override ( atom) { _ in . success( 200 ) }
171+ pipe. reset ( )
171172
172- pipe. reset ( )
173- let phase2 = await context. refresh ( atom)
173+ let phase = await context. refresh ( atom)
174174
175- XCTAssertEqual ( phase2. value, 200 )
176- XCTAssertEqual ( updateCount, 3 )
175+ XCTAssertEqual ( phase. value, 200 )
176+ XCTAssertEqual ( updateCount, 1 )
177+ }
177178 }
178179}
0 commit comments