Commit aa417e95 authored by Wandenberg Peixoto's avatar Wandenberg Peixoto

refactoring and adding some tests

parent 4d97bae4
......@@ -3,13 +3,19 @@ require 'popen4'
require 'tmpdir'
require 'erb'
require 'ftools'
require 'ruby-debug'
require 'test/unit'
require 'em-http'
require 'json'
module BaseTestCase
def setup
default_configuration
@test_config_file = "#{self.method_name}.conf"
config_test_name = "config_#{self.method_name}"
self.send(config_test_name) if self.respond_to?(config_test_name)
if @test_config_file and @test_config_file != ""
unless @disable_start_stop_server
self.create_config_file
self.stop_server
self.start_server
......@@ -17,9 +23,9 @@ module BaseTestCase
end
def teardown
if @test_config_file and @test_config_file != ""
unless @disable_start_stop_server
self.stop_server
self.delete_config_file
#self.delete_config_file
end
end
......@@ -77,16 +83,63 @@ module BaseTestCase
}
end
def default_configuration
@max_reserved_memory = '10m'
@authorized_channels_only = 'off'
@broadcast_channel_max_qtd = 3
@broadcast_channel_prefix = 'broad_'
@content_type = 'text/html; charset=utf-8'
@header_template = %{<html><head><meta http-equiv=\\"Content-Type\\" content=\\"text/html; charset=utf-8\\">\\r\\n<meta http-equiv=\\"Cache-Control\\" content=\\"no-store\\">\\r\\n<meta http-equiv=\\"Cache-Control\\" content=\\"no-cache\\">\\r\\n<meta http-equiv=\\"Expires\\" content=\\"Thu, 1 Jan 1970 00:00:00 GMT\\">\\r\\n<script type=\\"text/javascript\\">\\r\\nwindow.onError = null;\\r\\ndocument.domain = \\'localhost\\';\\r\\nparent.PushStream.register(this);\\r\\n</script>\\r\\n</head>\\r\\n<body onload=\\"try { parent.PushStream.reset(this) } catch (e) {}\\">}
@max_channel_id_length = 200
@max_message_buffer_length = 20
@max_number_of_broadcast_channels = nil
@max_number_of_channels = nil
@message_template = %{<script>p(~id~,\\'~channel~\\',\\'~text~\\');</script>}
@min_message_buffer_timeout = '50m'
@ping_message_interval = '10s'
@store_messages = 'on'
@subscriber_connection_timeout = nil
@subscriber_disconnect_interval = nil
@memory_cleanup_timeout = '5m'
end
def publish_message(channel, headers, body)
EventMachine.run {
pub_1 = EventMachine::HttpRequest.new(nginx_address + '/pub?id=' + channel.to_s).post :head => headers, :body => body, :timeout => 30
pub_1.callback {
assert_equal(200, pub_1.response_header.status, "Request was not accepted")
assert_not_equal(0, pub_1.response_header.content_length, "Empty response was received")
response = JSON.parse(pub_1.response)
assert_equal(channel, response["channel"].to_s, "Channel was not recognized")
EventMachine.stop
}
fail_if_connecttion_error(pub_1)
}
end
def create_channel_by_subscribe(channel, headers, timeout=60, &block)
EventMachine.run {
sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers, :timeout => timeout
sub_1.stream { |chunk|
block.call
}
sub_1.callback {
EventMachine.stop
}
fail_if_connecttion_error(sub_1)
}
end
@@config_template = %q{
pid logs/nginx.pid;
error_log logs/nginx-main_error.log debug;
# Development Mode
master_process off;
daemon on;
daemon off;
worker_processes 1;
events {
#worker_connections 1024;
worker_connections 1024;
use epoll;
}
......@@ -110,12 +163,20 @@ http {
client_body_buffer_size 1k;
ignore_invalid_headers on;
client_body_in_single_buffer on;
push_stream_max_reserved_memory <%= @max_reserved_memory.nil? ? "10m" : @max_reserved_memory %>;
<%= "push_stream_max_reserved_memory #{@max_reserved_memory};" unless @max_reserved_memory.nil? %>
server {
listen 9999;
server_name localhost;
location /channels_stats {
# activate channels statistics mode for this location
push_stream_channels_statistics;
# query string based channel id
set $push_stream_channel_id $arg_id;
}
location /pub {
# activate publisher mode for this location
push_stream_publisher;
......@@ -123,45 +184,59 @@ http {
# query string based channel id
set $push_stream_channel_id $arg_id;
# message template
push_stream_message_template "<%= @message_template.nil? ? '<script>p(~id~,\'~channel~\',\'~text~\');</script>' : @message_template %>";
<%= %{push_stream_message_template "#{@message_template}";} unless @message_template.nil? %>
# store messages
push_stream_store_messages <%= @store_messages.nil? ? 'on' : @store_messages %>;
<%= "push_stream_store_messages #{@store_messages};" unless @store_messages.nil? %>
# max messages to store in memory
push_stream_max_message_buffer_length <%= @max_message_buffer_length.nil? ? 20 : @max_message_buffer_length %>;
<%= "push_stream_max_message_buffer_length #{@max_message_buffer_length};" unless @max_message_buffer_length.nil? %>
# message ttl
push_stream_min_message_buffer_timeout <%= @min_message_buffer_timeout.nil? ? '50m' : @min_message_buffer_timeout %>;
<%= "push_stream_min_message_buffer_timeout #{@min_message_buffer_timeout};" unless @min_message_buffer_timeout.nil? %>
<%= "push_stream_max_channel_id_length #{@max_channel_id_length};" unless @max_channel_id_length.nil? %>
<%= %{push_stream_broadcast_channel_prefix "#{@broadcast_channel_prefix}";} unless @broadcast_channel_prefix.nil? %>
<%= "push_stream_broadcast_channel_max_qtd #{@broadcast_channel_max_qtd};" unless @broadcast_channel_max_qtd.nil? %>
<%= "push_stream_max_number_of_channels #{@max_number_of_channels};" unless @max_number_of_channels.nil? %>
<%= "push_stream_max_number_of_broadcast_channels #{@max_number_of_broadcast_channels};" unless @max_number_of_broadcast_channels.nil? %>
push_stream_max_channel_id_length <%= @max_channel_id_length.nil? ? 10 : @max_channel_id_length %>;
<%= "push_stream_memory_cleanup_timeout #{@memory_cleanup_timeout};" unless @memory_cleanup_timeout.nil? %>
# client_max_body_size MUST be equal to client_body_buffer_size or
# you will be sorry.
client_max_body_size 32k;
client_body_buffer_size 32k;
client_max_body_size <%= @client_max_body_size.nil? ? '32k' : @client_max_body_size %>;
client_body_buffer_size <%= @client_body_buffer_size.nil? ? '32k' : @client_body_buffer_size %>;
}
location ~ /sub/(.*) {
location ~ /sub/(.*)? {
# activate subscriber mode for this location
push_stream_subscriber;
# positional channel path
set $push_stream_channels_path $1;
<%= "push_stream_max_channel_id_length #{@max_channel_id_length};" unless @max_channel_id_length.nil? %>
# header to be sent when receiving new subscriber connection
push_stream_header_template "<%= @header_template.nil? ? '<html><head><meta http-equiv=\\"Content-Type\\" content=\\"text/html; charset=utf-8\\">\\r\\n<meta http-equiv=\\"Cache-Control\\" content=\\"no-store\\">\\r\\n<meta http-equiv=\\"Cache-Control\\" content=\\"no-cache\\">\\r\\n<meta http-equiv=\\"Pragma\\" content=\\"no-cache\\">\\r\\n<meta http-equiv=\\"Expires\\" content=\\"Thu, 1 Jan 1970 00:00:00 GMT\\">\\r\\n<script type=\\"text/javascript\\">\\r\\nwindow.onError = null;\\r\\ndocument.domain = \'localhost\';\\r\\nparent.PushStream.register(this);\\r\\n</script>\\r\\n</head>\\r\\n<body onload=\\"try { parent.PushStream.reset(this) } catch (e) {}\\">' : @header_template %>";
<%= %{push_stream_header_template "#{@header_template}";} unless @header_template.nil? %>
# message template
push_stream_message_template "<%= @message_template.nil? ? '<script>p(~id~,\'~channel~\',\'~text~\');</script>' : @message_template %>";
<%= %{push_stream_message_template "#{@message_template}";} unless @message_template.nil? %>
# content-type
push_stream_content_type "<%= @content_type.nil? ? 'text/html; charset=utf-8' : @content_type %>";
<%= %{push_stream_content_type "#{@content_type}";} unless @content_type.nil? %>
# subscriber may create channels on demand or only authorized
# (publisher) may do it?
push_stream_authorized_channels_only <%= @authorized_channels_only.nil? ? 'off' : @authorized_channels_only %>;
<%= "push_stream_authorized_channels_only #{@authorized_channels_only};" unless @authorized_channels_only.nil? %>
# ping frequency
push_stream_ping_message_interval <%= @ping_message_interval.nil? ? '10s' : @ping_message_interval %>;
<%= "push_stream_ping_message_interval #{@ping_message_interval};" unless @ping_message_interval.nil? %>
# disconnection candidates test frequency
push_stream_subscriber_disconnect_interval <%= @subscriber_disconnect_interval.nil? ? '0s' : @subscriber_disconnect_interval %>;
<%= "push_stream_subscriber_disconnect_interval #{@subscriber_disconnect_interval};" unless @subscriber_disconnect_interval.nil? %>
# connection ttl to enable recycle
push_stream_subscriber_connection_timeout <%= @subscriber_connection_timeout.nil? ? '0s' : @subscriber_connection_timeout %>;
push_stream_broadcast_channel_prefix "<%= @broadcast_channel_prefix.nil? ? 'broad_' : @broadcast_channel_prefix %>";
push_stream_broadcast_channel_max_qtd <%= @broadcast_channel_max_qtd.nil? ? '3' : @broadcast_channel_max_qtd %>;
<%= "push_stream_subscriber_connection_timeout #{@subscriber_connection_timeout};" unless @subscriber_connection_timeout.nil? %>
<%= %{push_stream_broadcast_channel_prefix "#{@broadcast_channel_prefix}";} unless @broadcast_channel_prefix.nil? %>
<%= "push_stream_broadcast_channel_max_qtd #{@broadcast_channel_max_qtd};" unless @broadcast_channel_max_qtd.nil? %>
<%= "push_stream_max_number_of_channels #{@max_number_of_channels};" unless @max_number_of_channels.nil? %>
<%= "push_stream_max_number_of_broadcast_channels #{@max_number_of_broadcast_channels};" unless @max_number_of_broadcast_channels.nil? %>
<%= "push_stream_memory_cleanup_timeout #{@memory_cleanup_timeout};" unless @memory_cleanup_timeout.nil? %>
# solving some leakage problem with persitent connections in
# Nginx's chunked filter (ngx_http_chunked_filter_module.c)
chunked_transfer_encoding off;
......
require 'test/unit'
require File.expand_path('test_broadcast_properties', File.dirname(__FILE__))
require File.expand_path('test_channel_statistics', File.dirname(__FILE__))
require File.expand_path('test_cleanup_memory', File.dirname(__FILE__))
require File.expand_path('test_comunication_properties', File.dirname(__FILE__))
require File.expand_path('test_create_many_channels', File.dirname(__FILE__))
require File.expand_path('test_publish_messages', File.dirname(__FILE__))
require File.expand_path('test_publisher_properties', File.dirname(__FILE__))
#require File.expand_path('test_setup_parameters', File.dirname(__FILE__))
require File.expand_path('test_publisher', File.dirname(__FILE__))
require File.expand_path('test_setup_parameters', File.dirname(__FILE__))
require File.expand_path('test_subscriber_connection_cleanup', File.dirname(__FILE__))
require File.expand_path('test_subscriber_properties', File.dirname(__FILE__))
require File.expand_path('test_subscriber', File.dirname(__FILE__))
......@@ -4,7 +4,6 @@ class TestBroadcastProperties < Test::Unit::TestCase
include BaseTestCase
def config_test_broadcast_channel_prefix
@test_config_file = "test_broadcast_channel_prefix.conf"
@authorized_channels_only = "on"
@header_template = "connected"
@broadcast_channel_prefix = "XXX_"
......@@ -38,7 +37,6 @@ class TestBroadcastProperties < Test::Unit::TestCase
end
def config_test_broadcast_channel_max_qtd
@test_config_file = "test_broadcast_channel_max_qtd.conf"
@authorized_channels_only = "on"
@header_template = "connected"
@broadcast_channel_prefix = "XXX_"
......
require File.expand_path('base_test_case', File.dirname(__FILE__))
class TestChannelStatistics < Test::Unit::TestCase
include BaseTestCase
def test_get_channel_statistics_whithout_created_channel
headers = {'accept' => 'application/json'}
channel = 'ch_test_get_channel_statistics_whithout_created_channel'
EventMachine.run {
pub_1 = EventMachine::HttpRequest.new(nginx_address + '/channels_stats?id=' + channel.to_s).get :head => headers, :timeout => 30
pub_1.callback {
assert_equal(404, pub_1.response_header.status, "Channel was founded")
assert_equal(0, pub_1.response_header.content_length, "Recieved a non empty response")
EventMachine.stop
}
fail_if_connecttion_error(pub_1)
}
end
def test_get_channel_statistics_to_existing_channel
headers = {'accept' => 'application/json'}
channel = 'ch_test_get_channel_statistics_to_existing_channel'
body = 'body'
#create channel
publish_message(channel, headers, body)
EventMachine.run {
pub_2 = EventMachine::HttpRequest.new(nginx_address + '/channels_stats?id=' + channel.to_s).get :head => headers, :timeout => 30
pub_2.callback {
assert_equal(200, pub_2.response_header.status, "Request was not accepted")
assert_not_equal(0, pub_2.response_header.content_length, "Empty response was received")
response = JSON.parse(pub_2.response)
assert_equal(channel, response["channel"].to_s, "Channel was not recognized")
assert_equal(1, response["published_messages"].to_i, "Message was not published")
assert_equal(1, response["stored_messages"].to_i, "Message was not stored")
assert_equal(0, response["subscribers"].to_i, "Wrong number for subscribers")
EventMachine.stop
}
fail_if_connecttion_error(pub_2)
}
end
def test_get_channel_statistics_to_existing_channel_with_subscriber
headers = {'accept' => 'application/json'}
channel = 'ch_test_get_channel_statistics_to_existing_channel_with_subscriber'
body = 'body'
create_channel_by_subscribe(channel, headers) do
pub_1 = EventMachine::HttpRequest.new(nginx_address + '/channels_stats?id=' + channel.to_s).get :head => headers, :timeout => 30
pub_1.callback {
assert_equal(200, pub_1.response_header.status, "Request was not accepted")
assert_not_equal(0, pub_1.response_header.content_length, "Empty response was received")
response = JSON.parse(pub_1.response)
assert_equal(channel, response["channel"].to_s, "Channel was not recognized")
assert_equal(0, response["published_messages"].to_i, "Wrong number for published messages")
assert_equal(0, response["stored_messages"].to_i, "Wrong number for stored messages")
assert_equal(1, response["subscribers"].to_i, "Wrong number for subscribers")
EventMachine.stop
}
fail_if_connecttion_error(pub_1)
end
end
def test_get_detailed_channels_statistics_whithout_created_channels
headers = {'accept' => 'application/json'}
EventMachine.run {
pub_2 = EventMachine::HttpRequest.new(nginx_address + '/channels_stats?id=ALL').get :head => headers, :timeout => 30
pub_2.callback {
assert_equal(200, pub_2.response_header.status, "Request was not accepted")
assert_not_equal(0, pub_2.response_header.content_length, "Empty response was received")
response = JSON.parse(pub_2.response)
assert_equal(0, response["infos"].length, "Received info whithout_created_channels")
EventMachine.stop
}
fail_if_connecttion_error(pub_2)
}
end
def test_get_detailed_channels_statistics_to_existing_channel
headers = {'accept' => 'application/json'}
channel = 'ch_test_get_detailed_channels_statistics_to_existing_channel'
body = 'body'
#create channel
publish_message(channel, headers, body)
EventMachine.run {
pub_2 = EventMachine::HttpRequest.new(nginx_address + '/channels_stats?id=ALL').get :head => headers, :timeout => 30
pub_2.callback {
assert_equal(200, pub_2.response_header.status, "Request was not accepted")
assert_not_equal(0, pub_2.response_header.content_length, "Empty response was received")
response = JSON.parse(pub_2.response)
assert_equal(1, response["infos"].length, "Didn't received info about the only created channel")
assert_equal(channel, response["infos"][0]["channel"].to_s, "Channel was not recognized")
assert_equal(1, response["infos"][0]["published_messages"].to_i, "Message was not published")
assert_equal(1, response["infos"][0]["stored_messages"].to_i, "Message was not stored")
assert_equal(0, response["infos"][0]["subscribers"].to_i, "Wrong number for subscribers")
EventMachine.stop
}
fail_if_connecttion_error(pub_2)
}
end
def config_test_get_detailed_channels_statistics_to_existing_broadcast_channel
@broadcast_channel_prefix = 'bd_'
@broadcast_channel_max_qtd = 1
end
def test_get_detailed_channels_statistics_to_existing_broadcast_channel
headers = {'accept' => 'application/json'}
channel = 'bd_test_get_detailed_channels_statistics_to_existing_broadcast_channel'
body = 'body'
#create channel
publish_message(channel, headers, body)
EventMachine.run {
pub_2 = EventMachine::HttpRequest.new(nginx_address + '/channels_stats?id=ALL').get :head => headers, :timeout => 30
pub_2.callback {
assert_equal(200, pub_2.response_header.status, "Request was not accepted")
assert_not_equal(0, pub_2.response_header.content_length, "Empty response was received")
response = JSON.parse(pub_2.response)
assert_equal(1, response["infos"].length, "Didn't received info about the only created channel")
assert_equal(0, response["channels"].to_i, "Channel was not recognized")
assert_equal(1, response["broadcast_channels"].to_i, "Channel was not recognized")
assert_equal(channel, response["infos"][0]["channel"].to_s, "Channel was not recognized")
assert_equal(1, response["infos"][0]["published_messages"].to_i, "Message was not published")
assert_equal(1, response["infos"][0]["stored_messages"].to_i, "Message was not stored")
assert_equal(0, response["infos"][0]["subscribers"].to_i, "Wrong number for subscribers")
EventMachine.stop
}
fail_if_connecttion_error(pub_2)
}
end
def test_detailed_channels_statistics_to_existing_channel_with_subscriber
headers = {'accept' => 'application/json'}
channel = 'ch_test_detailed_channels_statistics_to_existing_channel_with_subscriber'
body = 'body'
create_channel_by_subscribe(channel, headers) do
pub_1 = EventMachine::HttpRequest.new(nginx_address + '/channels_stats?id=ALL').get :head => headers, :timeout => 30
pub_1.callback {
assert_equal(200, pub_1.response_header.status, "Request was not accepted")
assert_not_equal(0, pub_1.response_header.content_length, "Empty response was received")
response = JSON.parse(pub_1.response)
assert_equal(1, response["infos"].length, "Didn't received info about the only created channel")
assert_equal(channel, response["infos"][0]["channel"].to_s, "Channel was not recognized")
assert_equal(0, response["infos"][0]["published_messages"].to_i, "Wrong number for published messages")
assert_equal(0, response["infos"][0]["stored_messages"].to_i, "Wrong number for stored messages")
assert_equal(1, response["infos"][0]["subscribers"].to_i, "Wrong number for subscribers")
EventMachine.stop
}
fail_if_connecttion_error(pub_1)
end
end
def test_get_summarized_channels_statistics_whithout_created_channels
headers = {'accept' => 'application/json'}
EventMachine.run {
pub_1 = EventMachine::HttpRequest.new(nginx_address + '/channels_stats').get :head => headers, :timeout => 30
pub_1.callback {
assert_equal(200, pub_1.response_header.status, "Don't get channels statistics")
assert_not_equal(0, pub_1.response_header.content_length, "Don't received channels statistics")
begin
response = JSON.parse(pub_1.response)
assert(response.has_key?("channels"), "Didn't received the correct answer with channels info")
assert_equal(0, response["channels"].to_i, "Returned values with channels created")
rescue JSON::ParserError
fail("Didn't receive a valid response")
end
EventMachine.stop
}
fail_if_connecttion_error(pub_1)
}
end
def test_get_summarized_channels_statistics_to_existing_channel
headers = {'accept' => 'application/json'}
channel = 'ch_test_get_summarized_channels_statistics_to_existing_channel'
body = 'body'
#create channel
publish_message(channel, headers, body)
EventMachine.run {
pub_2 = EventMachine::HttpRequest.new(nginx_address + '/channels_stats').get :head => headers, :timeout => 30
pub_2.callback {
assert_equal(200, pub_2.response_header.status, "Don't get channels statistics")
assert_not_equal(0, pub_2.response_header.content_length, "Don't received channels statistics")
begin
response = JSON.parse(pub_2.response)
assert(response.has_key?("channels"), "Didn't received the correct answer with channels info")
assert_equal(1, response["channels"].to_i, "Don't returned values with created channel")
assert_equal(1, response["published_messages"].to_i, "Message was not published")
assert_equal(0, response["subscribers"].to_i, "Wrong number for subscribers")
rescue JSON::ParserError
fail("Didn't receive a valid response")
end
EventMachine.stop
}
fail_if_connecttion_error(pub_2)
}
end
def config_test_get_summarized_channels_statistics_to_existing_broadcast_channel
@broadcast_channel_prefix = 'bd_'
@broadcast_channel_max_qtd = 1
end
def test_get_summarized_channels_statistics_to_existing_broadcast_channel
headers = {'accept' => 'application/json'}
channel = 'bd_test_get_summarized_channels_statistics_to_existing_broadcast_channel'
body = 'body'
#create channel
publish_message(channel, headers, body)
EventMachine.run {
pub_2 = EventMachine::HttpRequest.new(nginx_address + '/channels_stats').get :head => headers, :timeout => 30
pub_2.callback {
assert_equal(200, pub_2.response_header.status, "Don't get channels statistics")
assert_not_equal(0, pub_2.response_header.content_length, "Don't received channels statistics")
begin
response = JSON.parse(pub_2.response)
assert(response.has_key?("channels"), "Didn't received the correct answer with channels info")
assert_equal(0, response["channels"].to_i, "Don't returned values with created channel")
assert_equal(1, response["broadcast_channels"].to_i, "Don't returned values with created channel")
assert_equal(1, response["published_messages"].to_i, "Message was not published")
assert_equal(0, response["subscribers"].to_i, "Wrong number for subscribers")
rescue JSON::ParserError
fail("Didn't receive a valid response")
end
EventMachine.stop
}
fail_if_connecttion_error(pub_2)
}
end
def test_summarized_channels_statistics_to_existing_channel_with_subscriber
headers = {'accept' => 'application/json'}
channel = 'ch_test_summarized_channels_statistics_to_existing_channel_with_subscriber'
body = 'body'
create_channel_by_subscribe(channel, headers) do
pub_1 = EventMachine::HttpRequest.new(nginx_address + '/channels_stats').get :head => headers, :timeout => 30
pub_1.callback {
assert_equal(200, pub_1.response_header.status, "Request was not accepted")
assert_not_equal(0, pub_1.response_header.content_length, "Empty response was received")
response = JSON.parse(pub_1.response)
assert(response.has_key?("channels"), "Didn't received the correct answer with channels info")
assert_equal(1, response["channels"].to_i, "Don't returned values with created channel")
assert_equal(0, response["published_messages"].to_i, "Wrong number for published messages")
assert_equal(1, response["subscribers"].to_i, "Wrong number for subscribers")
EventMachine.stop
}
fail_if_connecttion_error(pub_1)
end
end
def test_accepted_methods_channel_statistics
EventMachine.run {
multi = EventMachine::MultiRequest.new
multi.add(EventMachine::HttpRequest.new(nginx_address + '/channels_stats').get)
multi.add(EventMachine::HttpRequest.new(nginx_address + '/channels_stats').put :body => 'body')
multi.add(EventMachine::HttpRequest.new(nginx_address + '/channels_stats').post)
multi.add(EventMachine::HttpRequest.new(nginx_address + '/channels_stats').delete)
multi.add(EventMachine::HttpRequest.new(nginx_address + '/channels_stats').head)
multi.callback {
assert_equal(5, multi.responses[:succeeded].length)
assert_not_equal(405, multi.responses[:succeeded][0].response_header.status, "Statistics does accept GET")
assert_equal("GET", multi.responses[:succeeded][0].method, "Array is with wrong order")
assert_equal(405, multi.responses[:succeeded][1].response_header.status, "Statistics does not accept PUT")
assert_equal("PUT", multi.responses[:succeeded][1].method, "Array is with wrong order")
assert_equal(405, multi.responses[:succeeded][2].response_header.status, "Statistics does not accept POST")
assert_equal("POST", multi.responses[:succeeded][2].method, "Array is with wrong order")
assert_equal(405, multi.responses[:succeeded][3].response_header.status, "Statistics does not accept DELETE")
assert_equal("DELETE", multi.responses[:succeeded][3].method, "Array is with wrong order")
assert_equal(405, multi.responses[:succeeded][4].response_header.status, "Statistics does not accept HEAD")
assert_equal("HEAD", multi.responses[:succeeded][4].method, "Array is with wrong order")
EventMachine.stop
}
fail_if_connecttion_error(multi)
}
end
def test_accepted_content_types
headers = {'accept' => 'application/json'}
channel = 'ch_test_accepted_content_types'
body = 'body'
#create channel
publish_message(channel, headers, body)
EventMachine.run {
multi = EventMachine::MultiRequest.new
multi.add(EventMachine::HttpRequest.new(nginx_address + '/channels_stats').get) # default content_type
multi.add(EventMachine::HttpRequest.new(nginx_address + '/channels_stats').get :head => {'accept' => 'text/plain'})
multi.add(EventMachine::HttpRequest.new(nginx_address + '/channels_stats').get :head => {'accept' => 'application/json'})
multi.add(EventMachine::HttpRequest.new(nginx_address + '/channels_stats').get :head => {'accept' => 'application/yaml'})
multi.add(EventMachine::HttpRequest.new(nginx_address + '/channels_stats').get :head => {'accept' => 'application/xml'})
multi.add(EventMachine::HttpRequest.new(nginx_address + '/channels_stats').get :head => {'accept' => 'text/x-json'})
multi.add(EventMachine::HttpRequest.new(nginx_address + '/channels_stats').get :head => {'accept' => 'text/x-yaml'})
multi.callback {
assert_equal(7, multi.responses[:succeeded].length)
i = 0
assert_equal(200, multi.responses[:succeeded][i].response_header.status, "Statistics does accept GET")
assert_equal("GET", multi.responses[:succeeded][i].method, "Array is with wrong order")
assert_equal("application/json", multi.responses[:succeeded][i].response_header["CONTENT_TYPE"], "wrong content-type")
i+=1
assert_equal(200, multi.responses[:succeeded][i].response_header.status, "Statistics does accept GET")
assert_equal("GET", multi.responses[:succeeded][i].method, "Array is with wrong order")
assert_equal("text/plain", multi.responses[:succeeded][i].response_header["CONTENT_TYPE"], "wrong content-type")
i+=1
assert_equal(200, multi.responses[:succeeded][i].response_header.status, "Statistics does accept GET")
assert_equal("GET", multi.responses[:succeeded][i].method, "Array is with wrong order")
assert_equal("application/json", multi.responses[:succeeded][i].response_header["CONTENT_TYPE"], "wrong content-type")
i+=1
assert_equal(200, multi.responses[:succeeded][i].response_header.status, "Statistics does accept GET")
assert_equal("GET", multi.responses[:succeeded][i].method, "Array is with wrong order")
assert_equal("application/yaml", multi.responses[:succeeded][i].response_header["CONTENT_TYPE"], "wrong content-type")
i+=1
assert_equal(200, multi.responses[:succeeded][i].response_header.status, "Statistics does accept GET")
assert_equal("GET", multi.responses[:succeeded][i].method, "Array is with wrong order")
assert_equal("application/xml", multi.responses[:succeeded][i].response_header["CONTENT_TYPE"], "wrong content-type")
i+=1
assert_equal(200, multi.responses[:succeeded][i].response_header.status, "Statistics does accept GET")
assert_equal("GET", multi.responses[:succeeded][i].method, "Array is with wrong order")
assert_equal("text/x-json", multi.responses[:succeeded][i].response_header["CONTENT_TYPE"], "wrong content-type")
i+=1
assert_equal(200, multi.responses[:succeeded][i].response_header.status, "Statistics does accept GET")
assert_equal("GET", multi.responses[:succeeded][i].method, "Array is with wrong order")
assert_equal("text/x-yaml", multi.responses[:succeeded][i].response_header["CONTENT_TYPE"], "wrong content-type")
EventMachine.stop
}
fail_if_connecttion_error(multi)
}
end
end
require File.expand_path('base_test_case', File.dirname(__FILE__))
class TestCreateManyChannels < Test::Unit::TestCase
include BaseTestCase
def config_test_message_cleanup
@min_message_buffer_timeout = '10s'
@max_reserved_memory = "32k"
@max_message_buffer_length = 100
@memory_cleanup_timeout = '30s'
end
def test_message_cleanup
channel = 'ch_test_message_cleanup'
headers = {'accept' => 'text/html'}
body = 'message to create a channel'
EventMachine.run {
# ensure space for a subscriber after memory was full
sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers, :timeout => 60
fail_if_connecttion_error(sub_1)
i = 0
EM.add_periodic_timer(0.05) do
i += 1
pub_1 = EventMachine::HttpRequest.new(nginx_address + '/pub?id=' + channel.to_s).post :head => headers, :body => body, :timeout => 60
pub_1.callback {
if pub_1.response_header.status == 500
EventMachine.stop
end
}
fail_if_connecttion_error(pub_1)
end
}
EventMachine.run {
# ensure channel will not be cleaned up
sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers, :timeout => 60
fail_if_connecttion_error(sub_1)
stored_messages_setp_1 = 0
stored_messages_setp_2 = 0
pub_2 = EventMachine::HttpRequest.new(nginx_address + '/channels_stats?id=' + channel.to_s).get :head => headers, :timeout => 60
pub_2.callback {
assert_equal(200, pub_2.response_header.status, "Don't get channels statistics")
assert_not_equal(0, pub_2.response_header.content_length, "Don't received channels statistics")
stored_messages_setp_1 = JSON.parse(pub_2.response)["stored_messages"].to_i
sleep(40) #wait for message timeout and for cleanup timer
pub_3 = EventMachine::HttpRequest.new(nginx_address + '/channels_stats?id=' + channel.to_s).get :head => headers, :timeout => 60
pub_3.callback {
assert_equal(200, pub_3.response_header.status, "Don't get channels statistics")
assert_not_equal(0, pub_3.response_header.content_length, "Don't received channels statistics")
stored_messages_setp_2 = JSON.parse(pub_3.response)["stored_messages"].to_i
assert(stored_messages_setp_1 > stored_messages_setp_2, "Messages weren't clean up: #{stored_messages_setp_1} <= #{stored_messages_setp_2}")
pub_4 = EventMachine::HttpRequest.new(nginx_address + '/pub?id=' + channel.to_s).post :head => headers, :body => body, :timeout => 60
pub_4.callback {
assert_equal(200, pub_4.response_header.status, "Don't get channels statistics")
assert_equal(stored_messages_setp_2 + 1, JSON.parse(pub_4.response)["stored_messages"].to_i, "Don't get channels statistics")
EventMachine.stop
}
fail_if_connecttion_error(pub_4)
}
fail_if_connecttion_error(pub_3)
}
fail_if_connecttion_error(pub_2)
}
end
def config_test_channel_cleanup
@min_message_buffer_timeout = '10s'
@max_reserved_memory = "32k"
@memory_cleanup_timeout = '30s'
end
def test_channel_cleanup
channel = 'ch_test_channel_cleanup_'
headers = {'accept' => 'text/html'}
body = 'message to create a channel'
i = 0
EventMachine.run {
EM.add_periodic_timer(0.05) do
i += 1
pub_1 = EventMachine::HttpRequest.new(nginx_address + '/pub?id=' + channel.to_s + i.to_s).post :head => headers, :body => body, :timeout => 60
pub_1.callback {
if pub_1.response_header.status == 500
EventMachine.stop
end
}
fail_if_connecttion_error(pub_1)
end
}
EventMachine.run {
channels_setp_1 = 0
channels_setp_2 = 0
pub_2 = EventMachine::HttpRequest.new(nginx_address + '/channels_stats').get :head => headers, :timeout => 60
pub_2.callback {
assert_equal(200, pub_2.response_header.status, "Don't get channels statistics")
assert_not_equal(0, pub_2.response_header.content_length, "Don't received channels statistics")
channels_setp_1 = JSON.parse(pub_2.response)["channels"].to_i
assert_equal(i, channels_setp_1, "Channels were not here anymore")
sleep(45) #wait for message timeout and for cleanup timer
pub_3 = EventMachine::HttpRequest.new(nginx_address + '/channels_stats').get :head => headers, :timeout => 60
pub_3.callback {
assert_equal(200, pub_3.response_header.status, "Don't get channels statistics")
assert_not_equal(0, pub_3.response_header.content_length, "Don't received channels statistics")
channels_setp_2 = JSON.parse(pub_3.response)["channels"].to_i
assert(channels_setp_1 > channels_setp_2, "Channels weren't clean up: #{channels_setp_1} <= #{channels_setp_2}")
pub_4 = EventMachine::HttpRequest.new(nginx_address + '/pub?id=' + channel.to_s + (i + 1).to_s).post :head => headers, :body => body, :timeout => 60
pub_4.callback {
assert_equal(200, pub_4.response_header.status, "Don't get channels statistics")
pub_5 = EventMachine::HttpRequest.new(nginx_address + '/channels_stats').get :head => headers, :timeout => 60
pub_5.callback {
assert_equal(200, pub_5.response_header.status, "Don't get channels statistics")
assert_not_equal(0, pub_5.response_header.content_length, "Don't received channels statistics")
assert_equal(channels_setp_2 + 1, JSON.parse(pub_5.response)["channels"].to_i, "Don't get channels statistics")
EventMachine.stop
}
fail_if_connecttion_error(pub_5)
}
fail_if_connecttion_error(pub_4)
}
fail_if_connecttion_error(pub_3)
}
fail_if_connecttion_error(pub_2)
}
end
end
require 'rubygems'
require 'em-http'
require 'test/unit'
require File.expand_path('base_test_case', File.dirname(__FILE__))
class TestComunicationProperties < Test::Unit::TestCase
include BaseTestCase
def config_test_all_authorized
@test_config_file = "test_all_authorized.conf"
@authorized_channels_only = "off"
@header_template = "connected"
end
def test_all_authorized
channel = 'ch1'
channel = 'ch_test_all_authorized'
headers = {'accept' => 'text/html'}
EventMachine.run {
......@@ -27,13 +23,12 @@ class TestComunicationProperties < Test::Unit::TestCase
end
def config_test_only_authorized
@test_config_file = "test_only_authorized.conf"
@authorized_channels_only = "on"
@header_template = "connected"
end
def test_only_authorized
channel = 'ch2'
channel = 'ch_test_only_authorized'
headers = {'accept' => 'text/html'}
body = 'message to create a channel'
......@@ -41,6 +36,8 @@ class TestComunicationProperties < Test::Unit::TestCase
sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers, :timeout => 60
sub_1.callback { |chunk|
assert_equal(403, sub_1.response_header.status, "Subscriber was not forbidden")
assert_equal(0, sub_1.response_header.content_length, "Should response only with headers")
assert_equal("Subscriber could not create channels.", sub_1.response_header['X_NGINX_PUSHSTREAM_EXPLAIN'], "Didn't receive the right error message")
pub = EventMachine::HttpRequest.new(nginx_address + '/pub?id=' + channel.to_s ).post :head => headers, :body => body, :timeout => 30
pub.callback {
......@@ -58,7 +55,6 @@ class TestComunicationProperties < Test::Unit::TestCase
end
def config_test_message_buffer_timeout
@test_config_file = "test_message_buffer_timeout.conf"
@authorized_channels_only = "off"
@header_template = "connected"
@message_template = "~text~"
......@@ -66,7 +62,7 @@ class TestComunicationProperties < Test::Unit::TestCase
end
def test_message_buffer_timeout
channel = 'ch3'
channel = 'ch_test_message_buffer_timeout'
headers = {'accept' => 'text/html'}
body = 'message to test buffer timeout '
response_1 = response_2 = response_3 = ""
......@@ -112,7 +108,6 @@ class TestComunicationProperties < Test::Unit::TestCase
end
def config_test_message_template
@test_config_file = "test_message_template.conf"
@authorized_channels_only = "off"
@header_template = "header"
@message_template = '{\"duplicated\":\"~channel~\", \"channel\":\"~channel~\", \"message\":\"~text~\", \"message_id\":\"~id~\"}'
......@@ -120,25 +115,25 @@ class TestComunicationProperties < Test::Unit::TestCase
end
def test_message_template
channel = 'ch4'
channel = 'ch_test_message_template'
headers = {'accept' => 'text/html'}
body = 'message to create a channel'
publish_message(channel, headers, body)
response = ""
EventMachine.run {
chunksReceived = 0
sub = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s + '.b1').get :head => headers, :timeout => 60
sub.stream { |chunk|
chunksReceived += 1
if chunksReceived == 1
assert_equal("#{@header_template}\r\n", chunk, "Didn't received header template")
pub = EventMachine::HttpRequest.new(nginx_address + '/pub?id=' + channel.to_s ).post :head => headers, :body => body, :timeout => 30
fail_if_connecttion_error(pub)
end
if chunksReceived == 2
assert_equal("{\"duplicated\":\"ch4\", \"channel\":\"#{channel}\", \"message\":\"#{body}\", \"message_id\":\"1\"}\r\n", chunk, "Didn't received message formatted: #{chunk}")
end
if chunksReceived == 3
assert_equal("{\"duplicated\":\"\", \"channel\":\"\", \"message\":\"\", \"message_id\":\"-1\"}\r\n", chunk, "Didn't received ping message: #{chunk}")
response += chunk
lines = response.split("\r\n")
if lines.length >= 3
assert_equal("#{@header_template}", lines[0], "Didn't received header template")
assert_equal("{\"duplicated\":\"#{channel}\", \"channel\":\"#{channel}\", \"message\":\"#{body}\", \"message_id\":\"1\"}", lines[1], "Didn't received message formatted: #{lines[1]}")
assert_equal("{\"duplicated\":\"\", \"channel\":\"\", \"message\":\"\", \"message_id\":\"-1\"}", lines[2], "Didn't received ping message: #{lines[2]}")
EventMachine.stop
end
}
......
require 'rubygems'
require 'popen4'
require 'em-http'
require 'test/unit'
require File.expand_path('base_test_case', File.dirname(__FILE__))
class TestCreateManyChannels < Test::Unit::TestCase
include BaseTestCase
def initialize(opts)
super(opts)
@test_config_file = "test_create_many_channels.conf"
def config_test_create_many_channels
@max_reserved_memory = "256m"
end
......@@ -24,8 +18,11 @@ class TestCreateManyChannels < Test::Unit::TestCase
EM.add_periodic_timer(0.05) do
i += 1
if i <= channels_to_be_created
pub = EventMachine::HttpRequest.new(nginx_address + '/pub?id=ch' + i.to_s ).post :head => headers, :body => body, :timeout => 30
pub = EventMachine::HttpRequest.new(nginx_address + '/pub?id=ch_test_create_many_channels_' + i.to_s ).post :head => headers, :body => body, :timeout => 30
pub.callback {
if pub.response_header.status != 200
assert_equal(200, pub.response_header.status, "Channel was not created: ch_test_create_many_channels_" + i.to_s)
end
channels_callback += 1
}
fail_if_connecttion_error(pub)
......
require 'rubygems'
require 'popen4'
require 'em-http'
require 'test/unit'
require File.expand_path('base_test_case', File.dirname(__FILE__))
class TestPublishMessages < Test::Unit::TestCase
include BaseTestCase
def initialize(opts)
super(opts)
@test_config_file = "test_publish_messages.conf"
@header_template = ""
def config_test_publish_messages
@header_template = nil
@message_template = "~text~"
end
def test_publish_messages
headers = {'accept' => 'text/html'}
body = 'published unique message'
channel = 'ch1'
channel = 'ch_test_publish_messages'
EventMachine.run {
sub = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers
......@@ -32,29 +26,30 @@ class TestPublishMessages < Test::Unit::TestCase
}
end
def config_test_publish_many_messages_in_the_same_channel
@header_template = nil
@message_template = "~text~"
@max_reserved_memory = "256m"
end
def test_publish_many_messages_in_the_same_channel
headers = {'accept' => 'text/html'}
body_prefix = 'published message '
channel = 'ch2'
channel = 'ch_test_publish_many_messages_in_the_same_channel'
messagens_to_publish = 400
recieved_messages = 0
response = ""
EventMachine.run {
sub = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers
sub.stream { | chunk |
chunk.each {|s|
if s.chomp and s.chomp != ""
recieved_messages +=1
end
}
response += chunk
recieved_messages = response.split("\r\n").length
if chunk.include?(body_prefix + messagens_to_publish.to_s + "\r\n")
if recieved_messages >= messagens_to_publish
EventMachine.stop
end
}
sub.callback {
assert_equal(messagens_to_publish, recieved_messages, "The published messages was not received correctly")
}
fail_if_connecttion_error(sub)
i = 0
......@@ -62,6 +57,11 @@ class TestPublishMessages < Test::Unit::TestCase
i += 1
if i <= messagens_to_publish
pub = EventMachine::HttpRequest.new(nginx_address + '/pub?id=' + channel.to_s ).post :head => headers, :body => body_prefix + i.to_s, :timeout => 30
pub.callback {
if pub.response_header.status != 200
assert_equal(200, pub.response_header.status, "Massage was not published: " + body_prefix + i.to_s)
end
}
fail_if_connecttion_error(pub)
end
end
......
require File.expand_path('base_test_case', File.dirname(__FILE__))
class TestPublisher < Test::Unit::TestCase
include BaseTestCase
def test_access_whithout_channel_id
headers = {'accept' => 'application/json'}
EventMachine.run {
pub = EventMachine::HttpRequest.new(nginx_address + '/pub?id=').get :head => headers, :timeout => 30
pub.callback {
assert_equal(0, pub.response_header.content_length, "Should response only with headers")
assert_equal(400, pub.response_header.status, "Request was not understood as a bad request")
assert_equal("No channel id provided.", pub.response_header['X_NGINX_PUSHSTREAM_EXPLAIN'], "Didn't receive the right error message")
EventMachine.stop
}
fail_if_connecttion_error(pub)
}
end
def test_access_whith_channel_id_to_absent_channel
headers = {'accept' => 'application/json'}
channel_1 = 'ch_test_access_whith_channel_id_to_absent_channel_1'
channel_2 = 'ch_test_access_whith_channel_id_to_absent_channel_2'
body = 'body'
EventMachine.run {
pub_1 = EventMachine::HttpRequest.new(nginx_address + '/pub?id=' + channel_1.to_s).get :head => headers, :timeout => 30
pub_1.callback {
assert_equal(404, pub_1.response_header.status, "Channel was founded")
assert_equal(0, pub_1.response_header.content_length, "Recieved a non empty response")
EventMachine.stop
}
fail_if_connecttion_error(pub_1)
}
EventMachine.run {
pub_2 = EventMachine::HttpRequest.new(nginx_address + '/pub?id=' + channel_2.to_s ).post :head => headers, :body => body, :timeout => 30
pub_2.callback {
assert_equal(200, pub_2.response_header.status, "Request was not accepted")
assert_not_equal(0, pub_2.response_header.content_length, "Empty response was received")
response = JSON.parse(pub_2.response)
assert_equal(channel_2, response["channel"].to_s, "Channel was not recognized")
EventMachine.stop
}
fail_if_connecttion_error(pub_2)
}
end
def test_access_whith_channel_id_to_existing_channel
headers = {'accept' => 'application/json'}
channel = 'ch_test_access_whith_channel_id_to_existing_channel'
body = 'body'
#create channel
EventMachine.run {
pub_1 = EventMachine::HttpRequest.new(nginx_address + '/pub?id=' + channel.to_s).post :head => headers, :body => body, :timeout => 30
pub_1.callback {
assert_equal(200, pub_1.response_header.status, "Request was not accepted")
assert_not_equal(0, pub_1.response_header.content_length, "Empty response was received")
response = JSON.parse(pub_1.response)
assert_equal(channel, response["channel"].to_s, "Channel was not recognized")
EventMachine.stop
}
fail_if_connecttion_error(pub_1)
}
EventMachine.run {
pub_2 = EventMachine::HttpRequest.new(nginx_address + '/pub?id=' + channel.to_s).get :head => headers, :timeout => 30
pub_2.callback {
assert_equal(200, pub_2.response_header.status, "Request was not accepted")
assert_not_equal(0, pub_2.response_header.content_length, "Empty response was received")
response = JSON.parse(pub_2.response)
assert_equal(channel, response["channel"].to_s, "Channel was not recognized")
EventMachine.stop
}
fail_if_connecttion_error(pub_2)
}
end
def test_accepted_methods
EventMachine.run {
multi = EventMachine::MultiRequest.new
multi.add(EventMachine::HttpRequest.new(nginx_address + '/pub?id=ch_test_accepted_methods_1').get)
multi.add(EventMachine::HttpRequest.new(nginx_address + '/pub?id=ch_test_accepted_methods_2').put :body => 'body')
multi.add(EventMachine::HttpRequest.new(nginx_address + '/pub?id=ch_test_accepted_methods_3').post)
multi.add(EventMachine::HttpRequest.new(nginx_address + '/pub?id=ch_test_accepted_methods_4').delete)
multi.add(EventMachine::HttpRequest.new(nginx_address + '/pub?id=ch_test_accepted_methods_5').head)
multi.callback {
assert_equal(5, multi.responses[:succeeded].length)
assert_not_equal(405, multi.responses[:succeeded][0].response_header.status, "Publisher does accept GET")
assert_equal("GET", multi.responses[:succeeded][0].method, "Array is with wrong order")
assert_equal(405, multi.responses[:succeeded][1].response_header.status, "Publisher does not accept PUT")
assert_equal("GET, POST", multi.responses[:succeeded][1].response_header['ALLOW'], "Didn't receive the right error message")
assert_equal("PUT", multi.responses[:succeeded][1].method, "Array is with wrong order")
assert_not_equal(405, multi.responses[:succeeded][2].response_header.status, "Publisher does accept POST")
assert_equal("POST", multi.responses[:succeeded][2].method, "Array is with wrong order")
assert_equal(405, multi.responses[:succeeded][3].response_header.status, "Publisher does not accept DELETE")
assert_equal("DELETE", multi.responses[:succeeded][3].method, "Array is with wrong order")
assert_equal("GET, POST", multi.responses[:succeeded][3].response_header['ALLOW'], "Didn't receive the right error message")
assert_equal(405, multi.responses[:succeeded][4].response_header.status, "Publisher does not accept HEAD")
assert_equal("HEAD", multi.responses[:succeeded][4].method, "Array is with wrong order")
assert_equal("GET, POST", multi.responses[:succeeded][4].response_header['ALLOW'], "Didn't receive the right error message")
EventMachine.stop
}
fail_if_connecttion_error(multi)
}
end
def test_cannot_create_a_channel_with_id_ALL
headers = {'accept' => 'application/json'}
channel = 'ALL'
body = 'body'
EventMachine.run {
pub_1 = EventMachine::HttpRequest.new(nginx_address + '/pub?id=' + channel.to_s).post :head => headers, :body => body, :timeout => 30
pub_1.callback {
assert_equal(403, pub_1.response_header.status, "Channel was created")
assert_equal(0, pub_1.response_header.content_length, "Received response for creating channel with id ALL")
assert_equal("Channel id not authorized for this method.", pub_1.response_header['X_NGINX_PUSHSTREAM_EXPLAIN'], "Didn't receive the right error message")
EventMachine.stop
}
fail_if_connecttion_error(pub_1)
}
end
def config_test_post_message_larger_than_max_body_size_should_be_rejected
@client_max_body_size = '2k'
@client_body_buffer_size = '1k'
end
def test_post_message_larger_than_max_body_size_should_be_rejected
headers = {'accept' => 'application/json'}
channel = 'ch_test_post_message_larger_than_max_body_size_should_be_rejected'
body = '^'
(1..40).each do |n|
body += '0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789|'
end
body += '$'
EventMachine.run {
pub_1 = EventMachine::HttpRequest.new(nginx_address + '/pub?id=' + channel.to_s).post :head => headers, :body => body, :timeout => 30
pub_1.callback {
assert_equal(413, pub_1.response_header.status, "Request was accepted")
EventMachine.stop
}
fail_if_connecttion_error(pub_1)
}
end
def config_test_post_message_larger_than_body_buffer_size_should_be_accepted
@client_max_body_size = '10k'
@client_body_buffer_size = '1k'
end
def test_post_message_larger_than_body_buffer_size_should_be_accepted
headers = {'accept' => 'application/json'}
channel = 'ch_test_post_message_larger_than_body_buffer_size_should_be_accepted'
body = '^'
(1..80).each do |n|
body += '0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789|'
end
body += '$'
EventMachine.run {
pub_1 = EventMachine::HttpRequest.new(nginx_address + '/pub?id=' + channel.to_s).post :head => headers, :body => body, :timeout => 30
pub_1.callback {
assert_equal(200, pub_1.response_header.status, "Request was not accepted")
EventMachine.stop
}
fail_if_connecttion_error(pub_1)
}
end
def config_test_post_message_shorter_than_body_buffer_size_should_be_accepted
@client_max_body_size = '10k'
@client_body_buffer_size = '6k'
end
def test_post_message_shorter_than_body_buffer_size_should_be_accepted
headers = {'accept' => 'application/json'}
channel = 'ch_test_post_message_shorter_than_body_buffer_size_should_be_accepted'
body = '^'
(1..40).each do |n|
body += '0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789|'
end
body += '$'
EventMachine.run {
pub_1 = EventMachine::HttpRequest.new(nginx_address + '/pub?id=' + channel.to_s).post :head => headers, :body => body, :timeout => 30
pub_1.callback {
assert_equal(200, pub_1.response_header.status, "Request was not accepted")
EventMachine.stop
}
fail_if_connecttion_error(pub_1)
}
end
def config_test_stored_messages
@store_messages = "on"
end
def test_stored_messages
headers = {'accept' => 'application/json'}
body = 'published message'
channel = 'ch_test_stored_messages'
EventMachine.run {
pub_1 = EventMachine::HttpRequest.new(nginx_address + '/pub?id=' + channel.to_s ).post :head => headers, :body => body, :timeout => 30
pub_1.callback {
response = JSON.parse(pub_1.response)
assert_equal(1, response["stored_messages"].to_i, "Not stored messages")
pub_2 = EventMachine::HttpRequest.new(nginx_address + '/pub?id=' + channel.to_s ).post :head => headers, :body => body, :timeout => 30
pub_2.callback {
response = JSON.parse(pub_2.response)
assert_equal(2, response["stored_messages"].to_i, "Not stored messages")
EventMachine.stop
}
fail_if_connecttion_error(pub_2)
}
fail_if_connecttion_error(pub_1)
}
end
def config_test_not_stored_messages
@store_messages = "off"
end
def test_not_stored_messages
headers = {'accept' => 'application/json'}
body = 'published message'
channel = 'ch_test_not_stored_messages'
EventMachine.run {
pub = EventMachine::HttpRequest.new(nginx_address + '/pub?id=' + channel.to_s ).post :head => headers, :body => body, :timeout => 30
pub.callback {
response = JSON.parse(pub.response)
assert_equal(0, response["stored_messages"].to_i, "Stored messages")
EventMachine.stop
}
fail_if_connecttion_error(pub)
}
end
def config_test_max_stored_messages
@store_messages = "on"
@max_message_buffer_length = 4
end
def test_max_stored_messages
headers = {'accept' => 'application/json'}
body_prefix = 'published message '
channel = 'ch_test_max_stored_messages'
messagens_to_publish = 10
EventMachine.run {
i = 0
stored_messages = 0
EM.add_periodic_timer(0.05) do
i += 1
if i <= messagens_to_publish
pub = EventMachine::HttpRequest.new(nginx_address + '/pub?id=' + channel.to_s ).post :head => headers, :body => body_prefix + i.to_s, :timeout => 30
pub.callback {
response = JSON.parse(pub.response)
stored_messages = response["stored_messages"].to_i
}
fail_if_connecttion_error(pub)
else
EventMachine.stop
assert(stored_messages == @max_message_buffer_length, "Stored more messages then configured")
end
end
}
end
def config_test_max_channel_id_length
@max_channel_id_length = 5
end
def test_max_channel_id_length
headers = {'accept' => 'application/json'}
body = 'published message'
channel = '123456'
EventMachine.run {
pub = EventMachine::HttpRequest.new(nginx_address + '/pub?id=' + channel.to_s ).post :head => headers, :body => body, :timeout => 30
pub.callback {
assert_equal(0, pub.response_header.content_length, "Should response only with headers")
assert_equal(400, pub.response_header.status, "Request was not understood as a bad request")
assert_equal("Channel id is too large.", pub.response_header['X_NGINX_PUSHSTREAM_EXPLAIN'], "Didn't receive the right error message")
EventMachine.stop
}
fail_if_connecttion_error(pub)
}
end
def config_test_max_number_of_channels
@max_number_of_channels = 1
end
def test_max_number_of_channels
headers = {'accept' => 'application/json'}
body = 'published message'
channel = 'ch_test_max_number_of_channels_'
EventMachine.run {
pub = EventMachine::HttpRequest.new(nginx_address + '/pub?id=' + channel.to_s + 1.to_s).post :head => headers, :body => body, :timeout => 30
pub.callback {
assert_equal(200, pub.response_header.status, "Channel was not created")
assert_not_equal(0, pub.response_header.content_length, "Should response channel info")
EventMachine.stop
}
fail_if_connecttion_error(pub)
}
EventMachine.run {
pub = EventMachine::HttpRequest.new(nginx_address + '/pub?id=' + channel.to_s + 2.to_s).post :head => headers, :body => body, :timeout => 30
pub.callback {
assert_equal(403, pub.response_header.status, "Request was not forbidden")
assert_equal(0, pub.response_header.content_length, "Should response only with headers")
assert_equal("Number of channels were exceeded.", pub.response_header['X_NGINX_PUSHSTREAM_EXPLAIN'], "Didn't receive the right error message")
EventMachine.stop
}
fail_if_connecttion_error(pub)
}
end
def config_test_max_number_of_broadcast_channels
@max_number_of_broadcast_channels = 1
@broadcast_channel_prefix = 'bd_'
@broadcast_channel_max_qtd = 1
end
def test_max_number_of_broadcast_channels
headers = {'accept' => 'application/json'}
body = 'published message'
channel = 'bd_test_max_number_of_broadcast_channels_'
EventMachine.run {
pub = EventMachine::HttpRequest.new(nginx_address + '/pub?id=' + channel.to_s + 1.to_s).post :head => headers, :body => body, :timeout => 30
pub.callback {
assert_equal(200, pub.response_header.status, "Channel was not created")
assert_not_equal(0, pub.response_header.content_length, "Should response channel info")
EventMachine.stop
}
fail_if_connecttion_error(pub)
}
EventMachine.run {
pub = EventMachine::HttpRequest.new(nginx_address + '/pub?id=' + channel.to_s + 2.to_s).post :head => headers, :body => body, :timeout => 30
pub.callback {
assert_equal(403, pub.response_header.status, "Request was not forbidden")
assert_equal(0, pub.response_header.content_length, "Should response only with headers")
assert_equal("Number of channels were exceeded.", pub.response_header['X_NGINX_PUSHSTREAM_EXPLAIN'], "Didn't receive the right error message")
EventMachine.stop
}
fail_if_connecttion_error(pub)
}
end
end
require 'rubygems'
require 'popen4'
require 'em-http'
require 'test/unit'
require 'json'
require File.expand_path('base_test_case', File.dirname(__FILE__))
class TestPublisherProperties < Test::Unit::TestCase
include BaseTestCase
def initialize(opts)
super(opts)
@header_template = ""
@message_template = "~text~"
end
def config_test_stored_messages
@test_config_file = "test_store_messages.conf"
@store_messages = "on"
end
def test_stored_messages
headers = {'accept' => 'application/json'}
body = 'published message'
channel = 'ch1'
EventMachine.run {
pub_1 = EventMachine::HttpRequest.new(nginx_address + '/pub?id=' + channel.to_s ).post :head => headers, :body => body, :timeout => 30
pub_1.callback {
response = JSON.parse(pub_1.response)
assert_equal(1, response["stored_messages"].to_i, "Not stored messages")
pub_2 = EventMachine::HttpRequest.new(nginx_address + '/pub?id=' + channel.to_s ).post :head => headers, :body => body, :timeout => 30
pub_2.callback {
response = JSON.parse(pub_2.response)
assert_equal(2, response["stored_messages"].to_i, "Not stored messages")
EventMachine.stop
}
fail_if_connecttion_error(pub_2)
}
fail_if_connecttion_error(pub_1)
}
end
def config_test_not_stored_messages
@test_config_file = "test_not_store_messages.conf"
@store_messages = "off"
end
def test_not_stored_messages
headers = {'accept' => 'application/json'}
body = 'published message'
channel = 'ch2'
EventMachine.run {
pub = EventMachine::HttpRequest.new(nginx_address + '/pub?id=' + channel.to_s ).post :head => headers, :body => body, :timeout => 30
pub.callback {
response = JSON.parse(pub.response)
assert_equal(0, response["stored_messages"].to_i, "Stored messages")
EventMachine.stop
}
fail_if_connecttion_error(pub)
}
end
def config_test_max_stored_messages
@test_config_file = "test_max_stored_messages.conf"
@store_messages = "on"
@max_message_buffer_length = 4
end
def test_max_stored_messages
headers = {'accept' => 'application/json'}
body_prefix = 'published message '
channel = 'ch3'
messagens_to_publish = 10
EventMachine.run {
i = 0
EM.add_periodic_timer(0.05) do
i += 1
if i <= messagens_to_publish
pub = EventMachine::HttpRequest.new(nginx_address + '/pub?id=' + channel.to_s ).post :head => headers, :body => body_prefix + i.to_s, :timeout => 30
pub.callback {
response = JSON.parse(pub.response)
assert(response["stored_messages"].to_i <= @max_message_buffer_length, "Stored more messages then configured")
}
fail_if_connecttion_error(pub)
else
EventMachine.stop
end
end
}
end
def config_test_max_channel_id_length
@test_config_file = "test_max_channel_id_length.conf"
@max_channel_id_length = 5
end
def test_max_channel_id_length
headers = {'accept' => 'application/json'}
body = 'published message'
channel = '123456'
EventMachine.run {
pub = EventMachine::HttpRequest.new(nginx_address + '/pub?id=' + channel.to_s ).post :head => headers, :body => body, :timeout => 30
pub.callback {
response = JSON.parse(pub.response)
assert_equal("12345", response["channel"], "No crop the channel id")
EventMachine.stop
}
fail_if_connecttion_error(pub)
}
end
end
......@@ -3,9 +3,13 @@ require File.expand_path('base_test_case', File.dirname(__FILE__))
class TestSetuParameters < Test::Unit::TestCase
include BaseTestCase
def initialize(opts)
super(opts)
@disable_start_stop_server = true
end
def test_ping_message_interval_cannot_be_zero
expected_error_message = "push_stream_ping_message_interval cannot be zero"
@test_config_file = "test_ping_message_interval_cannot_be_zero.conf"
@ping_message_interval = 0
self.create_config_file
......@@ -15,8 +19,8 @@ class TestSetuParameters < Test::Unit::TestCase
def test_ping_message_interval_cannot_be_set_without_a_message_template
expected_error_message = "cannot have ping message if push_stream_message_template is not set or blank"
@test_config_file = "test_ping_message_interval_cannot_be_set_without_a_message_template.conf"
@ping_message_interval = "1s"
@message_template = nil
self.create_config_file
stderr_msg = self.start_server
......@@ -25,7 +29,6 @@ class TestSetuParameters < Test::Unit::TestCase
def test_ping_message_interval_cannot_be_set_if_message_template_is_blank
expected_error_message = "cannot have ping message if push_stream_message_template is not set or blank"
@test_config_file = "test_ping_message_interval_cannot_be_set_if_message_template_is_blank.conf"
@ping_message_interval = "1s"
@message_template = ""
......@@ -36,7 +39,6 @@ class TestSetuParameters < Test::Unit::TestCase
def test_subscriber_disconnect_interval_cannot_be_zero
expected_error_message = "push_stream_subscriber_disconnect_interval cannot be zero"
@test_config_file = "test_subscriber_disconnect_interval_cannot_be_zero.conf"
@subscriber_disconnect_interval = 0
self.create_config_file
......@@ -46,7 +48,6 @@ class TestSetuParameters < Test::Unit::TestCase
def test_subscriber_connection_timeout_cannot_be_zero
expected_error_message = "push_stream_subscriber_connection_timeout cannot be zero"
@test_config_file = "test_subscriber_connection_timeout_cannot_be_zero.conf"
@subscriber_connection_timeout = 0
self.create_config_file
......@@ -56,7 +57,6 @@ class TestSetuParameters < Test::Unit::TestCase
def test_subscriber_disconnect_interval_cannot_be_set_without_a_connection_timeout
expected_error_message = "cannot set subscriber disconnect interval if push_stream_subscriber_connection_timeout is not set or zero"
@test_config_file = "test_subscriber_disconnect_interval_cannot_be_set_without_a_connection_timeout.conf"
@subscriber_disconnect_interval = "1s"
self.create_config_file
......@@ -66,7 +66,6 @@ class TestSetuParameters < Test::Unit::TestCase
def test_subscriber_connection_timeout_cannot_be_set_without_a_disconnect_interval
expected_error_message = "cannot set subscriber connection timeout if push_stream_subscriber_disconnect_interval is not set or zero"
@test_config_file = "test_subscriber_connection_timeout_cannot_be_set_without_a_disconnect_interval.conf"
@subscriber_connection_timeout = "1s"
self.create_config_file
......@@ -76,7 +75,6 @@ class TestSetuParameters < Test::Unit::TestCase
def test_max_channel_id_length_cannot_be_zero
expected_error_message = "push_stream_max_channel_id_length cannot be zero"
@test_config_file = "test_max_channel_id_length_cannot_be_zero.conf"
@max_channel_id_length = 0
self.create_config_file
......@@ -86,7 +84,6 @@ class TestSetuParameters < Test::Unit::TestCase
def test_min_message_buffer_timeout_cannot_be_zero
expected_error_message = "push_stream_min_message_buffer_timeout cannot be zero"
@test_config_file = "test_min_message_buffer_timeout_cannot_be_zero.conf"
@min_message_buffer_timeout = 0
self.create_config_file
......@@ -96,7 +93,6 @@ class TestSetuParameters < Test::Unit::TestCase
def test_max_message_buffer_length_cannot_be_zero
expected_error_message = "push_stream_max_message_buffer_length cannot be zero"
@test_config_file = "test_max_message_buffer_length_cannot_be_zero.conf"
@max_message_buffer_length = 0
self.create_config_file
......@@ -106,8 +102,9 @@ class TestSetuParameters < Test::Unit::TestCase
def test_store_messages_cannot_be_set_without_set_max_message_buffer_length_or_min_message_buffer_timeout
expected_error_message = "push_stream_store_messages cannot be set without set max message buffer length or min message buffer timeout"
@test_config_file = "test_store_messages_cannot_be_set_without_set_max_message_buffer_length_or_min_message_buffer_timeout.conf"
@store_messages = 'on'
@min_message_buffer_timeout = nil
@max_message_buffer_length = nil
self.create_config_file
stderr_msg = self.start_server
......@@ -116,7 +113,6 @@ class TestSetuParameters < Test::Unit::TestCase
def test_broadcast_channel_max_qtd_cannot_be_zero
expected_error_message = "push_stream_broadcast_channel_max_qtd cannot be zero"
@test_config_file = "test_broadcast_channel_max_qtd_cannot_be_zero.conf"
@broadcast_channel_max_qtd = 0
self.create_config_file
......@@ -126,7 +122,6 @@ class TestSetuParameters < Test::Unit::TestCase
def test_broadcast_channel_max_qtd_cannot_be_set_without_broadcast_channel_prefix
expected_error_message = "cannot set broadcast channel max qtd if push_stream_broadcast_channel_prefix is not set or blank"
@test_config_file = "test_broadcast_channel_max_qtd_cannot_be_set_without_broadcast_channel_prefix.conf"
@broadcast_channel_max_qtd = 1
self.create_config_file
......@@ -136,7 +131,6 @@ class TestSetuParameters < Test::Unit::TestCase
def test_broadcast_channel_max_qtd_cannot_be_set_without_broadcast_channel_prefix
expected_error_message = "cannot set broadcast channel max qtd if push_stream_broadcast_channel_prefix is not set or blank"
@test_config_file = "test_broadcast_channel_max_qtd_cannot_be_set_without_broadcast_channel_prefix.conf"
@broadcast_channel_max_qtd = 1
@broadcast_channel_prefix = ""
......@@ -147,7 +141,36 @@ class TestSetuParameters < Test::Unit::TestCase
def test_broadcast_channel_prefix_cannot_be_set_without_broadcast_channel_max_qtd
expected_error_message = "cannot set broadcast channel prefix if push_stream_broadcast_channel_max_qtd is not set"
@test_config_file = "test_broadcast_channel_prefix_cannot_be_set_without_broadcast_channel_max_qtd.conf"
@broadcast_channel_prefix = "broad_"
@broadcast_channel_max_qtd = nil
self.create_config_file
stderr_msg = self.start_server
assert(stderr_msg.include?(expected_error_message), "Message error not founded: '#{ expected_error_message }' recieved '#{ stderr_msg }'")
end
def test_max_number_of_channels_cannot_be_zero
expected_error_message = "push_stream_max_number_of_channels cannot be zero"
@max_number_of_channels = 0
self.create_config_file
stderr_msg = self.start_server
assert(stderr_msg.include?(expected_error_message), "Message error not founded: '#{ expected_error_message }' recieved '#{ stderr_msg }'")
end
def test_max_number_of_broadcast_channels_cannot_be_zero
expected_error_message = "push_stream_max_number_of_broadcast_channels cannot be zero"
@max_number_of_broadcast_channels = 0
self.create_config_file
stderr_msg = self.start_server
assert(stderr_msg.include?(expected_error_message), "Message error not founded: '#{ expected_error_message }' recieved '#{ stderr_msg }'")
end
def test_max_number_of_broadcast_channels_cannot_be_smaller_than_broadcast_channel_max_qtd
expected_error_message = "max number of broadcast channels cannot be smaller than value in push_stream_broadcast_channel_max_qtd"
@max_number_of_broadcast_channels = 3
@broadcast_channel_max_qtd = 4
@broadcast_channel_prefix = "broad_"
self.create_config_file
......@@ -155,4 +178,12 @@ class TestSetuParameters < Test::Unit::TestCase
assert(stderr_msg.include?(expected_error_message), "Message error not founded: '#{ expected_error_message }' recieved '#{ stderr_msg }'")
end
def test_memory_cleanup_timeout
expected_error_message = "memory cleanup timeout cannot't be less than 30."
@memory_cleanup_timeout = '15s'
self.create_config_file
stderr_msg = self.start_server
assert(stderr_msg.include?(expected_error_message), "Message error not founded: '#{ expected_error_message }' recieved '#{ stderr_msg }'")
end
end
require File.expand_path('base_test_case', File.dirname(__FILE__))
class TestPublisher < Test::Unit::TestCase
include BaseTestCase
def config_test_accepted_methods
@subscriber_connection_timeout = '1s'
@subscriber_disconnect_interval = '1s'
end
def test_accepted_methods
EventMachine.run {
multi = EventMachine::MultiRequest.new
multi.add(EventMachine::HttpRequest.new(nginx_address + '/sub/ch_test_accepted_methods_1').head)
multi.add(EventMachine::HttpRequest.new(nginx_address + '/sub/ch_test_accepted_methods_2').put :body => 'body')
multi.add(EventMachine::HttpRequest.new(nginx_address + '/sub/ch_test_accepted_methods_3').post)
multi.add(EventMachine::HttpRequest.new(nginx_address + '/sub/ch_test_accepted_methods_4').delete)
multi.add(EventMachine::HttpRequest.new(nginx_address + '/sub/ch_test_accepted_methods_5').get)
multi.callback {
assert_equal(5, multi.responses[:succeeded].length)
assert_equal(405, multi.responses[:succeeded][0].response_header.status, "Publisher does not accept HEAD")
assert_equal("HEAD", multi.responses[:succeeded][0].method, "Array is with wrong order")
assert_equal("GET", multi.responses[:succeeded][0].response_header['ALLOW'], "Didn't receive the right error message")
assert_equal(405, multi.responses[:succeeded][1].response_header.status, "Publisher does not accept PUT")
assert_equal("PUT", multi.responses[:succeeded][1].method, "Array is with wrong order")
assert_equal("GET", multi.responses[:succeeded][1].response_header['ALLOW'], "Didn't receive the right error message")
assert_equal(405, multi.responses[:succeeded][2].response_header.status, "Publisher does accept POST")
assert_equal("POST", multi.responses[:succeeded][2].method, "Array is with wrong order")
assert_equal("GET", multi.responses[:succeeded][1].response_header['ALLOW'], "Didn't receive the right error message")
assert_equal(405, multi.responses[:succeeded][3].response_header.status, "Publisher does not accept DELETE")
assert_equal("DELETE", multi.responses[:succeeded][3].method, "Array is with wrong order")
assert_equal("GET", multi.responses[:succeeded][3].response_header['ALLOW'], "Didn't receive the right error message")
assert_not_equal(405, multi.responses[:succeeded][4].response_header.status, "Publisher does accept GET")
assert_equal("GET", multi.responses[:succeeded][4].method, "Array is with wrong order")
EventMachine.stop
}
fail_if_connecttion_error(multi)
}
end
def test_access_whithout_channel_path
headers = {'accept' => 'application/json'}
EventMachine.run {
sub = EventMachine::HttpRequest.new(nginx_address + '/sub/').get :head => headers, :timeout => 30
sub.callback {
assert_equal(0, sub.response_header.content_length, "Should response only with headers")
assert_equal(400, sub.response_header.status, "Request was not understood as a bad request")
assert_equal("No channel id provided.", sub.response_header['X_NGINX_PUSHSTREAM_EXPLAIN'], "Didn't receive the right error message")
EventMachine.stop
}
fail_if_connecttion_error(sub)
}
end
def config_test_multi_channels
@subscriber_connection_timeout = '1s'
@subscriber_disconnect_interval = '1s'
end
def test_multi_channels
EventMachine.run {
multi = EventMachine::MultiRequest.new
multi.add(EventMachine::HttpRequest.new(nginx_address + '/sub/ch_multi_channels_1').get)
multi.add(EventMachine::HttpRequest.new(nginx_address + '/sub/ch_multi_channels_1.b10').get)
multi.add(EventMachine::HttpRequest.new(nginx_address + '/sub/ch_multi_channels_2/ch_multi_channels_3').get)
multi.add(EventMachine::HttpRequest.new(nginx_address + '/sub/ch_multi_channels_2.b2/ch_multi_channels_3').get)
multi.add(EventMachine::HttpRequest.new(nginx_address + '/sub/ch_multi_channels_2/ch_multi_channels_3.b3').get)
multi.add(EventMachine::HttpRequest.new(nginx_address + '/sub/ch_multi_channels_2.b2/ch_multi_channels_3.b3').get)
multi.add(EventMachine::HttpRequest.new(nginx_address + '/sub/ch_multi_channels_4.b').get)
multi.callback {
assert_equal(7, multi.responses[:succeeded].length)
0.upto(6) do |i|
assert_equal(200, multi.responses[:succeeded][i].response_header.status, "Subscriber not accepted")
end
EventMachine.stop
}
fail_if_connecttion_error(multi)
}
end
def config_test_max_channel_id_length
@max_channel_id_length = 5
end
def test_max_channel_id_length
headers = {'accept' => 'application/json'}
channel = '123456'
EventMachine.run {
sub = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s ).get :head => headers, :timeout => 30
sub.callback {
assert_equal(0, sub.response_header.content_length, "Should response only with headers")
assert_equal(400, sub.response_header.status, "Request was not understood as a bad request")
assert_equal("Channel id is too large.", sub.response_header['X_NGINX_PUSHSTREAM_EXPLAIN'], "Didn't receive the right error message")
EventMachine.stop
}
fail_if_connecttion_error(sub)
}
end
def test_cannot_access_a_channel_with_id_ALL
headers = {'accept' => 'application/json'}
channel = 'ALL'
EventMachine.run {
sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers, :timeout => 30
sub_1.callback {
assert_equal(403, sub_1.response_header.status, "Channel was created")
assert_equal(0, sub_1.response_header.content_length, "Received response for creating channel with id ALL")
assert_equal("Channel id not authorized for this method.", sub_1.response_header['X_NGINX_PUSHSTREAM_EXPLAIN'], "Didn't receive the right error message")
EventMachine.stop
}
fail_if_connecttion_error(sub_1)
}
end
def config_test_broadcast_channels_without_common_channel
@subscriber_connection_timeout = '1s'
@subscriber_disconnect_interval = '1s'
@broadcast_channel_prefix = "bd_"
end
def test_broadcast_channels_without_common_channel
headers = {'accept' => 'application/json'}
EventMachine.run {
multi = EventMachine::MultiRequest.new
multi.add(EventMachine::HttpRequest.new(nginx_address + '/sub/bd_test_broadcast_channels_without_common_channel').get)
multi.add(EventMachine::HttpRequest.new(nginx_address + '/sub/bd_').get)
multi.add(EventMachine::HttpRequest.new(nginx_address + '/sub/bd1').get)
multi.add(EventMachine::HttpRequest.new(nginx_address + '/sub/bd').get)
multi.callback {
assert_equal(4, multi.responses[:succeeded].length)
assert_equal(0, multi.responses[:succeeded][0].response_header.content_length, "Should response only with headers")
assert_equal(403, multi.responses[:succeeded][0].response_header.status, "Request was not understood as a bad request")
assert_equal("Subscribed too much broadcast channels.", multi.responses[:succeeded][0].response_header['X_NGINX_PUSHSTREAM_EXPLAIN'], "Didn't receive the right error message")
assert_equal(nginx_address + '/sub/bd_test_broadcast_channels_without_common_channel', multi.responses[:succeeded][0].uri.to_s, "Array is with wrong order")
assert_equal(0, multi.responses[:succeeded][1].response_header.content_length, "Should response only with headers")
assert_equal(403, multi.responses[:succeeded][1].response_header.status, "Request was not understood as a bad request")
assert_equal("Subscribed too much broadcast channels.", multi.responses[:succeeded][1].response_header['X_NGINX_PUSHSTREAM_EXPLAIN'], "Didn't receive the right error message")
assert_equal(nginx_address + '/sub/bd_', multi.responses[:succeeded][1].uri.to_s, "Array is with wrong order")
assert_equal(200, multi.responses[:succeeded][2].response_header.status, "Channel id starting with different prefix from broadcast was not accept")
assert_equal(nginx_address + '/sub/bd1', multi.responses[:succeeded][2].uri.to_s, "Array is with wrong order")
assert_equal(200, multi.responses[:succeeded][3].response_header.status, "Channel id starting with different prefix from broadcast was not accept")
assert_equal(nginx_address + '/sub/bd', multi.responses[:succeeded][3].uri.to_s, "Array is with wrong order")
EventMachine.stop
}
fail_if_connecttion_error(multi)
}
end
def config_test_broadcast_channels_with_common_channels
@subscriber_connection_timeout = '1s'
@subscriber_disconnect_interval = '1s'
@authorized_channels_only = "off"
@broadcast_channel_prefix = "bd_"
@broadcast_channel_max_qtd = 2
end
def test_broadcast_channels_with_common_channels
headers = {'accept' => 'application/json'}
EventMachine.run {
multi = EventMachine::MultiRequest.new
multi.add(EventMachine::HttpRequest.new(nginx_address + '/sub/bd1/bd2/bd3/bd4/bd_1/bd_2/bd_3').get)
multi.add(EventMachine::HttpRequest.new(nginx_address + '/sub/bd1/bd2/bd_1/bd_2').get)
multi.add(EventMachine::HttpRequest.new(nginx_address + '/sub/bd1/bd_1').get)
multi.add(EventMachine::HttpRequest.new(nginx_address + '/sub/bd1/bd2').get)
multi.callback {
assert_equal(4, multi.responses[:succeeded].length)
assert_equal(0, multi.responses[:succeeded][0].response_header.content_length, "Should response only with headers")
assert_equal(403, multi.responses[:succeeded][0].response_header.status, "Request was not understood as a bad request")
assert_equal("Subscribed too much broadcast channels.", multi.responses[:succeeded][0].response_header['X_NGINX_PUSHSTREAM_EXPLAIN'], "Didn't receive the right error message")
assert_equal(nginx_address + '/sub/bd1/bd2/bd3/bd4/bd_1/bd_2/bd_3', multi.responses[:succeeded][0].uri.to_s, "Array is with wrong order")
assert_equal(200, multi.responses[:succeeded][1].response_header.status, "Request was not understood as a bad request")
assert_equal(nginx_address + '/sub/bd1/bd2/bd_1/bd_2', multi.responses[:succeeded][1].uri.to_s, "Array is with wrong order")
assert_equal(200, multi.responses[:succeeded][2].response_header.status, "Channel id starting with different prefix from broadcast was not accept")
assert_equal(nginx_address + '/sub/bd1/bd_1', multi.responses[:succeeded][2].uri.to_s, "Array is with wrong order")
assert_equal(200, multi.responses[:succeeded][3].response_header.status, "Channel id starting with different prefix from broadcast was not accept")
assert_equal(nginx_address + '/sub/bd1/bd2', multi.responses[:succeeded][3].uri.to_s, "Array is with wrong order")
EventMachine.stop
}
fail_if_connecttion_error(multi)
}
end
def config_test_subscribe_an_absent_channel_with_authorized_only_on
@authorized_channels_only = 'on'
end
def test_subscribe_an_absent_channel_with_authorized_only_on
headers = {'accept' => 'application/json'}
channel = 'ch_test_subscribe_an_absent_channel_with_authorized_only_on'
EventMachine.run {
sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers, :timeout => 30
sub_1.callback {
assert_equal(403, sub_1.response_header.status, "Channel was founded")
assert_equal(0, sub_1.response_header.content_length, "Recieved a non empty response")
assert_equal("Subscriber could not create channels.", sub_1.response_header['X_NGINX_PUSHSTREAM_EXPLAIN'], "Didn't receive the right error message")
EventMachine.stop
}
fail_if_connecttion_error(sub_1)
}
end
def config_test_subscribe_an_existing_channel_with_authorized_only_on
@authorized_channels_only = 'on'
@subscriber_connection_timeout = '1s'
@subscriber_disconnect_interval = '1s'
end
def test_subscribe_an_existing_channel_with_authorized_only_on
headers = {'accept' => 'application/json'}
channel = 'ch_test_subscribe_an_existing_channel_with_authorized_only_on'
body = 'body'
#create channel
publish_message(channel, headers, body)
EventMachine.run {
sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers, :timeout => 30
sub_1.callback {
assert_equal(200, sub_1.response_header.status, "Channel was founded")
EventMachine.stop
}
fail_if_connecttion_error(sub_1)
}
end
def config_test_subscribe_an_existing_channel_and_absent_broadcast_channel_with_authorized_only_on
@authorized_channels_only = 'on'
@subscriber_connection_timeout = '1s'
@subscriber_disconnect_interval = '1s'
@broadcast_channel_prefix = "bd_"
@broadcast_channel_max_qtd = 1
end
def test_subscribe_an_existing_channel_and_absent_broadcast_channel_with_authorized_only_on
headers = {'accept' => 'application/json'}
channel = 'ch_test_subscribe_an_existing_channel_and_absent_broadcast_channel_with_authorized_only_on'
broadcast_channel = 'bd_test_subscribe_an_existing_channel_and_absent_broadcast_channel_with_authorized_only_on'
body = 'body'
#create channel
publish_message(channel, headers, body)
EventMachine.run {
sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s + '/' + broadcast_channel.to_s).get :head => headers, :timeout => 30
sub_1.callback {
assert_equal(200, sub_1.response_header.status, "Channel was founded")
EventMachine.stop
}
fail_if_connecttion_error(sub_1)
}
end
def config_test_subscribe_an_existing_channel_without_messages_and_with_authorized_only_on
@min_message_buffer_timeout = '1s'
@authorized_channels_only = 'on'
end
def test_subscribe_an_existing_channel_without_messages_and_with_authorized_only_on
headers = {'accept' => 'application/json'}
channel = 'ch_test_subscribe_an_existing_channel_without_messages_and_with_authorized_only_on'
body = 'body'
#create channel
publish_message(channel, headers, body)
sleep(2) #to ensure message was gone
EventMachine.run {
sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers, :timeout => 30
sub_1.callback {
assert_equal(403, sub_1.response_header.status, "Channel was founded")
assert_equal(0, sub_1.response_header.content_length, "Recieved a non empty response")
assert_equal("Subscriber could not create channels.", sub_1.response_header['X_NGINX_PUSHSTREAM_EXPLAIN'], "Didn't receive the right error message")
EventMachine.stop
}
fail_if_connecttion_error(sub_1)
}
end
def config_test_subscribe_an_existing_channel_without_messages_and_absent_broadcast_channel_and_with_authorized_only_on_should_fail
@min_message_buffer_timeout = '1s'
@authorized_channels_only = 'on'
@broadcast_channel_prefix = "bd_"
@broadcast_channel_max_qtd = 1
end
def test_subscribe_an_existing_channel_without_messages_and_absent_broadcast_channel_and_with_authorized_only_on_should_fail
headers = {'accept' => 'application/json'}
channel = 'ch_test_subscribe_an_existing_channel_without_messages_and_absent_broadcast_channel_and_with_authorized_only_on_should_fail'
broadcast_channel = 'bd_test_subscribe_an_existing_channel_without_messages_and_absent_broadcast_channel_and_with_authorized_only_on_should_fail'
body = 'body'
#create channel
publish_message(channel, headers, body)
sleep(2) #to ensure message was gone
EventMachine.run {
sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s + '/' + broadcast_channel.to_s).get :head => headers, :timeout => 30
sub_1.callback {
assert_equal(403, sub_1.response_header.status, "Channel was founded")
assert_equal(0, sub_1.response_header.content_length, "Recieved a non empty response")
assert_equal("Subscriber could not create channels.", sub_1.response_header['X_NGINX_PUSHSTREAM_EXPLAIN'], "Didn't receive the right error message")
EventMachine.stop
}
fail_if_connecttion_error(sub_1)
}
end
def config_test_retreive_old_messages_in_multichannel_subscribe
@header_template = 'HEADER'
@message_template = '{\"channel\":\"~channel~\", \"id\":\"~id~\", \"message\":\"~text~\"}'
end
def test_retreive_old_messages_in_multichannel_subscribe
headers = {'accept' => 'application/json'}
channel_1 = 'ch_test_retreive_old_messages_in_multichannel_subscribe_1'
channel_2 = 'ch_test_retreive_old_messages_in_multichannel_subscribe_2'
channel_3 = 'ch_test_retreive_old_messages_in_multichannel_subscribe_3'
body = 'body'
#create channels with some messages
1.upto(3) do |i|
publish_message(channel_1, headers, body + i.to_s)
publish_message(channel_2, headers, body + i.to_s)
publish_message(channel_3, headers, body + i.to_s)
end
response = ""
EventMachine.run {
sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel_1.to_s + '/' + channel_2.to_s + '.b5' + '/' + channel_3.to_s + '.b2').get :head => headers, :timeout => 30
sub_1.stream { |chunk|
response += chunk
lines = response.split("\r\n")
if lines.length >= 6
assert_equal('HEADER', lines[0], "Header was not received")
line = JSON.parse(lines[1])
assert_equal(channel_2.to_s, line['channel'], "Wrong channel")
assert_equal('body1', line['message'], "Wrong message")
assert_equal(1, line['id'].to_i, "Wrong message")
line = JSON.parse(lines[2])
assert_equal(channel_2.to_s, line['channel'], "Wrong channel")
assert_equal('body2', line['message'], "Wrong message")
assert_equal(2, line['id'].to_i, "Wrong message")
line = JSON.parse(lines[3])
assert_equal(channel_2.to_s, line['channel'], "Wrong channel")
assert_equal('body3', line['message'], "Wrong message")
assert_equal(3, line['id'].to_i, "Wrong message")
line = JSON.parse(lines[4])
assert_equal(channel_3.to_s, line['channel'], "Wrong channel")
assert_equal('body2', line['message'], "Wrong message")
assert_equal(2, line['id'].to_i, "Wrong message")
line = JSON.parse(lines[5])
assert_equal(channel_3.to_s, line['channel'], "Wrong channel")
assert_equal('body3', line['message'], "Wrong message")
assert_equal(3, line['id'].to_i, "Wrong message")
EventMachine.stop
end
}
fail_if_connecttion_error(sub_1)
}
end
def config_test_max_number_of_channels
@max_number_of_channels = 1
end
def test_max_number_of_channels
headers = {'accept' => 'application/json'}
channel = 'ch_test_max_number_of_channels_'
EventMachine.run {
sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s + 1.to_s).get :head => headers, :timeout => 30
sub_1.stream {
assert_equal(200, sub_1.response_header.status, "Channel was not created")
assert_not_equal(0, sub_1.response_header.content_length, "Should response channel info")
sub_2 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s + 2.to_s).get :head => headers, :timeout => 30
sub_2.callback {
assert_equal(403, sub_2.response_header.status, "Request was not forbidden")
assert_equal(0, sub_2.response_header.content_length, "Should response only with headers")
assert_equal("Number of channels were exceeded.", sub_2.response_header['X_NGINX_PUSHSTREAM_EXPLAIN'], "Didn't receive the right error message")
EventMachine.stop
}
fail_if_connecttion_error(sub_2)
}
fail_if_connecttion_error(sub_1)
}
end
def config_test_max_number_of_broadcast_channels
@max_number_of_broadcast_channels = 1
@broadcast_channel_prefix = 'bd_'
@broadcast_channel_max_qtd = 1
end
def test_max_number_of_broadcast_channels
headers = {'accept' => 'application/json'}
channel = 'bd_test_max_number_of_broadcast_channels_'
EventMachine.run {
sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/ch1/' + channel.to_s + 1.to_s).get :head => headers, :timeout => 30
sub_1.stream {
assert_equal(200, sub_1.response_header.status, "Channel was not created")
assert_not_equal(0, sub_1.response_header.content_length, "Should response channel info")
sub_2 = EventMachine::HttpRequest.new(nginx_address + '/sub/ch1/' + channel.to_s + 2.to_s).get :head => headers, :timeout => 30
sub_2.callback {
assert_equal(403, sub_2.response_header.status, "Request was not forbidden")
assert_equal(0, sub_2.response_header.content_length, "Should response only with headers")
assert_equal("Number of channels were exceeded.", sub_2.response_header['X_NGINX_PUSHSTREAM_EXPLAIN'], "Didn't receive the right error message")
EventMachine.stop
}
fail_if_connecttion_error(sub_2)
}
fail_if_connecttion_error(sub_1)
}
end
end
require 'rubygems'
require 'em-http'
require 'test/unit'
require File.expand_path('base_test_case', File.dirname(__FILE__))
class TestSubscriberConnectionCleanup < Test::Unit::TestCase
include BaseTestCase
def config_test_subscriber_connection_timeout
@test_config_file = "test_subscriber_connection_timeout.conf"
@subscriber_connection_timeout = "37s"
@subscriber_disconnect_interval = "1s"
@header_template = "HEADER_TEMPLATE"
@ping_message_interval = "0s"
@ping_message_interval = nil
end
def test_subscriber_connection_timeout
channel = 'ch1'
channel = 'ch_test_subscriber_connection_timeout'
headers = {'accept' => 'text/html'}
start = Time.now
......@@ -36,28 +32,27 @@ class TestSubscriberConnectionCleanup < Test::Unit::TestCase
end
def config_test_subscriber_disconnect_interval
@test_config_file = "test_subscriber_disconnect_interval.conf"
@subscriber_connection_timeout = "37s"
@ping_message_interval = "5s"
@subscriber_disconnect_interval = "5s"
@subscriber_disconnect_interval = "2s"
end
def test_subscriber_disconnect_interval
channel = 'ch2'
channel = 'ch_test_subscriber_disconnect_interval'
headers = {'accept' => 'text/html'}
start = Time.now
chuncksReceived = 0
chunksReceived = 0
EventMachine.run {
sub = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers, :timeout => 60
sub.stream { |chunk|
chuncksReceived += 1;
chunksReceived += 1;
}
sub.callback {
stop = Time.now
elapsed = time_diff_sec(start, stop)
assert(elapsed >= 40 && elapsed <= 40.5, "Disconnect was in #{elapsed} seconds")
assert_equal(9, chuncksReceived, "Received #{chuncksReceived} chuncks")
assert(elapsed >= 38 && elapsed <= 39.5, "Disconnect was in #{elapsed} seconds")
assert_equal(8, chunksReceived, "Received #{chunksReceived} chunks")
EventMachine.stop
}
fail_if_connecttion_error(sub)
......
require 'rubygems'
require 'em-http'
require 'test/unit'
require File.expand_path('base_test_case', File.dirname(__FILE__))
class TestSubscriberProperties < Test::Unit::TestCase
include BaseTestCase
def config_test_header_template
@test_config_file = "test_header_template.conf"
@header_template = "HEADER\r\nTEMPLATE\r\n1234\r\n"
@authorized_channels_only = "off"
@subscriber_disconnect_interval = nil
end
def test_header_template
channel = 'ch1'
channel = 'ch_test_header_template'
headers = {'accept' => 'text/html'}
EventMachine.run {
......@@ -27,13 +24,12 @@ class TestSubscriberProperties < Test::Unit::TestCase
end
def config_test_content_type
@test_config_file = "test_content_type.conf"
@content_type = "custom content type"
@authorized_channels_only = "off"
end
def test_content_type
channel = 'ch2'
channel = 'ch_test_content_type'
headers = {'accept' => 'text/html'}
EventMachine.run {
......@@ -47,32 +43,31 @@ class TestSubscriberProperties < Test::Unit::TestCase
end
def config_test_ping_message_interval
@test_config_file = "test_ping_message_interval.conf"
@subscriber_connection_timeout = "0s"
@subscriber_connection_timeout = nil
@ping_message_interval = "2s"
end
def test_ping_message_interval
channel = 'ch3'
channel = 'ch_test_ping_message_interval'
headers = {'accept' => 'text/html'}
step1 = step2 = step3 = step4 = nil
chuncksReceived = 0
chunksReceived = 0
EventMachine.run {
sub = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers, :timeout => 60
sub.stream { |chunk|
chuncksReceived += 1;
step1 = Time.now if chuncksReceived == 1
step2 = Time.now if chuncksReceived == 2
step3 = Time.now if chuncksReceived == 3
step4 = Time.now if chuncksReceived == 4
if chuncksReceived == 4
chunksReceived += 1;
step1 = Time.now if chunksReceived == 1
step2 = Time.now if chunksReceived == 2
step3 = Time.now if chunksReceived == 3
step4 = Time.now if chunksReceived == 4
if chunksReceived == 4
EventMachine.stop
end
}
sub.callback {
assert_equal(4, chuncksReceived, "Didn't received expected messages")
assert_equal(4, chunksReceived, "Didn't received expected messages")
interval1 = time_diff_sec(step2, step1).round
interval2 = time_diff_sec(step4, step3).round
assert_equal(interval1, interval2, "Wrong #{interval1}, #{interval2} intervals")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment