]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - test/test_journal.rb
introducing a persistent message queue, the "journal"
[user/henk/code/ruby/rbot.git] / test / test_journal.rb
1 $:.unshift File.join(File.dirname(__FILE__), '../lib')
2
3 require 'test/unit'
4 require 'rbot/ircbot'
5 require 'rbot/journal'
6
7 class JournalMessageTest < Test::Unit::TestCase
8
9   include Irc::Bot::Journal
10
11   def test_get
12     m = JournalMessage.create('foo', {'bar': 42, 'baz': nil, 'qux': {'quxx': 23}})
13     assert_equal(42, m.get('bar'))
14     assert_raise ArgumentError do
15       m.get('nope')
16     end
17     assert_nil(m.get('nope', nil))
18     assert_nil(m.get('baz'))
19     assert_equal(23, m.get('qux.quxx'))
20   end
21
22 end
23
24 class QueryTest < Test::Unit::TestCase
25
26   include Irc::Bot::Journal
27
28   def test_define
29
30     q = Query.define do
31       id 'foo'
32       id 'bar', 'baz'
33       topic 'log.irc.*'
34       topic 'log.core', 'baz'
35       timestamp from: Time.now, to: Time.now + 60 * 10
36       payload 'action': :privmsg, 'alice': 'bob'
37       payload 'channel': '#rbot'
38       payload 'foo.bar': 'baz'
39     end
40     assert_equal(['foo', 'bar', 'baz'], q.id)
41     assert_equal(['log.irc.*', 'log.core', 'baz'], q.topic)
42     assert_equal([:from, :to], q.timestamp.keys)
43     assert_equal(Time, q.timestamp[:to].class)
44     assert_equal(Time, q.timestamp[:from].class)
45     assert_equal({
46       'action': :privmsg, 'alice': 'bob',
47       'channel': '#rbot',
48       'foo.bar': 'baz'
49     }, q.payload)
50
51   end
52
53   def test_topic_matches
54     q = Query.define do
55       topic 'foo'
56     end
57     assert_true(q.topic_matches?('foo'))
58     assert_false(q.topic_matches?('bar'))
59     assert_false(q.topic_matches?('foo.bar'))
60
61     q = Query.define do
62       topic 'foo.bar'
63     end
64     assert_false(q.topic_matches?('foo'))
65     assert_false(q.topic_matches?('bar'))
66     assert_true(q.topic_matches?('foo.bar'))
67
68     q = Query.define do
69       topic 'foo.*'
70     end
71     assert_false(q.topic_matches?('foo'))
72     assert_false(q.topic_matches?('bar'))
73     assert_true(q.topic_matches?('foo.bar'))
74     assert_true(q.topic_matches?('foo.baz'))
75
76     q = Query.define do
77       topic '*.bar'
78     end
79     assert_false(q.topic_matches?('foo'))
80     assert_false(q.topic_matches?('bar'))
81     assert_true(q.topic_matches?('foo.bar'))
82     assert_true(q.topic_matches?('bar.bar'))
83     assert_false(q.topic_matches?('foo.foo'))
84
85     q = Query.define do
86       topic '*.*'
87     end
88     assert_false(q.topic_matches?('foo'))
89     assert_true(q.topic_matches?('foo.bar'))
90
91     q = Query.define do
92       topic 'foo'
93       topic 'bar'
94       topic 'baz.alice.bob.*.foo'
95     end
96     assert_true(q.topic_matches?('foo'))
97     assert_true(q.topic_matches?('bar'))
98     assert_true(q.topic_matches?('baz.alice.bob.asdf.foo'))
99     assert_false(q.topic_matches?('baz.alice.bob..foo'))
100
101   end
102
103   DAY=60*60*24
104   def test_matches
105     q = Query.define do
106       #id 'foo', 'bar'
107       topic 'log.irc.*', 'log.core'
108       timestamp from: Time.now - DAY, to: Time.now + DAY
109       payload 'action': 'privmsg', 'foo.bar': 'baz'
110     end
111     assert_true(q.matches? JournalMessage.create('log.irc.raw', {'action' => 'privmsg'}))
112     assert_false(q.matches? JournalMessage.create('baz', {}))
113     assert_true(q.matches? JournalMessage.create('log.core', {foo: {bar: 'baz'}}))
114
115     # tests timestamp from/to:
116     assert_true(q.matches? JournalMessage.new(
117       id: 'foo',
118       topic: 'log.core',
119       timestamp: Time.now,
120       payload: {action: 'privmsg'}))
121     assert_false(q.matches? JournalMessage.new(
122       id: 'foo',
123       topic: 'log.core',
124       timestamp: Time.now - DAY*3,
125       payload: {action: 'privmsg'}))
126     assert_false(q.matches? JournalMessage.new(
127       id: 'foo',
128       topic: 'log.core',
129       timestamp: Time.now + DAY*3,
130       payload: {action: 'privmsg'}))
131   end
132
133 end
134
135 class JournalBrokerTest < Test::Unit::TestCase
136
137   include Irc::Bot::Journal
138
139   def test_publish
140     received = []
141     journal = JournalBroker.new(consumer: Proc.new { |message|
142       received << message
143     })
144
145     # publish some messages:
146     journal.publish 'log.irc',
147       source: 'alice', message: '<3 pg'
148     journal.publish 'log.irc',
149       source: 'bob', message: 'mysql > pg'
150     journal.publish 'log.irc',
151       source: 'alice', target: 'bob', action: :kick
152
153     # wait for messages to be consumed:
154     sleep 0.1
155     assert_equal(3, received.length)
156   end
157
158   def test_subscribe
159     received = []
160     journal = JournalBroker.new
161
162     # subscribe to messages:
163     journal.subscribe(Query.define { topic 'foo' }) do |message|
164       received << message
165     end
166
167     # publish some messages:
168     journal.publish 'foo', {}
169     journal.publish 'bar', {}
170     journal.publish 'foo', {}
171
172     # wait for messages to be consumed:
173     sleep 0.1
174     assert_equal(2, received.length)
175   end
176
177 end
178