commit 5807e6fba719fc04ecea419638143cebb52709da
parent a57abddf9f7da053e044d5d6fe5f4d85cd983de3
Author: Kyle Milz <kyle@0x30.net>
Date: Sat, 27 Feb 2016 21:04:19 -0700
server: get test notifications going
- add new test list_add_notify
- makes sure mutual friends get notified on new list additions
- move socket to testd.socket
- move testd into its own file again
- this way we can reuse the IPC::OpenN idioms
Diffstat:
4 files changed, 68 insertions(+), 32 deletions(-)
diff --git a/server/SL/Test.pm b/server/SL/Test.pm
@@ -81,7 +81,7 @@ sub new {
my $args = { phone_number => $self->{phnum}, os => 'unix' };
$self->{device_id} = $self->device_add($args);
- $self->device_update({ pushtoken_hex => "token_$self->{phnum}" }, 'ok');
+ $self->device_update( "token_$self->{phnum}" );
}
return $self;
@@ -262,8 +262,7 @@ sub msg_str {
package SL::Test::Notify;
use strict;
-use IO::Socket::UNIX;
-use JSON::XS;
+use IPC::Open2;
sub new {
my $class = shift;
@@ -271,41 +270,23 @@ sub new {
my $self = {};
bless ($self, $class);
- $self->{socket_path} = "../testd.socket";
-
- my $server = IO::Socket::UNIX->new(
- Type => SOCK_STREAM(),
- Local => $self->{socket_path},
- Listen => 1,
- );
- die "$self->{socket_path}: couldn't create socket: $!\n" unless ($server);
-
- while (my $client = $server->accept()) {
- $client->read(my $data, 4096);
- my $notify = decode_json($data);
-
- my $num_devices = @{ $notify->{devices} };
- next if ($num_devices == 0);
-
- print "testd: message type '$notify->{msg_type}'\n";
- # print "testd: payload is '" . Dumper($notify->{payload}) . "'\n";
-
- for (@{ $notify->{devices} }) {
- #print Dumper($_);
- my ($os, $push_token) = @$_;
- print "testd: sending to '$push_token' os '$os'\n";
- }
- }
+ my $pid = open2(\*CHLD_OUT, undef, "perl", "SL/testd.pl");
+ $self->{pid} = $pid;
+ $self->{chld_out} = \*CHLD_OUT;
return $self;
}
+sub readline {
+ my $self = shift;
+ return readline $self->{chld_out};
+}
+
sub DESTROY {
my $self = shift;
- unlink $self->{socket_path};
- #kill 'TERM', $self->{pid};
- #waitpid( $self->{pid}, 0 );
+ kill 'TERM', $self->{pid};
+ waitpid( $self->{pid}, 0 );
}
1;
diff --git a/server/SL/testd.pl b/server/SL/testd.pl
@@ -0,0 +1,35 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+$| = 1;
+
+use IO::Socket::UNIX;
+use JSON::XS;
+
+my $socket_path = "testd.socket";
+
+$SIG{TERM} = sub { unlink $socket_path; exit 0 };
+$SIG{INT} = sub { unlink $socket_path; exit 0 };
+
+my $server = IO::Socket::UNIX->new(
+ Type => SOCK_STREAM(),
+ Local => $socket_path,
+ Listen => 1,
+) or die "$socket_path: couldn't create socket: $!\n";
+
+while (my $client = $server->accept()) {
+ $client->read(my $data, 4096);
+ my $notify = decode_json($data);
+
+ my $num_devices = @{ $notify->{devices} };
+ next if ($num_devices == 0);
+
+ print "testd: message type '$notify->{msg_type}'\n";
+ # print "testd: payload is '" . Dumper($notify->{payload}) . "'\n";
+
+ for (@{ $notify->{devices} }) {
+ #print Dumper($_);
+ my ($os, $push_token) = @$_;
+ print "testd: sending to '$push_token' os '$os'\n";
+ }
+}
diff --git a/server/sl b/server/sl
@@ -162,7 +162,7 @@ sub handle_message {
# Send to notification daemons
send_unix("../apnd.socket", $msg, $msg_len) unless ($args{t});
send_unix("../gcmd.socket", $msg, $msg_len) unless ($args{t});
- send_unix("../testd.socket", $msg, $msg_len) if ($args{t});
+ send_unix("testd.socket", $msg, $msg_len) if ($args{t});
}
# Takes a device id and verifies it is present and valid.
diff --git a/server/t/list_add_notify.t b/server/t/list_add_notify.t
@@ -0,0 +1,20 @@
+use strict;
+use Test;
+use SL::Test;
+
+BEGIN { plan tests => 9 }
+
+my $s = SL::Test::Server->new();
+my $n = SL::Test::Notify->new();
+
+my $A = SL::Test::Client->new();
+my $B = SL::Test::Client->new();
+
+$A->friend_add($B->phnum());
+$B->friend_add($A->phnum());
+
+my $list = $A->list_add({ name => 'as list', date => 0 });
+
+# Check that notifications would have been sent
+ok($n->readline(), "/message type 'friend_added_list'/");
+ok($n->readline(), "/sending to 'token_.*' os 'unix'/");