Class: StreamTest

Inherits:
Test::Unit::TestCase
  • Object
show all
Defined in:
lib/vendor/xmpp4r/test/tc_stream.rb

Constant Summary collapse

STREAM =
'<stream:stream xmlns:stream="http://etherx.jabber.org/streams">'

Instance Method Summary collapse

Instance Method Details

#setupObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/vendor/xmpp4r/test/tc_stream.rb', line 15

def setup
  @tmpfile = Tempfile.new("StreamSendTest")
  @tmpfilepath = @tmpfile.path()
  @tmpfile.unlink
  @servlisten = UNIXServer.new(@tmpfilepath)
  thServer = Thread.new { @server = @servlisten.accept }
  @iostream = UNIXSocket.new(@tmpfilepath)
  n = 0
  while not defined? @server and n < 10
    sleep 0.1
    n += 1
  end
  @stream = Stream.new
  @stream.start(@iostream)
end

#teardownObject



31
32
33
34
# File 'lib/vendor/xmpp4r/test/tc_stream.rb', line 31

def teardown
  @stream.close
  @server.close
end

#test_bidiObject



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
# File 'lib/vendor/xmpp4r/test/tc_stream.rb', line 169

def test_bidi
  @server.puts(STREAM)
  @server.flush
  finished = Semaphore.new
  ok = true
  n = 100

  Thread.new {
    n.times { |i|
      ok &&= (Iq.new(:get).set_id(i).delete_namespace.to_s == @server.gets('>'))
      @server.puts(Iq.new(:result).set_id(i).to_s)
      @server.flush
    }

    finished.run
  }

  n.times { |i|
    @stream.send(Iq.new(:get).set_id(i)) { |reply|
      ok &&= reply.kind_of? Iq
      ok &&= (:result == reply.type)
      ok &&= (i.to_s == reply.id)
      true
    }
  }

  finished.wait
  assert(ok)
end

#test_processObject

tests that connection really waits the call to process() to dispatch stanzas to filters



39
40
41
42
43
44
45
46
# File 'lib/vendor/xmpp4r/test/tc_stream.rb', line 39

def test_process
  called = false
  @stream.add_xml_callback { called = true }
  assert(!called)
  @server.puts(STREAM)
  @server.flush
  assert(called)
end

#test_process100Object



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
# File 'lib/vendor/xmpp4r/test/tc_stream.rb', line 48

def test_process100
  @server.puts(STREAM)
  @server.flush

  done = Semaphore.new
  n = 0
  @stream.add_message_callback {
    n += 1
    done.run if n % 100 == 0
  }

  100.times {
    @server.puts('<message/>')
    @server.flush
  }

  done.wait
  assert_equal(100, n)

  @server.puts('<message/>' * 100)
  @server.flush

  done.wait
  assert_equal(200, n)
end

#test_sendObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/vendor/xmpp4r/test/tc_stream.rb', line 74

def test_send
  @server.puts(STREAM)
  @server.flush

  Thread.new {
    assert_equal(Iq.new(:get).delete_namespace.to_s, @server.gets('>'))
    @stream.receive(Iq.new(:result))
  }

  called = 0
  @stream.send(Iq.new(:get)) { |reply|
    called += 1
    if reply.kind_of? Iq and reply.type == :result
      true
    else
      false
    end
  }

  assert_equal(1, called)
end

#test_send_in_callbackObject



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/vendor/xmpp4r/test/tc_stream.rb', line 148

def test_send_in_callback
  @server.puts(STREAM)
  @server.flush
  finished = Semaphore.new

  @stream.add_message_callback {
    @stream.send_with_id(Iq.new(:get)) { |reply|
      assert_equal(:result, reply.type)
    }
  }

  Thread.new {
    @server.gets('>')
    @server.puts(Iq.new(:result))
    finished.run
  }

  @server.puts(Message.new)
  finished.wait
end

#test_send_nestedObject



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
# File 'lib/vendor/xmpp4r/test/tc_stream.rb', line 96

def test_send_nested
  @server.puts(STREAM)
  @server.flush
  finished = Semaphore.new

  Thread.new {
    assert_equal(Iq.new(:get).delete_namespace.to_s, @server.gets('>'))
    @server.puts(Iq.new(:result).set_id('1').delete_namespace.to_s)
    @server.flush
    assert_equal(Iq.new(:set).delete_namespace.to_s, @server.gets('>'))
    @server.puts(Iq.new(:result).set_id('2').delete_namespace.to_s)
    @server.flush
    assert_equal(Iq.new(:get).delete_namespace.to_s, @server.gets('>'))
    @server.puts(Iq.new(:result).set_id('3').delete_namespace.to_s)
    @server.flush

    finished.run
  }

  called_outer = 0
  called_inner = 0

  @stream.send(Iq.new(:get)) do |reply|
    called_outer += 1
    assert_kind_of(Iq, reply)
    assert_equal(:result, reply.type)

    if reply.id == '1'
      @stream.send(Iq.new(:set)) do |reply2|
        called_inner += 1
        assert_kind_of(Iq, reply2)
        assert_equal(:result, reply2.type)
        assert_equal('2', reply2.id)

        @stream.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)

  finished.wait
end

#test_similar_childrenObject



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
# File 'lib/vendor/xmpp4r/test/tc_stream.rb', line 199

def test_similar_children
  delay = 0.1
  n = 0
  @stream.add_message_callback { n += 1 }
  assert_equal(0, n)
  @server.puts("#{STREAM}<message/>")
  @server.flush
  sleep delay
  assert_equal(1, n)
  @server.puts('<message>')
  @server.flush
  sleep delay
  assert_equal(1, n)
  @server.puts('<message/>')
  @server.flush
  sleep delay
  assert_equal(1, n)
  @server.puts('</message>')
  @server.flush
  sleep delay
  assert_equal(2, n)
  @server.puts("<message>#{STREAM}<message/></stream:stream>")
  @server.flush
  sleep delay
  assert_equal(2, n)
  @server.puts('</message>')
  @server.flush
  sleep delay
  assert_equal(3, n)
end