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
|
#!/usr/bin/ruby
$:.unshift '../lib'
$:.unshift './lib/'
require 'tempfile'
require 'test/unit'
require 'socket'
require 'xmpp4r/stream'
require 'xmpp4r/semaphore'
require 'clienttester'
include Jabber
# Jabber::debug = true
class StreamTest < Test::Unit::TestCase
include ClientTester
def busywait(&block)
n = 0
while not block.call and n < 10000
Thread::pass
n += 1
end
end
##
# tests that connection really waits the call to process() to dispatch
# stanzas to filters
def test_process
called = false
@client.add_xml_callback { called = true }
assert(!called)
@server.send('<iq/>')
busywait { called }
assert(called)
end
def test_process20
done = Semaphore.new
n = 0
@client.add_message_callback {
n += 1
done.run if n % 20 == 0
}
20.times {
@server.send('<message/>')
}
done.wait
assert_equal(20, n)
@server.send('<message/>' * 20)
done.wait
assert_equal(40, n)
end
def test_send
sem = Semaphore::new
@server.add_xml_callback { |e|
@server.send(Iq.new(:result))
sem.run
}
called = 0
@client.send(Iq.new(:get)) { |reply|
called += 1
if reply.kind_of? Iq and reply.type == :result
true
else
false
end
}
sem.wait
busywait { called }
assert_equal(1, called)
end
def test_send_nested
finished = Semaphore.new
id = 0
@server.add_xml_callback do |e|
id += 1
if id == 1
@server.send(Iq.new(:result).set_id('1').delete_namespace)
elsif id == 2
@server.send(Iq.new(:result).set_id('2').delete_namespace)
elsif id == 3
@server.send(Iq.new(:result).set_id('3').delete_namespace)
else
p e
end
end
called_outer = 0
called_inner = 0
@client.send(Iq.new(:get)) do |reply|
called_outer += 1
assert_kind_of(Iq, reply)
assert_equal(:result, reply.type)
if reply.id == '1'
@client.send(Iq.new(:set)) do |reply2|
called_inner += 1
assert_kind_of(Iq, reply2)
assert_equal(:result, reply2.type)
assert_equal('2', reply2.id)
@client.send(Iq.new(:get))
true
end
false
elsif reply.id == '3'
true
else
false
end
end
assert_equal(2, called_outer)
assert_equal(1, called_inner)
end
def test_send_in_callback
finished = Semaphore.new
@client.add_message_callback {
@client.send_with_id(Iq.new(:get)) { |reply|
assert_equal(:result, reply.type)
finished.run
}
}
@server.add_iq_callback { |iq|
@server.send(Iq.new(:result).set_id(iq.id))
}
@server.send(Message.new)
finished.wait
end
def test_similar_children
n = 0
@client.add_message_callback { n += 1 }
assert_equal(0, n)
@server.send("<message/>")
busywait { n == 1 }
assert_equal(1, n)
@server.send('<message>')
assert_equal(1, n)
@server.send('<message/>')
assert_equal(1, n)
@server.send('</message>')
busywait { n == 2 }
assert_equal(2, n)
@server.send("<message><stream:stream><message/></stream:stream>")
assert_equal(2, n)
@server.send('</message>')
busywait { n == 3 }
assert_equal(3, n)
end
end
|