-
Notifications
You must be signed in to change notification settings - Fork 15
/
54-psgix-io.t
160 lines (143 loc) · 4.58 KB
/
54-psgix-io.t
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
#!perl
use warnings;
use strict;
use constant CLIENTS => $ENV{RELEASE_TESTING} ? 10 : 2;
use constant ROUNDS => $ENV{RELEASE_TESTING} ? 25 : 4;
use Scalar::Util qw/refaddr/;
use Test::More tests => 3 + ROUNDS*(
CLIENTS*5 + # server setup
CLIENTS*3 + # client setup
CLIENTS + # server msg
CLIENTS + # client send
CLIENTS*CLIENTS + # client msg
4 # each round
);
use lib 't'; use Utils;
BEGIN { use_ok('Feersum') };
my ($socket,$port) = get_listen_socket();
ok $socket, "made listen socket";
my $evh = Feersum->new();
{
no warnings 'redefine';
*Feersum::DIED = sub {
my $err = shift;
fail "Died during request handler: $err";
};
}
$evh->use_socket($socket);
our $CRLF = "\015\012";
my $app = sub {
my $env = shift;
is $env->{HTTP_UPGRADE}, 'chatz', "server setup: got an upgrade req";
my $n = $env->{HTTP_X_CLIENT};
return sub {
my $respond = shift;
do_chat($n,$env);
};
};
$evh->psgi_request_handler($app);
my $cv;
# read lines, broadcast to other server-side handles
my @ss_handles;
sub do_chat {
my ($n, $env, $strm) = @_;
$cv->begin;
my $fh = $env->{'psgix.io'};
isa_ok $fh, 'IO::Socket', "server setup: $n fh";
my $websocket_turd;
read $fh, $websocket_turd, 8;
is $websocket_turd, '12345678', "server setup: $n websocket turd";
my $fh2 = $env->{'psgix.io'};
is refaddr($fh), refaddr($fh2),
"server setup: duplicated psgix.io read results in same handle";
# use AnyEvent::Handle here specifically as that's what Web::Hippie
# uses.
my $h = AnyEvent::Handle->new(fh => $fh);
$ss_handles[$n] = $h;
$h->{guard} = guard { pass "server setup: $n destroyed" };
$h->push_write("HTTP/1.1 101 Switching Protocols$CRLF$CRLF");
$h->push_read(line => sub {
my $line = $_[1];
is $line, "hello from $n", "server msg: read a line for server $n";
$line .= "\n";
$ss_handles[$_]->push_write($line) for (1..CLIENTS);
$cv->end;
});
$h->on_error(sub {
diag "server handle error: $_[2]";
$h->destroy; # important self-ref
$cv->croak("server handle: ".$_[2]);
});
}
for my $round (1..ROUNDS) {
$cv = AE::cv;
# connect a number of clients, keeping the handle in a client-side handles
# array. Once all of the clients are connected ($connect_cv synchronizes
# them) send a "hello from" line for each client. Check that every client
# gets every message.
my @cs_handles;
my $connect_cv = AE::cv(sub {
pass "round $round : all clients connected, sending chats...";
eval {
for my $n (1 .. CLIENTS) {
my $h = $cs_handles[$n];
$h->push_write("hello from $n\n");
pass "client send: wrote to $n";
}
};
warn "during connect cv: $@" if $@;
});
$connect_cv->begin;
for my $n (1 .. CLIENTS) {
$connect_cv->begin;
$cv->begin;
my $h = AnyEvent::Handle->new(
connect => ['127.0.0.1',$port],
timeout => 3,
);
$cs_handles[$n] = $h;
$h->{guard} = guard { pass "client setup: $n destroyed" };
$h->on_error(sub {
diag "client handle error: $_[2]";
$h->destroy;
$connect_cv->croak("client handle: ".$_[2]);
$cv->croak("client handle: ".$_[2]);
});
$h->push_write("GET / HTTP/1.1$CRLF".
"Upgrade: chatz$CRLF".
"X-Client: $n$CRLF".
$CRLF.
"12345678" # extra websocket v76 hanshake turd
);
# one line for the upgrade, CLIENTS lines for the chatting
$h->push_read(line => qr/$CRLF$CRLF/, sub {
my $line = $_[1];
is $line, 'HTTP/1.1 101 Switching Protocols',
"client setup: client $n got upgraded";
$connect_cv->end;
});
my $to_read = CLIENTS;
$h->push_read(line => sub {
my $line = $_[1];
$to_read--;
like $line, qr/^hello from \d+$/,
"client msg: $n got a chat, $to_read left";
unless ($to_read) {
pass "client setup: client $n is done";
$cv->end;
}
}) for (1 .. CLIENTS);
}
$connect_cv->end;
$connect_cv->recv;
pass "round: all connected";
$cv->recv;
pass "round: done round $round";
$_->destroy() for grep {defined} @cs_handles;
@cs_handles = ();
$_->destroy() for grep {defined} @ss_handles;
@ss_handles = ();
pass "round: cleaned up round $round";
}
pass "all done";
done_testing;