Sha256: 65ac2ec5fe3bd72691abb2fb49ee2590159370edc4e7b156ff3ef71de2baafec

Contents?: true

Size: 1.87 KB

Versions: 1

Compression:

Stored size: 1.87 KB

Contents

# encoding: utf-8

require File.join(File.dirname(__FILE__), 'helper')

class TestThreads < MosquittoTestCase
  def test_init
    threads = []
    threads << Thread.new do
      publisher = Mosquitto::Client.new
      publisher.loop_start
      assert publisher.connect(TEST_HOST, TEST_PORT, TIMEOUT)
      publisher.wait_readable

      publisher.loop_stop(true)
    end

    threads << Thread.new do
      subscriber = Mosquitto::Client.new
      subscriber.loop_start
      assert subscriber.connect(TEST_HOST, TEST_PORT, TIMEOUT)
      subscriber.wait_readable

      subscriber.loop_stop(true)
    end
    threads.each(&:join)
  end

  def test_pub_sub
    threads = []
    published = 0
    messages = []
    publisher, subscriber = nil
    threads << Thread.new do
      subscriber = Mosquitto::Client.new
      subscriber.loop_start
      subscriber.logger = Logger.new(STDOUT)
      subscriber.on_message do |msg|
        messages << msg.to_s
      end
      subscriber.on_connect do |rc|
        subscriber.subscribe(nil, "test_pub_sub", Mosquitto::AT_MOST_ONCE)
      end
      assert subscriber.connect(TEST_HOST, TEST_PORT, TIMEOUT)
      subscriber.wait_readable

      subscriber.loop_stop(true)
    end

    threads << Thread.new do
      publisher = Mosquitto::Client.new
      publisher.loop_start
      publisher.logger = Logger.new(STDOUT)
      publisher.on_publish do |mid|
        published += 1
      end
      publisher.on_connect do |rc|
        ('a'..'z').to_a.each do |message|
          publisher.publish(nil, "test_pub_sub", message, Mosquitto::AT_MOST_ONCE, true)
        end
      end
      assert publisher.connect(TEST_HOST, TEST_PORT, TIMEOUT)
      publisher.wait_readable

      assert_equal published, 26
      publisher.loop_stop(true)
    end

    threads.each{|t| t.join(5) }
    messages.uniq!
    messages.sort!
    assert_equal ('a'..'z').to_a, messages.sort
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mosquitto-0.3 test/test_threads.rb