Module: Jabber::ClientTester

Defined in:
lib/gems/xmpp4r-0.4/test/lib/clienttester.rb

Overview

The ClientTester is a mix-in which provides a setup and teardown method to prepare a Stream object (@client) and the method interfacing as the “server side”:

  • send(xml)

    Send a stanza to @client

The server side is a stream, too: add your callbacks to @server

ClientTester is written to test complex helper classes.

Constant Summary collapse

@@SOCKET_PORT =
65223

Instance Method Summary collapse

Instance Method Details

#send(xml) ⇒ Object



100
101
102
# File 'lib/gems/xmpp4r-0.4/test/lib/clienttester.rb', line 100

def send(xml)
  @server.send(xml)
end

#setupObject



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
# File 'lib/gems/xmpp4r-0.4/test/lib/clienttester.rb', line 31

def setup
  servlisten = TCPServer.new(@@SOCKET_PORT)
  serverwait = Semaphore.new
  stream = '<stream:stream xmlns:stream="http://etherx.jabber.org/streams">'

  Thread.new do
    Thread.current.abort_on_exception = true
    serversock = servlisten.accept
    servlisten.close
    serversock.sync = true
    @server = Stream.new
    @server.add_xml_callback do |xml|
      if xml.prefix == 'stream' and xml.name == 'stream'
        send(stream)
        true
      else
        false
      end
    end
    @server.start(serversock)

    serverwait.run
  end

  clientsock = TCPSocket.new('localhost', @@SOCKET_PORT)
  clientsock.sync = true
  @client = Stream.new
#=begin
  class << @client
    def jid
      begin
        #raise
      rescue
        puts $!.backtrace.join("\n")
      end
      JID.new('[email protected]/test')
    end
  end
#=end
  @client.start(clientsock)
  @client.send(stream) { |reply| true }

  @state = 0
  @states = []
  @state_wait = Semaphore.new
  @state_wait2 = Semaphore.new
  @server.add_stanza_callback { |stanza|
    if @state < @states.size
      begin
        @states[@state].call(stanza)
      rescue
        puts "Exception in state: #{$!.class}: #{$!}\n#{$!.join("\n")}"
      end
      @state += 1
      @state_wait2.wait
      @state_wait.run
    end

    false
  }

  serverwait.wait
end

#skip_stateObject



113
114
115
# File 'lib/gems/xmpp4r-0.4/test/lib/clienttester.rb', line 113

def skip_state
  @state_wait2.run
end

#state(&block) ⇒ Object



104
105
106
# File 'lib/gems/xmpp4r-0.4/test/lib/clienttester.rb', line 104

def state(&block)
  @states << block
end

#teardownObject



95
96
97
98
# File 'lib/gems/xmpp4r-0.4/test/lib/clienttester.rb', line 95

def teardown
  @client.close
  @server.close
end

#wait_stateObject



108
109
110
111
# File 'lib/gems/xmpp4r-0.4/test/lib/clienttester.rb', line 108

def wait_state
  @state_wait2.run
  @state_wait.wait
end