xmpp4r 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (103) hide show
  1. data/ChangeLog +12 -0
  2. data/Rakefile +2 -4
  3. data/data/doc/xmpp4r/examples/advanced/adventure/adventuremuc.rb +6 -6
  4. data/data/doc/xmpp4r/examples/advanced/adventure/world.rb +3 -3
  5. data/data/doc/xmpp4r/examples/advanced/minimuc.rb +5 -5
  6. data/data/doc/xmpp4r/examples/advanced/xmpping.rb +17 -4
  7. data/data/doc/xmpp4r/examples/advanced/xmppingrc.sample +5 -0
  8. data/data/doc/xmpp4r/examples/basic/echo_threaded.rb +6 -2
  9. data/data/doc/xmpp4r/examples/basic/muc_owner_config.rb +13 -0
  10. data/lib/xmpp4r.rb +0 -6
  11. data/lib/xmpp4r/bytestreams/helper/filetransfer.rb +6 -7
  12. data/lib/xmpp4r/bytestreams/helper/ibb/base.rb +5 -6
  13. data/lib/xmpp4r/bytestreams/helper/ibb/target.rb +3 -5
  14. data/lib/xmpp4r/bytestreams/helper/socks5bytestreams/base.rb +1 -1
  15. data/lib/xmpp4r/bytestreams/helper/socks5bytestreams/server.rb +10 -8
  16. data/lib/xmpp4r/bytestreams/helper/socks5bytestreams/target.rb +3 -5
  17. data/lib/xmpp4r/bytestreams/iq/bytestreams.rb +11 -17
  18. data/lib/xmpp4r/bytestreams/iq/si.rb +13 -32
  19. data/lib/xmpp4r/callbacks.rb +124 -0
  20. data/lib/xmpp4r/client.rb +2 -5
  21. data/lib/xmpp4r/command/helper/responder.rb +53 -0
  22. data/lib/xmpp4r/command/iq/command.rb +154 -0
  23. data/lib/xmpp4r/connection.rb +49 -12
  24. data/lib/xmpp4r/dataforms/x/data.rb +66 -29
  25. data/lib/xmpp4r/delay/x/delay.rb +2 -3
  26. data/lib/xmpp4r/discovery/iq/discoinfo.rb +20 -33
  27. data/lib/xmpp4r/discovery/iq/discoitems.rb +5 -28
  28. data/lib/xmpp4r/error.rb +5 -10
  29. data/lib/xmpp4r/feature_negotiation/iq/feature.rb +2 -20
  30. data/lib/xmpp4r/httpbinding.rb +5 -0
  31. data/lib/xmpp4r/httpbinding/client.rb +285 -0
  32. data/lib/xmpp4r/iq.rb +22 -41
  33. data/lib/xmpp4r/message.rb +8 -38
  34. data/lib/xmpp4r/muc.rb +2 -0
  35. data/lib/xmpp4r/muc/helper/mucclient.rb +50 -1
  36. data/lib/xmpp4r/muc/helper/simplemucclient.rb +2 -2
  37. data/lib/xmpp4r/muc/iq/mucowner.rb +11 -0
  38. data/lib/xmpp4r/muc/x/muc.rb +2 -30
  39. data/lib/xmpp4r/muc/x/mucuserinvite.rb +4 -2
  40. data/lib/xmpp4r/muc/x/mucuseritem.rb +4 -2
  41. data/lib/xmpp4r/presence.rb +7 -35
  42. data/lib/xmpp4r/pubsub.rb +1 -0
  43. data/lib/xmpp4r/pubsub/helper/nodebrowser.rb +174 -0
  44. data/lib/xmpp4r/pubsub/helper/nodehelper.rb +153 -0
  45. data/lib/xmpp4r/pubsub/helper/servicehelper.rb +326 -0
  46. data/lib/xmpp4r/pubsub/iq/pubsub.rb +19 -0
  47. data/lib/xmpp4r/pubsub/stanzas/event.rb +49 -0
  48. data/lib/xmpp4r/pubsub/stanzas/item.rb +27 -0
  49. data/lib/xmpp4r/pubsub/stanzas/items.rb +35 -0
  50. data/lib/xmpp4r/pubsub/stanzas/subscription.rb +58 -0
  51. data/lib/xmpp4r/query.rb +4 -32
  52. data/lib/xmpp4r/rexmladdons.rb +7 -772
  53. data/lib/xmpp4r/roster/helper/roster.rb +29 -50
  54. data/lib/xmpp4r/roster/iq/roster.rb +6 -35
  55. data/lib/xmpp4r/roster/x/roster.rb +13 -30
  56. data/lib/xmpp4r/rpc.rb +2 -0
  57. data/lib/xmpp4r/rpc/helper/client.rb +114 -0
  58. data/lib/xmpp4r/rpc/helper/server.rb +75 -0
  59. data/lib/xmpp4r/rpc/helper/xmlrpcaddons.rb +57 -0
  60. data/lib/xmpp4r/rpc/iq/rpc.rb +24 -0
  61. data/lib/xmpp4r/sasl.rb +1 -1
  62. data/lib/xmpp4r/semaphore.rb +38 -0
  63. data/lib/xmpp4r/stream.rb +104 -165
  64. data/lib/xmpp4r/streamparser.rb +2 -2
  65. data/lib/xmpp4r/vcard/iq/vcard.rb +5 -12
  66. data/lib/xmpp4r/version/helper/responder.rb +2 -1
  67. data/lib/xmpp4r/version/iq/version.rb +6 -18
  68. data/lib/xmpp4r/x.rb +20 -26
  69. data/lib/xmpp4r/xmpp4r.rb +1 -1
  70. data/lib/xmpp4r/xmppelement.rb +152 -0
  71. data/lib/xmpp4r/{xmlstanza.rb → xmppstanza.rb} +17 -29
  72. data/setup.rb +1 -0
  73. data/test/bytestreams/tc_ibb.rb +8 -8
  74. data/test/dataforms/tc_data.rb +81 -0
  75. data/test/lib/clienttester.rb +20 -17
  76. data/test/muc/tc_muc_mucclient.rb +48 -23
  77. data/test/muc/tc_muc_simplemucclient.rb +7 -4
  78. data/test/muc/tc_mucowner.rb +50 -0
  79. data/test/pubsub/tc_helper.rb +227 -0
  80. data/test/roster/tc_helper.rb +181 -55
  81. data/test/roster/tc_iqqueryroster.rb +33 -0
  82. data/test/roster/tc_xroster.rb +6 -3
  83. data/test/rpc/tc_helper.rb +84 -0
  84. data/test/tc_callbacks.rb +2 -1
  85. data/test/tc_class_names.rb +9 -1
  86. data/test/tc_error.rb +1 -0
  87. data/test/tc_iq.rb +13 -12
  88. data/test/tc_message.rb +1 -0
  89. data/test/tc_presence.rb +1 -0
  90. data/test/tc_rexml.rb +1 -1
  91. data/test/tc_stream.rb +147 -102
  92. data/test/tc_streamComponent.rb +94 -0
  93. data/test/tc_streamError.rb +67 -29
  94. data/test/tc_streamSend.rb +1 -1
  95. data/test/tc_xmppstanza.rb +125 -0
  96. data/test/ts_xmpp4r.rb +37 -28
  97. data/test/version/tc_helper.rb +14 -0
  98. data/test/version/tc_iqqueryversion.rb +4 -3
  99. metadata +163 -123
  100. data/data/doc/xmpp4r/examples/basic/echo_nonthreaded.rb +0 -32
  101. data/lib/callbacks.rb +0 -122
  102. data/test/tc_streamThreaded.rb +0 -168
  103. data/test/tc_xmlstanza.rb +0 -76
