summaryrefslogtreecommitdiff
path: root/source4/torture/local/messaging.c
blob: 75e51fb53441ea8a7efa47213d41eae82bac6460 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* 
   Unix SMB/CIFS implementation.

   local test for messaging code

   Copyright (C) Andrew Tridgell 2004
   
   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 2 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.
*/

#include "includes.h"
#include "system/filesys.h"
#include "lib/events/events.h"

enum {MY_PING=1000, MY_PONG, MY_EXIT};

static void ping_message(struct messaging_context *msg, void *private, 
			 uint32_t msg_type, uint32_t src, DATA_BLOB *data)
{
	NTSTATUS status;
	status = messaging_send(msg, src, MY_PONG, data);
	if (!NT_STATUS_IS_OK(status)) {
		printf("pong failed - %s\n", nt_errstr(status));
	}
}

static void pong_message(struct messaging_context *msg, void *private, 
			 uint32_t msg_type, uint32_t src, DATA_BLOB *data)
{
	int *count = private;
	(*count)++;
}

static void exit_message(struct messaging_context *msg, void *private, 
			 uint32_t msg_type, uint32_t src, DATA_BLOB *data)
{
	talloc_free(private);
	exit(0);
}

/*
  test ping speed
*/
static BOOL test_ping_speed(TALLOC_CTX *mem_ctx)
{
	struct event_context *ev;
	struct messaging_context *msg_ctx;
	struct messaging_context *msg_ctx2;
	int ping_count = 0;
	int pong_count = 0;
	BOOL ret = True;
	struct timeval tv;

	lp_set_cmdline("lock dir", "lockdir.tmp");

	ev = event_context_init(mem_ctx);

	msg_ctx2 = messaging_init(mem_ctx, 1, ev);
	
	if (!msg_ctx2) {
		exit(1);
	}
		
	messaging_register(msg_ctx2, NULL, MY_PING, ping_message);
	messaging_register(msg_ctx2, mem_ctx, MY_EXIT, exit_message);

	msg_ctx = messaging_init(mem_ctx, 2, ev);

	if (!msg_ctx) {
		printf("messaging_init() failed\n");
		return False;
	}

	messaging_register(msg_ctx, &pong_count, MY_PONG, pong_message);

	tv = timeval_current();

	printf("Sending pings for 10 seconds\n");
	while (timeval_elapsed(&tv) < 10.0) {
		DATA_BLOB data;
		NTSTATUS status1, status2;

		data.data = discard_const_p(uint8_t, "testing");
		data.length = strlen((const char *)data.data);

		status1 = messaging_send(msg_ctx, 1, MY_PING, &data);
		status2 = messaging_send(msg_ctx, 1, MY_PING, NULL);

		if (!NT_STATUS_IS_OK(status1)) {
			printf("msg1 failed - %s\n", nt_errstr(status1));
		}

		if (!NT_STATUS_IS_OK(status2)) {
			printf("msg2 failed - %s\n", nt_errstr(status2));
		}

		ping_count += 2;

		while (ping_count > pong_count + 20) {
			event_loop_once(ev);
		}
	}

	printf("waiting for %d remaining replies (done %d)\n", 
	       ping_count - pong_count, pong_count);
	while (timeval_elapsed(&tv) < 30 && pong_count < ping_count) {
		event_loop_once(ev);
	}

	printf("sending exit\n");
	messaging_send(msg_ctx, 1, MY_EXIT, NULL);

	if (ping_count != pong_count) {
		printf("ping test failed! received %d, sent %d\n", 
		       pong_count, ping_count);
		ret = False;
	}

	printf("ping rate of %.0f messages/sec\n", 
	       (ping_count+pong_count)/timeval_elapsed(&tv));

	talloc_free(msg_ctx);

	talloc_free(ev);

	return ret;
}

BOOL torture_local_messaging(void) 
{
	TALLOC_CTX *mem_ctx = talloc_init("torture_local_messaging");
	BOOL ret = True;

	ret &= test_ping_speed(mem_ctx);

	talloc_free(mem_ctx);

	return True;
}