From d0f312050397e5b10ce2fd74ebe1ccec6c05fd63 Mon Sep 17 00:00:00 2001 From: Alex Campbell Date: Tue, 15 Dec 2020 10:09:57 -0500 Subject: [PATCH] fix #377 fix requested changes --- src/tests/select-test/select-test.c | 204 +++++++++++++--------------- 1 file changed, 94 insertions(+), 110 deletions(-) diff --git a/src/tests/select-test/select-test.c b/src/tests/select-test/select-test.c index 80f99ce2d..036cd67b9 100644 --- a/src/tests/select-test/select-test.c +++ b/src/tests/select-test/select-test.c @@ -22,6 +22,8 @@ * Filename: select-test.c * * Purpose: This file contains functional tests for "osapi-select" + * Single select test will create a server and client to stream data between them and the select watches that stream. + * Multi select test will setup a second server and client also streaming data between them so that it can watch multiple streams. * */ @@ -33,6 +35,7 @@ #include "utassert.h" #include "uttest.h" #include "utbsp.h" +#define MAX_BUFFER_LOOP 1000000 osal_id_t s_task_id; osal_id_t s2_task_id; @@ -45,8 +48,6 @@ OS_SockAddr_t s2_addr; OS_SockAddr_t c_addr; OS_SockAddr_t c2_addr; osal_id_t bin_sem_id; -int max_buffer_loop = 1000000; - /* *************************************** MAIN ************************************** */ @@ -56,11 +57,10 @@ static osal_id_t setup_file(void) osal_id_t id; OS_mkfs(fsAddrPtr, "/ramdev3", "RAM3", 512, 20); OS_mount("/ramdev3", "/drive3"); - OS_OpenCreate(&id, "/drive3/select_test.txt", OS_FILE_FLAG_CREATE | OS_FILE_FLAG_TRUNCATE, OS_READ_ONLY); + OS_OpenCreate(&id, "/drive3/select_test.txt", OS_FILE_FLAG_CREATE, OS_READ_WRITE); return id; } - void BinSemSetup(void) { uint32 status; @@ -155,57 +155,6 @@ void Server_Fn(void) } /* end Server_Fn */ -void TestSelectSingleRead(void) -{ - /* - * Test Case For: - * int32 OS_SelectSingle(uint32 objid, uint32 *StateFlags, int32 msecs); - */ - int32 expected; - int32 actual; - - Setup_Server(); - Setup_Client(); - BinSemSetup(); - - expected = OS_SUCCESS; - - /* - * Create a server thread, and connect client from - * this thread to server thread and verify connection - */ - - /* Create a server task/thread */ - int32 status = OS_TaskCreate(&s_task_id, "Server", Server_Fn, OSAL_TASK_STACK_ALLOCATE, OSAL_SIZE_C(16384), - OSAL_PRIORITY_C(50), 0); - UtAssert_True(status == OS_SUCCESS, "OS_TaskCreate() (%ld) == OS_SUCCESS", (long)status); - - /* Connect to a server */ - actual = OS_SocketConnect(c_socket_id, &s_addr, 10); - UtAssert_True(actual == expected, "OS_SocketConnect() (%ld) == OS_SUCCESS", (long)actual); - - uint32 StateFlags; - expected = OS_ERROR_TIMEOUT; - StateFlags = OS_STREAM_STATE_READABLE; - actual = OS_SelectSingle(c_socket_id, &StateFlags, 100); - - /* Verify Outputs */ - UtAssert_True(actual == expected, "OS_SelectSingle() (%ld) == OS_ERROR_TIMEOUT", (long)actual); - UtAssert_True(StateFlags == 0, "OS_SelectSingle() (%d) == None", StateFlags); - - status = OS_BinSemGive(bin_sem_id); - - expected = OS_SUCCESS; - StateFlags = OS_STREAM_STATE_READABLE; - actual = OS_SelectSingle(c_socket_id, &StateFlags, 100); - - /* Verify Outputs */ - UtAssert_True(actual == expected, "OS_SelectSingle() (%ld) == OS_SUCCESS", (long)actual); - UtAssert_True(StateFlags == OS_STREAM_STATE_READABLE, "OS_SelectSingle() (%d) == OS_STREAM_STATE_READABLE", StateFlags); - - OS_BinSemDelete(bin_sem_id) ; -} - void Setup_Server2(void) { int32 expected; @@ -282,6 +231,72 @@ void Server_Fn2(void) } /* end Server_Fn */ +void Setup_Single(void){ + Setup_Server(); + Setup_Client(); + BinSemSetup(); +} + +void Setup_Multi(void){ + Setup_Single(); + Setup_Server2(); + Setup_Client2(); +} + +void Teardown_Single(void){ + OS_BinSemDelete(bin_sem_id) ; + OS_close(c_socket_id); +} + +void Teardown_Multi(void){ + OS_BinSemFlush(bin_sem_id); + OS_close(c2_socket_id); + Teardown_Single(); +} + +void TestSelectSingleRead(void) +{ + /* + * Test Case For: + * int32 OS_SelectSingle(uint32 objid, uint32 *StateFlags, int32 msecs); + */ + int32 expected = OS_SUCCESS; + int32 actual; + + /* + * Create a server thread, and connect client from + * this thread to server thread and verify connection + */ + + /* Create a server task/thread */ + int32 status = OS_TaskCreate(&s_task_id, "Server", Server_Fn, OSAL_TASK_STACK_ALLOCATE, OSAL_SIZE_C(16384), + OSAL_PRIORITY_C(50), 0); + UtAssert_True(status == OS_SUCCESS, "OS_TaskCreate() (%ld) == OS_SUCCESS", (long)status); + + /* Connect to a server */ + actual = OS_SocketConnect(c_socket_id, &s_addr, 10); + UtAssert_True(actual == expected, "OS_SocketConnect() (%ld) == OS_SUCCESS", (long)actual); + + uint32 StateFlags; + expected = OS_ERROR_TIMEOUT; + StateFlags = OS_STREAM_STATE_READABLE; + actual = OS_SelectSingle(c_socket_id, &StateFlags, 100); + + /* Verify Outputs */ + UtAssert_True(actual == expected, "OS_SelectSingle() (%ld) == OS_ERROR_TIMEOUT", (long)actual); + UtAssert_True(StateFlags == 0, "OS_SelectSingle() (%d) == None", StateFlags); + + status = OS_BinSemGive(bin_sem_id); + + expected = OS_SUCCESS; + StateFlags = OS_STREAM_STATE_READABLE; + actual = OS_SelectSingle(c_socket_id, &StateFlags, 100); + + /* Verify Outputs */ + UtAssert_True(actual == expected, "OS_SelectSingle() (%ld) == OS_SUCCESS", (long)actual); + UtAssert_True(StateFlags == OS_STREAM_STATE_READABLE, "OS_SelectSingle() (%d) == OS_STREAM_STATE_READABLE", StateFlags); +} + void TestSelectMultipleRead(void) { /* @@ -295,15 +310,7 @@ void TestSelectMultipleRead(void) int32 status; OS_SelectFdZero(&ReadSet); - OS_SelectFdZero(&WriteSet); - - Setup_Server(); - Setup_Client(); - Setup_Server2(); - Setup_Client2(); - BinSemSetup(); - - expected = OS_SUCCESS; + OS_SelectFdZero(&WriteSet); /* * Create a server thread, and connect client from @@ -339,9 +346,6 @@ void TestSelectMultipleRead(void) UtAssert_True(!OS_SelectFdIsSet(&ReadSet, c_socket_id), "OS_SelectFdIsSet(1) == false"); UtAssert_True(OS_SelectFdIsSet(&ReadSet, c2_socket_id), "OS_SelectFdIsSet(2) == true"); - - OS_BinSemFlush(bin_sem_id); - OS_BinSemDelete(bin_sem_id); } void TestSelectSingleWrite(void){ @@ -349,14 +353,12 @@ void TestSelectSingleWrite(void){ * Test Case For: * int32 OS_SelectSingle(uint32 objid, uint32 *StateFlags, int32 msecs); */ - int32 expected; - int32 actual; - - Setup_Server(); - Setup_Client(); - BinSemSetup(); - - expected = OS_SUCCESS; + + int32 actual; + uint32 StateFlags; + int32 expected = OS_SUCCESS; + int count = 0; + char Buf_send_c[16834] = {0}; /* * Create a server thread, and connect client from @@ -372,12 +374,7 @@ void TestSelectSingleWrite(void){ actual = OS_SocketConnect(c_socket_id, &s_addr, 10); UtAssert_True(actual == expected, "OS_SocketConnect() (%ld) == OS_SUCCESS", (long)actual); - uint32 StateFlags; - int count = 0; - char Buf_send_c[16834] = {0}; - expected = OS_ERROR_TIMEOUT; - - while (actual != OS_ERROR_TIMEOUT && count < max_buffer_loop) + while (actual != OS_ERROR_TIMEOUT && count < MAX_BUFFER_LOOP) { strcpy(Buf_send_c, "16 KB buffer filler"); actual = OS_TimedWrite(c_socket_id, Buf_send_c, sizeof(Buf_send_c), 10); @@ -390,9 +387,10 @@ void TestSelectSingleWrite(void){ status = OS_BinSemGive(bin_sem_id); - if(count >= max_buffer_loop){ - UtPrintf("Buffer size too large to test Select Single OS_STREAM_STATE_WRITABLE timeout\n"); + if(count >= MAX_BUFFER_LOOP){ + UtAssertEx(false, UTASSERT_CASETYPE_MIR, __FILE__, __LINE__, "%s", "Unable to cause OS_STREAM_STATE_WRITEABLE timeout with large looped writes, skipping verification"); }else{ + expected = OS_ERROR_TIMEOUT; /* Verify Outputs */ UtAssert_True(actual == expected, "OS_SelectSingle() (%ld) == OS_ERROR_TIMEOUT", (long)actual); UtAssert_True(StateFlags == 0, "OS_SelectSingle() (%d) == None", StateFlags); @@ -405,8 +403,6 @@ void TestSelectSingleWrite(void){ UtAssert_True(actual == expected, "OS_SelectSingle() (%ld) == OS_SUCCESS", (long)actual); UtAssert_True(StateFlags == OS_STREAM_STATE_WRITABLE, "OS_SelectSingle() (%d) == OS_STREAM_STATE_WRITABLE", StateFlags); } - - OS_BinSemDelete(bin_sem_id) ; } void TestSelectMultipleWrite(void){ @@ -419,18 +415,13 @@ void TestSelectMultipleWrite(void){ int32 expected = OS_SUCCESS; int32 actual; int32 status; + uint32 StateFlags; + int count = 0; + char Buf_send_c[16834] = {0}; OS_SelectFdZero(&ReadSet); OS_SelectFdZero(&WriteSet); - Setup_Server(); - Setup_Client(); - Setup_Server2(); - Setup_Client2(); - BinSemSetup(); - - expected = OS_SUCCESS; - /* * Create a server thread, and connect client from * this thread to server thread and verify connection @@ -459,11 +450,7 @@ void TestSelectMultipleWrite(void){ UtAssert_True(OS_SelectFdIsSet(&WriteSet, c_socket_id), "OS_SelectFdIsSet(1) == true"); UtAssert_True(OS_SelectFdIsSet(&WriteSet, c2_socket_id), "OS_SelectFdIsSet(1) == true"); - uint32 StateFlags; - int count = 0; - char Buf_send_c[16834] = {0}; - - while (actual != OS_ERROR_TIMEOUT && count < max_buffer_loop) + while (actual != OS_ERROR_TIMEOUT && count < MAX_BUFFER_LOOP) { strcpy(Buf_send_c, "16 KB buffer filler"); actual = OS_TimedWrite(c_socket_id, Buf_send_c, sizeof(Buf_send_c), 10); @@ -474,8 +461,8 @@ void TestSelectMultipleWrite(void){ count++; } - if(count >= max_buffer_loop){ - UtPrintf("Buffer size too large to test Select Multiple OS_STREAM_STATE_WRITABLE\n"); + if(count >= MAX_BUFFER_LOOP){ + UtAssertEx(false, UTASSERT_CASETYPE_MIR, __FILE__, __LINE__, "%s", "Unable to cause OS_STREAM_STATE_WRITEABLE timeout with large looped writes, skipping verification"); }else{ actual = OS_SelectMultiple(&ReadSet, &WriteSet, 100); /* Verify Outputs */ @@ -483,13 +470,10 @@ void TestSelectMultipleWrite(void){ UtAssert_True(!OS_SelectFdIsSet(&WriteSet, c_socket_id), "OS_SelectFdIsSet(1) == false"); UtAssert_True(OS_SelectFdIsSet(&WriteSet, c2_socket_id), "OS_SelectFdIsSet(2) == true"); - } - - OS_BinSemFlush(bin_sem_id); - OS_BinSemDelete(bin_sem_id); + } } -void TestSingleSelectFile(void) +void TestSelectSingleFile(void) { int32 expected = OS_SUCCESS; int32 actual; @@ -531,9 +515,9 @@ void UtTest_Setup(void) * Register the test setup and check routines in UT assert */ - UtTest_Add(TestSelectSingleRead, NULL, NULL, "TestSelectSingleRead"); - UtTest_Add(TestSelectMultipleRead, NULL, NULL, "TestSelectMultipleRead"); - UtTest_Add(TestSelectSingleWrite, NULL, NULL, "TestSelectSingleWrite"); - UtTest_Add(TestSelectMultipleWrite, NULL, NULL, "TestSelectMultipleWrite"); - UtTest_Add(TestSingleSelectFile, NULL, NULL, "TestSingleSelectFile"); + UtTest_Add(TestSelectSingleRead, Setup_Single, Teardown_Single, "TestSelectSingleRead"); + UtTest_Add(TestSelectMultipleRead, Setup_Multi, Teardown_Multi, "TestSelectMultipleRead"); + UtTest_Add(TestSelectSingleWrite, Setup_Single, Teardown_Single, "TestSelectSingleWrite"); + UtTest_Add(TestSelectMultipleWrite, Setup_Multi, Teardown_Multi, "TestSelectMultipleWrite"); + UtTest_Add(TestSelectSingleFile, NULL, NULL, "TestSelectSingleFile"); } \ No newline at end of file