[SCM] ci-tooling packaging branch, master, updated. eafec40dc2e03a614cdbb080e47531377061cd2c

Harald Sitter apachelogger-guest at moszumanska.debian.org
Mon Feb 23 14:14:43 UTC 2015


Gitweb-URL: http://git.debian.org/?p=pkg-kde/ci-tooling.git;a=commitdiff;h=eafec40

The following commit has been merged in the master branch:
commit eafec40dc2e03a614cdbb080e47531377061cd2c
Author: Harald Sitter <sitter at kde.org>
Date:   Mon Feb 23 15:14:40 2015 +0100

    add a blockingthreadpool implementation
    
    originally lived in update-projects, now moving to ci-tooling because it
    is plenty useful and we have the pattern appear all over the place
---
 lib/thread_pool.rb       | 20 ++++++++++++++++
 test/test_thread_pool.rb | 61 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 81 insertions(+)

diff --git a/lib/thread_pool.rb b/lib/thread_pool.rb
new file mode 100644
index 0000000..ceb7bf8
--- /dev/null
+++ b/lib/thread_pool.rb
@@ -0,0 +1,20 @@
+require 'thwait'
+
+# Simple thread pool implementation. Pass a block to run and it runs it in a
+# pool.
+# Note that the block code should be thread safe...
+module BlockingThreadPool
+  # Runs the passed block in a pool. This function blocks until all threads are
+  # done.
+  # @param count the thread count to use for the pool
+  def self.run(count = 16, abort_on_exception: true, &block)
+    threads = []
+    count.times do
+      threads << Thread.new(nil) do
+        Thread.current.abort_on_exception = abort_on_exception
+        block.call
+      end
+    end
+    ThreadsWait.all_waits(threads)
+  end
+end
diff --git a/test/test_thread_pool.rb b/test/test_thread_pool.rb
new file mode 100644
index 0000000..785ffa3
--- /dev/null
+++ b/test/test_thread_pool.rb
@@ -0,0 +1,61 @@
+require 'test/unit'
+require 'tmpdir'
+
+require_relative '../lib/thread_pool'
+
+# Test blocking thread pool.
+class BlockingThreadPoolTest < Test::Unit::TestCase
+  def setup
+    @tmpdir = Dir.mktmpdir(self.class.to_s)
+    Dir.chdir(@tmpdir)
+  end
+
+  def teardown
+    Dir.chdir('/')
+    FileUtils.rm_rf(@tmpdir)
+  end
+
+  def test_thread_pool
+    queue = Queue.new
+    32.times { |i| queue << i }
+    BlockingThreadPool.run do
+      until queue.empty?
+        i = queue.pop(true)
+        File.write(i.to_s, '')
+      end
+    end
+    32.times do |i|
+      assert(File.exist?(i.to_s), "File #{i} was not created")
+    end
+  end
+
+  def test_thread_pool_aborting
+    errors = Queue.new
+    BlockingThreadPool.run(1) do
+      errors << 'Thread not aborting' unless Thread.current.abort_on_exception
+    end
+
+    BlockingThreadPool.run(1, abort_on_exception: false) do
+      errors << 'Thread aborting' if Thread.current.abort_on_exception
+    end
+
+    until errors.empty?
+      error = errors.pop
+      assert(false, error)
+    end
+  end
+
+  def test_thread_pool_count
+    # If the queue count is equal to the thread count then all files should
+    # be created without additional looping inside the threads.
+    queue = Queue.new
+    4.times { |i| queue << i }
+    BlockingThreadPool.run(4) do
+      i = queue.pop(true)
+      File.write(i.to_s, '')
+    end
+    4.times do |i|
+      assert(File.exist?(i.to_s), "File #{i} was not created")
+    end
+  end
+end

-- 
ci-tooling packaging



More information about the pkg-kde-commits mailing list