posix_mq 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,225 @@
1
+ # -*- encoding: binary -*-
2
+ require 'test/unit'
3
+ require 'posix_mq'
4
+ require 'fcntl'
5
+ $stderr.sync = $stdout.sync = true
6
+
7
+ class Test_POSIX_MQ < Test::Unit::TestCase
8
+
9
+ HAVE_TO_IO = if POSIX_MQ.instance_methods.grep(/\Ato_io\z/).empty?
10
+ warn "POSIX_MQ#to_io not supported on this platform: #{RUBY_PLATFORM}"
11
+ false
12
+ else
13
+ true
14
+ end
15
+
16
+ def setup
17
+ @mq = nil
18
+ @path = "/posix_mq.rb.#{Time.now.to_i}.#$$.#{rand}"
19
+ end
20
+
21
+ def teardown
22
+ @mq or return
23
+ assert_equal @mq, @mq.unlink
24
+ assert ! @mq.closed?
25
+ assert_nil @mq.close
26
+ assert @mq.closed?
27
+ end
28
+
29
+ def test_timed_receive
30
+ interval = 0.01
31
+ @mq = POSIX_MQ.new(@path, :rw)
32
+ assert ! @mq.nonblock?
33
+ t0 = Time.now
34
+ assert_raises(Errno::ETIMEDOUT) { @mq.receive "", interval }
35
+ elapsed = Time.now - t0
36
+ assert elapsed > interval
37
+ end
38
+
39
+ def test_timed_send
40
+ interval = 0.01
41
+ @mq = POSIX_MQ.new(@path, :rw, 0666, POSIX_MQ::Attr[0, 1, 1, 0])
42
+ assert ! @mq.nonblock?
43
+ assert_nothing_raised { @mq.send "A", 1, interval }
44
+ t0 = Time.now
45
+ assert_raises(Errno::ETIMEDOUT) { @mq.send "B", 1, interval }
46
+ elapsed = Time.now - t0
47
+ assert elapsed > interval
48
+ end
49
+
50
+ def test_open
51
+ POSIX_MQ.open(@path, IO::CREAT|IO::WRONLY, 0666) do |mq|
52
+ @mq = mq
53
+ assert mq.kind_of?(POSIX_MQ)
54
+ assert_equal @path, mq.name
55
+ assert_nil mq.send("HI", 0)
56
+ assert_equal 1, mq.attr.curmsgs
57
+ assert_nil mq.close
58
+ assert_raises(IOError) { mq.close }
59
+ end
60
+ assert @mq.closed?
61
+ @mq = nil
62
+ POSIX_MQ.unlink(@path)
63
+ end
64
+
65
+ def test_name
66
+ path = "" << @path.dup
67
+ path.freeze
68
+ @mq = POSIX_MQ.new @path, IO::CREAT|IO::WRONLY, 0666
69
+ assert_equal path, @mq.name
70
+ end
71
+
72
+ def test_new_readonly
73
+ @mq = POSIX_MQ.new @path, IO::CREAT|IO::WRONLY, 0666
74
+ rd = POSIX_MQ.new @path, IO::RDONLY
75
+ assert_equal @mq.name, rd.name
76
+ assert_nil rd.close
77
+ end
78
+
79
+ def test_send0_receive
80
+ @mq = POSIX_MQ.new @path, IO::CREAT|IO::RDWR, 0666
81
+ assert_equal(@mq, @mq << "hello")
82
+ assert_equal [ "hello", 0 ], @mq.receive
83
+ end
84
+
85
+ def test_send0_chain
86
+ @mq = POSIX_MQ.new @path, IO::CREAT|IO::RDWR, 0666
87
+ @mq << "hello" << "world"
88
+ assert_equal [ "hello", 0 ], @mq.receive
89
+ assert_equal [ "world", 0 ], @mq.receive
90
+ end
91
+
92
+ def test_send_receive
93
+ @mq = POSIX_MQ.new @path, IO::CREAT|IO::RDWR, 0666
94
+ assert_nil @mq.send("hello", 0)
95
+ assert_equal [ "hello", 0 ], @mq.receive
96
+ end
97
+
98
+ def test_send_receive_buf
99
+ buf = ""
100
+ @mq = POSIX_MQ.new @path, IO::CREAT|IO::RDWR, 0666
101
+ assert_nil @mq.send("hello", 0)
102
+ assert_equal [ "hello", 0 ], @mq.receive(buf)
103
+ assert_equal "hello", buf
104
+ end
105
+
106
+ def test_send_receive_prio
107
+ @mq = POSIX_MQ.new @path, IO::CREAT|IO::RDWR, 0666
108
+ assert_nil @mq.send("hello", 2)
109
+ assert_equal [ "hello", 2 ], @mq.receive
110
+ end
111
+
112
+ def test_getattr
113
+ @mq = POSIX_MQ.new @path, IO::CREAT|IO::WRONLY, 0666
114
+ mq_attr = @mq.attr
115
+ assert_equal POSIX_MQ::Attr, mq_attr.class
116
+ assert mq_attr.flags.kind_of?(Integer)
117
+ assert mq_attr.maxmsg.kind_of?(Integer)
118
+ assert mq_attr.msgsize.kind_of?(Integer)
119
+ assert mq_attr.curmsgs.kind_of?(Integer)
120
+ end
121
+
122
+ def test_to_io
123
+ @mq = POSIX_MQ.new @path, IO::CREAT|IO::RDWR, 0666
124
+ assert @mq.to_io.kind_of?(IO)
125
+ assert_nothing_raised { IO.select([@mq], nil, nil, 0) }
126
+ end if HAVE_TO_IO
127
+
128
+ def test_notify
129
+ rd, wr = IO.pipe
130
+ orig = trap(:USR1) { wr.syswrite('.') }
131
+ @mq = POSIX_MQ.new @path, IO::CREAT|IO::RDWR, 0666
132
+ assert_nothing_raised { @mq.notify = :SIGUSR1 }
133
+ assert_nothing_raised { @mq.send("hello", 0) }
134
+ assert_equal [[rd], [], []], IO.select([rd], nil, nil, 10)
135
+ assert_equal '.', rd.sysread(1)
136
+ assert_nil(@mq.notify = nil)
137
+ assert_nothing_raised { @mq.send("hello", 0) }
138
+ assert_nil IO.select([rd], nil, nil, 0.1)
139
+ assert_raises(Errno::EBUSY) { @mq.notify = :USR1 }
140
+ ensure
141
+ trap(:USR1, orig)
142
+ end
143
+
144
+ def test_setattr
145
+ @mq = POSIX_MQ.new @path, IO::CREAT|IO::WRONLY, 0666
146
+ mq_attr = POSIX_MQ::Attr.new(IO::NONBLOCK)
147
+ @mq.attr = mq_attr
148
+ assert_equal IO::NONBLOCK, @mq.attr.flags
149
+ assert mq_attr.flags.kind_of?(Integer)
150
+
151
+ mq_attr.flags = 0
152
+ @mq.attr = mq_attr
153
+ assert_equal 0, @mq.attr.flags
154
+ end
155
+
156
+ def test_new_nonblocking
157
+ @mq = POSIX_MQ.new @path, IO::CREAT|IO::WRONLY|IO::NONBLOCK, 0666
158
+ assert @mq.nonblock?
159
+ end
160
+
161
+ def test_new_blocking
162
+ @mq = POSIX_MQ.new @path, IO::CREAT|IO::WRONLY, 0666
163
+ assert ! @mq.nonblock?
164
+ end
165
+
166
+ def test_nonblock_toggle
167
+ @mq = POSIX_MQ.new @path, IO::CREAT|IO::WRONLY, 0666
168
+ assert ! @mq.nonblock?
169
+ @mq.nonblock = true
170
+ assert @mq.nonblock?
171
+ @mq.nonblock = false
172
+ assert ! @mq.nonblock?
173
+ assert_raises(ArgumentError) { @mq.nonblock = nil }
174
+ end
175
+
176
+ def test_new_sym_w
177
+ @mq = POSIX_MQ.new @path, :w
178
+ assert_equal IO::WRONLY, @mq.to_io.fcntl(Fcntl::F_GETFL)
179
+ end if HAVE_TO_IO
180
+
181
+ def test_new_sym_r
182
+ @mq = POSIX_MQ.new @path, :w
183
+ mq = nil
184
+ assert_nothing_raised { mq = POSIX_MQ.new @path, :r }
185
+ assert_equal IO::RDONLY, mq.to_io.fcntl(Fcntl::F_GETFL)
186
+ assert_nil mq.close
187
+ end if HAVE_TO_IO
188
+
189
+ def test_new_path_only
190
+ @mq = POSIX_MQ.new @path, :w
191
+ mq = nil
192
+ assert_nothing_raised { mq = POSIX_MQ.new @path }
193
+ assert_equal IO::RDONLY, mq.to_io.fcntl(Fcntl::F_GETFL)
194
+ assert_nil mq.close
195
+ end if HAVE_TO_IO
196
+
197
+ def test_new_sym_wr
198
+ @mq = POSIX_MQ.new @path, :rw
199
+ assert_equal IO::RDWR, @mq.to_io.fcntl(Fcntl::F_GETFL)
200
+ end if HAVE_TO_IO
201
+
202
+ def test_new_attr
203
+ mq_attr = POSIX_MQ::Attr.new(IO::NONBLOCK, 1, 1, 0)
204
+ @mq = POSIX_MQ.new @path, IO::CREAT|IO::RDWR, 0666, mq_attr
205
+ assert @mq.nonblock?
206
+ assert_equal mq_attr, @mq.attr
207
+
208
+ assert_raises(Errno::EAGAIN) { @mq.receive }
209
+ assert_raises(Errno::EMSGSIZE) { @mq << '..' }
210
+ assert_nothing_raised { @mq << '.' }
211
+ assert_equal [ '.', 0 ], @mq.receive
212
+ assert_nothing_raised { @mq << '.' }
213
+ assert_raises(Errno::EAGAIN) { @mq << '.' }
214
+ end
215
+
216
+ def test_prio_max
217
+ min_posix_mq_prio_max = 31 # defined by POSIX
218
+ assert POSIX_MQ::PRIO_MAX >= min_posix_mq_prio_max
219
+ end
220
+
221
+ def test_open_max
222
+ assert POSIX_MQ::OPEN_MAX.kind_of?(Integer)
223
+ end
224
+
225
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: posix_mq
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ruby POSIX MQ hackers
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-02 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: |-
17
+ POSIX message queues allow local processes to exchange data in the form
18
+ of messages. This API is distinct from that provided by System V
19
+ message queues, but provides similar functionality.
20
+ email: ruby.posix.mq@librelist.com
21
+ executables:
22
+ - posix-mq.rb
23
+ extensions:
24
+ - ext/posix_mq/extconf.rb
25
+ extra_rdoc_files:
26
+ - README
27
+ - LICENSE
28
+ - NEWS
29
+ - ChangeLog
30
+ - lib/posix_mq.rb
31
+ - ext/posix_mq/posix_mq.c
32
+ files:
33
+ - .document
34
+ - .gitignore
35
+ - .manifest
36
+ - COPYING
37
+ - ChangeLog
38
+ - Documentation/.gitignore
39
+ - Documentation/GNUmakefile
40
+ - Documentation/posix-mq.rb.1.txt
41
+ - GIT-VERSION-FILE
42
+ - GIT-VERSION-GEN
43
+ - GNUmakefile
44
+ - LICENSE
45
+ - NEWS
46
+ - README
47
+ - Rakefile
48
+ - bin/posix-mq.rb
49
+ - ext/posix_mq/extconf.rb
50
+ - ext/posix_mq/posix_mq.c
51
+ - lib/posix_mq.rb
52
+ - local.mk.sample
53
+ - man/man1/posix-mq.rb.1
54
+ - posix_mq.gemspec
55
+ - setup.rb
56
+ - test/test_posix_mq.rb
57
+ has_rdoc: true
58
+ homepage: http://bogomips.org/ruby_posix_mq/
59
+ licenses: []
60
+
61
+ post_install_message:
62
+ rdoc_options:
63
+ - -Na
64
+ - -t
65
+ - posix_mq - POSIX Message Queues for Ruby
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ version:
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: "0"
79
+ version:
80
+ requirements: []
81
+
82
+ rubyforge_project: qrp
83
+ rubygems_version: 1.3.5
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: POSIX Message Queues for Ruby
87
+ test_files:
88
+ - test/test_posix_mq.rb