Class: Jabber::Test::ListenerMocker

Inherits:
Object
  • Object
show all
Defined in:
lib/xmpp4r/test/listener_mocker.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.mock_clientsObject

Returns the value of attribute mock_clients.



27
28
29
# File 'lib/xmpp4r/test/listener_mocker.rb', line 27

def mock_clients
  @mock_clients
end

.tracker_of_callersObject

Returns the value of attribute tracker_of_callers.



27
28
29
# File 'lib/xmpp4r/test/listener_mocker.rb', line 27

def tracker_of_callers
  @tracker_of_callers
end

.with_socket_mocked_callback_procObject

Returns the value of attribute with_socket_mocked_callback_proc.



6
7
8
# File 'lib/xmpp4r/test/listener_mocker.rb', line 6

def with_socket_mocked_callback_proc
  @with_socket_mocked_callback_proc
end

Class Method Details

.mock_out(listener) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/xmpp4r/test/listener_mocker.rb', line 99

def self.mock_out(listener)
  listener.instance_eval do
    class << self
      def setup_connection
        super
        @connection.instance_eval do
          class << self
            self.class_eval(&Jabber::Test::ListenerMocker.mocker_proc)
          end
        end
      end
    end
  end
    
  listener
end

.mock_out_all_connectionsObject



95
96
97
# File 'lib/xmpp4r/test/listener_mocker.rb', line 95

def self.mock_out_all_connections
  Jabber::Reliable::Connection.class_eval(&Jabber::Test::ListenerMocker.mocker_proc)
end

.mocker_procObject



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
# File 'lib/xmpp4r/test/listener_mocker.rb', line 30

def self.mocker_proc
  Proc.new do      
    attr_accessor :messagecbs, :connected
    ListenerMocker.mock_clients ||= {}
    ListenerMocker.tracker_of_callers ||= {}

    def connect
      Jabber::debuglog("(in mock) connected #{@jid.bare}")
      self.connected = true          
    end

    def close!
      ListenerMocker.mock_clients[@jid.bare.to_s] = nil
      ListenerMocker.tracker_of_callers[@jid.bare.to_s] = nil
      self.connected = false
    end

    def auth(password)
      auth_nonsasl(password)
    end

    def auth_nonsasl(password, digest=true)
      Jabber::debuglog("(in mock) authed #{@jid.bare}") 

      if(ListenerMocker.mock_clients[@jid.bare.to_s])
        #raise a stack trace about multiple clients
        raise "\n\n ---> READ ME: this is actualy 2 stack traces: <---- \n\n"+
        "There is already a client connected on that jid #{@jid.bare.to_s}. "+
        "The mock library cannot support multiple listeners connected as the same user! originally called in:\n"+
        ListenerMocker.tracker_of_callers[@jid.bare.to_s].backtrace.join("\n")+"\n\n second trace: \n"
      else
        #store a stack trace so that next time we have multiple client, we can alert about it...
        begin
          throw "just saving a stack trace"
        rescue => e
          ListenerMocker.tracker_of_callers[@jid.bare.to_s] = e
        end
      end

      ListenerMocker.mock_clients[@jid.bare.to_s] = self
      true
    end

    def send(xml, &block)
      Jabber::debuglog("(in mock) sending #{xml} #{xml.class}")
      if(xml.is_a?(Jabber::Message))
        xml.from = @jid
        # unless xml.to
        #   raise "no jid!"
        # end
        if ListenerMocker.mock_clients[xml.to.bare.to_s]
          Jabber::debuglog("(in mock) to #{xml.to.bare.to_s}")
          ListenerMocker.mock_clients[xml.to.bare.to_s].messagecbs.process(xml)
        else
          Jabber::debuglog("(in mock) no client listening as #{xml.to.bare.to_s}")
        end
      end
    end          

    def is_connected?
      self.connected
    end                
  end    
end

.with_socket_mocked(callback_proc) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/xmpp4r/test/listener_mocker.rb', line 9

def self.with_socket_mocked(callback_proc)
  TCPSocket.class_eval{ ListenerMocker.with_socket_mocked_callback_proc = callback_proc }
  TCPSocket.class_eval do
    alias_method :initialize_old, :initialize
    def initialize(*args)
      initialize_old(*args) if ListenerMocker.with_socket_mocked_callback_proc.call(args)
    end
  end
  yield
ensure
  TCPSocket.class_eval do
    if method_defined?(:initialize_old)
      alias_method :initialize, :initialize_old
    end
  end
end