| 1 |
# |
| 2 |
# Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
# or more contributor license agreements. See the NOTICE file |
| 4 |
# distributed with this work for additional information |
| 5 |
# regarding copyright ownership. The ASF licenses this file |
| 6 |
# to you under the Apache License, Version 2.0 (the |
| 7 |
# "License"); you may not use this file except in compliance |
| 8 |
# with the License. You may obtain a copy of the License at |
| 9 |
# |
| 10 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
# |
| 12 |
# Unless required by applicable law or agreed to in writing, |
| 13 |
# software distributed under the License is distributed on an |
| 14 |
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
# KIND, either express or implied. See the License for the |
| 16 |
# specific language governing permissions and limitations |
| 17 |
# under the License. |
| 18 |
# |
| 19 |
|
| 20 |
import os |
| 21 |
import subprocess |
| 22 |
import time |
| 23 |
import unittest |
| 24 |
|
| 25 |
|
| 26 |
class Popen(subprocess.Popen): |
| 27 |
|
| 28 |
# We always use these options |
| 29 |
def __init__(self, args, **kwargs): |
| 30 |
# Add the module directory to the path to be able to find the examples |
| 31 |
path = f"{os.path.dirname(__file__)}{os.pathsep}{os.environ['PATH']}" |
| 32 |
super(Popen, self).\ |
| 33 |
__init__(args, |
| 34 |
shell=False, |
| 35 |
stderr=subprocess.STDOUT, |
| 36 |
stdout=subprocess.PIPE, |
| 37 |
universal_newlines=True, |
| 38 |
env={'PATH': path}, |
| 39 |
**kwargs) |
| 40 |
|
| 41 |
|
| 42 |
# Like subprocess.run with our defaults but returning the Popen object |
| 43 |
def run(args, **kwargs): |
| 44 |
p = Popen(args, **kwargs) |
| 45 |
p.wait() |
| 46 |
return p |
| 47 |
|
| 48 |
|
| 49 |
def check_call(args, **kwargs): |
| 50 |
# Add the module directory to the path to be able to find the examples |
| 51 |
path = f"{os.path.dirname(__file__)}{os.pathsep}{os.environ['PATH']}" |
| 52 |
return subprocess.check_call(args, env={'PATH': path}, **kwargs) |
| 53 |
|
| 54 |
|
| 55 |
class ExamplesTest(unittest.TestCase): |
| 56 |
def test_helloworld(self, example="helloworld.py"): |
| 57 |
with run([example]) as p: |
| 58 |
output = [l.strip() for l in p.stdout] |
| 59 |
self.assertEqual(output, ['Hello World!']) |
| 60 |
|
| 61 |
def test_helloworld_direct(self): |
| 62 |
self.test_helloworld('helloworld_direct.py') |
| 63 |
|
| 64 |
def test_helloworld_blocking(self): |
| 65 |
self.test_helloworld('helloworld_blocking.py') |
| 66 |
|
| 67 |
def test_helloworld_tornado(self): |
| 68 |
self.test_helloworld('helloworld_tornado.py') |
| 69 |
|
| 70 |
def test_helloworld_direct_tornado(self): |
| 71 |
self.test_helloworld('helloworld_direct_tornado.py') |
| 72 |
|
| 73 |
def test_simple_send_recv(self, recv='simple_recv.py', send='simple_send.py'): |
| 74 |
with Popen([recv]) as r: |
| 75 |
with run([send]): |
| 76 |
pass |
| 77 |
actual = [l.strip() for l in r.stdout] |
| 78 |
expected = ["{'sequence': %i}" % (i + 1,) for i in range(100)] |
| 79 |
self.assertEqual(actual, expected) |
| 80 |
|
| 81 |
def test_client_server(self, client=['client.py'], server=['server.py'], sleep=0): |
| 82 |
with Popen(server) as s: |
| 83 |
if sleep: |
| 84 |
time.sleep(sleep) |
| 85 |
with run(client) as c: |
| 86 |
actual = [l.strip() for l in c.stdout] |
| 87 |
inputs = ["Twas brillig, and the slithy toves", |
| 88 |
"Did gire and gymble in the wabe.", |
| 89 |
"All mimsy were the borogroves,", |
| 90 |
"And the mome raths outgrabe."] |
| 91 |
expected = ["%s => %s" % (l, l.upper()) for l in inputs] |
| 92 |
self.assertEqual(actual, expected) |
| 93 |
s.terminate() |
| 94 |
|
| 95 |
def test_sync_client_server(self): |
| 96 |
self.test_client_server(client=['sync_client.py']) |
| 97 |
|
| 98 |
def test_client_server_tx(self): |
| 99 |
self.test_client_server(server=['server_tx.py']) |
| 100 |
|
| 101 |
def test_sync_client_server_tx(self): |
| 102 |
self.test_client_server(client=['sync_client.py'], server=['server_tx.py']) |
| 103 |
|
| 104 |
def test_client_server_direct(self): |
| 105 |
self.test_client_server(client=['client.py', '-a', 'localhost:8888/examples'], |
| 106 |
server=['server_direct.py'], sleep=0.5) |
| 107 |
|
| 108 |
def test_sync_client_server_direct(self): |
| 109 |
self.test_client_server(client=['sync_client.py', '-a', 'localhost:8888/examples'], |
| 110 |
server=['server_direct.py'], sleep=0.5) |
| 111 |
|
| 112 |
def test_db_send_recv(self): |
| 113 |
self.maxDiff = None |
| 114 |
# setup databases |
| 115 |
check_call(['db_ctrl.py', 'init', './src_db']) |
| 116 |
check_call(['db_ctrl.py', 'init', './dst_db']) |
| 117 |
with Popen(['db_ctrl.py', 'insert', './src_db'], stdin=subprocess.PIPE) as fill: |
| 118 |
for i in range(100): |
| 119 |
fill.stdin.write("Message-%i\n" % (i + 1)) |
| 120 |
fill.stdin.close() |
| 121 |
|
| 122 |
# run send and recv |
| 123 |
with Popen(['db_recv.py', '-m', '100']) as r: |
| 124 |
with run(['db_send.py', '-m', '100']): |
| 125 |
pass |
| 126 |
r.wait() |
| 127 |
# verify output of receive |
| 128 |
actual = [l.strip() for l in r.stdout] |
| 129 |
expected = ["inserted message %i" % (i + 1) for i in range(100)] |
| 130 |
self.assertEqual(actual, expected) |
| 131 |
|
| 132 |
# verify state of databases |
| 133 |
with run(['db_ctrl.py', 'list', './dst_db']) as v: |
| 134 |
expected = ["(%i, 'Message-%i')" % (i + 1, i + 1) for i in range(100)] |
| 135 |
actual = [l.strip() for l in v.stdout] |
| 136 |
self.assertEqual(actual, expected) |
| 137 |
|
| 138 |
def test_tx_send_tx_recv(self): |
| 139 |
self.test_simple_send_recv(recv='tx_recv.py', send='tx_send.py') |
| 140 |
|
| 141 |
def test_simple_send_direct_recv(self): |
| 142 |
self.maxDiff = None |
| 143 |
with Popen(['direct_recv.py', '-a', 'localhost:8888']) as r: |
| 144 |
time.sleep(0.5) |
| 145 |
with run(['simple_send.py', '-a', 'localhost:8888']): |
| 146 |
pass |
| 147 |
r.wait() |
| 148 |
actual = [l.strip() for l in r.stdout] |
| 149 |
expected = ["{'sequence': %i}" % (i + 1,) for i in range(100)] |
| 150 |
self.assertEqual(actual, expected) |
| 151 |
|
| 152 |
def test_direct_send_simple_recv(self): |
| 153 |
with Popen(['direct_send.py', '-a', 'localhost:8888']): |
| 154 |
time.sleep(0.5) |
| 155 |
with run(['simple_recv.py', '-a', 'localhost:8888']) as r: |
| 156 |
actual = [l.strip() for l in r.stdout] |
| 157 |
expected = ["{'sequence': %i}" % (i + 1,) for i in range(100)] |
| 158 |
self.assertEqual(actual, expected) |
| 159 |
|
| 160 |
def test_selected_recv(self): |
| 161 |
with run(['colour_send.py']): |
| 162 |
pass |
| 163 |
|
| 164 |
with run(['selected_recv.py', '-m', '50']) as r: |
| 165 |
actual = [l.strip() for l in r.stdout] |
| 166 |
expected = ["green %i" % (i + 1) for i in range(100) if i % 2 == 0] |
| 167 |
self.assertEqual(actual, expected) |
| 168 |
|
| 169 |
with run(['simple_recv.py', '-m', '50']) as r: |
| 170 |
actual = [l.strip() for l in r.stdout] |
| 171 |
expected = ["red %i" % (i + 1) for i in range(100) if i % 2 == 1] |
| 172 |
self.assertEqual(actual, expected) |