summaryrefslogtreecommitdiff
path: root/spec/mspec/lib/mspec/matchers/block_caller.rb
blob: 017bce3cb7952a83bc647520f4bdfeaf5adb47cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class BlockingMatcher
  def matches?(block)
    started = false
    blocking = true

    thread = Thread.new do
      started = true
      block.call

      blocking = false
    end

    while !started and status = thread.status and status != "sleep"
      Thread.pass
    end
    thread.kill
    thread.join

    blocking
  end

  def failure_message
    ['Expected the given Proc', 'to block the caller']
  end

  def negative_failure_message
    ['Expected the given Proc', 'to not block the caller']
  end
end

module MSpecMatchers
  private def block_caller(timeout = 0.1)
    BlockingMatcher.new
  end
end