Permalink
Browse files

Version 1.0.2 - now with some test coverage

  • Loading branch information...
1 parent 0bec690 commit e310179f9c103df75ded3433f95a6c6f676b89eb Ben Marini committed Mar 17, 2011
Showing with 133 additions and 1 deletion.
  1. +3 −0 .gitignore
  2. +3 −0 Gemfile
  3. +14 −0 Rakefile
  4. +6 −1 forker.gemspec
  5. +1 −0 lib/forker.rb
  6. +3 −0 lib/forker/version.rb
  7. +93 −0 spec/forker_spec.rb
  8. +10 −0 spec/harness
View
@@ -1 +1,4 @@
*.gem
+.bundle
+Gemfile.lock
+pkg/*
View
@@ -0,0 +1,3 @@
+source "http://rubygems.org"
+
+gemspec
View
@@ -0,0 +1,14 @@
+require 'bundler'
+Bundler::GemHelper.install_tasks
+
+require 'rake/testtask'
+
+task :default => [:test]
+
+task :test do
+ Rake::TestTask.new do |t|
+ t.libs << "spec"
+ t.pattern = 'spec/*_spec.rb'
+ t.verbose = true
+ end
+end
View
@@ -1,6 +1,10 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "forker/version"
+
Gem::Specification.new do |s|
s.name = "forker"
- s.version = "1.0.1"
+ s.version = Forker::VERSION
s.date = "2010-09-27"
s.summary = "Fork your ruby code with confidence"
s.email = "bmarini@gmail.com"
@@ -9,4 +13,5 @@ Gem::Specification.new do |s|
s.authors = ["Ben Marini"]
s.files = Dir.glob("lib/**/*") + %w(README.md)
s.add_dependency "SystemTimer", "~> 1.2"
+ s.add_development_dependency "minitest", "~> 2.0.2"
end
View
@@ -13,6 +13,7 @@
# Notes
# $$ = Process.pid
+require 'thread'
require 'system_timer'
module Forker
View
@@ -0,0 +1,3 @@
+module Forker
+ VERSION = "1.0.2"
+end
View
@@ -0,0 +1,93 @@
+require 'rubygems'
+require 'bundler/setup'
+require 'minitest/autorun'
+require 'minitest/spec'
+require 'fileutils'
+require 'forker'
+
+class Harness
+ attr_accessor :output
+
+ def run(args="")
+ @output = `spec/harness #{args}`
+ end
+
+ def start(args="")
+ run("start #{args}")
+ end
+
+ def stop(args="")
+ run("stop #{args}")
+ end
+
+ def pid
+ @output[/forked process is (\d+)/, 1]
+ end
+
+ def lsof
+ `lsof -p #{pid} | awk '{print $4, $9}'`.split("\n").grep(/^(0r|1w|2w) /)
+ end
+
+ FD = Struct.new(:name, :path)
+ def open_file_descriptors
+ @open_file_descriptors = lsof.map { |l| FD.new(*l.split) }
+ end
+end
+
+class ForkerSpec < MiniTest::Spec
+
+ describe "Forker::CLI" do
+ before { @harness = Harness.new }
+
+ it "should provide a helpful usage banner" do
+ @harness.run
+
+ @harness.output.must_equal <<-EOS.lstrip
+Usage: harness [start|stop] -p /path/to/pidfile.pid
+ -l, --logfile LOGFILE redirect output to this location
+ -p, --pidfile PIDFILE save pidfile to this location
+ EOS
+ end
+ end
+
+ describe "forking" do
+ before do
+ @harness = Harness.new
+ end
+
+ after do
+ @harness.stop "-p spec/harness.pid"
+ FileUtils.rm_f "spec/harness.log"
+ end
+
+ it "should announce the pid and logfile location" do
+ @harness.start "-p spec/harness.pid"
+
+ line1, line2 = @harness.output.split("\n")
+ line1.must_match /forked process is (\d+)/
+ line2.must_equal "output redirected to /dev/null"
+ end
+
+ it "should redirect stdin, stdout and stderr to dev/null by default" do
+ @harness.start "-p spec/harness.pid"
+
+ @harness.open_file_descriptors.each do |fd|
+ fd.path.must_equal "/dev/null"
+ end
+ end
+
+ it "should allow you to specify a logfile" do
+ @harness.start "-l spec/harness.log -p spec/harness.pid"
+
+ sleep 0.1
+ File.exist?("spec/harness.log").must_equal true
+ File.read("spec/harness.log").must_match /I love tests/
+ end
+
+ it "should allow you to specify a pidfile" do
+ @harness.start "-p spec/harness.pid"
+ File.exist?("spec/harness.pid").must_equal true
+ @harness.pid.must_equal File.read("spec/harness.pid")
+ end
+ end
+end
View
@@ -0,0 +1,10 @@
+#!/usr/bin/env ruby
+require 'rubygems'
+require 'bundler/setup'
+require 'forker'
+
+Forker::CLI.run(ARGV)
+loop do
+ puts "I love tests"
+ sleep 0.1
+end

0 comments on commit e310179

Please sign in to comment.