Skip to content

Commit

Permalink
fix #377 fix requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
zanzaben committed Dec 15, 2020
1 parent 5ad8b0e commit b18e9d3
Showing 1 changed file with 96 additions and 112 deletions.
208 changes: 96 additions & 112 deletions src/tests/select-test/select-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
*/

Expand All @@ -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;
Expand All @@ -45,22 +48,19 @@ 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 ************************************** */

char * fsAddrPtr = NULL;
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_mkfs(fsAddrPtr, "/ramdev0", "RAM", 512, 20);
OS_mount("/ramdev0", "/drive0");
OS_OpenCreate(&id, "/drive0/select_test.txt", OS_FILE_FLAG_CREATE, OS_READ_WRITE);
return id;
}


void BinSemSetup(void)
{
uint32 status;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
/*
Expand All @@ -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
Expand Down Expand Up @@ -339,24 +346,19 @@ 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){
/*
* 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
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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){
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -474,22 +461,19 @@ 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 */
UtAssert_True(actual == expected, "OS_SelectMultiple() (%ld) == OS_SUCCESS", (long)actual);

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;
Expand Down Expand Up @@ -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");
}

0 comments on commit b18e9d3

Please sign in to comment.