Skip to content

Commit

Permalink
Add more tests for ThreadLocal and LazyThreadLocal
Browse files Browse the repository at this point in the history
  • Loading branch information
kateinoigakukun committed Nov 28, 2024
1 parent 9a141cb commit 49b207a
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
51 changes: 50 additions & 1 deletion Tests/JavaScriptEventLoopTests/WebWorkerTaskExecutorTests.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#if compiler(>=6.1) && _runtime(_multithreaded)
import XCTest
import JavaScriptKit
import _CJavaScriptKit // For swjs_get_worker_thread_id
@testable import JavaScriptKit
@testable import JavaScriptEventLoop

@_extern(wasm, module: "JavaScriptEventLoopTestSupportTests", name: "isMainThread")
Expand Down Expand Up @@ -151,6 +151,55 @@ final class WebWorkerTaskExecutorTests: XCTestCase {
executor.terminate()
}

func testThreadLocalPerThreadValues() async throws {
struct Check {
@ThreadLocal(boxing: ())
static var value: Int?
}
let executor = try await WebWorkerTaskExecutor(numberOfThreads: 1)
XCTAssertNil(Check.value)
Check.value = 42
XCTAssertEqual(Check.value, 42)

let task = Task(executorPreference: executor) {
XCTAssertEqual(Check.value, nil)
Check.value = 100
XCTAssertEqual(Check.value, 100)
return Check.value
}
let result = await task.value
XCTAssertEqual(result, 100)
XCTAssertEqual(Check.value, 42)
}

func testLazyThreadLocalPerThreadInitialization() async throws {
struct Check {
static var valueToInitialize = 42
static var countOfInitialization = 0
@LazyThreadLocal(initialize: {
countOfInitialization += 1
return valueToInitialize
})
static var value: Int
}
let executor = try await WebWorkerTaskExecutor(numberOfThreads: 1)
XCTAssertEqual(Check.countOfInitialization, 0)
XCTAssertEqual(Check.value, 42)
XCTAssertEqual(Check.countOfInitialization, 1)

Check.valueToInitialize = 100

let task = Task(executorPreference: executor) {
XCTAssertEqual(Check.countOfInitialization, 1)
XCTAssertEqual(Check.value, 100)
XCTAssertEqual(Check.countOfInitialization, 2)
return Check.value
}
let result = await task.value
XCTAssertEqual(result, 100)
XCTAssertEqual(Check.countOfInitialization, 2)
}

/*
func testDeinitJSObjectOnDifferentThread() async throws {
let executor = try await WebWorkerTaskExecutor(numberOfThreads: 1)
Expand Down
16 changes: 16 additions & 0 deletions Tests/JavaScriptKitTests/ThreadLocalTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ import XCTest

final class ThreadLocalTests: XCTestCase {
class MyHeapObject {}
struct MyStruct {
var object: MyHeapObject
}

func testLeak() throws {
struct Check {
@ThreadLocal
var value: MyHeapObject?
@ThreadLocal(boxing: ())
var value2: MyStruct?
}
weak var weakObject: MyHeapObject?
do {
Expand All @@ -19,6 +24,17 @@ final class ThreadLocalTests: XCTestCase {
XCTAssertTrue(check.value === object)
}
XCTAssertNil(weakObject)

weak var weakObject2: MyHeapObject?
do {
let object = MyHeapObject()
weakObject2 = object
let check = Check()
check.value2 = MyStruct(object: object)
XCTAssertNotNil(check.value2)
XCTAssertTrue(check.value2!.object === object)
}
XCTAssertNil(weakObject2)
}

func testLazyThreadLocal() throws {
Expand Down

0 comments on commit 49b207a

Please sign in to comment.