9173d82aadf14b7b129b919c088775ca8be32819
[framework/telephony/libslp-tapi.git] / wearable / unit-test / test-thread.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <sys/types.h>
22 #include <asm/unistd.h>
23 #include <unistd.h>
24 #include <sys/syscall.h>
25 #include <pthread.h>
26
27 #include <glib.h>
28 #include <glib-object.h>
29
30 #include <dlog.h>
31 #include <tapi_common.h>
32 #include <ITapiModem.h>
33
34 #include "log.h"
35
36
37 static pid_t get_tid ()
38 {
39         return syscall (__NR_gettid);
40 }
41
42 struct meta_data {
43         pid_t tid;
44         GMainLoop *loop;
45         GMainContext *context;
46 };
47
48 static void on_cb (TapiHandle *h, int result, void *data, void *user_data)
49 {
50         struct meta_data *m = user_data;
51         int *status = data;
52
53         g_assert (m != NULL);
54         g_assert (m->tid == get_tid());
55
56         dbg ("response: %d", *status);
57         g_main_loop_quit (m->loop);
58 }
59
60 static gboolean on_timeout (gpointer user_data)
61 {
62         g_assert_not_reached ();
63         return FALSE;
64 }
65
66 static void *do_blocking_request_response (gpointer user_data)
67 {
68         struct meta_data m;
69         TapiHandle *h = user_data;
70         GSource *timer;
71         int ret;
72
73         m.tid = get_tid();
74         m.context = g_main_context_new();
75         m.loop = g_main_loop_new (m.context, FALSE);
76
77         g_main_context_push_thread_default (m.context);
78
79         if (!user_data) {
80                 h = tel_init (NULL);
81                 g_assert (h != NULL);
82         }
83
84         ret = tel_get_flight_mode (h, on_cb, &m);
85         g_assert (ret == TAPI_API_SUCCESS);
86
87         timer = g_timeout_source_new_seconds (60);
88         g_source_set_callback (timer, on_timeout, &m, NULL);
89         g_source_attach (timer, m.context);
90         g_source_unref (timer);
91
92         g_main_loop_run (m.loop);
93
94         g_main_context_pop_thread_default (m.context);
95         g_main_context_unref (m.context);
96         g_main_loop_unref (m.loop);
97
98         if (!user_data)
99                 tel_deinit (h);
100
101         return NULL;
102 }
103
104 static void test_each_request (void)
105 {
106         GThread *th1;
107         GThread *th2;
108
109         /* Request from A thread */
110         th1 = g_thread_new ("test1-A", do_blocking_request_response, NULL);
111         g_assert (th1 != NULL);
112
113         /* Request from B thread */
114         th2 = g_thread_new ("test1-B", do_blocking_request_response, NULL);
115         g_assert (th2 != NULL);
116
117         g_thread_join (th1);
118         g_thread_join (th2);
119 }
120
121 static void test_cross_request (void)
122 {
123         GThread *th1;
124         GThread *th2;
125         GThread *th3;
126         TapiHandle *h;
127
128         h = tel_init (NULL);
129         g_assert (h != NULL);
130
131         /* Request from A thread with shared handle */
132         th1 = g_thread_new ("test2-A", do_blocking_request_response, h);
133         g_assert (th1 != NULL);
134
135         /* Sync Request from Default context */
136         g_assert (tel_get_misc_me_imei_sync (h) != NULL);
137
138         /* Request from B thread with custom handle */
139         th2 = g_thread_new ("test2-B", do_blocking_request_response, NULL);
140         g_assert (th2 != NULL);
141
142         /* Request from C thread with shared handle */
143         th3 = g_thread_new ("test3-C", do_blocking_request_response, h);
144         g_assert (th3 != NULL);
145
146         g_thread_join (th1);
147         g_thread_join (th2);
148         g_thread_join (th3);
149
150         tel_deinit (h);
151 }
152
153 int main (int argc, char **argv)
154 {
155         g_test_init (&argc, &argv, NULL);
156         g_type_init ();
157
158         g_test_add_func ("/thread/each_request", test_each_request);
159         g_test_add_func ("/thread/cross_request", test_cross_request);
160
161         return g_test_run();
162 }