File: device_spec.rb

package info (click to toggle)
ruby-ffi-rzmq 2.0.7-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 392 kB
  • sloc: ruby: 2,992; sh: 21; makefile: 2
file content (78 lines) | stat: -rw-r--r-- 1,872 bytes parent folder | download | duplicates (2)
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

require File.join(File.dirname(__FILE__), %w[spec_helper])

module ZMQ
  describe Device do
    include APIHelper

    before(:each) do
      @ctx = Context.new
      poller_setup
      @front_endpoint = "inproc://device_front_test"
      @back_endpoint = "inproc://device_back_test"
      @mutex = Mutex.new
    end

    after(:each) do
      @ctx.terminate
    end

    def create_streamer
      @device_thread = false

      Thread.new do
        back  = @ctx.socket(ZMQ::PULL)
        back.bind(@back_endpoint)
        front = @ctx.socket(ZMQ::PUSH)
        front.bind(@front_endpoint)
        @mutex.synchronize { @device_thread = true }
        puts "create streamer device and running..." if $DEBUG
        Device.new(back, front)
        puts "device exited" if $DEBUG
        back.close
        front.close
      end
    end
    
    def wait_for_device
      loop do
        can_break = @mutex.synchronize { @device_thread }
        
        break if can_break
      end
      puts "broke out of wait_for_device loop" if $DEBUG
    end

    it "should create a device without error given valid opts" do
      create_streamer
      wait_for_device
    end

    it "should be able to send messages through the device" do
      create_streamer
      wait_for_device

      pusher = @ctx.socket(ZMQ::PUSH)
      connect_to_inproc(pusher, @back_endpoint)
      puller = @ctx.socket(ZMQ::PULL)
      connect_to_inproc(puller, @front_endpoint)

      poll_it_for_read(puller) do
        pusher.send_string("hello")
      end

      res = ''
      rc = puller.recv_string(res, ZMQ::DONTWAIT)
      expect(res).to eq("hello")

      pusher.close
      puller.close
    end

    it "should raise an ArgumentError when trying to pass non-socket objects into the device" do
      expect {
        Device.new(1,2)
      }.to raise_exception(ArgumentError)
    end
  end
end