File: callbacks.tt

package info (click to toggle)
libio-event-perl 0.813-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 300 kB
  • sloc: perl: 3,176; makefile: 2
file content (263 lines) | stat: -rwxr-xr-x 4,997 bytes parent folder | download | duplicates (3)
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/usr/bin/perl -I.

use strict;

my $slowest = 4;
my $pause = 0;
my $debug = 0;

my $c = 1;
$| = 1;
my $testcount = 6;

use Carp qw(verbose);
use Sys::Hostname;

my $startingport = 1025;

package T;

use IO::Event;
use IO::Socket::INET;
use Carp;

BEGIN {
	eval { require Time::HiRes };
	if ($@) {
		print "1..0 $@";
		exit;
	}
}

# support routine
sub pickport
{
	for (my $i = 0; $i < 1000; $i++) {
		my $s = new IO::Socket::INET (
			Listen => 1,
			LocalPort => $startingport,
		);
		if ($s) {
			$s->close();
			return $startingport++;
		}
		$startingport++;
	}
	die "could not find an open port";
}

# print "ok N" or "not ok N"
sub okay
{
        my ($cond, $message) = @_;
        if ($cond) {
		$message =~ s/\n/\\n/g;
                print "ok $c # $message\n";
        } else {
		my($package, $filename, $line, $subroutine, $hasargs, $wantarray, $evaltext, $is_require) = caller(0);
		print "not ok $c # $filename:$line $message\n";
        }
	if ($c > $testcount) {
		print STDERR "too many test results\n";
		exit(0);
	}
        $c++;
}

# default to oops
sub ie_input
{
	confess "we shoudn't be here";
}

print "1..$testcount\n";

# let's listen on a socket.  We'll expect to receive
# test numbers.  We'll print ok.

my $rp = T::pickport;
my $results = IO::Event::Socket::INET->new(
	Listen => 10,
	Proto => 'tcp',
	LocalPort => $rp,
	LocalAddr => '127.0.0.1',
	Handler => 'ListenPrintOkay',
	Description => 'Listener',
);

die unless $results;
die unless $results->filehandle;

my $fh = $results->filehandle;
my $fn = $fh->fileno;

my $idle;
my $time = time;
my $waitingfor = $c;
my $ptime;

# first number is number of test events for this test
my (@tests) = (
	1 =>	sub {
			my $s = IO::Socket::INET->new(
				Proto => 'tcp',
				PeerPort => $rp,
				PeerAddr => '127.0.0.1',
			);
			$s or T::okay(0, "Create socket to 127.0.0.1:$rp: $!");
			my $ioe = SendOne::registerIOE($s);
			$ioe or T::okay(0, "resiter IOE");
			$ioe->print("$c\n");
		},
	1 =>	sub {
			IO::Event::Socket::INET->new (
				Proto => 'tcp',
				PeerPort => $rp,
				PeerAddr => '127.0.0.1',
				Handler => 'SendOne',
			) or T::okay(0, "create Sendone to $rp: $@");
		},
	1 =>	sub {
			my $rp = T::pickport;
			IO::Event::Socket::INET->new(
				Listen => 1,
				Proto => 'tcp',
				LocalPort => $rp,
				LocalAddr => '127.0.0.1',
				Handler => 'ListenGetLine',
				Description => 'Listener2',
			) or T::okay(0, "create listener2 at $rp: $@");
			IO::Event::Socket::INET->new (
				Proto => 'tcp',
				PeerPort => $rp,
				PeerAddr => '127.0.0.1',
				Handler => 'SendOne',
			) or T::okay(0, "create SendOne2 to $rp: $@");
		},
);

my $timer = IO::Event->timer (
	cb => \&runstuff,
	reentrant => 0,
	repeat => 1,
	interval => 0.05,
);

okay($results, "start listening on results socket");

my $r = IO::Event::loop();
okay($r == 7, "loop finshed ($r)");

okay(1, "all done");
exit(0);

my $run1er;
sub runstuff
{
	if ($ptime) {
		return if (time < $ptime + $pause);
	} elsif ($c >= $waitingfor) {
		print STDERR "runstuff: time to start another test\n"
			if $debug;
		# T::okay(1, "runstuff happy");
		$ptime = time;
	} elsif (time - $time > $slowest) {
		print STDERR "runstuff: uh oh: test timed out\n"
			if $debug;
		# let's start the next test anyway...
		T::okay(0, "runstuff timetout");
		$ptime = time;
	} else {
		print STDERR "runstuff: idle\n"
			if $debug && (time > $idle);
		$idle = time;
		return;
	}
	unless (@tests) {
		print STDERR "runstuff: no more tests\n"
			if $debug;
		IO::Event::unloop_all(7);
		return;
	}
	return if $pause && (time < $ptime + $pause);
	undef $ptime;
	my ($count, $test) = splice(@tests, 0, 2);
	$waitingfor = $c + $count;
	$time = time;
	print STDERR "runstuff: starting another test ($c + $count)\n"
		if $debug;
	eval { &$test };
	T::okay(0, "test evaled: $@")
		if $@;
}

# Once we're connected, send the current test number to the server.
# print the reply.
package SendOne;

sub registerIOE
{
	my ($s) = @_;
	IO::Event->new($s);
}

sub ie_connected
{
	my ($self, $s1) = @_;
	$s1->print("$c\n");
}

sub ie_input
{
	my ($self, $s, $br) = @_;
	print $s->getlines();
}

# Listen for connections.  When One is received, and input is received
# on it, wait for input.  The input should be the current test number.
# Use "get()"
#
package ListenPrintOkay;

sub ie_connection
{
	my ($self, $s) = @_;
	my $x = $s->accept('ReceivePrintOkay');
}

package ReceivePrintOkay;

sub ie_input
{
	my ($self, $s) = @_;
	my $l;
	while (defined ($l = $s->get)) {
		T::okay($l eq $c, "input '$l' == '$c' on results socket");
	}
}

#
# Listen for connections.  When One is received, and input is received
# on it, wait for input.  The input should be the current test number.
# Use "getline()"
#
package ListenGetLine;

sub ie_connection
{
	my ($self, $s) = @_;
	$s->accept('ReceiveGetLine');
}

package ReceiveGetLine;

sub ie_input
{
	my ($self, $s) = @_;
	my $l;
	while (defined ($l = $s->getline)) {
		T::okay($l eq "$c\n", "input '$l' == '$c' on results socket");
	}
}

1;