Skip to content
This repository
tree: 54c8ef7c2f
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 81 lines (70 sloc) 2.027 kb
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
use strict;
use Find::Lib libs => '.', '../lib';
use Test::More tests => 10;
use ControlFreak;
use AnyEvent;
use AnyEvent::Handle;

use_ok 'ControlFreak::Proxy';
require 'testutils.pl';
shutoff_logs();

my $ctrl = ControlFreak->new();
my $error;
my $ok;

### Helpers
sub process {
    $error = undef;
    $ok = undef;
    ControlFreak::Command->process(
        ctrl => $ctrl,
        err_cb => sub { $error = shift },
        ok_cb => sub { $ok = 1 },
        @_
    );
    return;
};

sub like_error {
    my $re = shift;
    process(@_);
    !$error ? ok 0, "no error" : like $error, $re, "error in process";
}

sub process_ok {
    process(@_);
    my %p = @_;
    ok $ok, $p{cmd};
}

## test proxy interface
{
    my $s = Find::Lib->catfile('..', 'bin', 'cfk-share-mem-proxy.pl');
    my $p = Find::Lib->catfile('preload.pl');
    my $i = Find::Lib->catdir('../lib');
    my $cmd = Find::Lib->catfile('sleeper.pl');
    my $ctrl = ControlFreak->new();
    my $svc = ControlFreak::Service->new(
        ctrl => $ctrl,
        name => 'somesvc',
        cmd => $cmd,
        startwait_secs => .25,
    );

    my $proxy = ControlFreak::Proxy->new(
        ctrl => $ctrl,
        name => 'a',
        cmd => "$^X -I $i $s --preload $p",
    );
    ok $proxy->auto, "proxy is auto";
    $proxy->add_service($svc);
    is scalar $proxy->services, 1, "one service";

    ok $svc->{proxy}, "proxy is still there";
    is $svc->{proxy}, $proxy, "same proxy";
    ok ! $proxy->is_running, "proxy not running yet";

    $svc->start;
    ok $svc->is_starting, "service is starting";
    wait_for (sub { $svc->is_running });
    ok $svc->is_running, "svc is running";
    ok $proxy->is_running, "proxy automatically started";
    wait_for (sub { $svc->is_running });
    $svc->stop;
    wait_for (sub { !$proxy->is_running }, 4);
    ok !$proxy->is_running, "proxy automatically stopped";

    ## Service is dying, proxy should restart each time too. getting a new PID
    #$svc->set_cmd(Find::Lib->catfile('die.pl'));
}
Something went wrong with that request. Please try again.