aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_logger.c
blob: 41467ee6e95e139c41b80dc3ad2da3c3c9d7cee8 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2009, Digium, Inc.
 *
 * Kevin P. Fleming <kpfleming@digium.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*! \file
 *
 * \brief Test module for the logging subsystem
 *
 * \author\verbatim Kevin P. Fleming <kpfleming@digium.com> \endverbatim
 *
 * \ingroup tests
 */

/*** MODULEINFO
	<defaultenabled>no</defaultenabled>
	<support_level>extended</support_level>
 ***/

#include "asterisk.h"

ASTERISK_FILE_VERSION(__FILE__, "$Revision$")

#include "asterisk/file.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/module.h"
#include "asterisk/lock.h"
#include "asterisk/app.h"
#include "asterisk/cli.h"

struct test {
	const char *name;
	unsigned int x_success;
	unsigned int x_failure;
	unsigned int u_success;
	unsigned int u_failure;
};

static void output_tests(struct test *tests, size_t num_tests, int fd)
{
	unsigned int x;

	for (x = 0; x < num_tests; x++) {
		ast_cli(fd, "Test %d: %s\n", x + 1, tests[x].name);
		ast_cli(fd, "\tExpected Successes: %d\n", tests[x].x_success);
		ast_cli(fd, "\tExpected Failures: %d\n", tests[x].x_failure);
		ast_cli(fd, "\tUnexpected Successes: %d\n", tests[x].u_success);
		ast_cli(fd, "\tUnexpected Failures: %d\n", tests[x].u_failure);
		ast_cli(fd, "Test %d Result: %s\n", x + 1, (tests[x].u_success + tests[x].u_failure) ? "FAIL" : "PASS");
	}
}

static char *handle_cli_dynamic_level_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
	unsigned int level;
	unsigned int x;
	unsigned int test;
	struct test tests[] = {
		{ .name = "Simple register/message/unregister",
		},
		{ .name = "Register multiple levels",
		},
	};

	switch (cmd) {
	case CLI_INIT:
		e->command = "logger test dynamic";
		e->usage = ""
			"Usage: logger test dynamic\n"
			"";
		return NULL;
	case CLI_GENERATE:
		return NULL;
	}

	for (test = 0; test < ARRAY_LEN(tests); test++) {
		ast_cli(a->fd, "Test %d: %s.\n", test + 1, tests[test].name);
		switch (test) {
		case 0:
			if ((level = ast_logger_register_level("test")) != -1) {
				ast_cli(a->fd, "Test: got level %d\n", level);
				ast_log_dynamic_level(level, "Logger Dynamic Test: Test 1\n");
				ast_logger_unregister_level("test");
				tests[test].x_success++;
			} else {
				ast_cli(a->fd, "Test: Failed, could not register level 'test'.\n");
				tests[test].u_failure++;
			}
			break;
		case 1:
		{
			char level_name[18][8];

			for (x = 0; x < ARRAY_LEN(level_name); x++) {
				sprintf(level_name[x], "level%02d", x);
				if ((level = ast_logger_register_level(level_name[x])) == -1) {
					if (x < 16) {
						tests[test].u_failure++;
					} else {
						tests[test].x_failure++;
					}
					level_name[x][0] = '\0';
				} else {
					ast_cli(a->fd, "Test: registered '%s', got level %d\n", level_name[x], level);
					if (x < 16) {
						tests[test].x_success++;
					} else {
						tests[test].u_success++;
					}
				}
			}

			for (x = 0; x < ARRAY_LEN(level_name); x++) {
				if (!ast_strlen_zero(level_name[x])) {
					ast_logger_unregister_level(level_name[x]);
				}
			}
		}
		}
	}

	output_tests(tests, ARRAY_LEN(tests), a->fd);

	return CLI_SUCCESS;
}

static char *handle_cli_performance_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
	unsigned int level;
	unsigned int test;
	struct test tests[] = {
		{ .name = "Log 10,000 messages",
		},
	};

	switch (cmd) {
	case CLI_INIT:
		e->command = "logger test performance";
		e->usage = ""
			"Usage: logger test performance\n"
			"";
		return NULL;
	case CLI_GENERATE:
		return NULL;
	}

	for (test = 0; test < ARRAY_LEN(tests); test++) {
		ast_cli(a->fd, "Test %d: %s.\n", test + 1, tests[test].name);
		switch (test) {
		case 0:
			if ((level = ast_logger_register_level("perftest")) != -1) {
				unsigned int x;
				struct timeval start, end;
				int elapsed;

				ast_cli(a->fd, "Test: got level %d\n", level);
				start = ast_tvnow();
				for (x = 0; x < 10000; x++) {
					ast_log_dynamic_level(level, "Performance test log message\n");
				}
				end = ast_tvnow();
				elapsed = ast_tvdiff_ms(end, start);
				ast_cli(a->fd, "Test: 10,000 messages in %f seconds.\n", (float) elapsed / 1000);
				ast_logger_unregister_level("perftest");
				tests[test].x_success++;
			} else {
				ast_cli(a->fd, "Test: Failed, could not register level 'perftest'.\n");
				tests[test].u_failure++;
			}
			break;
		}
	}

	output_tests(tests, ARRAY_LEN(tests), a->fd);

	return CLI_SUCCESS;
}

static struct ast_cli_entry cli_logger[] = {
	AST_CLI_DEFINE(handle_cli_dynamic_level_test, "Test the dynamic logger level implementation"),
	AST_CLI_DEFINE(handle_cli_performance_test, "Test the logger performance"),
};

static int unload_module(void)
{
	ast_cli_unregister_multiple(cli_logger, ARRAY_LEN(cli_logger));
	return 0;
}

static int load_module(void)
{
	ast_cli_register_multiple(cli_logger, ARRAY_LEN(cli_logger));
	return AST_MODULE_LOAD_SUCCESS;
}

AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Logger Test Module");