Commit 95a85754 authored by Wandenberg Peixoto's avatar Wandenberg Peixoto

intervals assigned with time zero will be considered as not assigned

parent 85b56d0b
build/
.cproject
.project
test/.bundle
rvm ruby-1.8.7@nginx-push-stream-module --create
source 'http://rubygems.org'
gem 'POpen4', '0.1.4'
gem 'em-http-request', '0.2.14'
gem 'json', '1.4.3'
GEM
remote: http://rubygems.org/
specs:
POpen4 (0.1.4)
Platform (>= 0.4.0)
open4
Platform (0.4.0)
addressable (2.2.2)
em-http-request (0.2.14)
addressable (>= 2.0.0)
eventmachine (>= 0.12.9)
eventmachine (0.12.10)
json (1.4.3)
open4 (1.0.1)
PLATFORMS
ruby
DEPENDENCIES
POpen4 (= 0.1.4)
em-http-request (= 0.2.14)
json (= 1.4.3)
To run these tests is needed to install some gems
You can install with bundler (bundler is required to be installed before)
bundle install
or individually
gem install --remote --include-dependencies POpen4 -v 0.1.4
gem install --remote --include-dependencies em-http-request -v 0.2.14
gem install --remote --include-dependencies json -v 1.4.3
require 'rubygems'
require 'popen4'
require 'tmpdir'
require 'erb'
require 'ftools'
module BaseTestCase
def setup
if @test_config_file and @test_config_file != ""
self.create_config_file
self.start_server
end
end
def teardown
if @test_config_file and @test_config_file != ""
self.stop_server
self.delete_config_file
end
end
def nginx_executable
return "/usr/local/nginxpushstream/source/nginx-0.7.67/objs/nginx"
end
def nginx_address
return "http://localhost:9999"
end
def start_server
error_message = ""
status = POpen4::popen4("#{ nginx_executable } -c #{ Dir.tmpdir }/#{ @test_config_file }") do |stdout, stderr, stdin, pid|
error_message = stderr.read.strip unless stderr.eof
return error_message unless error_message.nil?
end
assert_equal(0, status.exitstatus, "Server doesn't started - #{error_message}")
end
def stop_server
error_message = ""
status = POpen4::popen4("#{ nginx_executable } -c #{ Dir.tmpdir }/#{ @test_config_file } -s stop") do |stdout, stderr, stdin, pid|
error_message = stderr.read.strip unless stderr.eof
return error_message unless error_message.nil?
end
assert_equal(0, status.exitstatus, "Server doesn't stop - #{error_message}")
end
def create_config_file
template = ERB.new @@config_template
config_content = template.result(binding)
File.open(Dir.tmpdir + "/#{ @test_config_file }", 'w') {|f| f.write(config_content) }
File.open(Dir.tmpdir + "/mime.types", 'w') {|f| f.write(@@mime_tipes_template) }
end
def delete_config_file
File.delete(Dir.tmpdir + "/#{ @test_config_file }")
File.delete(Dir.tmpdir + "/mime.types")
end
def time_diff_milli(start, finish)
(finish - start) * 1000.0
end
def time_diff_sec(start, finish)
(finish - start)
end
@@config_template = %q{
pid logs/nginx.pid;
error_log logs/nginx-main_error.log debug;
# Development Mode
master_process off;
daemon on;
worker_processes 1;
events {
#worker_connections 1024;
use epoll;
}
http {
include mime.types;
default_type application/octet-stream;
access_log logs/nginx-http_access.log;
error_log logs/nginx-http_error.log debug;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 10;
send_timeout 10;
client_body_timeout 10;
client_header_timeout 10;
sendfile on;
client_header_buffer_size 1k;
large_client_header_buffers 2 4k;
client_max_body_size 1k;
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 %>;
server {
listen 9999;
server_name localhost;
location /pub {
# activate publisher mode for this location
push_stream_publisher;
# 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 %>";
# max messages to store in memory
push_stream_max_message_buffer_length <%= @max_message_buffer_length.nil? ? 20 : @max_message_buffer_length %>;
# min messages to store in memory
push_stream_min_message_buffer_length <%= @min_message_buffer_length.nil? ? 0 : @min_message_buffer_length %>;
# message ttl
push_stream_min_message_buffer_timeout 50m;
# 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;
}
location ~ /sub/(.*) {
# activate subscriber mode for this location
push_stream_subscriber;
# positional channel path
set $push_stream_channels_path $1;
# 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 %>";
# message template
push_stream_message_template "<%= @message_template.nil? ? '<script>p(~id~,\'~channel~\',\'~text~\');</script>' : @message_template %>";
# content-type
push_stream_content_type "text/html; charset=utf-8";
# subscriber may create channels on demand or only authorized
# (publisher) may do it?
push_stream_authorized_channels_only off;
# ping frequency
push_stream_ping_message_interval <%= @ping_message_interval.nil? ? '10s' : @ping_message_interval %>;
# disconnection candidates test frequency
push_stream_subscriber_disconnect_interval 1s;
# connection ttl to enable recycle
push_stream_subscriber_connection_timeout <%= @subscriber_connection_timeout.nil? ? '0s' : @subscriber_connection_timeout %>;
# solving some leakage problem with persitent connections in
# Nginx's chunked filter (ngx_http_chunked_filter_module.c)
chunked_transfer_encoding off;
}
}
}
}
@@mime_tipes_template = %q{
types {
text/html html htm shtml;
text/css css;
text/xml xml;
image/gif gif;
image/jpeg jpeg jpg;
application/x-javascript js;
application/atom+xml atom;
application/rss+xml rss;
text/mathml mml;
text/plain txt;
text/vnd.sun.j2me.app-descriptor jad;
text/vnd.wap.wml wml;
text/x-component htc;
image/png png;
image/tiff tif tiff;
image/vnd.wap.wbmp wbmp;
image/x-icon ico;
image/x-jng jng;
image/x-ms-bmp bmp;
image/svg+xml svg;
application/java-archive jar war ear;
application/mac-binhex40 hqx;
application/msword doc;
application/pdf pdf;
application/postscript ps eps ai;
application/rtf rtf;
application/vnd.ms-excel xls;
application/vnd.ms-powerpoint ppt;
application/vnd.wap.wmlc wmlc;
application/vnd.wap.xhtml+xml xhtml;
application/vnd.google-earth.kml+xml kml;
application/vnd.google-earth.kmz kmz;
application/x-cocoa cco;
application/x-java-archive-diff jardiff;
application/x-java-jnlp-file jnlp;
application/x-makeself run;
application/x-perl pl pm;
application/x-pilot prc pdb;
application/x-rar-compressed rar;
application/x-redhat-package-manager rpm;
application/x-sea sea;
application/x-shockwave-flash swf;
application/x-stuffit sit;
application/x-tcl tcl tk;
application/x-x509-ca-cert der pem crt;
application/x-xpinstall xpi;
application/zip zip;
application/octet-stream bin exe dll;
application/octet-stream deb;
application/octet-stream dmg;
application/octet-stream eot;
application/octet-stream iso img;
application/octet-stream msi msp msm;
audio/midi mid midi kar;
audio/mpeg mp3;
audio/x-realaudio ra;
video/3gpp 3gpp 3gp;
video/mpeg mpeg mpg;
video/quicktime mov;
video/x-flv flv;
video/x-mng mng;
video/x-ms-asf asx asf;
video/x-ms-wmv wmv;
video/x-msvideo avi;
}
}
end
require 'test/unit'
require File.expand_path('test_publish_messages', File.dirname(__FILE__))
require File.expand_path('test_subscriber_connection_cleanup', File.dirname(__FILE__))
require File.expand_path('test_setup_parameters', File.dirname(__FILE__))
require File.expand_path('test_create_many_channels', File.dirname(__FILE__))
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"
@max_reserved_memory = "256m"
end
def test_create_many_channels
headers = {'accept' => 'application/json'}
body = 'channel started'
channels_to_be_created = 200
channels_callback = 0;
EventMachine.run {
i = 0
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.callback {
channels_callback += 1
}
pub.errback { |error|
fail("Erro inexperado na execucao do teste: #{error.last_effective_url.nil? ? "" : error.last_effective_url.request_uri} #{error.response}")
}
else
EventMachine.stop
end
end
}
end
end
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 = ""
@message_template = "~text~"
end
def test_publish_messages
headers = {'accept' => 'text/html'}
body = 'published unique message'
channel = 'ch1'
EventMachine.run {
sub = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers
sub.stream { | chunk |
assert_equal(body + "\r\n", chunk, "The published message was not received correctly")
EventMachine.stop
}
sub.errback { |error|
fail("Erro inexperado na execucao do teste: #{error.last_effective_url.nil? ? "" : error.last_effective_url.request_uri} #{error.response}")
}
pub = EventMachine::HttpRequest.new(nginx_address + '/pub?id=' + channel.to_s ).post :head => headers, :body => body, :timeout => 30
pub.errback { |error|
fail("Erro inexperado na execucao do teste: #{error.last_effective_url.nil? ? "" : error.last_effective_url.request_uri} #{error.response}")
}
}
end
def test_publish_many_messages_in_the_same_channel
headers = {'accept' => 'text/html'}
body_prefix = 'published message '
channel = 'ch2'
messagens_to_publish = 400
recieved_messages = 0
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
}
if chunk.include?(body_prefix + messagens_to_publish.to_s + "\r\n")
EventMachine.stop
end
}
sub.callback {
assert_equal(messagens_to_publish, recieved_messages, "The published messages was not received correctly")
}
sub.errback { |error|
fail("Erro inexperado na execucao do teste: #{error.last_effective_url.nil? ? "" : error.last_effective_url.request_uri} #{error.response}")
}
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.errback { |error|
fail("Erro inexperado na execucao do teste: #{error.last_effective_url.nil? ? "" : error.last_effective_url.request_uri} #{error.response}")
}
end
end
}
end
end
require 'rubygems'
require 'popen4'
require 'test/unit'
require File.expand_path('base_test_case', File.dirname(__FILE__))
class TestSetuParameters < Test::Unit::TestCase
include BaseTestCase
def test_min_buffer_messages_greater_them_max_buffer_messages
expected_error_message = "push_stream_max_message_buffer_length cannot be smaller than push_stream_min_message_buffer_length"
@test_config_file = "test_min_buffer_messages_greater_them_max_buffer_messages.conf"
@max_message_buffer_length = 20
@min_message_buffer_length = 21
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 '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 initialize(opts)
super(opts)
@test_config_file = "test_subscriber_connection_cleanup.conf"
@subscriber_connection_timeout = "37s"
@header_template = "HEADER_TEMPLATE"
@ping_message_interval = "0s"
end
def test_subscriber_connection_cleanup
channel = 'ch1'
headers = {'accept' => 'text/html'}
start = Time.now
receivedHeaderTemplate = false
EventMachine.run {
http = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers, :timeout => 60
http.stream { |chunk|
assert(chunk.include?(@header_template), "Didn't received header template")
}
http.callback {
stop = Time.now
elapsed = time_diff_sec(start, stop)
assert(elapsed >= 37 && elapsed <= 38.5, "Disconnect was in #{elapsed} seconds")
EventMachine.stop
}
http.errback { |error|
fail("Erro inexperado na execucao do teste: #{error.last_effective_url.nil? ? "" : error.last_effective_url.request_uri} #{error.response}")
}
}
end
end
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