summaryrefslogtreecommitdiff
path: root/spec/mspec/lib/mspec/matchers/block_caller.rb
blob: 4149586747d385d4460a0ec252778dbdb0f3013d (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
36
37
38
39
class BlockingMatcher
  def matches?(block)
    started = false
    blocking = true

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

      blocking = false
    end

    Thread.pass while !started

    # Wait until the Thread status is "sleep" (then it's blocking)
    # or nil (the Thread finished execution, it did not block)
    while 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