@@ -5,6 +5,8 @@ $:.unshift File::dirname(__FILE__) + '/../../lib'
5
5
  require 'test/unit'
6
6
  require 'xmpp4r/rexmladdons'
7
7
  require 'xmpp4r/roster/iq/roster'
8
+ require 'xmpp4r/jid'
9
+ require 'xmpp4r/iq'
8
10
  include Jabber
9
11
 
10
12
  class Roster::IqQueryRosterTest < Test::Unit::TestCase
@@ -25,6 +27,37 @@ class Roster::IqQueryRosterTest < Test::Unit::TestCase
25
27
  assert_equal(Roster::IqQueryRoster, iq2.query.class)
26
28
  end
27
29
 
30
+ def test_answer
31
+ iq = Iq::new_rosterget
32
+ assert_equal(:get, iq.type)
33
+ assert_nil(iq.to)
34
+ assert_equal('jabber:client', iq.namespace)
35
+ assert_equal('jabber:iq:roster', iq.queryns)
36
+ assert_equal(0, iq.query.children.size)
37
+
38
+ iq2 = iq.answer(true)
39
+ assert_equal(:get, iq2.type)
40
+ assert_nil(iq2.from)
41
+ assert_equal('jabber:client', iq2.namespace)
42
+ assert_equal('jabber:iq:roster', iq2.queryns)
43
+ assert_equal(0, iq2.query.children.size)
44
+ end
45
+
46
+ def test_xmlns
47
+ ri = Roster::RosterItem.new
48
+ assert_equal('jabber:iq:roster', ri.namespace)
49
+ assert_equal('jabber:iq:roster', ri.attributes['xmlns'])
50
+
51
+ r = Roster::IqQueryRoster.new
52
+ assert_equal('jabber:iq:roster', r.namespace)
53
+ assert_equal('jabber:iq:roster', r.attributes['xmlns'])
54
+
55
+ r.add(ri)
56
+
57
+ assert_equal('jabber:iq:roster', ri.namespace)
58
+ assert_nil(ri.attributes['xmlns'])
59
+ end
60
+
28
61
  def test_items
