Algorithm-Classifier-IsolationForest

 view release on metacpan or  search on metacpan

t/92-streamc.t  view on Meta::CPAN

#!perl
# 92-streamc.t
#
# Integration test for `iforest streamc`: spawns a streamd daemon (as
# t/91 does) and drives it through the real streamc CLI in a
# subprocess.  Covers command mode (--ping/--stats/--save, exit codes,
# --json), CSV stream mode in all three --mode settings with -d,
# --jsonl tagged rows, client- and daemon-side error attribution by
# input line, --set socket resolution, and connect-failure behaviour.
#
# Skipped on Windows (Unix sockets + fork) and without JSON::MaybeXS.

use strict;
use warnings;
use Test::More;
use File::Temp qw(tempdir);
use File::Spec;

my $bin = File::Spec->rel2abs('bin/iforest');
plan skip_all => 'bin/iforest not found' unless -x $bin;
plan skip_all => 'streamc needs Unix sockets and fork()' if $^O eq 'MSWin32';
plan skip_all => 'JSON::MaybeXS is not installed'
	unless eval { require JSON::MaybeXS; 1 };

my $JSON = JSON::MaybeXS->new( utf8 => 1 );

my $tmp  = tempdir( CLEANUP => 1 );
my $mdir = "$tmp/models";
my $logf = "$tmp/streamd.log";

plan skip_all => 'temp socket path too long for a Unix socket'
	if length("$tmp/alpha.sock") > 100;

my @ALL_PIDS;

END {
	for my $pid (@ALL_PIDS) {
		kill( 'TERM', $pid ) if kill( 0, $pid );
	}
}

sub spawn_daemon {
	my ( $wait_sock, @argv ) = @_;
	my $pid = fork();
	die "fork failed: $!" unless defined $pid;
	if ( !$pid ) {
		open( STDOUT, '>>', $logf ) or die $!;
		open( STDERR, '>>', $logf ) or die $!;
		exec( $^X, '-Ilib', $bin, 'streamd', '-f', @argv ) or die "exec failed: $!";
	}
	push @ALL_PIDS, $pid;
	for ( 1 .. 100 ) {
		last if -S $wait_sock;
		select( undef, undef, undef, 0.1 );    ## no critic (ProhibitSleepViaSelect)
	}
	return $pid;
} ## end sub spawn_daemon

# The daemon under test: a named set, so streamc's --set resolution is
# exercised by every call.
my $daemon = spawn_daemon(
	"$tmp/alpha.sock",
	'--set'           => 'alpha',
	'--socket'        => $tmp,
	'--pid'           => $tmp,
	'--model-dir'     => $mdir,
	'--save-interval' => 60,
	'-n'              => 20,
	'--window'        => 64,
	'--eta'           => 8,
	'-s'              => 42,
	'-t'              => 'cpu',
	'-t'              => 'mem'
);
ok( -S "$tmp/alpha.sock", 'daemon is up' ) or diag( scalar `cat $logf` );

my $sc = "$^X -Ilib $bin streamc --set alpha --socket $tmp";

# Fixture CSVs.
my $warm_csv = "$tmp/warm.csv";
my $q_csv    = "$tmp/q.csv";
my $bad_csv  = "$tmp/bad.csv";
{
	open my $fh, '>', $warm_csv or die $!;
	srand(3);



( run in 1.978 second using v1.01-cache-2.11-cpan-600a1bdf6e4 )