1require File.expand_path('../helper', __FILE__)
2
3require 'rake/thread_history_display'
4
5class TestThreadHistoryDisplay < Rake::TestCase
6  def setup
7    super
8    @time = 1000000
9    @stats = []
10    @display = Rake::ThreadHistoryDisplay.new(@stats)
11  end
12
13  def test_banner
14    out, _ = capture_io do
15      @display.show
16    end
17    assert_match(/Job History/i, out)
18  end
19
20  def test_item_queued
21    @stats << event(:item_queued,  :item_id => 123)
22    out, _ = capture_io do
23      @display.show
24    end
25    assert_match(/^ *1000000 +A +item_queued +item_id:1$/, out)
26  end
27
28  def test_item_dequeued
29    @stats << event(:item_dequeued,  :item_id => 123)
30    out, _ = capture_io do
31      @display.show
32    end
33    assert_match(/^ *1000000 +A +item_dequeued +item_id:1$/, out)
34  end
35
36  def test_multiple_items
37    @stats << event(:item_queued,  :item_id => 123)
38    @stats << event(:item_queued,  :item_id => 124)
39    out, _ = capture_io do
40      @display.show
41    end
42    assert_match(/^ *1000000 +A +item_queued +item_id:1$/, out)
43    assert_match(/^ *1000001 +A +item_queued +item_id:2$/, out)
44  end
45
46  def test_waiting
47    @stats << event(:waiting, :item_id => 123)
48    out, _ = capture_io do
49      @display.show
50    end
51    assert_match(/^ *1000000 +A +waiting +item_id:1$/, out)
52  end
53
54  def test_continue
55    @stats << event(:continue, :item_id => 123)
56    out, _ = capture_io do
57      @display.show
58    end
59    assert_match(/^ *1000000 +A +continue +item_id:1$/, out)
60  end
61
62  def test_thread_deleted
63    @stats << event(:thread_deleted, :deleted_thread => 123456, :thread_count => 12)
64    out, _ = capture_io do
65      @display.show
66    end
67    assert_match(/^ *1000000 +A +thread_deleted( +deleted_thread:B| +thread_count:12){2}$/, out)
68  end
69
70  def test_thread_created
71    @stats << event(:thread_created, :new_thread => 123456, :thread_count => 13)
72    out, _ = capture_io do
73      @display.show
74    end
75    assert_match(/^ *1000000 +A +thread_created( +new_thread:B| +thread_count:13){2}$/, out)
76  end
77
78  private
79
80  def event(type, data={})
81    result = {
82      :event => type,
83      :time  => @time / 1_000_000.0,
84      :data  => data,
85      :thread => Thread.current.object_id
86    }
87    @time += 1
88    result
89  end
90
91end
92