shlist

share and manage lists between multiple people
Log | Files | Refs

commit 80a9bc611dff7423c6f81ff40c72e33f3d9a1cd8
parent 10a3dd0857f9eb8257f927520c2d27d85e5099f4
Author: Kyle Milz <kyle@0x30.net>
Date:   Fri, 26 Feb 2016 20:42:31 -0700

apnd: switch test harness to TAP::Harness

- switch test suite over to TAP::Harness which is pretty good
- start a new server process in each test
  - this is the only way to prevent madness
  - also too running tests standalone will be easier

Diffstat:
Aapnd/APND.pm | 71+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aapnd/t/bad_msg.t | 20++++++++++++++++++++
Aapnd/t/non_ios.t | 28++++++++++++++++++++++++++++
Aapnd/test.pl | 9+++++++++
Dapnd/test.sh | 6------
Dapnd/test_non_ios.sh | 5-----
6 files changed, 128 insertions(+), 11 deletions(-)

diff --git a/apnd/APND.pm b/apnd/APND.pm @@ -0,0 +1,71 @@ +package APND::Server; +use strict; + +use IPC::Open3; + +sub new { + my $class = shift; + + my $self = {}; + bless ($self, $class); + + my $socket_path = "apnd_test.socket"; + + my $pid = open3(undef, undef, \*CHLD_ERR, "apnd", "-p", $socket_path); + + $self->{pid} = $pid; + $self->{CHLD_ERR} = \*CHLD_ERR; + return $self; +} + +sub readline { + my $self = shift; + + return readline $self->{CHLD_ERR}; +} + +sub kill { + my $self = shift; + + kill 'TERM', $self->{pid}; + waitpid( $self->{pid}, 0 ); +} + +1; + +package APND::Socket; +use strict; + +use IO::Socket::UNIX; + +sub new { + my $class = shift; + + my $self = {}; + bless ($self, $class); + + my $socket_path = "apnd_test.socket"; + + my $socket = undef; + my $i = 0; + while (! $socket) { + $socket = IO::Socket::UNIX->new( + Type => SOCK_STREAM(), + Peer => $socket_path + ); + $i++; + } + die "$socket_path: connect failed: $!\n" unless ($socket); + + print STDERR "looped $i times\n"; + + $self->{socket} = $socket; + return $self; +} + +sub write { + my ($self, $data) = @_; + $self->{socket}->syswrite($data); +} + +1; diff --git a/apnd/t/bad_msg.t b/apnd/t/bad_msg.t @@ -0,0 +1,20 @@ +use strict; +use Test; + +BEGIN { plan tests => 1 } + +use APND; +use JSON::XS; + +my $server = APND::Server->new(); +my $socket = APND::Socket->new(); + +my $msg = { +}; + +my $encoded_json = encode_json($msg); +$socket->write($encoded_json); + +ok($server->readline(), "/sending message type '' to 0 device/"); + +$server->kill(); diff --git a/apnd/t/non_ios.t b/apnd/t/non_ios.t @@ -0,0 +1,28 @@ +use strict; +use Test; + +BEGIN { plan tests => 3 } + +use APND; +use JSON::XS; + +my $server = APND::Server->new(); +my $socket = APND::Socket->new(); + +my $msg = { + msg_type => "updated_list", + payload => { }, + devices => [ + [ "not_ios", "hex" ], + [ "android", "some_token" ] + ] +}; + +my $encoded_json = encode_json($msg); +$socket->write($encoded_json); + +ok($server->readline(), "/sending message type 'updated_list' to 2 device/"); +ok($server->readline(), '/hex: not an ios device/'); +ok($server->readline(), '/some_token: not an ios device/'); + +$server->kill(); diff --git a/apnd/test.pl b/apnd/test.pl @@ -0,0 +1,9 @@ +use strict; +use warnings; + +use TAP::Harness; + +my $harness = TAP::Harness->new({}); + +my @test_files = ("t/non_ios.t", "t/bad_msg.t"); +$harness->runtests(@test_files); diff --git a/apnd/test.sh b/apnd/test.sh @@ -1,6 +0,0 @@ -#!/bin/sh - -hex_token="DE2D368BB6C80E1D8BCB86D20CB6C2161BD5CEC5BA35A1E1AA0DB382849ED9B2" -json_str="{\"msg_type\":\"updated_list\",\"payload\":{},\"devices\":[[\"ios\",\"$hex_token\"]]}" - -echo "$json_str" | nc -U ../apnd.socket - diff --git a/apnd/test_non_ios.sh b/apnd/test_non_ios.sh @@ -1,5 +0,0 @@ -#!/bin/sh - -json_str='{"msg_type":"updated_list","payload":{},"devices":[["not_ios","hex"],["android","some_token"]]}' - -echo "$json_str" | nc -U ../apnd.socket -