29
62
  r = Roster::IqQueryRoster::new
30
63
  r.add(Roster::RosterItem.new)
@@ -10,9 +10,12 @@ include Jabber
10
10
 
11
11
  class Roster::XRosterTest < Test::Unit::TestCase
12
12
  def test_create
13
- r = Roster::XRoster.new
14
- assert_equal('x', r.name)
15
- assert_equal('http://jabber.org/protocol/rosterx', r.namespace)
13
+ r1 = Roster::XRoster.new
14
+ assert_equal('x', r1.name)
15
+ assert_equal('jabber:x:roster', r1.namespace)
16
+ r2 = Roster::RosterX.new
17
+ assert_equal('x', r2.name)
18
+ assert_equal('http://jabber.org/protocol/rosterx', r2.namespace)
16
19
  end
17
20
 
18
21
  def test_import
@@ -0,0 +1,84 @@
1
+ #!/usr/bin/ruby
2
+
3
+ $:.unshift File::dirname(__FILE__) + '/../../lib'
4
+
5
+ require 'test/unit'
6
+ require File::dirname(__FILE__) + '/../lib/clienttester'
7
+
8
+ require 'xmpp4r'
9
+ require 'xmpp4r/rpc/helper/client'
10
+ require 'xmpp4r/rpc/helper/server'
11
+ include Jabber
12
+
13
+ class RPC::HelperTest < Test::Unit::TestCase
14
+ include ClientTester
15
+
16
+ def give_client_jid!
17
+ class << @client
18
+ def jid; Jabber::JID.new('client@test.com/clienttester'); end
19
+ end
20
+ end
21
+
22
+ def test_create
23
+ give_client_jid!
24
+
25
+ cl = RPC::Client.new(@client, 'a@b/c')
26
+ assert_kind_of(RPC::Client, cl)
27
+ sv = RPC::Server.new(@server)
28
+ assert_kind_of(RPC::Server, sv)
29
+ end
30
+
31
+ def test_simple
32
+ give_client_jid!
33
+
34
+ sv = RPC::Server.new(@server)
35
+ sv.add_handler("echo") do |s| s end
36
+
37
+ cl = RPC::Client.new(@client, 'a@b/c')
38
+ assert_equal('Test string', cl.call("echo", 'Test string'))
39
+ end
40
+
41
+ def test_introspection
42
+ give_client_jid!
43
+
44
+ sv = RPC::Server.new(@server)
45
+ sv.add_introspection
46
+
47
+ cl = RPC::Client.new(@client, 'a@b/c')
48
+ cl_methods = cl.call("system.listMethods")
49
+ assert(cl_methods.size > 0)
50
+ cl_methods.each { |method|
51
+ assert_kind_of(String, method)
52
+ assert(method =~ /^system\./)
53
+ }
54
+ end
55
+
56
+ def test_multicall
57
+ give_client_jid!
58
+
59
+ sv = RPC::Server.new(@server)
60
+ sv.add_multicall
61
+ sv.add_handler("reverse") do |s| s.reverse end
62
+ sv.add_handler("upcase") do |s| s.upcase end
63
+
64
+ cl = RPC::Client.new(@client, 'a@b/c')
65
+ assert_equal(['tseT', 'TEST'], cl.multicall(['reverse', 'Test'], ['upcase', 'Test']))
66
+ end
67
+
68
+ def test_100calls
69
+ give_client_jid!
70
+
71
+ sv = RPC::Server.new(@server)
72
+ sv.add_handler("add") do |a,b| a+b end
73
+
74
+ cl = RPC::Client.new(@client, 'a@b/c')
75
+ correct = true
76
+ 100.times {
77
+ a, b = rand(1000), rand(1000)
78
+ correct &&= (cl.call('add', a, b) == a + b)
79
+ }
80
+
81
+ assert(correct)
82
+ end
83
+ end
84
+
@@ -3,7 +3,8 @@
3
3
  $:.unshift '../lib'
