@@ -38,6 +38,7 @@ final class WebWorkerTaskExecutorTests: XCTestCase {
3838
3939 func testAwaitInsideTask( ) async throws {
4040 let executor = try await WebWorkerTaskExecutor ( numberOfThreads: 1 )
41+ defer { executor. terminate ( ) }
4142
4243 let task = Task ( executorPreference: executor) {
4344 await Task . yield ( )
@@ -46,8 +47,6 @@ final class WebWorkerTaskExecutorTests: XCTestCase {
4647 }
4748 let taskRunOnMainThread = try await task. value
4849 XCTAssertFalse ( taskRunOnMainThread)
49-
50- executor. terminate ( )
5150 }
5251
5352 func testSleepInsideTask( ) async throws {
@@ -170,6 +169,7 @@ final class WebWorkerTaskExecutorTests: XCTestCase {
170169 let result = await task. value
171170 XCTAssertEqual ( result, 100 )
172171 XCTAssertEqual ( Check . value, 42 )
172+ executor. terminate ( )
173173 }
174174
175175 func testLazyThreadLocalPerThreadInitialization( ) async throws {
@@ -198,6 +198,72 @@ final class WebWorkerTaskExecutorTests: XCTestCase {
198198 let result = await task. value
199199 XCTAssertEqual ( result, 100 )
200200 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 ( )
201267 }
202268
203269/*
0 commit comments