Debian Codegraph @

c/examples/testme

raw
1 #!/usr/bin/python
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 # Run the C examples and verify that they behave as expected.
22 # Example executables must be in PATH
23
24 import unittest
25
26 from test_subprocess import Popen, Server, TestProcessError, check_output
27
28 MESSAGES=10
29
30 def receive_expect_messages(n=MESSAGES): return ''.join(['{"sequence"=%s}\n'%i for i in range(1, n+1)])
31 def receive_expect_total(n=MESSAGES): return "%s messages received\n"%n
32 def receive_expect(n=MESSAGES): return receive_expect_messages(n)+receive_expect_total(n)
33 def send_expect(n=MESSAGES): return "%s messages sent and acknowledged\n" % n
34 def send_abort_expect(n=MESSAGES): return "%s messages started and aborted\n" % n
35
36 class Broker(Server):
37 def __init__(self):
38 super(Broker, self).__init__(["broker", "", "0"], kill_me=True)
39
40 class ExampleTest(unittest.TestCase):
41
42 def runex(self, name, port, messages=MESSAGES):
43 """Run an example with standard arguments, return output"""
44 return check_output([name, "", port, "xtest", str(messages)])
45
46 def startex(self, name, port, messages=MESSAGES):
47 """Start an example sub-process with standard arguments"""
48 return Popen([name, "", port, "xtest", str(messages)])
49
50 def test_send_receive(self):
51 """Send first then receive"""
52 with Broker() as b:
53 self.assertEqual(send_expect(), self.runex("send", b.port))
54 self.assertMultiLineEqual(receive_expect(), self.runex("receive", b.port))
55
56 def test_receive_send(self):
57 """Start receiving first, then send."""
58 with Broker() as b:
59 r = self.startex("receive", b.port)
60 self.assertEqual(send_expect(), self.runex("send", b.port))
61 self.assertMultiLineEqual(receive_expect(), r.communicate()[0])
62
63 def test_send_direct(self):
64 """Send to direct server"""
65 d = Server(["direct", "", "0"])
66 self.assertEqual(send_expect(), self.runex("send", d.port))
67 self.assertMultiLineEqual(receive_expect(), d.communicate()[0])
68
69 def test_receive_direct(self):
70 """Receive from direct server"""
71 d = Server(["direct", "", "0"])
72 self.assertMultiLineEqual(receive_expect(), self.runex("receive", d.port))
73 self.assertEqual("10 messages sent and acknowledged\n", d.communicate()[0])
74
75 def test_send_abort_broker(self):
76 """Sending aborted messages to a broker"""
77 with Broker() as b:
78 self.assertEqual(send_expect(), self.runex("send", b.port))
79 self.assertEqual(send_abort_expect(), self.runex("send-abort", b.port))
80 for i in range(MESSAGES):
81 self.assertEqual("Message aborted\n", b.stdout.readline())
82 self.assertEqual(send_expect(), self.runex("send", b.port))
83 expect = receive_expect_messages(MESSAGES)+receive_expect_messages(MESSAGES)+receive_expect_total(20)
84 self.assertMultiLineEqual(expect, self.runex("receive", b.port, "20"))
85
86 def test_send_abort_direct(self):
87 """Send aborted messages to the direct server"""
88 d = Server(["direct", "", "0", "examples", "20"])
89 self.assertEqual(send_expect(), self.runex("send", d.port))
90 self.assertEqual(send_abort_expect(), self.runex("send-abort", d.port))
91 self.assertEqual(send_expect(), self.runex("send", d.port))
92 expect = receive_expect_messages() + "Message aborted\n"*MESSAGES + receive_expect_messages()+receive_expect_total(20)
93 self.maxDiff = None
94 self.assertMultiLineEqual(expect, d.communicate()[0])
95
96 def test_send_ssl_receive(self):
97 """Send with SSL, then receive"""
98 try:
99 with Broker() as b:
100 got = self.runex("send-ssl", b.port)
101 self.assertIn("secure connection:", got)
102 self.assertIn(send_expect(), got)
103 self.assertMultiLineEqual(receive_expect(), self.runex("receive", b.port))
104 except TestProcessError as e:
105 if e.output and e.output.startswith(b"error initializing SSL"):
106 print("Skipping %s: SSL not available" % self.id())
107 else:
108 raise
109
110 if __name__ == "__main__":
111 unittest.main()