4
4
 
5
5
  require 'test/unit'
6
- require 'callbacks'
6
+ require 'xmpp4r/callbacks'
7
+ include Jabber
7
8
 
8
9
  class CallbacksTest < Test::Unit::TestCase
9
10
  def test_test1
@@ -29,7 +29,8 @@ class JIDTest < Test::Unit::TestCase
29
29
  assert_kind_of(Class, Jabber::Stream)
30
30
  assert_kind_of(Class, Jabber::StreamParser)
31
31
  assert_kind_of(Class, Jabber::X)
32
- assert_kind_of(Class, Jabber::XMLStanza)
32
+ assert_kind_of(Class, Jabber::XMPPElement)
33
+ assert_kind_of(Class, Jabber::XMPPStanza)
33
34
  end
34
35
 
35
36
  def test_roster
@@ -126,4 +127,11 @@ class JIDTest < Test::Unit::TestCase
126
127
  assert_kind_of(Class, Jabber::Version::SimpleResponder)
127
128
  assert_kind_of(Class, Jabber::Version::IqQueryVersion)
128
129
  end
130
+
131
+ def test_rpc
132
+ require 'xmpp4r/rpc'
133
+ assert_kind_of(Module, Jabber::RPC)
134
+ assert_kind_of(Class, Jabber::RPC::Server)
135
+ assert_kind_of(Class, Jabber::RPC::Client)
136
+ end
129
137
  end
@@ -3,6 +3,7 @@
3
3
  $:.unshift '../lib'
4
4
 
5
5
  require 'test/unit'
6
+ require 'xmpp4r'
6
7
  require 'xmpp4r/rexmladdons'
7
8
  require 'xmpp4r/error'
8
9
  require 'xmpp4r/message'
@@ -12,41 +12,42 @@ class IqTest < Test::Unit::TestCase
12
12
  def test_create
13
13
  x = Iq::new()
14
14
  assert_equal("iq", x.name)
15
- assert_equal("<iq/>", x.to_s)
15
+ assert_equal("jabber:client", x.namespace)
16
+ assert_equal("<iq xmlns='jabber:client'/>", x.to_s)
16
17
  end
17
18
 
18
19
  def test_iqauth
19
20
  x = Iq::new_authset(JID::new('node@domain/resource'), 'password')
