blob: 3af2eeb17a6238fd03bef1d0e35ef8eba45e509c [file] [log] [blame]
Roger Meierc1010922010-11-26 10:17:48 +00001/*
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 Meier213a6642010-10-27 12:30:11 +000020#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 Meiere3da7682013-01-11 11:41:53 +010027#include <thrift/c_glib/thrift.h>
28#include <thrift/c_glib/processor/thrift_processor.h>
29#include <thrift/c_glib/transport/thrift_server_socket.h>
Roger Meier213a6642010-10-27 12:30:11 +000030
31#define TEST_PORT 51199
32
Roger Meiere3da7682013-01-11 11:41:53 +010033#include <thrift/c_glib/server/thrift_simple_server.c>
Roger Meier213a6642010-10-27 12:30:11 +000034
35/* create a rudimentary processor */
36#define TEST_PROCESSOR_TYPE (test_processor_get_type ())
37
38struct _TestProcessor
39{
40 ThriftProcessor parent;
41};
42typedef struct _TestProcessor TestProcessor;
43
44struct _TestProcessorClass
45{
46 ThriftProcessorClass parent;
47};
48typedef struct _TestProcessorClass TestProcessorClass;
49
Roger Meierc1010922010-11-26 10:17:48 +000050G_DEFINE_TYPE(TestProcessor, test_processor, THRIFT_TYPE_PROCESSOR)
51
Roger Meier213a6642010-10-27 12:30:11 +000052gboolean
53test_processor_process (ThriftProcessor *processor, ThriftProtocol *in,
Roger Meier63243c62014-09-29 20:29:58 +020054 ThriftProtocol *out, GError **error)
Roger Meier213a6642010-10-27 12:30:11 +000055{
Simon South38e71552015-08-03 10:51:16 +000056 THRIFT_UNUSED_VAR (processor);
57 THRIFT_UNUSED_VAR (in);
58 THRIFT_UNUSED_VAR (out);
59 THRIFT_UNUSED_VAR (error);
60
Roger Meier213a6642010-10-27 12:30:11 +000061 return FALSE;
62}
63
64static void
Roger Meierc1010922010-11-26 10:17:48 +000065test_processor_init (TestProcessor *p)
Roger Meier213a6642010-10-27 12:30:11 +000066{
Roger Meierc1010922010-11-26 10:17:48 +000067 THRIFT_UNUSED_VAR (p);
Roger Meier213a6642010-10-27 12:30:11 +000068}
69
Roger Meierc1010922010-11-26 10:17:48 +000070static void
71test_processor_class_init (TestProcessorClass *proc)
Roger Meier213a6642010-10-27 12:30:11 +000072{
Roger Meierc1010922010-11-26 10:17:48 +000073 (THRIFT_PROCESSOR_CLASS(proc))->process = test_processor_process;
Roger Meier213a6642010-10-27 12:30:11 +000074}
75
76static void
77test_server (void)
78{
79 int status;
80 pid_t pid;
81 TestProcessor *p = NULL;
82 ThriftServerSocket *tss = NULL;
83 ThriftSimpleServer *ss = NULL;
84
85 p = g_object_new (TEST_PROCESSOR_TYPE, NULL);
86 tss = g_object_new (THRIFT_TYPE_SERVER_SOCKET, "port", TEST_PORT, NULL);
87 ss = g_object_new (THRIFT_TYPE_SIMPLE_SERVER, "processor", p,
88 "server_transport", THRIFT_SERVER_TRANSPORT (tss), NULL);
89
90 /* run the server in a child process */
91 pid = fork ();
92 assert (pid >= 0);
93
94 if (pid == 0)
95 {
Roger Meier63243c62014-09-29 20:29:58 +020096 THRIFT_SERVER_GET_CLASS (THRIFT_SERVER (ss))->serve (THRIFT_SERVER (ss),
97 NULL);
Roger Meier213a6642010-10-27 12:30:11 +000098 exit (0);
99 } else {
100 sleep (5);
101 kill (pid, SIGINT);
102
103 g_object_unref (ss);
104 g_object_unref (tss);
105 g_object_unref (p);
106 assert (wait (&status) == pid);
107 assert (status == SIGINT);
108 }
109}
110
111int
Roger Meierc1010922010-11-26 10:17:48 +0000112main(int argc, char *argv[])
Roger Meier213a6642010-10-27 12:30:11 +0000113{
Jens Geyer1c190272015-07-28 23:15:18 +0200114#if (!GLIB_CHECK_VERSION (2, 36, 0))
Roger Meierc1010922010-11-26 10:17:48 +0000115 g_type_init();
Jens Geyer1c190272015-07-28 23:15:18 +0200116#endif
117
Roger Meierc1010922010-11-26 10:17:48 +0000118 g_test_init (&argc, &argv, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000119
Roger Meierc1010922010-11-26 10:17:48 +0000120 g_test_add_func ("/testsimpleserver/SimpleServer", test_server);
121
122 return g_test_run ();
Roger Meier213a6642010-10-27 12:30:11 +0000123}