Improve thread-safety

This commit is contained in:
Riyyi
2026-08-21 17:29:44 +02:00
parent 114aee655c
commit 9437e8d97d
3 changed files with 15 additions and 15 deletions
+1 -1
View File
@@ -68,7 +68,7 @@ compute_chunk_path :: proc(
err: Error, err: Error,
) { ) {
@(static) buf: [20]u8 buf: [20]u8 // not @(static), that would make it shared across all threads!
chunk_index_str := strconv.write_int(buf[:], cast(i64)chunk_index, 10) chunk_index_str := strconv.write_int(buf[:], cast(i64)chunk_index, 10)
chunk_name := strings.concatenate({CHUNK, chunk_index_str}, allocator) chunk_name := strings.concatenate({CHUNK, chunk_index_str}, allocator)
+10 -10
View File
@@ -21,15 +21,19 @@ Test_State :: struct {
test_state_init :: proc( test_state_init :: proc(
t: ^testing.T, t: ^testing.T,
name: string,
allocator := context.allocator, allocator := context.allocator,
) -> Test_State { ) -> Test_State {
ensure_dir(t, TEST_INPUT_DIR) ensure_dir(t, TEST_INPUT_DIR)
ensure_dir(t, TEST_OUTPUT_DIR) ensure_dir(t, TEST_OUTPUT_DIR)
temp_input_path, err := os.mkdir_temp(TEST_INPUT_DIR, "", allocator) // Use the test name as the pattern so parallel tests with the same
// RNG seed generate distinct temp directory names.
temp_input_path, err := os.mkdir_temp(TEST_INPUT_DIR, name, allocator)
testing.expect(t, err == nil, os.error_string(err)) testing.expect(t, err == nil, os.error_string(err))
temp_output_path, err2 := os.mkdir_temp(TEST_OUTPUT_DIR, "", allocator) temp_output_path, err2 := os.mkdir_temp(TEST_OUTPUT_DIR, name, allocator)
testing.expect(t, err2 == nil, os.error_string(err2)) testing.expect(t, err2 == nil, os.error_string(err2))
return Test_State { return Test_State {
@@ -59,10 +63,8 @@ test_state_destroy :: proc(
// ----------------------------------------- // -----------------------------------------
ensure_dir :: proc(t: ^testing.T, dir: string) { ensure_dir :: proc(t: ^testing.T, dir: string) {
if !os.exists(dir) { err := os.make_directory_all(dir)
err := os.make_directory_all(dir) testing.expect(t, err == nil || err == .Exist, os.error_string(err)) // TOCTOU safe
testing.expect(t, err == nil, os.error_string(err))
}
} }
create_file :: proc( create_file :: proc(
@@ -77,10 +79,8 @@ create_file :: proc(
defer delete(relpath, allocator) defer delete(relpath, allocator)
dir := os.dir(relpath) dir := os.dir(relpath)
if !os.exists(dir) { err2 := os.make_directory_all(dir)
err2 := os.make_directory_all(dir) testing.expect(t, err2 == nil || err2 == .Exist, os.error_string(err2)) // TOCTOU safe
testing.expect(t, err2 == nil, os.error_string(err2))
}
file, err3 := os.create(relpath) file, err3 := os.create(relpath)
testing.expect(t, err3 == nil, os.error_string(err3)) testing.expect(t, err3 == nil, os.error_string(err3))
+4 -4
View File
@@ -6,7 +6,7 @@ import "core:testing"
single_tiny_file_read_from_chunk :: proc(t: ^testing.T) { single_tiny_file_read_from_chunk :: proc(t: ^testing.T) {
// Arrange // Arrange
ts := test_state_init(t) ts := test_state_init(t, #procedure)
defer test_state_destroy(&ts, t) defer test_state_destroy(&ts, t)
delete_path(&ts, t, "") delete_path(&ts, t, "")
@@ -27,7 +27,7 @@ single_tiny_file_read_from_chunk :: proc(t: ^testing.T) {
single_file_single_chunk_read_from_path :: proc(t: ^testing.T) { single_file_single_chunk_read_from_path :: proc(t: ^testing.T) {
// Arrange // Arrange
ts := test_state_init(t) ts := test_state_init(t, #procedure)
defer test_state_destroy(&ts, t) defer test_state_destroy(&ts, t)
delete_path(&ts, t, "") delete_path(&ts, t, "")
@@ -49,7 +49,7 @@ single_file_single_chunk_read_from_path :: proc(t: ^testing.T) {
multiple_files_single_chunk_read_from_chunk :: proc(t: ^testing.T) { multiple_files_single_chunk_read_from_chunk :: proc(t: ^testing.T) {
// Arrange // Arrange
ts := test_state_init(t) ts := test_state_init(t, #procedure)
defer test_state_destroy(&ts, t) defer test_state_destroy(&ts, t)
delete_path(&ts, t, "") delete_path(&ts, t, "")
@@ -76,7 +76,7 @@ multiple_files_single_chunk_read_from_chunk :: proc(t: ^testing.T) {
multiple_files_single_chunk_read_from_path :: proc(t: ^testing.T) { multiple_files_single_chunk_read_from_path :: proc(t: ^testing.T) {
// Arrange // Arrange
ts := test_state_init(t) ts := test_state_init(t, #procedure)
defer test_state_destroy(&ts, t) defer test_state_destroy(&ts, t)
delete_path(&ts, t, "") delete_path(&ts, t, "")