@@ -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,7 @@ 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 ( )
201
202
}
202
203
203
204
func testJSValueDecoderOnWorker( ) async throws {
@@ -211,10 +212,10 @@ final class WebWorkerTaskExecutorTests: XCTestCase {
211
212
let prop_3 : Bool
212
213
let prop_7 : Float
213
214
let prop_8 : String
215
+ let prop_9 : [ String ]
214
216
}
215
217
216
- let executor = try await WebWorkerTaskExecutor ( numberOfThreads: 1 )
217
- let task = Task ( executorPreference: executor) {
218
+ func decodeJob( ) throws {
218
219
let json = """
219
220
{
220
221
" prop_1 " : {
@@ -223,20 +224,46 @@ final class WebWorkerTaskExecutorTests: XCTestCase {
223
224
" prop_2 " : 100,
224
225
" prop_3 " : true,
225
226
" prop_7 " : 3.14,
226
- " prop_8 " : " Hello, World! "
227
+ " prop_8 " : " Hello, World! " ,
228
+ " prop_9 " : [ " a " , " b " , " c " ]
227
229
}
228
230
"""
229
231
let object = JSObject . global. JSON. parse ( json)
230
232
let decoder = JSValueDecoder ( )
231
- let decoded = try decoder. decode ( DecodeMe . self, from: object)
232
- return decoded
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 )
233
260
}
234
- let result = try await task . value
235
- XCTAssertEqual ( result . prop_1 . nested_prop , 42 )
236
- XCTAssertEqual ( result . prop_2 , 100 )
237
- XCTAssertEqual ( result . prop_3 , true )
238
- XCTAssertEqual ( result . prop_7 , 3.14 )
239
- XCTAssertEqual ( result . prop_8 , " Hello, World! " )
261
+ check ( )
262
+ let task = Task ( executorPreference : executor ) {
263
+ check ( )
264
+ }
265
+ await task . value
266
+ executor . terminate ( )
240
267
}
241
268
242
269
/*
0 commit comments