Module: CocaineRuntimeMock::Server

Defined in:
lib/cocaine/testing/mock_server.rb

Instance Method Summary collapse

Instance Method Details

#initialize(name, responses = nil, hook = nil, options = {}) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/cocaine/testing/mock_server.rb', line 31

def initialize(name, responses=nil, hook=nil, options={})
  $log.debug "new connection for '#{name}'"
  @name = name
  @responses = responses || {}
  @hook = hook || Hook.new
  @options = options
end

#post_initObject



39
40
41
# File 'lib/cocaine/testing/mock_server.rb', line 39

def post_init
  @hook.callbacks[:connected].call
end

#receive_data(data) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cocaine/testing/mock_server.rb', line 43

def receive_data(data)
  unpacker ||= MessagePack::Unpacker.new
  unpacker.feed_each data do |chunk|
    id, session, data = chunk
    $log.debug "received message: [#{id}, #{session}, #{data}]"

    if @hook.callbacks[:message].has_key? chunk
      response = @hook.callbacks[:message][chunk].call
      send_response session, response
    elsif @responses.has_key? chunk
      response = @responses[chunk]
      send_response session, response
    end
  end
end