@@ -38,6 +38,7 @@ final class WebWorkerTaskExecutorTests: XCTestCase {
38
38
39
39
func testAwaitInsideTask( ) async throws {
40
40
let executor = try await WebWorkerTaskExecutor ( numberOfThreads: 1 )
41
+ defer { executor. terminate ( ) }
41
42
42
43
let task = Task ( executorPreference: executor) {
43
44
await Task . yield ( )
@@ -46,8 +47,6 @@ final class WebWorkerTaskExecutorTests: XCTestCase {
46
47
}
47
48
let taskRunOnMainThread = try await task. value
48
49
XCTAssertFalse ( taskRunOnMainThread)
49
-
50
- executor. terminate ( )
51
50
}
52
51
53
52
func testSleepInsideTask( ) async throws {
@@ -170,6 +169,7 @@ final class WebWorkerTaskExecutorTests: XCTestCase {
170
169
let result = await task. value
171
170
XCTAssertEqual ( result, 100 )
172
171
XCTAssertEqual ( Check . value, 42 )
172
+ executor. terminate ( )
173
173
}
174
174
175
175
func testLazyThreadLocalPerThreadInitialization( ) async throws {
@@ -198,6 +198,72 @@ final class WebWorkerTaskExecutorTests: XCTestCase {
198
198
let result = await task. value
199
199
XCTAssertEqual ( result, 100 )
200
200
XCTAssertEqual ( Check . countOfInitialization, 2 )
201
+ executor. terminate ( )
202
+ }
203
+
204
+ func testJSValueDecoderOnWorker( ) async throws {
205
+ struct DecodeMe : Codable {
206
+ struct Prop1 : Codable {
207
+ let nested_prop : Int
208
+ }
209
+
210
+ let prop_1 : Prop1
211
+ let prop_2 : Int
212
+ let prop_3 : Bool
213
+ let prop_7 : Float
214
+ let prop_8 : String
215
+ let prop_9 : [ String ]
216
+ }
217
+
218
+ func decodeJob( ) throws {
219
+ let json = """
220
+ {
221
+ " prop_1 " : {
222
+ " nested_prop " : 42
223
+ },
224
+ " prop_2 " : 100,
225
+ " prop_3 " : true,
226
+ " prop_7 " : 3.14,
227
+ " prop_8 " : " Hello, World! " ,
228
+ " prop_9 " : [ " a " , " b " , " c " ]
229
+ }
230
+ """
231
+ let object = JSObject . global. JSON. parse ( json)
232
+ let decoder = JSValueDecoder ( )
233
+ let result = try decoder. decode ( DecodeMe . self, from: object)
234
+ XCTAssertEqual ( result. prop_1. nested_prop, 42 )
235
+ XCTAssertEqual ( result. prop_2, 100 )
236
+ XCTAssertEqual ( result. prop_3, true )
237
+ XCTAssertEqual ( result. prop_7, 3.14 )
238
+ XCTAssertEqual ( result. prop_8, " Hello, World! " )
239
+ XCTAssertEqual ( result. prop_9, [ " a " , " b " , " c " ] )
240
+ }
241
+ // Run the job on the main thread first to initialize the object cache
242
+ try decodeJob ( )
243
+
244
+ let executor = try await WebWorkerTaskExecutor ( numberOfThreads: 1 )
245
+ defer { executor. terminate ( ) }
246
+ let task = Task ( executorPreference: executor) {
247
+ // Run the job on the worker thread to test the object cache
248
+ // is not shared with the main thread
249
+ try decodeJob ( )
250
+ }
251
+ try await task. value
252
+ }
253
+
254
+ func testJSArrayCountOnWorker( ) async throws {
255
+ let executor = try await WebWorkerTaskExecutor ( numberOfThreads: 1 )
256
+ func check( ) {
257
+ let object = JSObject . global. Array. function!. new ( 1 , 2 , 3 , 4 , 5 )
258
+ let array = JSArray ( object) !
259
+ XCTAssertEqual ( array. count, 5 )
260
+ }
261
+ check ( )
262
+ let task = Task ( executorPreference: executor) {
263
+ check ( )
264
+ }
265
+ await task. value
266
+ executor. terminate ( )
201
267
}
202
268
203
269
/*
0 commit comments