Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 1 | #include <assert.h> |
| 2 | #include <glib.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <unistd.h> |
| 5 | #include <sys/types.h> |
| 6 | #include <sys/wait.h> |
| 7 | |
| 8 | #include "processor/thrift_processor.h" |
| 9 | #include "transport/thrift_server_socket.h" |
| 10 | |
| 11 | #define TEST_PORT 51199 |
| 12 | |
| 13 | #include "server/thrift_simple_server.c" |
| 14 | |
| 15 | /* create a rudimentary processor */ |
| 16 | #define TEST_PROCESSOR_TYPE (test_processor_get_type ()) |
| 17 | |
| 18 | struct _TestProcessor |
| 19 | { |
| 20 | ThriftProcessor parent; |
| 21 | }; |
| 22 | typedef struct _TestProcessor TestProcessor; |
| 23 | |
| 24 | struct _TestProcessorClass |
| 25 | { |
| 26 | ThriftProcessorClass parent; |
| 27 | }; |
| 28 | typedef struct _TestProcessorClass TestProcessorClass; |
| 29 | |
| 30 | gboolean |
| 31 | test_processor_process (ThriftProcessor *processor, ThriftProtocol *in, |
| 32 | ThriftProtocol *out) |
| 33 | { |
| 34 | return FALSE; |
| 35 | } |
| 36 | |
| 37 | static void |
| 38 | test_processor_class_init (ThriftProcessorClass *proc) |
| 39 | { |
| 40 | proc->process = test_processor_process; |
| 41 | } |
| 42 | |
| 43 | GType |
| 44 | test_processor_get_type (void) |
| 45 | { |
| 46 | static GType type = 0; |
| 47 | |
| 48 | if (type == 0) |
| 49 | { |
| 50 | static const GTypeInfo info = |
| 51 | { |
| 52 | sizeof (TestProcessorClass), |
| 53 | NULL, /* base_init */ |
| 54 | NULL, /* base_finalize */ |
| 55 | (GClassInitFunc) test_processor_class_init, |
| 56 | NULL, /* class_finalize */ |
| 57 | NULL, /* class_data */ |
| 58 | sizeof (TestProcessor), |
| 59 | 0, /* n_preallocs */ |
| 60 | NULL, /* instance_init */ |
| 61 | NULL, /* value_table */ |
| 62 | }; |
| 63 | |
| 64 | type = g_type_register_static (THRIFT_TYPE_PROCESSOR, |
| 65 | "TestProcessorType", |
| 66 | &info, 0); |
| 67 | } |
| 68 | |
| 69 | return type; |
| 70 | } |
| 71 | |
| 72 | static void |
| 73 | test_server (void) |
| 74 | { |
| 75 | int status; |
| 76 | pid_t pid; |
| 77 | TestProcessor *p = NULL; |
| 78 | ThriftServerSocket *tss = NULL; |
| 79 | ThriftSimpleServer *ss = NULL; |
| 80 | |
| 81 | p = g_object_new (TEST_PROCESSOR_TYPE, NULL); |
| 82 | tss = g_object_new (THRIFT_TYPE_SERVER_SOCKET, "port", TEST_PORT, NULL); |
| 83 | ss = g_object_new (THRIFT_TYPE_SIMPLE_SERVER, "processor", p, |
| 84 | "server_transport", THRIFT_SERVER_TRANSPORT (tss), NULL); |
| 85 | |
| 86 | /* run the server in a child process */ |
| 87 | pid = fork (); |
| 88 | assert (pid >= 0); |
| 89 | |
| 90 | if (pid == 0) |
| 91 | { |
| 92 | THRIFT_SERVER_GET_CLASS (THRIFT_SERVER (ss))->serve (THRIFT_SERVER (ss)); |
| 93 | exit (0); |
| 94 | } else { |
| 95 | sleep (5); |
| 96 | kill (pid, SIGINT); |
| 97 | |
| 98 | g_object_unref (ss); |
| 99 | g_object_unref (tss); |
| 100 | g_object_unref (p); |
| 101 | assert (wait (&status) == pid); |
| 102 | assert (status == SIGINT); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | int |
| 107 | main (void) |
| 108 | { |
| 109 | g_type_init (); |
| 110 | test_server (); |
| 111 | |
| 112 | return 0; |
| 113 | } |