Permalink
Browse files

rspec configuration and tests

  • Loading branch information...
1 parent 6f96e2e commit 6febad04fa4848485daefc8a305f22bef296accc @cocoa committed Feb 16, 2013
Showing with 87 additions and 31 deletions.
  1. +4 −0 Rakefile
  2. +38 −30 lib/threadpool.rb
  3. +1 −1 lib/threadpool/version.rb
  4. +10 −0 spec/spec_helper.rb
  5. +30 −0 spec/threadpool_spec.rb
  6. +4 −0 threadpool.gemspec
View
@@ -1 +1,5 @@
require "bundler/gem_tasks"
+
+# rspec
+require 'rspec/core/rake_task'
+RSpec::Core::RakeTask.new('spec')
View
@@ -1,46 +1,54 @@
require 'thread'
-class ThreadPool
+module Threadpool
+ class Tpool
- def initialize(max_size)
- @pool = []
- @max_size = max_size
- @pool_mutex = Mutex.new
- @cv = ConditionVariable.new
- end
+ attr_reader :max_size, :pool
- def dispatch(*args)
- Thread.new do
- @pool_mutex.synchronize do
- while @pool.size >= @max_size
- print "Pool is full; waiting to run #{args.join(',')}...\n" if $DEBUG
- @cv.wait(@pool_mutex)
- end
- end
- @pool << Thread.current
- begin
- yield(*args)
- rescue => e
- exception(self, e, *args)
- ensure
+ def initialize(max_size)
+ @pool = []
+ @max_size = max_size
+ @pool_mutex = Mutex.new
+ @cv = ConditionVariable.new
+ end
+
+ def dispatch(*args)
+ Thread.new do
@pool_mutex.synchronize do
- @pool.delete(Thread.current)
- @cv.signal
+ while @pool.size >= @max_size
+ print "Pool is full; waiting to run #{args.join(',')}...\n" if $DEBUG
+ @cv.wait(@pool_mutex)
+ end
+ end
+ @pool << Thread.current
+ begin
+ yield(*args)
+ rescue => e
+ exception(self, e, *args)
+ ensure
+ @pool_mutex.synchronize do
+ @pool.delete(Thread.current)
+ @cv.signal
+ end
end
end
end
+
+ def shutdown
+ sleep(0.1)
+ @pool_mutex.synchronize {@cv.wait(@pool_mutex) until @pool.empty? }
+ end
+
+ def exception(thread, exception, *original_args)
+ puts "Exception in thread #{thread}: #{exception}"
end
- def shutdown
- sleep(0.1)
- @pool_mutex.synchronize {@cv.wait(@pool_mutex) until @pool.empty? }
+ def self.version_string
+ "ThreadPool version #{VERSION}"
end
- def exception(thread, exception, *original_args)
- puts "Exception in thread #{thread}: #{exception}"
- end
end
-
+end
@@ -1,3 +1,3 @@
module Threadpool
- VERSION = "0.0.1"
+ VERSION = "0.0.2"
end
View
@@ -0,0 +1,10 @@
+# rspec
+
+require 'rspec'
+require 'threadpool'
+
+
+RSpec.configure do |config|
+ config.color_enabled = true
+ config.formatter = 'documentation'
+end
View
@@ -0,0 +1,30 @@
+require 'spec_helper'
+
+include Threadpool
+
+describe Threadpool do
+
+ it "should return correct version string" do
+ Tpool.version_string.should == "ThreadPool version #{Threadpool::VERSION}"
+ end
+
+ it "should be created with correct pool size" do
+ tp = Tpool.new(5)
+ tp.max_size.should == 5
+ end
+
+ it "should run N threads simoultaneously" do
+ max_threads = 2
+ tp = Tpool.new(max_threads)
+ 1.upto(10) do |i|
+ tp.dispatch(i) do |n|
+ #puts "Started #{n}..."
+ sleep(3)
+ #puts "Ended #{n}..."
+ end
+ end
+ sleep(0.1)
+ tp.pool.size.should == max_threads
+ end
+
+end
View
@@ -16,4 +16,8 @@ Gem::Specification.new do |gem|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.require_paths = ["lib"]
+
+ # development dependency for testing with rspec
+ gem.add_development_dependency "rspec"
+
end

0 comments on commit 6febad0

Please sign in to comment.