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
|
require 'stomp_server'
require 'fileutils'
require 'test/unit' unless defined? $ZENTEST and $ZENTEST
require 'tesly'
#$DEBUG = true
class TestStompServer < Test::Unit::TestCase
# Is it a mock? it is what we are testing, but of
# course I am really testing the module, so I say
# yes it is a mock :-)
class MockStompServer
include StompServer
attr_accessor :sent, :connected
def initialize
@sent = ''
@connected = true
end
def send_data(data)
@sent += data
end
def close_connection_after_writing
@connected = false
end
alias close_connection close_connection_after_writing
def stomp(cmd, headers={}, body='', flush_prev = true)
@sent = '' if flush_prev
sf = StompFrame.new(cmd, headers, body)
receive_data(sf.to_s)
end
def do_connect(flush = true)
stomp('CONNECT')
@sent = '' if flush
end
def self.make_client(start_connection=true, flush=true)
ss = MockStompServer.new
ss.post_init
ss.do_connect(flush) if start_connection
ss
end
end
def assert_stomp_error(ss=@ss, match = nil)
assert_match(/ERROR/, ss.sent)
assert_match(match, ss.sent) if match
assert(!ss.connected)
end
def setup
@ss = MockStompServer.make_client
end
def test_version
assert(StompServer.const_defined?(:VERSION))
end
def test_invalid_command
assert_nothing_raised do
@ss.stomp('INVALID')
end
assert_stomp_error
end
def test_unconnected_command
ss = MockStompServer.make_client(false)
assert_nothing_raised do
ss.stomp('SEND')
end
assert_stomp_error(ss)
end
def test_connect
ss = MockStompServer.make_client(false)
assert(!ss.connected)
assert_nothing_raised do
ss.connect(false)
end
assert_match(/CONNECTED/, ss.sent)
assert(ss.connected)
end
def test_disconnect
assert_nothing_raised do
@ss.stomp('DISCONNECT')
end
assert(!@ss.connected)
end
def test_receipt
assert_nothing_raised do
@ss.stomp('SUBSCRIBE', {'receipt' => 'foobar'})
end
assert_match(/RECEIPT/, @ss.sent)
assert_match(/receipt-id:foobar/, @ss.sent)
assert(@ss.connected)
end
def test_topic
assert_equal('', @ss.sent)
# setup two clients (@ss and this one)
ss2 = MockStompServer.make_client
assert_equal('', ss2.sent)
@ss.stomp('SEND', {'destination' => '/topic/foo'}, 'Hi Pat')
@ss.stomp('SEND', {'destination' => '/topic/foo'}, 'Hi Sue')
assert_equal('', @ss.sent)
assert_equal('', ss2.sent)
ss2.stomp("SUBSCRIBE", {'destination' => '/topic/foo'})
assert_equal('', ss2.sent)
@ss.stomp('SEND', {'destination' => '/topic/foo'}, 'Hi Pat')
assert_match(/Hi Pat/, ss2.sent)
assert_equal('', @ss.sent)
end
def test_bad_topic
assert_equal('', @ss.sent)
# setup two clients (@ss and this one)
ss2 = MockStompServer.make_client
assert_equal('', ss2.sent)
ss2.stomp("SUBSCRIBE", {'destination' => '/badtopic/foo'})
assert_equal('', ss2.sent)
@ss.stomp('SEND', {'destination' => '/badtopic/foo'}, 'Hi Pat')
assert_match(/Hi Pat/, ss2.sent)
assert_equal('', @ss.sent)
end
def test_multiple_subscriber_topic
assert_equal('', @ss.sent)
# setup two clients (@ss and this one)
ss2 = MockStompServer.make_client
assert_equal('', ss2.sent)
@ss.stomp("SUBSCRIBE", {'destination' => '/topic/foo'})
ss2.stomp("SUBSCRIBE", {'destination' => '/topic/foo'})
assert_equal('', @ss.sent)
assert_equal('', ss2.sent)
@ss.stomp('SEND', {'destination' => '/topic/foo'}, 'Hi Pat')
assert_match(/Hi Pat/, ss2.sent)
assert_match(/Hi Pat/, @ss.sent)
end
def test_invalid_transaction
@ss.stomp("SEND", {'transaction' => 't'})
assert_stomp_error
end
def test_simple_transaction
ss1 = MockStompServer.make_client
ss2 = MockStompServer.make_client
ss2.stomp("SUBSCRIBE", {"destination" => '/topic/foo'})
assert_equal('', ss2.sent)
ss1.stomp("BEGIN", {"transaction" => 'simple'})
ss1.stomp("SEND", {"transaction" => 'simple', 'destination' => '/topic/foo'}, 'Hi Pat')
assert_equal('', ss2.sent)
assert_equal('', ss1.sent)
ss1.stomp("COMMIT", {"transaction" => 'simple'})
assert_equal('', ss1.sent)
assert_match(/Hi Pat/, ss2.sent)
end
def test_simple_transaction2
ss1 = MockStompServer.make_client
ss2 = MockStompServer.make_client
ss2.stomp("BEGIN", {"transaction" => 'simple'})
ss2.stomp("SUBSCRIBE", {"transaction" => 'simple', "destination" => '/topic/foo'})
assert_equal('', ss2.sent)
ss1.stomp("SEND", {'destination' => '/topic/foo'}, 'Hi Pat')
assert_equal('', ss2.sent)
assert_equal('', ss1.sent)
ss2.stomp("COMMIT", {"transaction" => 'simple'})
assert_equal('', ss1.sent)
assert_equal('', ss2.sent)
ss1.stomp("SEND", {'destination' => '/topic/foo'}, 'Hi Pat')
assert_match(/Hi Pat/, ss2.sent)
end
def test_simple_abort_transaction
ss1 = MockStompServer.make_client
ss2 = MockStompServer.make_client
ss2.stomp("BEGIN", {"transaction" => 'simple'})
ss2.stomp("SUBSCRIBE", {"transaction" => 'simple', "destination" => '/topic/foo'})
assert_equal('', ss2.sent)
ss1.stomp("SEND", {'destination' => '/topic/foo'}, 'Hi Pat')
assert_equal('', ss2.sent)
assert_equal('', ss1.sent)
ss2.stomp("ABORT", {"transaction" => 'simple'})
assert_equal('', ss1.sent)
assert_equal('', ss2.sent)
ss1.stomp("SEND", {'destination' => '/topic/foo'}, 'Hi Pat')
assert_match('', ss2.sent)
end
def test_simple_queue_message
ss1 = MockStompServer.make_client
ss1.stomp("SEND", {'destination' => '/queue/foo'}, 'Hi Pat')
ss1.stomp("DISCONNECT")
ss2 = MockStompServer.make_client
ss2.stomp("SUBSCRIBE", {"destination" => '/queue/foo'})
assert_match(/Hi Pat/, ss2.sent)
end
end
|