1#!/usr/bin/perl
2#
3# DESCRIPTION:
4#       Test sending lots of results at the same time
5#
6# COPYRIGHT:
7#       Copyright (C) 2007 Altinity Limited
8#       Copyright is freely given to Ethan Galstad if included in the NSCA distribution
9#
10# LICENCE:
11#       GNU GPLv2
12
13
14use strict;
15use NSCATest;
16use Test::More;
17use Clone qw(clone);
18use Parallel::Forker;
19
20my $iterations = 100;
21my $timeout = 8;
22
23plan tests => 4;
24
25my $data = [
26        ["hostname", "0", "Plugin output"],
27        ["hostname-with-other-bits", "1", "More data to be read"],
28        ["hostname.here", "2", "Check that ; are okay to receive"],
29        ["host", "service", 0, "A good result here"],
30        ["host54", "service with spaces", 1, "Warning! My flies are undone!"],
31        ["host-robin", "service with a :)", 2, "Critical? Alert! Alert!"],
32        ["host-batman", "another service", 3, "Unknown - the only way to travel"],
33	];
34
35
36my $Fork = Parallel::Forker->new;
37$SIG{CHLD} = sub { $Fork->sig_child; };
38$SIG{TERM} = sub { $Fork->kill_tree_all('TERM') if $Fork; die "Quitting..."; };
39
40foreach my $type qw(--single --daemon) {
41	my $expected = [];
42	my $nsca = NSCATest->new( config => "basic", timeout => $timeout );
43
44	$nsca->start($type);
45
46	for (my $i = 0; $i < $iterations; $i++) {
47		my $c = clone($data);
48		push @$c, [ "host$i", 2, "Some unique data: ".rand() ];
49		push @$expected, @$c;
50		$Fork->schedule(
51			run_on_start => sub { $nsca->send($c) },
52			);
53	}
54
55	$Fork->ready_all;
56	$Fork->wait_all;
57
58	sleep 1;		# Need to wait for --daemon to finish processing
59
60	my $output = $nsca->read_cmd;
61
62	is( scalar @$output, scalar @$expected, "Got all ".scalar @$expected." packets of data" );
63	is_deeply_sorted( $output, $expected, "All data as expected" );
64
65	$nsca->stop;
66}
67
68sub is_deeply_sorted {
69	my ($expected, $against, $text) = @_;
70	my $e = [ sort map { join(";", map { $_ } @$_) } @$expected ];
71	my $a = [ sort map { join(";", map { $_ } @$_) } @$against ];
72	is_deeply($e, $a, $text);
73}
74
75