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

Harald Sitter apachelogger-guest at moszumanska.debian.org
Tue May 12 08:29:47 UTC 2015


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

The following commit has been merged in the master branch:
commit 1ff2352c9b4db75d781d87ab21c8106c196e31e6
Author: Harald Sitter <sitter at kde.org>
Date:   Tue May 12 09:37:25 2015 +0200

    new class queue for array de/serialization
    
    pulled in via thread_pool as pretty much all uses would occur through TP
---
 lib/queue.rb       | 27 +++++++++++++++++++++++++++
 lib/thread_pool.rb |  2 ++
 test/test_queue.rb | 22 ++++++++++++++++++++++
 3 files changed, 51 insertions(+)

diff --git a/lib/queue.rb b/lib/queue.rb
new file mode 100644
index 0000000..41f2449
--- /dev/null
+++ b/lib/queue.rb
@@ -0,0 +1,27 @@
+require 'thread'
+
+# Thread-safe queue container.
+# Queue is monkey patched to support incredibly useful Array<=>Queue conversion.
+class Queue
+  alias_method :super_init, :initialize
+
+  def initialize(array = nil)
+    super_init
+    return if array.nil?
+    fail 'Queue can only be constructed from an Array' unless array.is_a?(Array)
+    array.each { |i| self << i }
+  end
+
+  def to_a
+    # Queue isn't exactly the most nejoable thing in the world as it doesn't
+    # allow for random access so you cannot iterate over it, and it doesn't
+    # implement dup nor clone so you can't deep copy it either.
+    # Now since we need to iterate queue to convert it in an array and iteration
+    # means destructive popping we first need to pop it into an Array and then
+    # iterate over the array to push the values back into the queue. Quite mad.
+    ret = []
+    ret << pop until empty?
+    ret.each { |i| self << i }
+    ret
+  end
+end
diff --git a/lib/thread_pool.rb b/lib/thread_pool.rb
index f954505..d16fdca 100644
--- a/lib/thread_pool.rb
+++ b/lib/thread_pool.rb
@@ -1,5 +1,7 @@
 require 'thwait'
 
+require_relative 'queue'
+
 # 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...
diff --git a/test/test_queue.rb b/test/test_queue.rb
new file mode 100644
index 0000000..f52cc56
--- /dev/null
+++ b/test/test_queue.rb
@@ -0,0 +1,22 @@
+require_relative 'lib/testcase'
+require_relative '../lib/queue'
+
+# Test queue
+class QueueTest < TestCase
+  # We are implying that construction from Array works after we have tested
+  # that. Otherwise we'd have to construct the queues manually each time.
+  self.test_order = :defined
+
+  def test_new_from_array
+    array = %w(a b c d e f)
+    queue = Queue.new(array)
+    assert_equal(array.size, queue.size)
+    assert_equal(array.shift, queue.pop) until queue.empty?
+  end
+
+  def test_to_array
+    array = %w(a b c d e f)
+    queue = Queue.new(array)
+    assert_equal(array, queue.to_a)
+  end
+end

-- 
ci-tooling packaging



More information about the pkg-kde-commits mailing list