summaryrefslogtreecommitdiff
path: root/test/test_plugins_priority.rb
blob: b5d3450f373c8866b7e1cbe9badc05e9b98268c8 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
$:.unshift File.join(File.dirname(__FILE__), '../lib')

require 'test/unit'
require 'rbot/config'
require 'rbot/plugins'

require 'pp'

include Irc::Bot::Plugins

class TestRealBotModule < BotModule
  def initialize
  end
end

class MockModule < BotModule
  attr_reader :test_called_at
  attr_reader :connect_called_at

  def initialize(prio)
    @test_called_at = []
    @connect_called_at = []
    @priority = prio
  end

  def test
    @test_called_at << Time.new
  end

  # an connect fast-delegate event
  def connect
    @connect_called_at << Time.new
  end

  def botmodule_class
    :CoreBotModule
  end
end

class PluginsPriorityTest < Test::Unit::TestCase
  @@manager = nil

  def setup
    @mock1 = MockModule.new(1)
    @mock2 = MockModule.new(2)
    @mock3 = MockModule.new(3)
    @mock4 = MockModule.new(4)
    @mock5 = MockModule.new(5)
      
    # This whole thing is a PITA because PluginManagerClass is a singleton
    unless @@manager
      @@manager = PluginManagerClass.instance

      # this is needed because debug is setup in the rbot starter
      def @@manager.debug(m); puts m; end
      @@manager.instance_eval { alias real_sort_modules sort_modules }
      def @@manager.sort_modules
        @sort_call_count ||= 0
        @sort_call_count += 1
        real_sort_modules
      end
    end
    @@manager.instance_eval { @sort_call_count = nil }
    @@manager.mark_priorities_dirty

    # We add the modules to the lists in the wrong order 
    # on purpose to make sure the sort is working
    @@manager.plugins.clear
    @@manager.core_modules.clear
    @@manager.plugins << @mock1
    @@manager.plugins << @mock4
    @@manager.plugins << @mock3
    @@manager.plugins << @mock2
    @@manager.plugins << @mock5

    dlist = @@manager.instance_eval {@delegate_list['connect'.intern]}
    dlist.clear
    dlist << @mock1
    dlist << @mock4
    dlist << @mock3
    dlist << @mock2
    dlist << @mock5
  end
    
  def test_default_priority
    plugin = TestRealBotModule.new
    assert_equal 1, plugin.priority
  end

  def test_sort_called
    @@manager.delegate('test')

    assert @@manager.instance_eval { @sort_call_count }
  end

  def test_sort_called_once
    @@manager.delegate('test')
    @@manager.delegate('test')
    @@manager.delegate('test')
    @@manager.delegate('test')

    assert_equal 1, @@manager.instance_eval { @sort_call_count }
  end

  def test_sorted
    plugins = @@manager.plugins
    assert_equal @mock1, plugins[0]
    assert_equal @mock4, plugins[1]
    assert_equal @mock3, plugins[2]
    assert_equal @mock2, plugins[3]
    assert_equal @mock5, plugins[4]

    @@manager.sort_modules
    plugins = @@manager.instance_eval { @sorted_modules }

    assert_equal @mock1, plugins[0]
    assert_equal @mock2, plugins[1]
    assert_equal @mock3, plugins[2]
    assert_equal @mock4, plugins[3]
    assert_equal @mock5, plugins[4]
  end

  def test_fast_delegate_sort
    list = @@manager.instance_eval {@delegate_list['connect'.intern]}
    assert_equal @mock1, list[0]
    assert_equal @mock4, list[1]
    assert_equal @mock3, list[2]
    assert_equal @mock2, list[3]
    assert_equal @mock5, list[4]

    @@manager.sort_modules
    assert_equal @mock1, list[0]
    assert_equal @mock2, list[1]
    assert_equal @mock3, list[2]
    assert_equal @mock4, list[3]
    assert_equal @mock5, list[4]
  end

  def test_slow_called_in_order
    @@manager.delegate('test')
    assert_equal 1, @mock1.test_called_at.size
    assert_equal 1, @mock2.test_called_at.size
    assert_equal 1, @mock3.test_called_at.size
    assert_equal 1, @mock4.test_called_at.size
    assert_equal 1, @mock5.test_called_at.size

    assert @mock1.test_called_at.first < @mock2.test_called_at.first
    assert @mock2.test_called_at.first < @mock3.test_called_at.first
    assert @mock3.test_called_at.first < @mock4.test_called_at.first
    assert @mock4.test_called_at.first < @mock5.test_called_at.first
  end

  def test_fast_called_in_order
    @@manager.delegate('connect')
    assert_equal 1, @mock1.connect_called_at.size
    assert_equal 1, @mock2.connect_called_at.size
    assert_equal 1, @mock3.connect_called_at.size
    assert_equal 1, @mock4.connect_called_at.size
    assert_equal 1, @mock5.connect_called_at.size

    assert @mock1.connect_called_at.first < @mock2.connect_called_at.first
    assert @mock2.connect_called_at.first < @mock3.connect_called_at.first
    assert @mock3.connect_called_at.first < @mock4.connect_called_at.first
    assert @mock4.connect_called_at.first < @mock5.connect_called_at.first
  end

  def test_add_botmodule
    @@manager.sort_modules
    mock_n1 = MockModule.new(-1)
    @@manager.add_botmodule mock_n1
    @@manager.delegate('test')
    assert mock_n1.test_called_at.first < @mock1.test_called_at.first
  end
end