]> git.droids-corp.org - dpdk.git/commitdiff
test/threads: add unit test for get/set priority
authorTyler Retzlaff <roretzla@linux.microsoft.com>
Tue, 24 May 2022 11:08:37 +0000 (04:08 -0700)
committerDavid Marchand <david.marchand@redhat.com>
Tue, 7 Jun 2022 11:33:14 +0000 (13:33 +0200)
Add unit tests to exercise and demonstrate rte_thread_{get,set}_priority().

Signed-off-by: Tyler Retzlaff <roretzla@linux.microsoft.com>
app/test/test_threads.c

index e1a2ea5be1041c6bbfc469b19f63617d6348228c..b9d8b4e97e1dd5658f4fbbf8d7c155bbd6d112db 100644 (file)
@@ -20,9 +20,64 @@ thread_main(void *arg)
        *(rte_thread_t *)arg = rte_thread_self();
        __atomic_store_n(&thread_id_ready, 1, __ATOMIC_RELEASE);
 
+       while (__atomic_load_n(&thread_id_ready, __ATOMIC_ACQUIRE) == 1)
+               ;
+
        return NULL;
 }
 
+static int
+test_thread_priority(void)
+{
+       pthread_t id;
+       rte_thread_t thread_id;
+       enum rte_thread_priority priority;
+
+       thread_id_ready = 0;
+       RTE_TEST_ASSERT(pthread_create(&id, NULL, thread_main, &thread_id) == 0,
+               "Failed to create thread");
+
+       while (__atomic_load_n(&thread_id_ready, __ATOMIC_ACQUIRE) == 0)
+               ;
+
+       priority = RTE_THREAD_PRIORITY_NORMAL;
+       RTE_TEST_ASSERT(rte_thread_set_priority(thread_id, priority) == 0,
+               "Failed to set thread priority");
+       RTE_TEST_ASSERT(rte_thread_get_priority(thread_id, &priority) == 0,
+               "Failed to get thread priority");
+       RTE_TEST_ASSERT(priority == RTE_THREAD_PRIORITY_NORMAL,
+               "Priority set mismatches priority get");
+
+       priority = RTE_THREAD_PRIORITY_REALTIME_CRITICAL;
+#ifndef RTE_EXEC_ENV_WINDOWS
+       RTE_TEST_ASSERT(rte_thread_set_priority(thread_id, priority) == ENOTSUP,
+               "Priority set to critical should fail");
+       RTE_TEST_ASSERT(rte_thread_get_priority(thread_id, &priority) == 0,
+               "Failed to get thread priority");
+       RTE_TEST_ASSERT(priority == RTE_THREAD_PRIORITY_NORMAL,
+               "Failed set to critical should have retained normal");
+#else
+       RTE_TEST_ASSERT(rte_thread_set_priority(thread_id, priority) == 0,
+               "Priority set to critical should succeed");
+       RTE_TEST_ASSERT(rte_thread_get_priority(thread_id, &priority) == 0,
+               "Failed to get thread priority");
+       RTE_TEST_ASSERT(priority == RTE_THREAD_PRIORITY_REALTIME_CRITICAL,
+               "Priority set mismatches priority get");
+#endif
+
+       priority = RTE_THREAD_PRIORITY_NORMAL;
+       RTE_TEST_ASSERT(rte_thread_set_priority(thread_id, priority) == 0,
+               "Failed to set thread priority");
+       RTE_TEST_ASSERT(rte_thread_get_priority(thread_id, &priority) == 0,
+               "Failed to get thread priority");
+       RTE_TEST_ASSERT(priority == RTE_THREAD_PRIORITY_NORMAL,
+               "Priority set mismatches priority get");
+
+       __atomic_store_n(&thread_id_ready, 2, __ATOMIC_RELEASE);
+
+       return 0;
+}
+
 static int
 test_thread_affinity(void)
 {
@@ -31,6 +86,7 @@ test_thread_affinity(void)
        rte_cpuset_t cpuset0;
        rte_cpuset_t cpuset1;
 
+       thread_id_ready = 0;
        RTE_TEST_ASSERT(pthread_create(&id, NULL, thread_main, &thread_id) == 0,
                "Failed to create thread");
 
@@ -68,6 +124,7 @@ static struct unit_test_suite threads_test_suite = {
        .teardown = NULL,
        .unit_test_cases = {
                TEST_CASE(test_thread_affinity),
+               TEST_CASE(test_thread_priority),
                TEST_CASES_END()
        }
 };