summaryrefslogtreecommitdiff
path: root/test/test_journal.rb
blob: f1653c164783e35eb3830228aac58531403f337e (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
$:.unshift File.join(File.dirname(__FILE__), '../lib')

require 'test/unit'
require 'rbot/ircbot'
require 'rbot/journal'
require 'rbot/journal/postgres.rb'
require 'rbot/journal/mongo.rb'

require 'benchmark'

DAY=60*60*24

class JournalMessageTest < Test::Unit::TestCase

  include Irc::Bot::Journal

  def test_get
    m = JournalMessage.create('foo', {'bar': 42, 'baz': nil, 'qux': {'quxx': 23}})
    assert_equal(42, m.get('bar'))
    assert_raise ArgumentError do
      m.get('nope')
    end
    assert_nil(m.get('nope', nil))
    assert_nil(m.get('baz'))
    assert_equal(23, m['qux.quxx'])
    assert_equal(nil, m['qux.nope'])
    assert_raise(ArgumentError) { m.get('qux.nope') }
  end

end

class QueryTest < Test::Unit::TestCase

  include Irc::Bot::Journal

  def test_define

    q = Query.define do
      id 'foo'
      id 'bar', 'baz'
      topic 'log.irc.*'
      topic 'log.core', 'baz'
      timestamp from: Time.now, to: Time.now + 60 * 10
      payload 'action': :privmsg, 'alice': 'bob'
      payload 'channel': '#rbot'
      payload 'foo.bar': 'baz'
    end
    assert_equal(['foo', 'bar', 'baz'], q.id)
    assert_equal(['log.irc.*', 'log.core', 'baz'], q.topic)
    assert_equal([:from, :to], q.timestamp.keys)
    assert_equal(Time, q.timestamp[:to].class)
    assert_equal(Time, q.timestamp[:from].class)
    assert_equal({
      'action': :privmsg, 'alice': 'bob',
      'channel': '#rbot',
      'foo.bar': 'baz'
    }, q.payload)

  end

  def test_topic_matches
    q = Query.define do
      topic 'foo'
    end
    assert_true(q.topic_matches?('foo'))
    assert_false(q.topic_matches?('bar'))
    assert_false(q.topic_matches?('foo.bar'))

    q = Query.define do
      topic 'foo.bar'
    end
    assert_false(q.topic_matches?('foo'))
    assert_false(q.topic_matches?('bar'))
    assert_true(q.topic_matches?('foo.bar'))

    q = Query.define do
      topic 'foo.*'
    end
    assert_false(q.topic_matches?('foo'))
    assert_false(q.topic_matches?('bar'))
    assert_true(q.topic_matches?('foo.bar'))
    assert_true(q.topic_matches?('foo.baz'))

    q = Query.define do
      topic '*.bar'
    end
    assert_false(q.topic_matches?('foo'))
    assert_false(q.topic_matches?('bar'))
    assert_true(q.topic_matches?('foo.bar'))
    assert_true(q.topic_matches?('bar.bar'))
    assert_false(q.topic_matches?('foo.foo'))

    q = Query.define do
      topic '*.*'
    end
    assert_false(q.topic_matches?('foo'))
    assert_true(q.topic_matches?('foo.bar'))

    q = Query.define do
      topic 'foo'
      topic 'bar'
      topic 'baz.alice.bob.*.foo'
    end
    assert_true(q.topic_matches?('foo'))
    assert_true(q.topic_matches?('bar'))
    assert_true(q.topic_matches?('baz.alice.bob.asdf.foo'))
    assert_false(q.topic_matches?('baz.alice.bob..foo'))

  end
  def test_matches
    q = Query.define do
      #id 'foo', 'bar'
      topic 'log.irc.*', 'log.core'
      timestamp from: Time.now - DAY, to: Time.now + DAY
      payload 'action': 'privmsg', 'foo.bar': 'baz'
    end
    assert_true(q.matches? JournalMessage.create('log.irc.raw', {'action' => 'privmsg'}))
    assert_false(q.matches? JournalMessage.create('baz', {}))
    assert_true(q.matches? JournalMessage.create('log.core', {foo: {bar: 'baz'}}))

    # tests timestamp from/to:
    assert_true(q.matches? JournalMessage.new(
      id: 'foo',
      topic: 'log.core',
      timestamp: Time.now,
      payload: {action: 'privmsg'}))
    assert_false(q.matches? JournalMessage.new(
      id: 'foo',
      topic: 'log.core',
      timestamp: Time.now - DAY*3,
      payload: {action: 'privmsg'}))
    assert_false(q.matches? JournalMessage.new(
      id: 'foo',
      topic: 'log.core',
      timestamp: Time.now + DAY*3,
      payload: {action: 'privmsg'}))
  end

end

class JournalBrokerTest < Test::Unit::TestCase

  include Irc::Bot::Journal

  def test_publish
    received = []
    journal = JournalBroker.new(consumer: Proc.new { |message|
      received << message
    })

    # publish some messages:
    journal.publish 'log.irc',
      source: 'alice', message: '<3 pg'
    journal.publish 'log.irc',
      source: 'bob', message: 'mysql > pg'
    journal.publish 'log.irc',
      source: 'alice', target: 'bob', action: :kick

    # wait for messages to be consumed:
    sleep 0.1
    assert_equal(3, received.length)
  end

  def test_subscribe
    received = []
    journal = JournalBroker.new

    # subscribe to messages for topic foo:
    sub = journal.subscribe('foo') do |message|
      received << message
    end

    # publish some messages:
    journal.publish 'foo', {}
    journal.publish 'bar', {}
    journal.publish 'foo', {}

    # wait for messages to be consumed:
    sleep 0.1
    assert_equal(2, received.length)

    received.clear

    journal.publish 'foo', {}
    sleep 0.1
    sub.cancel
    journal.publish 'foo', {}
    sleep 0.1
    assert_equal(1, received.length)
  end

end

module JournalStorageTestMixin

  include Irc::Bot::Journal

  def teardown
    @storage.drop
  end

  def test_operations
    # insertion
    m = JournalMessage.create('log.core', {foo: {bar: 'baz', qux: 42}})
    @storage.insert(m)

    # query by id
    res = @storage.find(Query.define { id m.id })
    assert_equal(1, res.length)
    assert_equal(m, res.first)

    # check timestamp was returned correctly:
    assert_equal(m.timestamp.strftime('%Y-%m-%d %H:%M:%S%z'),
                 res.first.timestamp.strftime('%Y-%m-%d %H:%M:%S%z'))

    # check if payload was returned correctly:
    assert_equal({'foo' => {'bar' => 'baz', 'qux' => 42}}, res.first.payload)

    # query by topic
    assert_equal(m, @storage.find(Query.define { topic('log.core') }).first)
    assert_equal(m, @storage.find(Query.define { topic('log.*') }).first)
    assert_equal(m, @storage.find(Query.define { topic('*.*') }).first)

    # query by timestamp range
    assert_equal(1, @storage.find(Query.define {
      timestamp(from: Time.now-DAY, to: Time.now+DAY) }).length)
    assert_equal(0, @storage.find(Query.define {
      timestamp(from: Time.now-DAY*2, to: Time.now-DAY) }).length)

    # query by payload
    res = @storage.find(Query.define { payload('foo.bar' => 'baz') })
    assert_equal(m, res.first)
    res = @storage.find(Query.define { payload('foo.bar' => 'x') })
    assert_true(res.empty?)

    # without arguments: find and count
    assert_equal(1, @storage.count)
    assert_equal(m, @storage.find.first)
  end

  def test_find
    # tests limit/offset and block parameters of find()
    @storage.insert(JournalMessage.create('irclogs', {message: 'foo'}))
    @storage.insert(JournalMessage.create('irclogs', {message: 'bar'}))
    @storage.insert(JournalMessage.create('irclogs', {message: 'baz'}))
    @storage.insert(JournalMessage.create('irclogs', {message: 'qux'}))

    msgs = []
    @storage.find(Query.define({topic: 'irclogs'}), 2, 1) do |m|
      msgs << m
    end
    assert_equal(2, msgs.length)
    assert_equal('bar', msgs.first['message'])
    assert_equal('baz', msgs.last['message'])

    msgs = []
    @storage.find(Query.define({topic: 'irclogs'})) do |m|
      msgs << m
    end
    assert_equal(4, msgs.length)
    assert_equal('foo', msgs.first['message'])
    assert_equal('qux', msgs.last['message'])

  end

  def test_operations_multiple
    # test operations on multiple messages
    # insert a bunch:
    @storage.insert(JournalMessage.create('test.topic', {name: 'one'}))
    @storage.insert(JournalMessage.create('test.topic', {name: 'two'}))
    @storage.insert(JournalMessage.create('test.topic', {name: 'three'}))
    @storage.insert(JournalMessage.create('archived.topic', {name: 'four'},
      timestamp: Time.now - DAY*100))
    @storage.insert(JournalMessage.create('complex', {name: 'five', country: {
      name: 'Italy'
    }}))
    @storage.insert(JournalMessage.create('complex', {name: 'six', country: {
      name: 'Austria'
    }}))

    # query by topic
    assert_equal(3, @storage.find(Query.define { topic 'test.*' }).length)
    # query by payload
    assert_equal(1, @storage.find(Query.define {
      payload('country.name' => 'Austria') }).length)
    # query by timestamp range
    assert_equal(1, @storage.find(Query.define {
      timestamp(from: Time.now - DAY*150, to: Time.now - DAY*50) }).length)

    # count with query
    assert_equal(2, @storage.count(Query.define { topic('complex') }))
    assert_equal(6, @storage.count)
    @storage.remove(Query.define { topic('archived.*') })
    assert_equal(5, @storage.count)
    @storage.remove
    assert_equal(0, @storage.count)
  end

  def test_broker_interface
    journal = JournalBroker.new(storage: @storage) 

    journal.publish 'irclogs', message: 'foo'
    journal.publish 'irclogs', message: 'bar'
    journal.publish 'irclogs', message: 'baz'
    journal.publish 'irclogs', message: 'qux'

    # wait for messages to be consumed:
    sleep 0.1

    msgs = []
    journal.find({topic: 'irclogs'}, 2, 1) do |m|
      msgs << m
    end
    assert_equal(2, msgs.length)
    assert_equal('bar', msgs.first['message'])
    assert_equal('baz', msgs.last['message'])

    journal.ensure_payload_index('foo.bar.baz')
  end

  NUM=100 # 1_000_000
  def test_benchmark
    puts

    assert_equal(0, @storage.count)
    # prepare messages to insert, we benchmark the storage backend not ruby
    num = 0
    messages = (0...NUM).map do
      num += 1
      JournalMessage.create(
            'test.topic.num_'+num.to_s, {answer: {number: '42', word: 'forty-two'}})
    end

    # iter is the number of operations performed WITHIN block
    def benchmark(label, iter, &block)
      time = Benchmark.realtime do
        yield
      end
      puts label + ' %d iterations, duration: %.3fms (%.3fms / iteration)' % [iter, time*1000, (time*1000) / iter]
    end

    benchmark(@storage.class.to_s+'~insert', messages.length) do
      messages.each { |m|
        @storage.insert(m)
      }
    end

    benchmark(@storage.class.to_s+'~find_by_id', messages.length) do
      messages.each { |m|
        @storage.find(Query.define { id m.id })
      }
    end
    benchmark(@storage.class.to_s+'~find_by_topic', messages.length) do
      messages.each { |m|
        @storage.find(Query.define { topic m.topic })
      }
    end
    benchmark(@storage.class.to_s+'~find_by_topic_wildcard', messages.length) do
      messages.each { |m|
        @storage.find(Query.define { topic m.topic.gsub('topic', '*') })
      }
    end
  end

end

if ENV['PG_URI']
class JournalStoragePostgresTest < Test::Unit::TestCase

  include JournalStorageTestMixin

  def setup
    @storage = Storage::PostgresStorage.new(
      uri: ENV['PG_URI'] || 'postgresql://localhost/rbot_journal',
      drop: true)
  end

  def test_query_to_sql
    q = Query.define do
      id 'foo'
      id 'bar', 'baz'
      topic 'log.irc.*'
      topic 'log.core', 'baz'
      timestamp from: Time.now, to: Time.now + 60 * 10
      payload 'action': :privmsg, 'alice': 'bob'
      payload 'channel': '#rbot'
      payload 'foo.bar': 'baz'
    end
    sql = @storage.query_to_sql(q)
    assert_equal("(id = $1 OR id = $2 OR id = $3) AND (topic ILIKE $4 OR topic ILIKE $5 OR topic ILIKE $6) AND (timestamp >= $7 AND timestamp <= $8) AND (payload->>'action' = $9 OR payload->>'alice' = $10 OR payload->>'channel' = $11 OR payload->'foo'->>'bar' = $12)", sql[0])
    q = Query.define do
      id 'foo'
    end
    assert_equal('(id = $1)', @storage.query_to_sql(q)[0])
    q = Query.define do
      topic 'foo.*.bar'
    end
    assert_equal('(topic ILIKE $1)', @storage.query_to_sql(q)[0])
    assert_equal(['foo.%.bar'], @storage.query_to_sql(q)[1])
  end

end
else
  puts 'NOTE: Set PG_URI environment variable to test postgresql storage.'
end

if ENV['MONGO_URI']
class JournalStorageMongoTest < Test::Unit::TestCase

  include JournalStorageTestMixin

  def setup
    @storage = Storage::MongoStorage.new(
      uri: ENV['MONGO_URI'] || 'mongodb://127.0.0.1:27017/rbot',
      drop: true)
  end
end
else
  puts 'NOTE: Set MONGO_URI environment variable to test postgresql storage.'
end