summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorSteve Huston <shuston@riverace.com>2002-11-11 17:53:02 +0000
committerSteve Huston <shuston@riverace.com>2002-11-11 17:53:02 +0000
commit75b26599e94b3b6224af545e2486812dd31f0013 (patch)
tree0649241ce23b9130551a3b88e0faee85cebab9cc /tests
parent3be7344c73865dc677a294c55bf5937cbaf0857e (diff)
downloadATCD-75b26599e94b3b6224af545e2486812dd31f0013.tar.gz
ChangeLogTag:Mon Nov 11 12:39:18 2002 Steve Huston <shuston@riverace.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/FIFO_Test.cpp263
-rw-r--r--tests/Makefile1
-rw-r--r--tests/run_test.lst1
3 files changed, 265 insertions, 0 deletions
diff --git a/tests/FIFO_Test.cpp b/tests/FIFO_Test.cpp
new file mode 100644
index 00000000000..35315fad2a9
--- /dev/null
+++ b/tests/FIFO_Test.cpp
@@ -0,0 +1,263 @@
+// $Id$
+
+// ============================================================================
+//
+// = LIBRARY
+// tests
+//
+// = FILENAME
+// FIFO_Test.cpp
+//
+// = DESCRIPTION
+// This is a test of basic ACE_FIFO_* class functionality.
+// The test forks two processes or spawns two threads (depending
+// upon the platform) and then tests various data exchange
+// scenarios.
+//
+// = AUTHOR
+// Steve Huston <shuston@riverace.com>
+//
+// ============================================================================
+
+#include "test_config.h"
+#include "ace/OS.h"
+#include "ace/ACE.h"
+#include "ace/FIFO_Send_Msg.h"
+#include "ace/FIFO_Recv_Msg.h"
+#include "ace/Handle_Set.h"
+#include "ace/Lib_Find.h"
+#include "ace/Thread.h"
+#include "ace/Thread_Manager.h"
+
+ACE_RCSID(tests, SOCK_Test, "$Id$")
+
+static const ACE_TCHAR ACE_ALPHABET[] =
+ ACE_TEXT ("abcdefghijklmnopqrstuvwxyz");
+
+static void *
+client (void *arg)
+{
+ ACE_TCHAR *fifo_path = ACE_reinterpret_cast (ACE_TCHAR *, arg);
+ ACE_FIFO_Send_Msg fifo;
+ ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P|%t) client opening %s\n"), fifo_path));
+ if (fifo.open (fifo_path) == -1)
+ ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("(%P|%t) %p\n"), fifo_path), 0);
+
+ // Try some transfers - the server part is expecting this data.
+ // First, try a nice, easy send.
+ ssize_t send_count;
+ ssize_t expect = ACE_static_cast (ssize_t, ACE_OS::strlen (ACE_ALPHABET));
+ send_count = fifo.send (ACE_ALPHABET, ACE_OS::strlen (ACE_ALPHABET));
+ if (send_count == expect)
+ {
+ // Ok, so far so good. Now try one that will overflow the reader
+ // side to be sure it properly tosses the overflow. Then send another
+ // to be sure it finds the start of the next message ok.
+ const size_t big_size = 1024*1024; // About a MB
+ char big[big_size];
+ for (size_t i = 0; i < big_size; ++i)
+ big[i] = (i % 2) ? 0x05 : 0x0A; // Make nice pattern in blown stack
+ expect = ACE_static_cast (ssize_t, big_size);
+ send_count = fifo.send (big, big_size);
+ if (send_count == expect)
+ {
+ expect = ACE_static_cast (ssize_t, ACE_OS::strlen (ACE_ALPHABET));
+ send_count = fifo.send (ACE_ALPHABET, ACE_OS::strlen (ACE_ALPHABET));
+ if (send_count != expect)
+ ACE_ERROR ((LM_ERROR,
+ ACE_TEXT ("(%P|%t) Final send; sent %d, expected %d")
+ ACE_TEXT ("%p\n"),
+ send_count, expect, ACE_TEXT ("send")));
+ else
+ ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P|%t) All sends ok\n")));
+ }
+ else
+ {
+ ACE_ERROR
+ ((LM_ERROR,
+ ACE_TEXT ("(%P|%t) Second send expected %d; sent %d. %p\n"),
+ expect, send_count, ACE_TEXT ("send")));
+ }
+ }
+ else
+ {
+ ACE_ERROR ((LM_ERROR,
+ ACE_TEXT ("(%P|%t) First send expected %d; sent %d. %p\n"),
+ expect, send_count, ACE_TEXT ("send")));
+ }
+
+ if (fifo.close () != 0)
+ ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%P|%t) %p\n"), ACE_TEXT ("fifo close")));
+
+ return 0;
+}
+
+static void *
+server (void *arg)
+{
+ ACE_FIFO_Recv_Msg *fifo = ACE_reinterpret_cast (ACE_FIFO_Recv_Msg *, arg);
+
+ // Wait for the client to get going and open the FIFO.
+ ACE_Handle_Set h;
+ ACE_Time_Value delay (10);
+ h.set_bit (fifo->get_handle ());
+ if (ACE::select (h.max_set () + 1, h, &delay) == -1)
+ ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("(%P|%t) server %p\n"),
+ ACE_TEXT ("select")),
+ 0);
+
+ // Read the things the client is sending; alphabet, huge overflow, then
+ // alphabet.
+
+ char buf[BUFSIZ];
+ ssize_t recv_count;
+ ssize_t expect = ACE_static_cast (ssize_t, ACE_OS::strlen (ACE_ALPHABET));
+ recv_count = fifo->recv (buf, sizeof (buf));
+ if (recv_count != expect)
+ ACE_ERROR_RETURN ((LM_ERROR,
+ ACE_TEXT ("(%P|%t) Recv 1 expected %d, got %d. %p\n"),
+ expect, recv_count, ACE_TEXT ("recv")),
+ 0);
+ buf[recv_count] = '\0';
+ if (ACE_OS::strcmp (buf, ACE_ALPHABET) != 0)
+ ACE_ERROR ((LM_ERROR,
+ ACE_TEXT ("(%P|%t) Recv 1 expected alphabet; got %s\n"),
+ buf));
+
+ expect = BUFSIZ;
+ recv_count = fifo->recv (buf, sizeof (buf));
+ // See documented return values for ACE_FIFO_Recv_Msg...
+ // We are being sent a message much longer than BUFSIZ.
+#if defined (ACE_HAS_STREAM_PIPES)
+ if (recv_count != expect)
+#else
+ if (recv_count <= BUFSIZ)
+#endif /* ACE_HAS_STREAM_PIPES */
+ ACE_ERROR_RETURN ((LM_ERROR,
+ ACE_TEXT ("(%P|%t) Recv 2 expected %d, got %d. %p\n"),
+ expect, recv_count, ACE_TEXT ("recv")),
+ 0);
+
+ expect = ACE_static_cast (ssize_t, ACE_OS::strlen (ACE_ALPHABET));
+ recv_count = fifo->recv (buf, sizeof (buf));
+ if (recv_count != expect)
+ ACE_ERROR_RETURN ((LM_ERROR,
+ ACE_TEXT ("(%P|%t) Recv 3 expected %d, got %d. %p\n"),
+ expect, recv_count, ACE_TEXT ("recv")),
+ 0);
+ buf[recv_count] = '\0';
+ if (ACE_OS::strcmp (buf, ACE_ALPHABET) != 0)
+ ACE_ERROR ((LM_ERROR,
+ ACE_TEXT ("(%P|%t) Recv 3 expected alphabet; got %s\n"),
+ buf));
+
+ return 0;
+}
+
+static int
+test_fifo_msg (void)
+{
+ // Reader side opens first - it may fail if fifo not supported on this
+ // platform.
+ ACE_TCHAR fifo_path[MAXPATHLEN];
+ if (ACE_Lib_Find::get_temp_dir (fifo_path, MAXPATHLEN) == -1)
+ ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"),
+ ACE_TEXT ("get_temp_dir")), 1);
+ ACE_OS_String::strcat (fifo_path, ACE_TEXT ("FIFO_Test"));
+ ACE_FIFO_Recv_Msg read_side;
+ // Open read only, not persistent (4th arg is 0)
+ if (-1 == read_side.open (fifo_path,
+ O_CREAT | O_RDONLY,
+ ACE_DEFAULT_FILE_PERMS,
+ 0))
+ {
+#if defined (ACE_WIN32)
+ if (errno == ENOTSUP)
+ {
+ ACE_DEBUG ((LM_DEBUG,
+ ACE_TEXT ("FIFO not supported on Win32; ")
+ ACE_TEXT ("this is correct.\n")));
+ return 0;
+ }
+#endif /* ACE_WIN32 */
+ ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"),
+ ACE_TEXT ("ACE_FIFO_Recv_Msg::open")),
+ 1);
+ }
+
+ // Ok, the FIFO opened clean for read. Now it's safe to spawn a
+ // process/thread and test some transfers.
+
+ int status = 0; // Test status; innocent until proven guilty.
+
+#if !defined (ACE_LACKS_FORK)
+ switch (ACE_OS::fork ("child"))
+ {
+ case -1:
+ ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("fork failed")));
+ status = 1;
+ break;
+ case 0:
+ client (fifo_path);
+ ACE_OS::exit (0);
+ /* NOTREACHED */
+ default:
+ server (&read_side);
+ ACE_OS::wait ();
+ }
+#elif defined (ACE_HAS_THREADS)
+ if (ACE_Thread_Manager::instance ()->spawn
+ (ACE_THR_FUNC (server),
+ &read_side,
+ THR_NEW_LWP | THR_DETACHED) == -1)
+ {
+ ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("server spawn")));
+ ++status;
+ }
+
+ if (ACE_Thread_Manager::instance ()->spawn
+ (ACE_THR_FUNC (client),
+ fifo_path,
+ THR_NEW_LWP | THR_DETACHED) == -1)
+ {
+ ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("client spawn")));
+ ++status;
+ }
+
+ // Wait for the threads to exit.
+ ACE_Thread_Manager::instance ()->wait ();
+#else
+ ACE_ERROR ((LM_INFO,
+ ACE_TEXT ("This test requires multiple threads ")
+ ACE_TEXT ("or processes.\n")));
+#endif /* ACE_HAS_THREADS */
+
+ if (read_side.remove () != 0)
+ {
+ ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("fifo remove")));
+ ++status;
+ }
+
+ ACE_stat fifo_stat;
+ if (ACE_OS::stat (fifo_path, &fifo_stat) == 0)
+ {
+ ACE_ERROR ((LM_ERROR, ACE_TEXT ("FIFO %s should be gone but isn't\n"),
+ fifo_path));
+ ++status;
+ ACE_OS::unlink (fifo_path); // Try to get rid of it.
+ }
+
+ return status;
+}
+
+int
+main (int, ACE_TCHAR *[])
+{
+ ACE_START_TEST (ACE_TEXT ("FIFO_Test"));
+
+ int errors = 0;
+ errors += test_fifo_msg ();
+
+ ACE_END_TEST;
+ return errors;
+}
diff --git a/tests/Makefile b/tests/Makefile
index 638989c27a1..424a9e31a56 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -36,6 +36,7 @@ BIN = Aio_Platform_Test \
DLL_Test\
Enum_Interfaces_Test \
Env_Value_Test \
+ FIFO_Test \
Future_Test \
Future_Set_Test \
Get_Opt_Test \
diff --git a/tests/run_test.lst b/tests/run_test.lst
index f7588b76a64..9a05c27bac9 100644
--- a/tests/run_test.lst
+++ b/tests/run_test.lst
@@ -42,6 +42,7 @@ DLList_Test
Dynamic_Priority_Test
Enum_Interfaces_Test
Env_Value_Test: !chorus
+FIFO_Test: !MSVC
Framework_Component_Test: !STATIC
Future_Test
Future_Set_Test