THRIFT-4329: multiplexed processor, client and server for c_glib
Client: c_glib
This closes #1361
diff --git a/lib/c_glib/test/testtransportsocket.c b/lib/c_glib/test/testtransportsocket.c
index 8e96375..fedbad6 100755
--- a/lib/c_glib/test/testtransportsocket.c
+++ b/lib/c_glib/test/testtransportsocket.c
@@ -33,9 +33,9 @@
my_socket(int domain, int type, int protocol)
{
if (socket_error == 0)
- {
- return socket (domain, type, protocol);
- }
+ {
+ return socket (domain, type, protocol);
+ }
return -1;
}
@@ -44,9 +44,9 @@
my_recv(int socket, void *buffer, size_t length, int flags)
{
if (recv_error == 0)
- {
- return recv (socket, buffer, length, flags);
- }
+ {
+ return recv (socket, buffer, length, flags);
+ }
return -1;
}
@@ -55,9 +55,9 @@
my_send(int socket, const void *buffer, size_t length, int flags)
{
if (send_error == 0)
- {
- return send (socket, buffer, length, flags);
- }
+ {
+ return send (socket, buffer, length, flags);
+ }
return -1;
}
@@ -70,7 +70,7 @@
#undef send
static void thrift_socket_server (const int port);
-
+static void thrift_socket_server_open (const int port, int times);
/* test object creation and destruction */
static void
test_create_and_destroy(void)
@@ -93,82 +93,100 @@
ThriftSocket *tsocket = NULL;
ThriftTransport *transport = NULL;
GError *err = NULL;
+ int port = 51199;
+ pid_t pid;
+ int status;
- /* open a connection and close it */
- tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
- "port", 51188, NULL);
- transport = THRIFT_TRANSPORT (tsocket);
- thrift_socket_open (transport, NULL);
- g_assert (thrift_socket_is_open (transport) == TRUE);
- thrift_socket_close (transport, NULL);
- g_assert (thrift_socket_is_open (transport) == FALSE);
+ pid = fork ();
+ g_assert ( pid >= 0 );
- /* test close failure */
- tsocket->sd = -1;
- thrift_socket_close (transport, NULL);
- g_object_unref (tsocket);
+ if ( pid == 0 )
+ {
+ /* child listens */
+ thrift_socket_server_open (port, 1);
+ exit (0);
+ } else {
+ /* parent connects, wait a bit for the socket to be created */
+ sleep (1);
- /* try a hostname lookup failure */
- tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost.broken",
- NULL);
- transport = THRIFT_TRANSPORT (tsocket);
- g_assert (thrift_socket_open (transport, &err) == FALSE);
- g_object_unref (tsocket);
- g_error_free (err);
- err = NULL;
+ /* open a connection and close it */
+ tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
+ "port", port, NULL);
+ transport = THRIFT_TRANSPORT (tsocket);
+ thrift_socket_open (transport, NULL);
+ g_assert (thrift_socket_is_open (transport) == TRUE);
+ thrift_socket_close (transport, NULL);
+ g_assert (thrift_socket_is_open (transport) == FALSE);
- /* try an error call to socket() */
- tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost", NULL);
- transport = THRIFT_TRANSPORT (tsocket);
- socket_error = 1;
- g_assert (thrift_socket_open (transport, &err) == FALSE);
- socket_error = 0;
- g_object_unref (tsocket);
- g_error_free (err);
+ /* test close failure */
+ tsocket->sd = -1;
+ thrift_socket_close (transport, NULL);
+ g_object_unref (tsocket);
+
+ /* try a hostname lookup failure */
+ tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost.broken",
+ NULL);
+ transport = THRIFT_TRANSPORT (tsocket);
+ g_assert (thrift_socket_open (transport, &err) == FALSE);
+ g_object_unref (tsocket);
+ g_error_free (err);
+ err = NULL;
+
+ /* try an error call to socket() */
+ tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost", NULL);
+ transport = THRIFT_TRANSPORT (tsocket);
+ socket_error = 1;
+ g_assert (thrift_socket_open (transport, &err) == FALSE);
+ socket_error = 0;
+ g_object_unref (tsocket);
+ g_error_free (err);
+ g_assert ( wait (&status) == pid );
+ g_assert ( status == 0 );
+ }
}
static void
test_read_and_write(void)
{
- int status;
- pid_t pid;
ThriftSocket *tsocket = NULL;
ThriftTransport *transport = NULL;
+ pid_t pid;
int port = 51199;
+ int status;
guchar buf[10] = TEST_DATA; /* a buffer */
pid = fork ();
g_assert ( pid >= 0 );
if ( pid == 0 )
- {
- /* child listens */
- thrift_socket_server (port);
- exit (0);
- } else {
- /* parent connects, wait a bit for the socket to be created */
- sleep (1);
+ {
+ /* child listens */
+ thrift_socket_server (port);
+ exit (0);
+ } else {
+ /* parent connects, wait a bit for the socket to be created */
+ sleep (1);
- tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
- "port", port, NULL);
- transport = THRIFT_TRANSPORT (tsocket);
- g_assert (thrift_socket_open (transport, NULL) == TRUE);
- g_assert (thrift_socket_is_open (transport));
- thrift_socket_write (transport, buf, 10, NULL);
+ tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
+ "port", port, NULL);
+ transport = THRIFT_TRANSPORT (tsocket);
+ g_assert (thrift_socket_open (transport, NULL) == TRUE);
+ g_assert (thrift_socket_is_open (transport));
+ thrift_socket_write (transport, buf, 10, NULL);
- /* write fail */
- send_error = 1;
- thrift_socket_write (transport, buf, 1, NULL);
- send_error = 0;
+ /* write fail */
+ send_error = 1;
+ thrift_socket_write (transport, buf, 1, NULL);
+ send_error = 0;
- thrift_socket_write_end (transport, NULL);
- thrift_socket_flush (transport, NULL);
- thrift_socket_close (transport, NULL);
- g_object_unref (tsocket);
+ thrift_socket_write_end (transport, NULL);
+ thrift_socket_flush (transport, NULL);
+ thrift_socket_close (transport, NULL);
+ g_object_unref (tsocket);
- g_assert ( wait (&status) == pid );
- g_assert ( status == 0 );
- }
+ g_assert ( wait (&status) == pid );
+ g_assert ( status == 0 );
+ }
}
/* test ThriftSocket's peek() implementation */
@@ -183,9 +201,9 @@
GError *error = NULL;
client_transport = g_object_new (THRIFT_TYPE_SOCKET,
- "hostname", "localhost",
- "port", port,
- NULL);
+ "hostname", "localhost",
+ "port", port,
+ NULL);
/* thrift_transport_peek returns FALSE when the socket is closed */
g_assert (thrift_transport_is_open (client_transport) == FALSE);
@@ -196,81 +214,105 @@
g_assert (pid >= 0);
if (pid == 0)
- {
- ThriftServerTransport *server_transport = NULL;
+ {
+ ThriftServerTransport *server_transport = NULL;
- g_object_unref (client_transport);
+ g_object_unref (client_transport);
- /* child listens */
- server_transport = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
- "port", port,
- NULL);
- g_assert (server_transport != NULL);
+ /* child listens */
+ server_transport = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
+ "port", port,
+ NULL);
+ g_assert (server_transport != NULL);
- thrift_server_transport_listen (server_transport, &error);
- g_assert (error == NULL);
+ thrift_server_transport_listen (server_transport, &error);
+ g_assert (error == NULL);
- client_transport = g_object_new
- (THRIFT_TYPE_BUFFERED_TRANSPORT,
- "transport", thrift_server_transport_accept (server_transport, &error),
- "r_buf_size", 0,
- "w_buf_size", sizeof data,
- NULL);
- g_assert (error == NULL);
- g_assert (client_transport != NULL);
+ client_transport = g_object_new
+ (THRIFT_TYPE_BUFFERED_TRANSPORT,
+ "transport", thrift_server_transport_accept (server_transport, &error),
+ "r_buf_size", 0,
+ "w_buf_size", sizeof data,
+ NULL);
+ g_assert (error == NULL);
+ g_assert (client_transport != NULL);
- /* write exactly one character to the client */
- g_assert (thrift_transport_write (client_transport,
- &data,
- sizeof data,
- &error) == TRUE);
+ /* write exactly one character to the client */
+ g_assert (thrift_transport_write (client_transport,
+ &data,
+ sizeof data,
+ &error) == TRUE);
- thrift_transport_flush (client_transport, &error);
- thrift_transport_write_end (client_transport, &error);
- thrift_transport_close (client_transport, &error);
+ thrift_transport_flush (client_transport, &error);
+ thrift_transport_write_end (client_transport, &error);
+ thrift_transport_close (client_transport, &error);
- g_object_unref (client_transport);
- g_object_unref (server_transport);
+ g_object_unref (client_transport);
+ g_object_unref (server_transport);
- exit (0);
- }
+ exit (0);
+ }
else {
- /* parent connects, wait a bit for the socket to be created */
- sleep (1);
+ /* parent connects, wait a bit for the socket to be created */
+ sleep (1);
- /* connect to the child */
- thrift_transport_open (client_transport, &error);
- g_assert (error == NULL);
- g_assert (thrift_transport_is_open (client_transport) == TRUE);
+ /* connect to the child */
+ thrift_transport_open (client_transport, &error);
+ g_assert (error == NULL);
+ g_assert (thrift_transport_is_open (client_transport) == TRUE);
- /* thrift_transport_peek returns TRUE when the socket is open and there is
+ /* thrift_transport_peek returns TRUE when the socket is open and there is
data available to be read */
- g_assert (thrift_transport_peek (client_transport, &error) == TRUE);
- g_assert (error == NULL);
+ g_assert (thrift_transport_peek (client_transport, &error) == TRUE);
+ g_assert (error == NULL);
- /* read exactly one character from the server */
- g_assert_cmpint (thrift_transport_read (client_transport,
- &data,
- sizeof data,
- &error), ==, sizeof data);
+ /* read exactly one character from the server */
+ g_assert_cmpint (thrift_transport_read (client_transport,
+ &data,
+ sizeof data,
+ &error), ==, sizeof data);
- /* thrift_transport_peek returns FALSE when the socket is open but there is
+ /* thrift_transport_peek returns FALSE when the socket is open but there is
no (more) data available to be read */
- g_assert (thrift_transport_is_open (client_transport) == TRUE);
- g_assert (thrift_transport_peek (client_transport, &error) == FALSE);
- g_assert (error == NULL);
+ g_assert (thrift_transport_is_open (client_transport) == TRUE);
+ g_assert (thrift_transport_peek (client_transport, &error) == FALSE);
+ g_assert (error == NULL);
- thrift_transport_read_end (client_transport, &error);
- thrift_transport_close (client_transport, &error);
+ thrift_transport_read_end (client_transport, &error);
+ thrift_transport_close (client_transport, &error);
- g_object_unref (client_transport);
+ g_object_unref (client_transport);
- g_assert (wait (&status) == pid);
- g_assert (status == 0);
+ g_assert (wait (&status) == pid);
+ g_assert (status == 0);
}
}
static void
+thrift_socket_server_open (const int port, int times)
+{
+ int bytes = 0;
+ ThriftServerTransport *transport = NULL;
+ ThriftTransport *client = NULL;
+ guchar buf[10]; /* a buffer */
+ guchar match[10] = TEST_DATA;
+ int i;
+ ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
+ "port", port, NULL);
+
+ transport = THRIFT_SERVER_TRANSPORT (tsocket);
+ thrift_server_transport_listen (transport, NULL);
+ for(i=0;i<times;i++){
+ client = thrift_server_transport_accept (transport, NULL);
+ g_assert (client != NULL);
+ thrift_socket_close (client, NULL);
+ g_object_unref (client);
+ }
+ g_object_unref (tsocket);
+}
+
+
+static void
thrift_socket_server (const int port)
{
int bytes = 0;
@@ -280,7 +322,7 @@
guchar match[10] = TEST_DATA;
ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
- "port", port, NULL);
+ "port", port, NULL);
transport = THRIFT_SERVER_TRANSPORT (tsocket);
thrift_server_transport_listen (transport, NULL);