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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
import tempfile
from stomp.__main__ import StompCLI
from .testutils import *
username = get_default_user()
password = get_default_password()
(host, port) = get_default_host()[0]
test_text = '''subscribe /queue/testfile
send /queue/testfile this is a test
unsubscribe /queue/testfile'''
def create_test_file():
with tempfile.NamedTemporaryFile('w', delete=False) as f:
f.write('''subscribe /queue/testfile
send /queue/testfile this is a test
unsubscribe /queue/testfile''')
return f
class TestCLI(object):
def test_invalid_version(self):
with pytest.raises(RuntimeError):
StompCLI(host, port, username, password, "invalid")
def test_help(self):
teststdin = StubStdin()
cli = StompCLI(host, port, username, password, "1.0", stdin=teststdin)
cli.help_help()
cli.help_quit()
cli.help_exit()
cli.help_EOF()
cli.help_subscribe()
cli.help_unsubscribe()
cli.help_send()
cli.help_sendrec()
cli.help_sendreply()
cli.help_sendfile()
cli.help_version()
cli.help_ack()
cli.help_nack()
cli.help_abort()
cli.help_commit()
cli.help_stats()
cli.help_run()
cli.help_begin()
def testsubscribe(self):
teststdin = StubStdin()
teststdout = StubStdout(self)
teststdout.expect("CONNECTED")
cli = StompCLI(host, port, username, password, "1.0", stdin=teststdin, stdout=teststdout)
time.sleep(3)
teststdout.expect("Subscribing to '/queue/testsubscribe' with acknowledge set to 'auto', id set to '1'")
cli.onecmd("subscribe /queue/testsubscribe")
teststdout.expect("MESSAGE")
teststdout.expect("this is a test")
cli.onecmd("send /queue/testsubscribe this is a test")
time.sleep(3)
teststdout.expect("Unsubscribing from '/queue/testsubscribe'")
cli.onecmd("unsubscribe /queue/testsubscribe")
teststdout.expect("Shutting down, please wait")
cli.onecmd("quit")
def testsendrec(self):
teststdin = StubStdin()
teststdout = StubStdout(self)
teststdout.expect("CONNECTED")
cli = StompCLI(host, port, username, password, "1.1", stdin=teststdin, stdout=teststdout)
time.sleep(3)
teststdout.expect("RECEIPT")
cli.onecmd("sendrec /queue/testsendrec this is a test")
time.sleep(3)
teststdout.expect("Shutting down, please wait")
cli.onecmd("quit")
def testsendfile(self):
f = create_test_file()
teststdin = StubStdin()
teststdout = StubStdout(self)
teststdout.expect("CONNECTED")
cli = StompCLI(host, port, username, password, "1.2", stdin=teststdin, stdout=teststdout)
time.sleep(3)
teststdout.expect("Subscribing to '/queue/testsendfile' with acknowledge set to 'auto', id set to '1'")
cli.onecmd("subscribe /queue/testsendfile")
time.sleep(3)
teststdout.expect("MESSAGE")
cli.onecmd("sendfile /queue/testsendfile %s" % f.name)
time.sleep(3)
teststdout.expect("Shutting down, please wait")
cli.onecmd("quit")
def testsendfileheaders(self):
f = create_test_file()
teststdin = StubStdin()
teststdout = StubStdout(self)
teststdout.expect("CONNECTED")
cli = StompCLI(host, port, username, password, "1.2", stdin=teststdin, stdout=teststdout)
time.sleep(3)
cli.onecmd("sendfile /queue/testsendfile %s { \"custom\" : \"header\" }" % f.name)
time.sleep(3)
teststdout.expect("Shutting down, please wait")
cli.onecmd("quit")
def testabort(self):
teststdin = StubStdin()
teststdout = StubStdout(self)
teststdout.expect("CONNECTED")
cli = StompCLI(host, port, username, password, "1.2", stdin=teststdin, stdout=teststdout)
time.sleep(3)
teststdout.expect("Subscribing to '/queue/testabort' with acknowledge set to 'auto', id set to '1'")
cli.onecmd("subscribe /queue/testabort")
cli.onecmd("begin")
cli.onecmd("send /queue/testabort this is a test")
cli.onecmd("abort")
time.sleep(3)
teststdout.expect("Unsubscribing from '/queue/testabort'")
cli.onecmd("unsubscribe /queue/testabort")
teststdout.expect("Shutting down, please wait")
cli.onecmd("quit")
def testcommit(self):
teststdin = StubStdin()
teststdout = StubStdout(self)
teststdout.expect("CONNECTED")
cli = StompCLI(host, port, username, password, "1.0", stdin=teststdin, stdout=teststdout)
time.sleep(3)
teststdout.expect("Subscribing to '/queue/testcommit' with acknowledge set to 'auto', id set to '1'")
cli.onecmd("subscribe /queue/testcommit")
cli.onecmd("begin")
cli.onecmd("send /queue/testcommit this is a test")
teststdout.expect("Committing.*")
teststdout.expect("MESSAGE")
teststdout.expect("this is a test")
cli.onecmd("commit")
time.sleep(3)
teststdout.expect("Unsubscribing from '/queue/testcommit'")
cli.onecmd("unsubscribe /queue/testcommit")
teststdout.expect("Shutting down, please wait")
cli.onecmd("quit")
def teststats(self):
teststdin = StubStdin()
teststdout = StubStdout(self)
teststdout.expect("CONNECTED")
cli = StompCLI(host, port, username, password, "1.0", stdin=teststdin, stdout=teststdout)
time.sleep(3)
teststdout.expect("Subscribing to '/queue/teststats' with acknowledge set to 'auto', id set to '1'")
cli.onecmd("subscribe /queue/teststats")
teststdout.expect(".*No stats available.*")
cli.onecmd("stats")
time.sleep(1)
cli.onecmd("stats on")
cli.onecmd("send /queue/teststats this is a test")
teststdout.expect("MESSAGE")
teststdout.expect("this is a test")
time.sleep(3)
cli.onecmd("stats")
teststdout.expect("Unsubscribing from '/queue/teststats'")
cli.onecmd("unsubscribe /queue/teststats")
teststdout.expect("Shutting down, please wait")
cli.onecmd("quit")
def testrun(self):
f = create_test_file()
teststdin = StubStdin()
teststdout = StubStdout(self)
teststdout.expect("CONNECTED")
cli = StompCLI(host, port, username, password, "1.0", stdin=teststdin, stdout=teststdout)
time.sleep(3)
teststdout.expect("Subscribing to '/queue/testfile' with acknowledge set to 'auto', id set to '1'")
teststdout.expect("this is a test")
teststdout.expect("MESSAGE")
teststdout.expect("Unsubscribing from '/queue/testfile'")
cli.onecmd("run %s" % f.name)
teststdout.expect("Shutting down, please wait")
cli.onecmd("quit")
def testrunarg(self):
f = create_test_file()
teststdin = StubStdin()
teststdout = StubStdout(self)
teststdout.expect("CONNECTED")
cli = StompCLI(host, port, username, password, "1.0", stdin=teststdin, stdout=teststdout)
time.sleep(3)
teststdout.expect("Subscribing to '/queue/testfile' with acknowledge set to 'auto', id set to '1'")
teststdout.expect("this is a test")
teststdout.expect("MESSAGE")
teststdout.expect("Unsubscribing from '/queue/testfile'")
cli.do_run(f.name)
teststdout.expect("Shutting down, please wait")
cli.onecmd("quit")
def test_multicast(self):
teststdin = StubStdin()
teststdout = StubStdout(self)
teststdout.expect("CONNECTED")
cli = StompCLI(None, None, None, None, "multicast", stdin=teststdin, stdout=teststdout)
teststdout.expect("Subscribing to '/queue/testmulticastclisubscribe' with acknowledge set to 'auto', id set to '1'")
cli.onecmd("subscribe /queue/testmulticastclisubscribe")
teststdout.expect("MESSAGE")
teststdout.expect("this is a test")
cli.onecmd("send /queue/testsubscribe this is a test")
teststdout.expect("Shutting down, please wait")
cli.onecmd("quit")
# just here for coverage
def test_on_disconnected_edgecase(self):
teststdin = StubStdin()
teststdout = StubStdout(self)
teststdout.expect("CONNECTED")
cli = StompCLI(host, port, username, password, "1.0", stdin=teststdin, stdout=teststdout)
teststdout.expect(".*lost connection.*")
cli.on_disconnected()
|