20
- assert_equal("<iq type='set'><query xmlns='jabber:iq:auth'><username>node</username><password>password</password><resource>resource</resource></query></iq>", x.to_s)
21
+ assert_equal("<iq type='set' xmlns='jabber:client'><query xmlns='jabber:iq:auth'><username>node</username><password>password</password><resource>resource</resource></query></iq>", x.to_s)
21
22
  end
22
23
 
23
24
  def test_iqauth_digest
24
25
  x = Iq::new_authset_digest(JID::new('node@domain/resource'), '', 'password')
25
- assert_equal("<iq type='set'><query xmlns='jabber:iq:auth'><username>node</username><digest>#{Digest::SHA1.hexdigest('password')}</digest><resource>resource</resource></query></iq>", x.to_s)
26
+ assert_equal("<iq type='set' xmlns='jabber:client'><query xmlns='jabber:iq:auth'><username>node</username><digest>#{Digest::SHA1.hexdigest('password')}</digest><resource>resource</resource></query></iq>", x.to_s)
26
27
  end
27
28
 
28
29
  def test_register
29
30
  x1 = Iq::new_register
30
- assert_equal("<iq type='set'><query xmlns='jabber:iq:register'/></iq>", x1.to_s)
31
+ assert_equal("<iq type='set' xmlns='jabber:client'><query xmlns='jabber:iq:register'/></iq>", x1.to_s)
31
32
  x2 = Iq::new_register('node')
32
- assert_equal("<iq type='set'><query xmlns='jabber:iq:register'><username>node</username></query></iq>", x2.to_s)
33
+ assert_equal("<iq type='set' xmlns='jabber:client'><query xmlns='jabber:iq:register'><username>node</username></query></iq>", x2.to_s)
33
34
  x3 = Iq::new_register('node', 'password')
34
- assert_equal("<iq type='set'><query xmlns='jabber:iq:register'><username>node</username><password>password</password></query></iq>", x3.to_s)
35
+ assert_equal("<iq type='set' xmlns='jabber:client'><query xmlns='jabber:iq:register'><username>node</username><password>password</password></query></iq>", x3.to_s)
35
36
  end
36
37
 
37
38
  def test_rosterget
38
39
  x = Iq::new_rosterget
39
- assert_equal("<iq type='get'><query xmlns='jabber:iq:roster'/></iq>", x.to_s)
40
+ assert_equal("<iq type='get' xmlns='jabber:client'><query xmlns='jabber:iq:roster'/></iq>", x.to_s)
40
41
  end
41
42
 
42
43
  def test_rosterset
43
44
  x = Iq::new_rosterset
44
- assert_equal("<iq type='set'><query xmlns='jabber:iq:roster'/></iq>", x.to_s)
45
+ assert_equal("<iq type='set' xmlns='jabber:client'><query xmlns='jabber:iq:roster'/></iq>", x.to_s)
45
46
  end
46
47
 
47
48
  def test_browseget
48
49
  x = Iq::new_browseget
49
- assert_equal("<iq type='get'><query xmlns='jabber:iq:browse'/></iq>", x.to_s)
50
+ assert_equal("<iq type='get' xmlns='jabber:client'><query xmlns='jabber:iq:browse'/></iq>", x.to_s)
50
51
  end
51
52
 
52
53
  def test_types
@@ -69,14 +70,14 @@ class IqTest < Test::Unit::TestCase
69
70
  assert_equal(nil, x.queryns)
70
71
  query = REXML::Element::new('query')
71
72
  x.add(query)
72
- assert_equal('', x.queryns)
73
+ assert_equal('jabber:client', x.queryns)
73
74
  query.add_namespace('jabber:iq:auth')
74
75
  assert_equal(query.to_s, x.query.to_s)
75
76
  assert_equal('jabber:iq:auth', x.queryns)
76
77
 
77
78
  query2 = REXML::Element::new('query')
78
79
  x.query = query2
79
- assert_equal('', x.queryns)
80
+ assert_equal('jabber:client', x.queryns)
80
81
  query2.add_namespace('jabber:iq:register')
