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 <glib.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <unistd.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <sys/wait.h> |
| 25 | |
Roger Meier | e3da768 | 2013-01-11 11:41:53 +0100 | [diff] [blame] | 26 | #include <thrift/c_glib/thrift.h> |
| 27 | #include <thrift/c_glib/processor/thrift_processor.h> |
| 28 | #include <thrift/c_glib/transport/thrift_server_socket.h> |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 29 | |
| 30 | #define TEST_PORT 51199 |
| 31 | |
Roger Meier | e3da768 | 2013-01-11 11:41:53 +0100 | [diff] [blame] | 32 | #include <thrift/c_glib/server/thrift_simple_server.c> |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 33 | |
| 34 | /* create a rudimentary processor */ |
| 35 | #define TEST_PROCESSOR_TYPE (test_processor_get_type ()) |
| 36 | |
| 37 | struct _TestProcessor |
| 38 | { |
| 39 | ThriftProcessor parent; |
| 40 | }; |
| 41 | typedef struct _TestProcessor TestProcessor; |
| 42 | |
| 43 | struct _TestProcessorClass |
| 44 | { |
| 45 | ThriftProcessorClass parent; |
| 46 | }; |
| 47 | typedef struct _TestProcessorClass TestProcessorClass; |
| 48 | |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 49 | G_DEFINE_TYPE(TestProcessor, test_processor, THRIFT_TYPE_PROCESSOR) |
| 50 | |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 51 | gboolean |
| 52 | test_processor_process (ThriftProcessor *processor, ThriftProtocol *in, |
Roger Meier | 63243c6 | 2014-09-29 20:29:58 +0200 | [diff] [blame] | 53 | ThriftProtocol *out, GError **error) |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 54 | { |
Simon South | 38e7155 | 2015-08-03 10:51:16 +0000 | [diff] [blame] | 55 | THRIFT_UNUSED_VAR (processor); |
| 56 | THRIFT_UNUSED_VAR (in); |
| 57 | THRIFT_UNUSED_VAR (out); |
| 58 | THRIFT_UNUSED_VAR (error); |
| 59 | |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 60 | return FALSE; |
| 61 | } |
| 62 | |
| 63 | static void |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 64 | test_processor_init (TestProcessor *p) |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 65 | { |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 66 | THRIFT_UNUSED_VAR (p); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 69 | static void |
| 70 | test_processor_class_init (TestProcessorClass *proc) |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 71 | { |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 72 | (THRIFT_PROCESSOR_CLASS(proc))->process = test_processor_process; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static void |
| 76 | test_server (void) |
| 77 | { |
| 78 | int status; |
| 79 | pid_t pid; |
| 80 | TestProcessor *p = NULL; |
| 81 | ThriftServerSocket *tss = NULL; |
| 82 | ThriftSimpleServer *ss = NULL; |
| 83 | |
| 84 | p = g_object_new (TEST_PROCESSOR_TYPE, NULL); |
| 85 | tss = g_object_new (THRIFT_TYPE_SERVER_SOCKET, "port", TEST_PORT, NULL); |
| 86 | ss = g_object_new (THRIFT_TYPE_SIMPLE_SERVER, "processor", p, |
| 87 | "server_transport", THRIFT_SERVER_TRANSPORT (tss), NULL); |
| 88 | |
| 89 | /* run the server in a child process */ |
| 90 | pid = fork (); |
James E. King, III | 43f4bf2 | 2017-10-28 12:54:02 -0400 | [diff] [blame] | 91 | g_assert (pid >= 0); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 92 | |
| 93 | if (pid == 0) |
| 94 | { |
Roger Meier | 63243c6 | 2014-09-29 20:29:58 +0200 | [diff] [blame] | 95 | THRIFT_SERVER_GET_CLASS (THRIFT_SERVER (ss))->serve (THRIFT_SERVER (ss), |
| 96 | NULL); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 97 | exit (0); |
| 98 | } else { |
| 99 | sleep (5); |
| 100 | kill (pid, SIGINT); |
| 101 | |
| 102 | g_object_unref (ss); |
| 103 | g_object_unref (tss); |
| 104 | g_object_unref (p); |
James E. King, III | 43f4bf2 | 2017-10-28 12:54:02 -0400 | [diff] [blame] | 105 | g_assert (wait (&status) == pid); |
| 106 | g_assert (status == SIGINT); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
| 110 | int |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 111 | main(int argc, char *argv[]) |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 112 | { |
Jens Geyer | 1c19027 | 2015-07-28 23:15:18 +0200 | [diff] [blame] | 113 | #if (!GLIB_CHECK_VERSION (2, 36, 0)) |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 114 | g_type_init(); |
Jens Geyer | 1c19027 | 2015-07-28 23:15:18 +0200 | [diff] [blame] | 115 | #endif |
| 116 | |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 117 | g_test_init (&argc, &argv, NULL); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 118 | |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 119 | g_test_add_func ("/testsimpleserver/SimpleServer", test_server); |
| 120 | |
| 121 | return g_test_run (); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 122 | } |