Sha256: 514d819b17392ec0ffcdabc9107c3a3b7d228bc46fb734957e3c89e7fa6169e6

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

require 'hara'

Celluloid.logger = nil

def wait_until wait_time = 3
  timeout wait_time do
    sleep 0.1 until yield
  end
end

class FakeHandshake
  def headers_downcased
    {'host' => 'localhost:8080'}
  end
end

class FakeSocket
  attr_accessor :remote_ip, :close_info, :app

  def initialize
    @client_messages = []
    @server_messages = []
    @mri_peername = "\x02\x00\x00P\x7F\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00"
    @jruby_peername = "\x00\x02\x8Av\x7F\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00"
    @send_message_ids = {}
  end

  def app= app
    @app = app
    @alive = true
  end

  def get_peername
    defined?(JRuby) ? @jruby_peername : @mri_peername
  end

  def alive= alive
    @alive = alive
  end

  def alive?
    @alive
  end

  def close code = nil, body = nil
    if @alive
      @close_info = [code, body]
      @alive = false
      @app.terminate! if @app
    end
  end

  def client_send action, args
    id = SecureRandom.uuid
    @server_messages << Hara.encode_msg(id: id, action: action, args: args)
    @app.process_msg @server_messages.shift
  end

  def send message
    @client_messages << message
  end

  def client_read
    Hara.decode_msg(@client_messages.shift)['args']
  end

  [:onclose, :onmessage, :onopen].each do |method|
    attribute = "#{method}_block".to_sym
    attr_accessor attribute
    define_method method do |&blk|
      __send__ "#{attribute}=", blk
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hara-0.4.0 spec/spec_helper.rb