81
82
  assert_equal('jabber:iq:register', x.queryns)
82
83
  end
@@ -104,6 +105,6 @@ class IqTest < Test::Unit::TestCase
104
105
  assert_equal(nil, x.from)
105
106
  assert_equal(JID.new('a@b/c'), x.to)
106
107
  assert_kind_of(IqQuery, x.query)
107
- assert_equal('', x.queryns)
108
+ assert_equal('jabber:client', x.queryns)
108
109
  end
109
110
  end
@@ -12,6 +12,7 @@ class MessageTest < Test::Unit::TestCase
12
12
  def test_create
13
13
  x = Message::new()
14
14
  assert_equal("message", x.name)
15
+ assert_equal("jabber:client", x.namespace)
15
16
  assert_equal(nil, x.to)
16
17
  assert_equal(nil, x.body)
17
18
 
@@ -12,6 +12,7 @@ class PresenceTest < Test::Unit::TestCase
12
12
  def test_create
13
13
  x = Presence::new()
14
14
  assert_equal("presence", x.name)
15
+ assert_equal("jabber:client", x.namespace)
15
16
  assert_equal(nil, x.to)
16
17
  assert_equal(nil, x.show)
17
18
  assert_equal(nil, x.status)
@@ -51,7 +51,7 @@ class REXMLTest < Test::Unit::TestCase
51
51
  e.attributes['x'] = '&'
52
52
  assert_equal('&', e.attributes['x'])
53
53
  e.attributes['x'] = '&amp;'
54
- assert_equal('&amp;', e.attributes['x'])
54
+ assert_equal('&', e.attributes['x']) # this one should not be escaped
55
55
  e.attributes['x'] = '&nbsp'
56
56
  assert_equal('&nbsp', e.attributes['x'])
57
57
  e.attributes['x'] = '&nbsp;'
@@ -6,24 +6,23 @@ require 'tempfile'
6
6
  require 'test/unit'
7
7
  require 'socket'
8
8
  require 'xmpp4r/stream'
9
+ require 'xmpp4r/semaphore'
9
10
  include Jabber
10
11
 
11
-
12
12
  class StreamTest < Test::Unit::TestCase
13
13
  def setup
14
14
  @tmpfile = Tempfile::new("StreamSendTest")
15
15
  @tmpfilepath = @tmpfile.path()
16
16
  @tmpfile.unlink
17
17
  @servlisten = UNIXServer::new(@tmpfilepath)
18
- @server = nil
19
18
  thServer = Thread.new { @server = @servlisten.accept }
20
19
  @iostream = UNIXSocket::new(@tmpfilepath)
21
20
  n = 0
22
- while @server.nil? and n < 10
21
+ while not defined? @server and n < 10
23
22
  sleep 0.1
24
23
  n += 1
25
24
  end
26
- @stream = Stream::new(false)
25
+ @stream = Stream::new
27
26
  @stream.start(@iostream)
28
27
  end
29
28
 
@@ -33,150 +32,196 @@ class StreamTest < Test::Unit::TestCase
33
32
  end
34
33
 
35
34
  ##
36
- # tests that stream really waits the call to process() to dispatch
35
+ # tests that connection really waits the call to process() to dispatch
37
36
  # stanzas to filters
38
37
  def test_process
39
- =begin
40
- Disabled, because non-threaded mode is broken
41
-
42
38
  called = false
43
39
  @stream.add_xml_callback { called = true }
44
40
  assert(!called)
45
41
  @server.puts('<stream:stream>')
46
42
  @server.flush
47
- sleep 0.1
48
- assert(!called)
49
- @stream.process
50
43
  assert(called)
51
- =end
52
44
  end
53
45
 
54
- ##
55
- # tests that you can select how many messages you want to get with process
56
- def test_process_multi
57
- =begin
58
- Disabled, because non-threaded mode is broken
46
+ def test_process100
47
+ @server.puts('<stream:stream>')
48
+ @server.flush
59
49
 
60
- nbcalls = 0
61
- called = false
62
- @stream.add_xml_callback { |element|
63
- nbcalls += 1
64
- if element.name == "message"
65
- called = true
66
- end
50
+ done = Semaphore.new
51
+ n = 0
52
+ @stream.add_message_callback {
53
+ n += 1
54
+ done.run if n % 100 == 0
67
55
  }
68
- assert(!called)
69
- @server.puts('<stream:stream/>')
70
- @server.flush
71
- assert(!called)
72
- @stream.process
73
- assert(!called)
74
- assert_equal(1, nbcalls)
75
- for i in 1..10
76
- @server.puts('<presence/>')
56
+
57
+ 100.times {
58
+ @server.puts('<message/>')
77
59
  @server.flush
78
- end
79
- @server.puts('<message/>')
60
+ }
61
+
62
+ done.wait
63
+ assert_equal(100, n)
64
+
65
+ @server.puts('<message/>' * 100)
80
66
  @server.flush
81
- assert(!called)
82
- assert_equal(1, nbcalls)
83
- @stream.process(8)
84
- assert_equal(9, nbcalls)
85
- assert(!called)
86
- @stream.process(2)
87
- assert_equal(11, nbcalls)
88
- assert(!called)
89
- @stream.process(1)
90
- assert_equal(12, nbcalls)
91
- assert(called)
92
- =end
67
+
68
+ done.wait
69
+ assert_equal(200, n)
93
70
  end
94
71
 
95
- # tests that you can get all waiting messages if you don't use a parameter
96
- def test_process_multi2
97
- =begin
98
- Disabled, because non-threaded mode is broken
99
-
100
- @called = false
101
- @nbcalls = 0
102
- @stream.add_xml_callback { |element|
103
- @nbcalls += 1
104
- if element.name == "message"
105
- @called = true
72
+ def test_send
73
+ @server.puts('<stream:stream>')
74
+ @server.flush
75
+
76
+ Thread.new {
77
+ assert_equal(Iq.new(:get).delete_namespace.to_s, @server.gets('>'))
78
+ @stream.receive(Iq.new(:result))
79
+ }
80
+
81
+ called = 0
82
+ @stream.send(Iq.new(:get)) { |reply|
83
+ called += 1
84
+ if reply.kind_of? Iq and reply.type == :result
85
+ true
86
+ else
87
+ false
106
88
  end
107
89
  }
108
- assert(!@called)
90
+
91
+ assert_equal(1, called)
92
+ end
93
+
94
+ def test_send_nested
109
95
  @server.puts('<stream:stream>')
110
96
  @server.flush
111
- assert(!@called)
112
- @stream.process
113
- assert(!@called)
114
- assert_equal(1, @nbcalls)
115
- for i in 1..20
116
- @server.puts('<iq/>')
97
+ finished = Semaphore.new
98
+
99
+ Thread.new {
100
+ assert_equal(Iq.new(:get).delete_namespace.to_s, @server.gets('>'))
101
+ @server.puts(Iq.new(:result).set_id('1').delete_namespace.to_s)
102
+ @server.flush
103
+ assert_equal(Iq.new(:set).delete_namespace.to_s, @server.gets('>'))
104
+ @server.puts(Iq.new(:result).set_id('2').delete_namespace.to_s)
105
+ @server.flush
106
+ assert_equal(Iq.new(:get).delete_namespace.to_s, @server.gets('>'))
107
+ @server.puts(Iq.new(:result).set_id('3').delete_namespace.to_s)
117
108
  @server.flush
109
+
110
+ finished.run
111
+ }
112
+
113
+ called_outer = 0
114
+ called_inner = 0
115
+
116
+ @stream.send(Iq.new(:get)) do |reply|
117
+ called_outer += 1
118
+ assert_kind_of(Iq, reply)
119
+ assert_equal(:result, reply.type)
120
+
121
+ if reply.id == '1'
122
+ @stream.send(Iq.new(:set)) do |reply2|
123
+ called_inner += 1
124
+ assert_kind_of(Iq, reply2)
125
+ assert_equal(:result, reply2.type)
126
+ assert_equal('2', reply2.id)
127
+
128
+ @stream.send(Iq.new(:get))
129
+
130
+ true
131
+ end
132
+ false
133
+ elsif reply.id == '3'
134
+ true
135
+ else
136
+ false
137
+ end
118
138
  end
119
- @server.puts('<message/>')
139
+
140
+ assert_equal(2, called_outer)
141
+ assert_equal(1, called_inner)
142
+
143
+ finished.wait
144
+ end
145
+
146
+ def test_send_in_callback
147
+ @server.puts('<stream:stream>')
120
148
  @server.flush
121
- assert(!@called)
122
- assert_equal(1, @nbcalls)
123
- @stream.process
124
- assert_equal(22, @nbcalls)
125
- assert(@called)
126
- =end
149
+ finished = Semaphore.new
150
+
151
+ @stream.add_message_callback {
152
+ @stream.send_with_id(Iq.new(:get)) { |reply|
153
+ assert_equal(:result, reply.type)
154
+ }
155
+ }
156
+
157
+ Thread.new {
158
+ @server.gets('>')
159
+ @server.puts(Iq.new(:result))
160
+ finished.run
161
+ }
162
+
163
+ @server.puts(Message.new)
164
+ finished.wait
165
+ end
166
+
167
+ def test_bidi
168
+ @server.puts('<stream:stream>')
169
+ @server.flush
170
+ finished = Semaphore.new
171
+ ok = true
172
+ n = 100
173
+
174
+ Thread.new {
175
+ n.times { |i|
176
+ ok &&= (Iq.new(:get).set_id(i).delete_namespace.to_s == @server.gets('>'))
177
+ @server.puts(Iq.new(:result).set_id(i).to_s)
178
+ @server.flush
179
+ }
180
+
181
+ finished.run
182
+ }
183
+
184
+ n.times { |i|
185
+ @stream.send(Iq.new(:get).set_id(i)) { |reply|
186
+ ok &&= reply.kind_of? Iq
187
+ ok &&= (:result == reply.type)
188
+ ok &&= (i.to_s == reply.id)
189
+ true
190
+ }
191
+ }
192
+
193
+ finished.wait
194
+ assert(ok)
127
195
  end
128
196
 
129
- # Check that <message><message/></message> is recognized as one Message
130
197
  def test_similar_children
198
+ delay = 0.1
131
199
  n = 0
132
200
  @stream.add_message_callback { n += 1 }
133
201
  assert_equal(0, n)
134
202
  @server.puts('<stream:stream><message/>')
135
203
  @server.flush
136
- @stream.process
204
+ sleep delay
137
205
  assert_equal(1, n)
138
206
  @server.puts('<message>')
139
207
  @server.flush
140
- @stream.process
208
+ sleep delay
141
209
  assert_equal(1, n)
142
210
  @server.puts('<message/>')
143
211
  @server.flush
144
- @stream.process
212
+ sleep delay
145
213
  assert_equal(1, n)
146
214
  @server.puts('</message>')
147
215
  @server.flush
148
- @stream.process
216
+ sleep delay
149
217
  assert_equal(2, n)
150
218
  @server.puts('<message><stream:stream><message/></stream:stream>')
151
219
  @server.flush
152
- @stream.process
220
+ sleep delay
153
221
  assert_equal(2, n)
154
222
  @server.puts('</message>')
155
223
  @server.flush
156
- @stream.process
224
+ sleep delay
157
225
  assert_equal(3, n)
158
226
  end
159
-
160
- def test_send
161
- @server.puts('<stream:stream>')
162
- @server.flush
163
- @stream.process
164
-
165
- Thread.new {
166
- assert_equal(Iq.new(:get).to_s, @server.gets('>'))
167
- @stream.receive(Iq.new(:result))
168
- }
169
-
170
- called = 0
171
- @stream.send(Iq.new(:get)) { |reply|
172
- called += 1
173
- if reply.kind_of? Iq and reply.type == :result
174
- true
175
- else
176
- false
177
- end
178
- }
179
-
180
- assert_equal(1, called)
181
- end
182
227
  end