r5197: moved events code to lib/events/ (suggestion from metze)
[Samba.git] / source / torture / local / messaging.c
blob958fcaa8f082b69d951c0b059ab5ee7e362ad93e
1 /*
2 Unix SMB/CIFS implementation.
4 local test for messaging code
6 Copyright (C) Andrew Tridgell 2004
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "includes.h"
24 #include "lib/events/events.h"
26 enum {MY_PING=1000, MY_PONG, MY_EXIT};
28 static void ping_message(struct messaging_context *msg, void *private,
29 uint32_t msg_type, uint32_t src, DATA_BLOB *data)
31 NTSTATUS status;
32 status = messaging_send(msg, src, MY_PONG, data);
33 if (!NT_STATUS_IS_OK(status)) {
34 printf("pong failed - %s\n", nt_errstr(status));
38 static void pong_message(struct messaging_context *msg, void *private,
39 uint32_t msg_type, uint32_t src, DATA_BLOB *data)
41 int *count = private;
42 (*count)++;
45 static void exit_message(struct messaging_context *msg, void *private,
46 uint32_t msg_type, uint32_t src, DATA_BLOB *data)
48 talloc_free(private);
49 exit(0);
53 test ping speed
55 static BOOL test_ping_speed(TALLOC_CTX *mem_ctx)
57 struct event_context *ev = event_context_init(mem_ctx);
58 struct messaging_context *msg_ctx;
59 int ping_count = 0;
60 int pong_count = 0;
61 BOOL ret = True;
62 struct timeval tv;
64 if (fork() == 0) {
65 struct messaging_context *msg_ctx2 = messaging_init(mem_ctx, 1, ev);
67 if (!msg_ctx2) {
68 exit(1);
71 messaging_register(msg_ctx2, NULL, MY_PING, ping_message);
72 messaging_register(msg_ctx2, mem_ctx, MY_EXIT, exit_message);
73 event_loop_wait(ev);
74 exit(0);
77 sleep(2);
79 msg_ctx = messaging_init(mem_ctx, 2, ev);
81 if (!msg_ctx) {
82 printf("messaging_init() failed\n");
83 return False;
86 messaging_register(msg_ctx, &pong_count, MY_PONG, pong_message);
88 tv = timeval_current();
90 printf("Sending pings for 10 seconds\n");
91 while (timeval_elapsed(&tv) < 10.0) {
92 DATA_BLOB data;
93 NTSTATUS status1, status2;
95 data.data = discard_const_p(uint8_t, "testing");
96 data.length = strlen((const char *)data.data);
98 status1 = messaging_send(msg_ctx, 1, MY_PING, &data);
99 status2 = messaging_send(msg_ctx, 1, MY_PING, NULL);
101 if (!NT_STATUS_IS_OK(status1)) {
102 printf("msg1 failed - %s\n", nt_errstr(status1));
105 if (!NT_STATUS_IS_OK(status2)) {
106 printf("msg2 failed - %s\n", nt_errstr(status2));
109 ping_count += 2;
111 while (ping_count > pong_count + 20) {
112 event_loop_once(ev);
116 printf("waiting for %d remaining replies (done %d)\n",
117 ping_count - pong_count, pong_count);
118 while (timeval_elapsed(&tv) < 30 && pong_count < ping_count) {
119 event_loop_once(ev);
122 printf("sending exit\n");
123 messaging_send(msg_ctx, 1, MY_EXIT, NULL);
125 if (ping_count != pong_count) {
126 printf("ping test failed! received %d, sent %d\n", pong_count, ping_count);
127 ret = False;
130 printf("ping rate of %.0f messages/sec\n",
131 (ping_count+pong_count)/timeval_elapsed(&tv));
133 talloc_free(msg_ctx);
135 talloc_free(ev);
136 return ret;
139 BOOL torture_local_messaging(void)
141 TALLOC_CTX *mem_ctx = talloc_init("torture_local_messaging");
142 BOOL ret = True;
144 ret &= test_ping_speed(mem_ctx);
146 talloc_free(mem_ctx);
148 return True;