summaryrefslogtreecommitdiff
path: root/test/rake/test_thread_history_display.rb
blob: bb5879cff55d19399d1fc7096106a9e85f307040 (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
require File.expand_path('../helper', __FILE__)

require 'rake/thread_history_display'

class TestThreadHistoryDisplay < Rake::TestCase
  def setup
    super
    @time = 1_000_000
    @stats = []
    @display = Rake::ThreadHistoryDisplay.new(@stats)
  end

  def test_banner
    out, _ = capture_io do
      @display.show
    end
    assert_match(/Job History/i, out)
  end

  def test_item_queued
    @stats << event(:item_queued,  :item_id => 123)
    out, _ = capture_io do
      @display.show
    end
    assert_match(/^ *1000000 +A +item_queued +item_id:1$/, out)
  end

  def test_item_dequeued
    @stats << event(:item_dequeued,  :item_id => 123)
    out, _ = capture_io do
      @display.show
    end
    assert_match(/^ *1000000 +A +item_dequeued +item_id:1$/, out)
  end

  def test_multiple_items
    @stats << event(:item_queued,  :item_id => 123)
    @stats << event(:item_queued,  :item_id => 124)
    out, _ = capture_io do
      @display.show
    end
    assert_match(/^ *1000000 +A +item_queued +item_id:1$/, out)
    assert_match(/^ *1000001 +A +item_queued +item_id:2$/, out)
  end

  def test_waiting
    @stats << event(:waiting, :item_id => 123)
    out, _ = capture_io do
      @display.show
    end
    assert_match(/^ *1000000 +A +waiting +item_id:1$/, out)
  end

  def test_continue
    @stats << event(:continue, :item_id => 123)
    out, _ = capture_io do
      @display.show
    end
    assert_match(/^ *1000000 +A +continue +item_id:1$/, out)
  end

  def test_thread_deleted
    @stats << event(
      :thread_deleted,
      :deleted_thread => 123_456,
      :thread_count => 12)
    out, _ = capture_io do
      @display.show
    end
    assert_match(
      /^ *1000000 +A +thread_deleted( +deleted_thread:B| +thread_count:12){2}$/,
      out)
  end

  def test_thread_created
    @stats << event(
      :thread_created,
      :new_thread => 123_456,
      :thread_count => 13)
    out, _ = capture_io do
      @display.show
    end
    assert_match(
      /^ *1000000 +A +thread_created( +new_thread:B| +thread_count:13){2}$/,
      out)
  end

  private

  def event(type, data = {})
    result = {
      :event => type,
      :time  => @time / 1_000_000.0,
      :data  => data,
      :thread => Thread.current.object_id
    }
    @time += 1
    result
  end

end