mike-jumper commented on code in PR #614: URL: https://github.com/apache/guacamole-server/pull/614#discussion_r2425726387
########## src/libguac/tests/thread-local/thread_local_storage_error_isolation.c: ########## @@ -0,0 +1,295 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include <CUnit/CUnit.h> +#include <guacamole/error.h> + +#include <stdlib.h> +#include <pthread.h> +#include <string.h> +#include <unistd.h> + +/** + * Thread function that sets different error. + */ +static void* isolation_thread_func(void* arg) { + (void)arg; /* Unused parameter */ + + /* Initially should have no error in new thread */ + CU_ASSERT_EQUAL(guac_error, GUAC_STATUS_SUCCESS); + CU_ASSERT_PTR_NULL(guac_error_message); + + /* Set different error */ + guac_error = GUAC_STATUS_INVALID_ARGUMENT; + guac_error_message = "Thread error"; + + /* Verify error is set correctly */ + CU_ASSERT_EQUAL(guac_error, GUAC_STATUS_INVALID_ARGUMENT); + CU_ASSERT_STRING_EQUAL(guac_error_message, "Thread error"); + + return NULL; +} + +/** + * Test which verifies error isolation between threads. + */ +void test_thread_local_storage__error_isolation() { + /* Set an error in main thread */ + guac_error = GUAC_STATUS_NO_MEMORY; + guac_error_message = "Main thread error"; + + /* Verify error is set */ + CU_ASSERT_EQUAL(guac_error, GUAC_STATUS_NO_MEMORY); + CU_ASSERT_STRING_EQUAL(guac_error_message, "Main thread error"); + + /* Create a thread that sets different error */ + pthread_t thread; + + CU_ASSERT_EQUAL(pthread_create(&thread, NULL, isolation_thread_func, NULL), 0); + CU_ASSERT_EQUAL(pthread_join(thread, NULL), 0); + + /* Main thread error should be unchanged */ + CU_ASSERT_EQUAL(guac_error, GUAC_STATUS_NO_MEMORY); + CU_ASSERT_STRING_EQUAL(guac_error_message, "Main thread error"); + + /* Reset error */ + guac_error = GUAC_STATUS_SUCCESS; + guac_error_message = NULL; +} + +/** + * Structure for passing data to worker threads. + */ +typedef struct error_test_data { + int thread_id; + guac_status expected_error; + const char* expected_message; + volatile int* results; +} error_test_data_t; + +/** + * Worker function for multi-threaded error isolation test. + */ +static void* error_isolation_worker(void* arg) { + error_test_data_t* data = (error_test_data_t*)arg; + int id = data->thread_id; + + /* Each thread should start with clean error state */ + if (guac_error != GUAC_STATUS_SUCCESS || guac_error_message != NULL) { + data->results[id] = 1; /* Failure */ + return NULL; + } + + /* Set unique error for this thread */ + guac_error = data->expected_error; + guac_error_message = data->expected_message; + + /* Removed usleep - it doesn't actually increase race condition probability */ Review Comment: @orange-guo, just want to check real quick: was an AI used to generate the changes in this PR? If so, to what degree? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
