[Slim-Checkins] r8875 - in /trunk/tests: lib/Slim/Test/Base.pm t/00use.t t/01server.t

dsully at svn.slimdevices.com dsully at svn.slimdevices.com
Tue Aug 8 18:29:07 PDT 2006


Author: dsully
Date: Tue Aug  8 18:29:06 2006
New Revision: 8875

URL: http://svn.slimdevices.com?rev=8875&view=rev
Log:
Bug: N/A
Description: Add code & tests to start & stop SlimServer.

Added:
    trunk/tests/t/01server.t
Modified:
    trunk/tests/lib/Slim/Test/Base.pm
    trunk/tests/t/00use.t

Modified: trunk/tests/lib/Slim/Test/Base.pm
URL: http://svn.slimdevices.com/trunk/tests/lib/Slim/Test/Base.pm?rev=8875&r1=8874&r2=8875&view=diff
==============================================================================
--- trunk/tests/lib/Slim/Test/Base.pm (original)
+++ trunk/tests/lib/Slim/Test/Base.pm Tue Aug  8 18:29:06 2006
@@ -5,34 +5,43 @@
 use strict;
 use base qw(Test::Class);
 
-use Cwd;
 use Config;
-use Getopt::Long;
 use FindBin qw($Bin);
+use File::Path;
+use File::Slurp;
 use File::Spec::Functions qw(:ALL);
 use Test::More;
 
-use lib qw(../server);
+use lib "$Bin/../../server";
+use lib "$Bin/../../server/lib";
 
 BEGIN {
 	use Slim::bootstrap;
 	use Slim::Utils::OSDetect;
 
-	Slim::bootstrap->loadModules(
-		[qw(Time::HiRes DBD::mysql DBI HTML::Parser XML::Parser::Expat YAML::Syck)],
-		[],
-		'../server',
-	);
-};
+	Slim::bootstrap->loadModules(undef, undef, "$Bin/../../server")
+}
 
 # Do some basics
+use Proc::Background;
 use Slim::Utils::OSDetect;
 use Slim::Utils::Prefs;
 use Slim::Utils::Timers;
 
-my $dbName = 'slimserver';
+my $dbName  = 'slimserver';
+my $baseDir = "$Bin/../../server";
+my $tmpDir  = "$Bin/../tmp/$$";
+my $bgProc  = undef;
 
-{
+mkpath($tmpDir);
+
+END {
+	if (-d $tmpDir) {
+		rmtree($tmpDir);
+	}
+}
+
+sub foo {
 	# Remove any existing prefs file.
 	# unlink(Slim::Utils::Prefs::prefsFile());
 
@@ -57,6 +66,111 @@
 	Slim::Utils::Prefs::set('dbsource', sprintf('dbi:mysql:database=%s', $dbName));
 }
 
+sub pidFile {
+	return "$tmpDir/slimserver.pid";
+}
+
+sub prefFile {
+	return "$tmpDir/slimserver.pref";
+}
+
+sub logFile {
+	return "$tmpDir/slimserver.log";
+}
+
+sub cacheDir {
+	return "$tmpDir/cache";
+}
+
+sub audioDir {
+
+}
+
+sub serverCommandLine {
+	my $class = shift;
+
+	return (
+		"$baseDir/slimserver.pl",
+		sprintf("--pidfile=%s", $class->pidFile),
+		sprintf("--prefsfile=%s", $class->prefFile),
+		sprintf("--logfile=%s", $class->logFile),
+		sprintf("--cachedir=%s", $class->cacheDir),
+	);
+}
+
+sub startServer {
+	my $class = shift;
+
+	$bgProc = Proc::Background->new($class->serverCommandLine(@_));
+}
+
+sub stopServer {
+	my $class = shift;
+
+	if ($bgProc && $bgProc->alive) {
+		$bgProc->die;
+		$bgProc->wait;
+	}
+
+	# Still alive?
+	# Try to use the SlimServer PID
+	if ($bgProc && $bgProc->alive) {
+
+		my $pid = eval { read_file($class->pidFile) };
+
+		# Try and kill the process.
+		if ($pid) {
+			kill('TERM', $pid);
+			sleep 5;
+		}
+
+		# Check to see if it's been killed - try harder.
+		if ($pid && kill(0, $pid)) {
+
+			kill('KILL', $pid);
+			sleep 5;
+
+			if (kill(0, $pid)) {
+				return 0;
+			}
+		}
+	}
+
+	for my $file ($class->pidFile, $class->prefFile, $class->logFile, $class->cacheDir) {
+		rmtree($file);
+	}
+
+	return 1;
+}
+
+sub restartServer {
+	my $class = shift;
+
+	my $ret   = $class->stopServer(@_);
+
+	if ($ret) {
+		return $class->startServer(@_);
+	}
+
+	return $ret;
+}
+
+sub serverIsAlive {
+	my $class = shift;
+
+	if ($bgProc && $bgProc->alive) {
+		return 1;
+	}
+
+	my $pid   = eval { read_file($class->pidFile) };
+
+	if ($pid && kill(0, $pid)) {
+		return 1;
+	}
+
+	return 0;
+}
+
 sub main::cleanup {};
 
 1;

Modified: trunk/tests/t/00use.t
URL: http://svn.slimdevices.com/trunk/tests/t/00use.t?rev=8875&r1=8874&r2=8875&view=diff
==============================================================================
--- trunk/tests/t/00use.t (original)
+++ trunk/tests/t/00use.t Tue Aug  8 18:29:06 2006
@@ -5,24 +5,19 @@
 # Load all the modules included with SlimServer
 
 use strict;
+use lib qw(lib);
 use FindBin qw($Bin);
-
-use lib "$Bin/../../server";
-use lib "$Bin/../lib";
 
 use File::Find::Rule;
 use Test::More qw(no_plan);
 
 BEGIN {
+	use_ok('Slim::Test::Base');
 	use_ok('Slim::bootstrap');
-	use_ok('Slim::Utils::OSDetect');
-	use_ok('Slim::Test::Base');
-
-	ok(Slim::bootstrap->loadModules());
 }
 
 my @dirs  = ("$Bin/../../server/Slim", "$Bin/../../server/Plugins");
-my @files = File::Find::Rule->file->name('*.pm') ->in(@dirs);
+my @files = File::Find::Rule->file->name('*.pm')->in(@dirs);
 
 for my $module (@files) {
 

Added: trunk/tests/t/01server.t
URL: http://svn.slimdevices.com/trunk/tests/t/01server.t?rev=8875&view=auto
==============================================================================
--- trunk/tests/t/01server.t (added)
+++ trunk/tests/t/01server.t Tue Aug  8 18:29:06 2006
@@ -1,0 +1,34 @@
+#!/usr/bin/perl -w
+
+# $Id$
+# 
+# Basic start & stop of a slimserver instance.
+
+use strict;
+use lib qw(lib);
+
+use Slim::Test::Base;
+use Test::More tests => 4;
+
+my $secs = 30;
+
+ok(Slim::Test::Base->startServer);
+
+sleep $secs;
+
+# Server should be alive at this point
+ok(Slim::Test::Base->serverIsAlive);
+
+ok(Slim::Test::Base->stopServer);
+
+# And now check that it's dead.
+for (my $i = 0; $i < $secs; $i++) {
+
+	if (Slim::Test::Base->serverIsAlive) {
+		sleep 1;
+		next;
+	}
+
+	ok(1);
+	last;
+}



More information about the checkins mailing list