Debian Codegraph @

ruby/examples/testme

raw
1 #!/usr/bin/ruby
2 #
3 # Licensed to the Apache Software Foundation (ASF) under one
4 # or more contributor license agreements. See the NOTICE file
5 # distributed with this work for additional information
6 # regarding copyright ownership. The ASF licenses this file
7 # to you under the Apache License, Version 2.0 (the
8 # "License"); you may not use this file except in compliance
9 # with the License. You may obtain a copy of the License at
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing,
14 # software distributed under the License is distributed on an
15 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 # KIND, either express or implied. See the License for the
17 # specific language governing permissions and limitations
18 # under the License.
19 #
20
21 require 'minitest/autorun'
22 require 'qpid_proton'
23 require 'socket'
24 require 'rbconfig'
25
26 begin
27 MiniTest::Test
28 rescue NameError # For older versions of MiniTest
29 MiniTest::Test = MiniTest::Unit::TestCase
30 end
31
32 def listening_port(s)
33 /Listening on ([0-9]+)/.match(s)[1]
34 end
35
36 def listening_url(s)
37 ":#{listening_port s}"
38 end
39
40 class ExampleTest < MiniTest::Test
41
42 def run_script(*args)
43 return IO.popen([ RbConfig.ruby ] + args.map { |a| a.to_s })
44 end
45
46 def assert_output(want, *args)
47 assert_equal(want.strip, run_script(*args).read.strip)
48 end
49
50 def test_helloworld
51 assert_output("Hello world!", "helloworld.rb", $url, "examples")
52 end
53
54 def test_client_server
55 want = <<EOS
56 -> Twas brillig, and the slithy toves
57 <- TWAS BRILLIG, AND THE SLITHY TOVES
58 -> Did gire and gymble in the wabe.
59 <- DID GIRE AND GYMBLE IN THE WABE.
60 -> All mimsy were the borogroves,
61 <- ALL MIMSY WERE THE BOROGROVES,
62 -> And the mome raths outgrabe.
63 <- AND THE MOME RATHS OUTGRABE.
64 EOS
65 server = run_script("server.rb", $url, "examples")
66 assert_output(want.strip, "client.rb", $url, "examples")
67 ensure
68 Process.kill :TERM, server.pid if server
69 end
70
71 def test_send_recv
72 assert_output("All 10 messages confirmed!", "simple_send.rb", $url, "examples")
73 want = (0..9).reduce("") { |x,y| x << "Received: sequence #{y}\n" }
74 assert_output(want.strip, "simple_recv.rb", $url, "examples")
75 end
76
77 def test_ssl_send_recv
78 skip 'SSL not available' unless Qpid::Proton::SSL.present?
79 out = run_script("ssl_send.rb", $url, "examples").read.strip
80 assert_match(/Connection secured with "...*\"\nAll 10 messages confirmed!/, out)
81 want = (0..9).reduce("") { |x,y| x << "Received: sequence #{y}\n" }
82 assert_output(want.strip, "simple_recv.rb", $url, "examples")
83 end
84
85 def test_direct_recv
86 p = run_script("direct_recv.rb", ":0", "examples")
87 url = listening_url(p.readline) # Wait till ready
88 assert_output("All 10 messages confirmed!", "simple_send.rb", url, "examples")
89 want = (0..9).reduce("") { |x,y| x << "Received: sequence #{y}\n" }
90 assert_equal(want.strip, p.read.strip)
91 end
92
93 def test_direct_send
94 p = run_script("direct_send.rb", ":0", "examples")
95 url = listening_url(p.readline) # Wait till ready
96 want = (0..9).reduce("") { |x,y| x << "Received: sequence #{y}\n" }
97 assert_output(want.strip, "simple_recv.rb", url, "examples")
98 assert_equal("All 10 messages confirmed!", p.read.strip)
99 end
100 end
101
102 # Start the broker before all tests.
103 $broker = IO.popen([RbConfig.ruby, 'broker.rb', ":0"])
104 l = $broker.readline
105 $url = listening_url(l)
106
107 # Kill the broker after all tests
108 MiniTest.after_run do
109 Process.kill(:TERM, $broker.pid) if $broker
110 end