Logo AND Algorithmique Numérique Distribuée

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