Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 20 | #include <assert.h> |
| 21 | #include <glib.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <unistd.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/wait.h> |
| 26 | |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 27 | #include "thrift.h" |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 28 | #include "processor/thrift_processor.h" |
| 29 | #include "transport/thrift_server_socket.h" |
| 30 | |
| 31 | #define TEST_PORT 51199 |
| 32 | |
| 33 | #include "server/thrift_simple_server.c" |
| 34 | |
| 35 | /* create a rudimentary processor */ |
| 36 | #define TEST_PROCESSOR_TYPE (test_processor_get_type ()) |
| 37 | |
| 38 | struct _TestProcessor |
| 39 | { |
| 40 | ThriftProcessor parent; |
| 41 | }; |
| 42 | typedef struct _TestProcessor TestProcessor; |
| 43 | |
| 44 | struct _TestProcessorClass |
| 45 | { |
| 46 | ThriftProcessorClass parent; |
| 47 | }; |
| 48 | typedef struct _TestProcessorClass TestProcessorClass; |
| 49 | |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 50 | G_DEFINE_TYPE(TestProcessor, test_processor, THRIFT_TYPE_PROCESSOR) |
| 51 | |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 52 | gboolean |
| 53 | test_processor_process (ThriftProcessor *processor, ThriftProtocol *in, |
| 54 | ThriftProtocol *out) |
| 55 | { |
| 56 | return FALSE; |
| 57 | } |
| 58 | |
| 59 | static void |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 60 | test_processor_init (TestProcessor *p) |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 61 | { |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 62 | THRIFT_UNUSED_VAR (p); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 65 | static void |
| 66 | test_processor_class_init (TestProcessorClass *proc) |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 67 | { |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 68 | (THRIFT_PROCESSOR_CLASS(proc))->process = test_processor_process; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | static void |
| 72 | test_server (void) |
| 73 | { |
| 74 | int status; |
| 75 | pid_t pid; |
| 76 | TestProcessor *p = NULL; |
| 77 | ThriftServerSocket *tss = NULL; |
| 78 | ThriftSimpleServer *ss = NULL; |
| 79 | |
| 80 | p = g_object_new (TEST_PROCESSOR_TYPE, NULL); |
| 81 | tss = g_object_new (THRIFT_TYPE_SERVER_SOCKET, "port", TEST_PORT, NULL); |
| 82 | ss = g_object_new (THRIFT_TYPE_SIMPLE_SERVER, "processor", p, |
| 83 | "server_transport", THRIFT_SERVER_TRANSPORT (tss), NULL); |
| 84 | |
| 85 | /* run the server in a child process */ |
| 86 | pid = fork (); |
| 87 | assert (pid >= 0); |
| 88 | |
| 89 | if (pid == 0) |
| 90 | { |
| 91 | THRIFT_SERVER_GET_CLASS (THRIFT_SERVER (ss))->serve (THRIFT_SERVER (ss)); |
| 92 | exit (0); |
| 93 | } else { |
| 94 | sleep (5); |
| 95 | kill (pid, SIGINT); |
| 96 | |
| 97 | g_object_unref (ss); |
| 98 | g_object_unref (tss); |
| 99 | g_object_unref (p); |
| 100 | assert (wait (&status) == pid); |
| 101 | assert (status == SIGINT); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | int |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 106 | main(int argc, char *argv[]) |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 107 | { |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 108 | g_type_init(); |
| 109 | g_test_init (&argc, &argv, NULL); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 110 | |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 111 | g_test_add_func ("/testsimpleserver/SimpleServer", test_server); |
| 112 | |
| 113 | return g_test_run (); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 114 | } |