Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix make dist.
[simgrid.git] / teshsuite / gras / msg_handle / msg_handle.c
1 /* msg_handle - ensures the semantic of gras_msg_handle(i) for i<0,=0 or >0 */
2
3 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5 /* Thanks to Flavien Vernier for reporting an issue around this             */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include "gras.h"
11
12 XBT_LOG_NEW_DEFAULT_CATEGORY(test, "Messages specific to this test");
13
14 int server(int argc, char *argv[]);
15 int client(int argc, char *argv[]);
16
17 static int server_cb_hello_handler(gras_msg_cb_ctx_t ctx,
18                                    void *payload_data)
19 {
20   XBT_INFO("Got the message");
21   return 0;
22 }
23
24 int server(int argc, char *argv[])
25 {
26   volatile xbt_socket_t me = NULL, pal = NULL;
27   int myport;
28   char *palstr;
29
30   xbt_ex_t e;
31   _XBT_GNUC_UNUSED int got_expected;
32   _XBT_GNUC_UNUSED double now;
33
34
35   gras_init(&argc, argv);
36
37   xbt_assert(argc == 3, "Usage: server <myport> <client>");
38   myport = atoi(argv[1]);
39   palstr = argv[2];
40
41   gras_msgtype_declare("hello", NULL);
42   gras_cb_register("hello", &server_cb_hello_handler);
43
44   XBT_INFO("Launch server (port=%d)", myport);
45   TRY {
46     me = gras_socket_server(myport);
47   }
48   CATCH_ANONYMOUS {
49     RETHROWF("Unable to establish a server socket: %s");
50   }
51   gras_os_sleep(1);             /* Wait for pal to startup */
52   TRY {
53     pal = gras_socket_client_from_string(palstr);
54   }
55   CATCH_ANONYMOUS {
56     RETHROWF("Unable to establish a socket to %s: %s", palstr);
57   }
58   XBT_INFO("Initialization done.");
59   now = gras_os_time();
60
61   /* Launch handle(0) when there is no message. Timeout expected */
62   got_expected = 0;
63   TRY {
64     gras_msg_handle(0);
65   }
66   CATCH(e) {
67     if (e.category == timeout_error) {
68       got_expected = 1;
69       xbt_ex_free(e);
70     } else {
71       RETHROWF("Didn't got the expected timeout: %s");
72     }
73   }
74   xbt_assert(got_expected,
75               "gras_msg_handle(0) do not lead to any timeout exception");
76   xbt_assert(gras_os_time() - now < 0.01,
77               "gras_msg_handle(0) do not anwser immediately (%.4fsec)",
78               gras_os_time() - now);
79   XBT_INFO("gras_msg_handle(0) works as expected (immediate timeout)");
80   /* Launch handle(0) when there is no message. Timeout expected */
81   got_expected = 0;
82   TRY {
83     gras_msg_handle(1);
84   }
85   CATCH(e) {
86     if (e.category == timeout_error) {
87       got_expected = 1;
88       xbt_ex_free(e);
89     } else {
90       RETHROWF("Didn't got the expected timeout: %s");
91     }
92   }
93   xbt_assert(got_expected,
94               "gras_msg_handle(1) do not lead to any timeout exception");
95   xbt_assert(gras_os_time() - now < 1.5,
96               "gras_msg_handle(1) needs more than 1.5 sec to answer (%.4fsec)",
97               gras_os_time() - now);
98   xbt_assert(gras_os_time() - now >= 1.0,
99               "gras_msg_handle(1) answers in less than one second (%.4fsec)",
100               gras_os_time() - now);
101   XBT_INFO("gras_msg_handle(1) works as expected (delayed timeout)");
102   gras_os_sleep(3);
103
104   /* Send an hello to the client to unlock it */
105   XBT_INFO("Unlock pal");
106   gras_msg_send(pal, "hello", NULL);
107
108   /* Frees the allocated resources, and shut GRAS down */
109   gras_socket_close(me);
110   gras_socket_close(pal);
111   gras_exit();
112   return 0;
113 }
114
115 int client(int argc, char *argv[])
116 {
117   volatile xbt_socket_t me = NULL, pal = NULL;
118   int myport;
119   char *palstr;
120
121   gras_init(&argc, argv);
122   xbt_assert(argc == 3, "Usage: client <myport> <server>");
123   myport = atoi(argv[1]);
124   palstr = argv[2];
125
126   gras_msgtype_declare("hello", NULL);
127   gras_cb_register("hello", &server_cb_hello_handler);
128
129   XBT_INFO("Launch client (port=%d)", myport);
130   TRY {
131     me = gras_socket_server(myport);
132   }
133   CATCH_ANONYMOUS {
134     RETHROWF("Unable to establish a server socket: %s");
135   }
136   gras_os_sleep(1);             /* Wait for pal to startup */
137   TRY {
138     pal = gras_socket_client_from_string(palstr);
139   }
140   CATCH_ANONYMOUS {
141     RETHROWF("Unable to establish a socket to %s: %s", palstr);
142   }
143   XBT_INFO("Initialization done.");
144
145   /* Launch handle(-1). Lock until message from server expected */
146   TRY {
147     gras_msg_handle(-1);
148   }
149   CATCH_ANONYMOUS {
150     RETHROWF("No exception expected during handle(-1), but got %s");
151   }
152   XBT_INFO("gras_msg_handle(-1) works as expected (locked)");
153
154   /* Frees the allocated resources, and shut GRAS down */
155   gras_socket_close(me);
156   gras_socket_close(pal);
157   gras_exit();
158   return 0;
159 }