wscript: Add check for --wrap linker flag
[vlendec/samba-autobuild/.git] / source3 / torture / msgtest.c
index 9019e6a9579c98d882c24660ecce611a7b0dbf65..1d2a8a9b1c86275a74d6cd11ec23cd6d6858e3a4 100644 (file)
@@ -1,20 +1,19 @@
 /* 
    Unix SMB/CIFS implementation.
    Copyright (C) Andrew Tridgell 2000
-   
+
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
-   
+
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
-   
+
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 /*
@@ -22,6 +21,7 @@
  */
 
 #include "includes.h"
+#include "messages.h"
 
 static int pong_count;
 
@@ -40,27 +40,31 @@ static void pong_message(struct messaging_context *msg_ctx,
 
  int main(int argc, char *argv[])
 {
-       struct event_context *evt_ctx;
+       struct tevent_context *evt_ctx;
        struct messaging_context *msg_ctx;
        pid_t pid;
        int i, n;
        char buf[12];
+       int ret;
+       TALLOC_CTX *frame = talloc_stackframe();
 
-       load_case_tables();
+       smb_init_locale();
 
-       setup_logging(argv[0],True);
-       
-       lp_load(dyn_CONFIGFILE,False,False,False,True);
+       setup_logging(argv[0], DEBUG_STDOUT);
 
-       if (!(evt_ctx = event_context_init(NULL)) ||
-           !(msg_ctx = messaging_init(NULL, server_id_self(), evt_ctx))) {
+       lp_load_global(get_dyn_CONFIGFILE());
+
+       if (!(evt_ctx = samba_tevent_context_init(NULL)) ||
+           !(msg_ctx = messaging_init(NULL, evt_ctx))) {
                fprintf(stderr, "could not init messaging context\n");
+               TALLOC_FREE(frame);
                exit(1);
        }
-       
+
        if (argc != 3) {
                fprintf(stderr, "%s: Usage - %s pid count\n", argv[0],
                        argv[0]);
+               TALLOC_FREE(frame);
                exit(1);
        }
 
@@ -75,29 +79,43 @@ static void pong_message(struct messaging_context *msg_ctx,
        }
 
        while (pong_count < i) {
-               message_dispatch(msg_ctx);
-               smb_msleep(1);
+               ret = tevent_loop_once(evt_ctx);
+               if (ret != 0) {
+                       break;
+               }
        }
 
-       /* Now test that the duplicate filtering code works. */
+       /* Ensure all messages get through to ourselves. */
        pong_count = 0;
 
-       safe_strcpy(buf, "1234567890", sizeof(buf)-1);
+       strlcpy(buf, "1234567890", sizeof(buf));
 
        for (i=0;i<n;i++) {
-               messaging_send(msg_ctx, pid_to_procid(getpid()), MSG_PING,
+               messaging_send(msg_ctx, messaging_server_id(msg_ctx), MSG_PING,
                               &data_blob_null);
-               messaging_send_buf(msg_ctx, pid_to_procid(getpid()), MSG_PING,
-                                  (uint8 *)buf, 11);
+               messaging_send_buf(msg_ctx, messaging_server_id(msg_ctx),
+                                  MSG_PING,(uint8_t *)buf, 11);
        }
 
-       for (i=0;i<n;i++) {
-               message_dispatch(msg_ctx);
-               smb_msleep(1);
+       /*
+        * We have to loop at least 2 times for
+        * each message as local ping messages are
+        * handled by an immediate callback, that
+        * has to be dispatched, which sends a pong
+        * message, which also has to be dispatched.
+        * Above we sent 2*n messages, which means
+        * we have to dispatch 4*n times.
+        */
+
+       while (pong_count < n*2) {
+               ret = tevent_loop_once(evt_ctx);
+               if (ret != 0) {
+                       break;
+               }
        }
 
-       if (pong_count != 2) {
-               fprintf(stderr, "Duplicate filter failed (%d).\n", pong_count);
+       if (pong_count != 2*n) {
+               fprintf(stderr, "Message count failed (%d).\n", pong_count);
        }
 
        /* Speed testing */
@@ -114,7 +132,7 @@ static void pong_message(struct messaging_context *msg_ctx,
                        if(NT_STATUS_IS_OK(messaging_send_buf(
                                                   msg_ctx, pid_to_procid(pid),
                                                   MSG_PING,
-                                                  (uint8 *)buf, 11)))
+                                                  (uint8_t *)buf, 11)))
                           ping_count++;
                        if(NT_STATUS_IS_OK(messaging_send(
                                                   msg_ctx, pid_to_procid(pid),
@@ -122,25 +140,32 @@ static void pong_message(struct messaging_context *msg_ctx,
                           ping_count++;
 
                        while (ping_count > pong_count + 20) {
-                               message_dispatch(msg_ctx);
+                               ret = tevent_loop_once(evt_ctx);
+                               if (ret != 0) {
+                                       break;
+                               }
                        }
                }
-               
+
                printf("waiting for %d remaining replies (done %d)\n", 
                       (int)(ping_count - pong_count), pong_count);
                while (timeval_elapsed(&tv) < 30 && pong_count < ping_count) {
-                       message_dispatch(msg_ctx);
+                       ret = tevent_loop_once(evt_ctx);
+                       if (ret != 0) {
+                               break;
+                       }
                }
-               
+
                if (ping_count != pong_count) {
                        fprintf(stderr, "ping test failed! received %d, sent "
                                "%d\n", pong_count, (int)ping_count);
                }
-               
+
                printf("ping rate of %.0f messages/sec\n", 
                       (ping_count+pong_count)/timeval_elapsed(&tv));
        }
 
+       TALLOC_FREE(frame);
        return (0);
 }