Class: Goliath::TestHelper::SSEHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/goliath/test_helper_sse.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ SSEHelper

Returns a new instance of SSEHelper.



7
8
9
10
11
# File 'lib/goliath/test_helper_sse.rb', line 7

def initialize(url)
  @message_queue = EM::Queue.new
  @named_queues = {}
  @connection = EM::EventSource.new(url)
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



6
7
8
# File 'lib/goliath/test_helper_sse.rb', line 6

def connection
  @connection
end

Instance Method Details

#listenObject



13
14
15
16
17
# File 'lib/goliath/test_helper_sse.rb', line 13

def listen
  @connection.message do |message|
    @message_queue.push(message)
  end
end

#listen_to(name) ⇒ Object



19
20
21
22
23
24
# File 'lib/goliath/test_helper_sse.rb', line 19

def listen_to(name)
  queue = (@named_queues[name] ||= [])
  @connection.on(name) do |message|
    queue.push(message)
  end
end

#receiveObject



26
27
28
# File 'lib/goliath/test_helper_sse.rb', line 26

def receive
  pop_queue(@message_queue)
end

#receive_on(name) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/goliath/test_helper_sse.rb', line 30

def receive_on(name)
  queue = @named_queues.fetch(name) do
    raise ArgumentError, "You have to call listen_to('#{name}') first"
  end

  pop_queue(queue)
end

#with_async_httpObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/goliath/test_helper_sse.rb', line 38

def with_async_http
  klass = EM::HttpConnection
  if klass.instance_methods.include?(:aget)
    begin
      klass.send(:class_eval) do
        alias :sget :get
        alias :get :aget
      end
      yield if block_given?
    ensure
      klass.send(:class_eval) do
        alias :get :sget
        remove_method :sget
      end
    end
  else
    yield if block_given?
  end
end