Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 391 lines (305 sloc) 7.597 kb
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
gem 'test-unit'
require 'test/unit'
require 'yaml'
require_relative 'test_base'

class IronMQTests < TestBase
  def setup
    super

    queues = @client.queues.list
    p queues

    clear_queue()

  end

  def test_performance_post_100_messages
    @client.queue_name = 'test_perf_100'
    # slower to rackspace since this is running on aws
    timeout = @client.host.include?('rackspace') ? 40 : 12
    assert_performance timeout do
      100.times do
        @client.messages.post("hello world!")
      end
    end
  end


  def test_basics
    queue_name = 'test_basics_7'
    @client.queue_name = queue_name
    clear_queue

    res = @client.messages.post("hello world!")
    p res
    assert res["id"]
    assert res.id
    assert res.msg

    queue = @client.queues.get(:name => @client.queue_name)
    p queue
    assert_equal 1, queue.reload.size
    res = @client.messages.get()
    p res
    assert res["id"]
    assert res.id

    res = @client.messages.delete(res["id"])
    p res
    puts "shouldn't be any more"
    res = @client.messages.get()
    p res
    assert_nil res

    queue = @client.queues.get(:name => @client.queue_name)
    assert_equal 0, queue.size

    res = @client.messages.post("hello world 2!")
    p res

    msg = @client.messages.get()
    p msg
    assert msg

    res = msg.delete
    p res

    puts "shouldn't be any more"
    res = @client.messages.get()
    p res
    assert_nil res


    # new style of referencing queue
    queue = @client.queue(queue_name)
    v = "hello big world"
    res = queue.post(v)
    p res
    assert res.msg

    res = queue.get()
    p res
    assert res["id"]
    assert res.id
    assert_equal v, res.body

    res = queue.delete(res.id)
    p res
    puts "shouldn't be any more"
    res = queue.get()
    p res
    assert_nil res

    # test delete by item
    res = queue.post(v)
    p res
    assert res.msg

    res = queue.get()
    p res
    assert res.body
    res = res.delete
    p res
    puts "shouldn't be any more"
    res = queue.get()
    p res
    assert_nil res

  end


  def test_queues_list
    return
    queue_name = 'test_queues_list'
    @client.queue_name = queue_name
    clear_queue

    res = @client.messages.post("hello world!")
    p res

    res = @client.queues.list
    res.each do |q|
      puts "#{q.name} and #{queue_name}";
      if q.name == queue_name
        assert_equal q.size, 1
      end
    end
  end


  # TODO: pass :timeout in post/get messages and test those
  def test_timeout
    @client.queue_name = "test_timeout_6"
    clear_queue

    res = @client.messages.post("hello world timeout!")
    p res

    msg = @client.messages.get()
    p msg
    assert msg

    msg4 = @client.messages.get()
    p msg4
    assert_nil msg4

    puts 'sleeping 45 seconds...'
    sleep 45

    msg3 = @client.messages.get()
    p msg3
    assert_nil msg3

    puts 'sleeping another 45 seconds...'
    sleep 45

    msg2 = @client.messages.get()
    assert msg2
    assert_equal msg2.id, msg.id

    msg2.delete

    # now try explicit timeout
    res = @client.messages.post("hello world timeout2!", :timeout => 10)
    p res
    msg = @client.messages.get()
    p msg
    assert msg
    msg4 = @client.messages.get()
    p msg4
    assert_nil msg4
    puts 'sleeping 15 seconds...'
    sleep 15
    msg2 = @client.messages.get()
    assert msg2
    assert_equal msg2.id, msg.id

  end

  def test_queues
    puts 'test_queues'

    assert_raise Rest::HttpError do
      # should raise a 404
      q = @client.queues.get(:name => "some_queue_that_does_not_exist")
    end

    res = @client.queues.list()
    puts "res.size: " + res.size.to_s
    res.each do |q|
      puts "queue_name: " + q.name
      puts "queue size: " + q.size.to_s
      assert q.size >= 0
    end
    assert res.size > 0

    res = @client.queues.list(:page => 15)
    puts "res.size 2: " + res.size.to_s
    res.each do |q|
      p q.name
    end
    assert_equal 0, res.size

  end

  def test_delay
    puts 'test_delay'
    @client.queue_name = "test_delay_6"
    clear_queue
    msgTxt = "testMessage-"+Time.now.to_s
    puts msgTxt
    @client.messages.post(msgTxt, {:delay => 10})
    msg = @client.messages.get
    p msg
    assert_nil msg
    sleep 10
    msg = @client.messages.get
    p msg
    assert msg
  end

  def test_batch
    puts 'test_batch'
    @client.queue_name = "test_batch_6"
    clear_queue

    x = []
    10.times do |i|
      x << {:body => "body #{i}"}
    end
    resp = @client.messages.post(x)
    assert resp["ids"]
    assert resp["ids"].is_a?(Array)
    assert_equal 10, resp["ids"].size

    msg = @client.messages.get()
    assert msg
    assert msg['id']
    msg.delete

    msgs = @client.messages.get(:n => 10)
    assert msgs.is_a?(Array)
    assert msgs.size == 9, "size should be 9, but it's #{msgs.size}"
    assert msgs[0]["id"]

    msgs.each do |m|
      m.delete
    end
  end

  def test_release
    puts 'test_release'
    @client.queue_name = "test_release_6"
    clear_queue
    msgTxt = "testMessage-"+Time.now.to_s
    puts msgTxt
    msg_id = @client.messages.post(msgTxt, {:timeout => 60*5}).id
    puts "msg_id: #{msg_id}"
    msg = @client.messages.get
    p msg
    assert_equal msg_id, msg.id
    # Ok, so should have received same message, now let's release it quicker than the original timeout

    # but first, ensure the next get is nil
    msg = @client.messages.get
    p msg
    assert_nil msg

    # now release it instantly
    @client.messages.release(msg_id)
    msg = @client.messages.get
    p msg
    assert msg
    assert_equal msg_id, msg.id

    # ok, so should be reserved again
    msgr = @client.messages.get
    p msgr
    assert_nil msgr

    # let's release it in 10 seconds
    @client.messages.release(msg_id, :delay => 10)
    msg = @client.messages.get
    p msg
    assert_nil msg

    sleep 11

    msg = @client.messages.get
    p msg
    assert msg

    msg.release(:delay => 5)
    msg = @client.messages.get
    p msg
    assert_nil msg

    sleep 6

    msg = @client.messages.get
    p msg
    assert msg

  end

  def test_clear

    q = @client.queue("test_clear_7")

    clear_queue(q.name)

    val = "hi mr clean"
    q.post(val)

    sleep 0.5 # make sure the counter has time to update
    assert_equal 1, q.size

    q.clear

    msg = q.get
    assert_nil msg

    assert_equal 0, q.reload.size
  end



  def test_poll
    queue = @client.queue("test_poll_6")
    queue.clear

    v = "hello world"
    5.times do
      queue.post(v)
    end

    i = 0
    queue.poll(:break_if_nil=>true) do |msg|
      assert msg.body.include?("hello")
      i += 1
    end

    assert_equal 5, i

    assert_equal 0, queue.reload.size

  end
  #
  #def test_delete
  # queue = @client.queue("test_delete")
  # queue.post("hi")
  # queue.reload
  # old_id = queue.id
  # queue.delete_queue
  #
  # puts "sleeping for a bit to let queue delete..."
  # sleep 60
  #
  # queue.post("hi2")
  # p queue
  # queue.reload
  # assert queue.id != old_id, "old_id: #{old_id} is equal to new id: #{queue.id}"
  # assert queue.size == 1
  # queue.get("").body == "hi2"
  #
  #end

  def test_webhooks
    qname ="webhook_queue"
    path = "#{IronMQ::Messages.path(project_id: @client.project_id, queue_name: qname)}/webhook"
    url = "#{@client.base_url}#{path}"
    url << "?oauth=#{@client.token}"
    p url

    v = "hello webhook"

    @rest = Rest::Client.new
    p @rest.post(url, body: v)

    queue = @client.queue(qname)
    msg = queue.get
    p msg
    assert_equal v, msg.body


  end


end

Something went wrong with that request. Please try again.