eventmachine 0.12.6-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (136) hide show
  1. data/.gitignore +13 -0
  2. data/Rakefile +262 -0
  3. data/docs/COPYING +60 -0
  4. data/docs/ChangeLog +211 -0
  5. data/docs/DEFERRABLES +138 -0
  6. data/docs/EPOLL +141 -0
  7. data/docs/GNU +281 -0
  8. data/docs/INSTALL +15 -0
  9. data/docs/KEYBOARD +38 -0
  10. data/docs/LEGAL +25 -0
  11. data/docs/LIGHTWEIGHT_CONCURRENCY +72 -0
  12. data/docs/PURE_RUBY +77 -0
  13. data/docs/README +74 -0
  14. data/docs/RELEASE_NOTES +96 -0
  15. data/docs/SMTP +9 -0
  16. data/docs/SPAWNED_PROCESSES +93 -0
  17. data/docs/TODO +10 -0
  18. data/eventmachine.gemspec +32 -0
  19. data/ext/binder.cpp +126 -0
  20. data/ext/binder.h +48 -0
  21. data/ext/cmain.cpp +586 -0
  22. data/ext/cplusplus.cpp +193 -0
  23. data/ext/ed.cpp +1522 -0
  24. data/ext/ed.h +380 -0
  25. data/ext/em.cpp +1937 -0
  26. data/ext/em.h +186 -0
  27. data/ext/emwin.cpp +300 -0
  28. data/ext/emwin.h +94 -0
  29. data/ext/epoll.cpp +26 -0
  30. data/ext/epoll.h +25 -0
  31. data/ext/eventmachine.h +98 -0
  32. data/ext/eventmachine_cpp.h +95 -0
  33. data/ext/extconf.rb +129 -0
  34. data/ext/fastfilereader/extconf.rb +77 -0
  35. data/ext/fastfilereader/mapper.cpp +214 -0
  36. data/ext/fastfilereader/mapper.h +59 -0
  37. data/ext/fastfilereader/rubymain.cpp +127 -0
  38. data/ext/files.cpp +94 -0
  39. data/ext/files.h +65 -0
  40. data/ext/kb.cpp +82 -0
  41. data/ext/page.cpp +107 -0
  42. data/ext/page.h +51 -0
  43. data/ext/pipe.cpp +351 -0
  44. data/ext/project.h +119 -0
  45. data/ext/rubymain.cpp +847 -0
  46. data/ext/sigs.cpp +89 -0
  47. data/ext/sigs.h +32 -0
  48. data/ext/ssl.cpp +423 -0
  49. data/ext/ssl.h +90 -0
  50. data/java/.classpath +8 -0
  51. data/java/.project +17 -0
  52. data/java/src/com/rubyeventmachine/Application.java +196 -0
  53. data/java/src/com/rubyeventmachine/Connection.java +74 -0
  54. data/java/src/com/rubyeventmachine/ConnectionFactory.java +37 -0
  55. data/java/src/com/rubyeventmachine/DefaultConnectionFactory.java +46 -0
  56. data/java/src/com/rubyeventmachine/EmReactor.java +408 -0
  57. data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
  58. data/java/src/com/rubyeventmachine/EventableChannel.java +57 -0
  59. data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +171 -0
  60. data/java/src/com/rubyeventmachine/EventableSocketChannel.java +244 -0
  61. data/java/src/com/rubyeventmachine/PeriodicTimer.java +38 -0
  62. data/java/src/com/rubyeventmachine/Timer.java +54 -0
  63. data/java/src/com/rubyeventmachine/tests/ApplicationTest.java +108 -0
  64. data/java/src/com/rubyeventmachine/tests/ConnectTest.java +124 -0
  65. data/java/src/com/rubyeventmachine/tests/EMTest.java +80 -0
  66. data/java/src/com/rubyeventmachine/tests/TestDatagrams.java +53 -0
  67. data/java/src/com/rubyeventmachine/tests/TestServers.java +74 -0
  68. data/java/src/com/rubyeventmachine/tests/TestTimers.java +89 -0
  69. data/lib/em/deferrable.rb +208 -0
  70. data/lib/em/eventable.rb +39 -0
  71. data/lib/em/future.rb +62 -0
  72. data/lib/em/messages.rb +66 -0
  73. data/lib/em/processes.rb +113 -0
  74. data/lib/em/spawnable.rb +88 -0
  75. data/lib/em/streamer.rb +112 -0
  76. data/lib/eventmachine.rb +1926 -0
  77. data/lib/eventmachine_version.rb +31 -0
  78. data/lib/evma.rb +32 -0
  79. data/lib/evma/callback.rb +32 -0
  80. data/lib/evma/container.rb +75 -0
  81. data/lib/evma/factory.rb +77 -0
  82. data/lib/evma/protocol.rb +87 -0
  83. data/lib/evma/reactor.rb +48 -0
  84. data/lib/jeventmachine.rb +137 -0
  85. data/lib/pr_eventmachine.rb +1011 -0
  86. data/lib/protocols/buftok.rb +127 -0
  87. data/lib/protocols/header_and_content.rb +129 -0
  88. data/lib/protocols/httpcli2.rb +803 -0
  89. data/lib/protocols/httpclient.rb +270 -0
  90. data/lib/protocols/line_and_text.rb +126 -0
  91. data/lib/protocols/linetext2.rb +161 -0
  92. data/lib/protocols/memcache.rb +293 -0
  93. data/lib/protocols/postgres.rb +261 -0
  94. data/lib/protocols/saslauth.rb +179 -0
  95. data/lib/protocols/smtpclient.rb +308 -0
  96. data/lib/protocols/smtpserver.rb +556 -0
  97. data/lib/protocols/stomp.rb +153 -0
  98. data/lib/protocols/tcptest.rb +57 -0
  99. data/setup.rb +1585 -0
  100. data/tasks/cpp.rake +77 -0
  101. data/tasks/project.rake +78 -0
  102. data/tasks/tests.rake +193 -0
  103. data/tests/test_attach.rb +83 -0
  104. data/tests/test_basic.rb +231 -0
  105. data/tests/test_connection_count.rb +45 -0
  106. data/tests/test_defer.rb +47 -0
  107. data/tests/test_epoll.rb +163 -0
  108. data/tests/test_error_handler.rb +35 -0
  109. data/tests/test_errors.rb +82 -0
  110. data/tests/test_eventables.rb +77 -0
  111. data/tests/test_exc.rb +58 -0
  112. data/tests/test_futures.rb +214 -0
  113. data/tests/test_handler_check.rb +37 -0
  114. data/tests/test_hc.rb +218 -0
  115. data/tests/test_httpclient.rb +215 -0
  116. data/tests/test_httpclient2.rb +155 -0
  117. data/tests/test_kb.rb +61 -0
  118. data/tests/test_ltp.rb +188 -0
  119. data/tests/test_ltp2.rb +320 -0
  120. data/tests/test_next_tick.rb +109 -0
  121. data/tests/test_processes.rb +95 -0
  122. data/tests/test_pure.rb +129 -0
  123. data/tests/test_running.rb +47 -0
  124. data/tests/test_sasl.rb +74 -0
  125. data/tests/test_send_file.rb +243 -0
  126. data/tests/test_servers.rb +80 -0
  127. data/tests/test_smtpclient.rb +83 -0
  128. data/tests/test_smtpserver.rb +93 -0
  129. data/tests/test_spawn.rb +329 -0
  130. data/tests/test_ssl_args.rb +68 -0
  131. data/tests/test_ssl_methods.rb +50 -0
  132. data/tests/test_timers.rb +148 -0
  133. data/tests/test_ud.rb +43 -0
  134. data/tests/testem.rb +31 -0
  135. data/web/whatis +7 -0
  136. metadata +214 -0
@@ -0,0 +1,77 @@
1
+ # EventMachine C++ Rakefile Stab Case
2
+ # TODO : track header files as a build dependency...
3
+ # TODO : cross platform support
4
+ # TODO : configure style functionality
5
+ namespace :cpp do
6
+
7
+ require 'rake/clean'
8
+
9
+ # *nix only atm...
10
+ module Cpp
11
+ class <<self
12
+ def cpp; "g++"; end
13
+ def archive; "ar"; end
14
+ def compile file, output, includes=nil, flags=nil
15
+ sh %{#{cpp} #{file} #{includes} #{flags} -c -o #{output}}
16
+ end
17
+ def link file, output, libs=nil, flags=nil
18
+ sh %{#{cpp} #{file} #{libs} #{flags} -o #{output}}
19
+ end
20
+ def static output, files
21
+ sh %{#{archive} cr #{output} #{files}}
22
+ end
23
+ end
24
+ end
25
+
26
+ module EmConfig
27
+ Path = ENV['EVENTMACHINE_SOURCE'] || 'ext'
28
+ Sources = FileList["#{Path}/*.cpp"]
29
+ Sources.delete_if { |s| /ruby/ =~ s }
30
+ Compiled = Sources.sub(%r{^#{Path}/(.*)\.cpp}, "#{Path}/\\1.o")
31
+
32
+ Flags = "-O2 -pipe -fno-common -DOS_UNIX -DWITHOUT_SSL"
33
+ Includes = ""
34
+ Libs = ''
35
+ end
36
+ CLEAN.include(EmConfig::Compiled)
37
+
38
+ rule %r{^#{EmConfig::Path}/.*\.o$} => [proc { |targ|
39
+ targ.sub(%r{^#{EmConfig::Path}/(.*)\.o$}, "#{EmConfig::Path}/\\1.cpp")
40
+ }] do |t|
41
+ Cpp.compile t.source, t.name, EmConfig::Includes, EmConfig::Flags
42
+ end
43
+
44
+ file "#{EmConfig::Path}/libeventmachine.a" => EmConfig::Compiled do |t|
45
+ Cpp.static t.name, EmConfig::Compiled
46
+ end
47
+ CLEAN.include("#{EmConfig::Path}/libeventmachine.a")
48
+
49
+ module AppConfig
50
+ Appname = 'echo_em'
51
+ Sources = FileList['*.cpp']
52
+ Compiled = Sources.sub(%r{^(.*)\.cpp}, '\\1.o')
53
+
54
+ Flags = ["", EmConfig::Flags].join(' ')
55
+ Includes = ["-I. -I#{EmConfig::Path}", EmConfig::Includes].join(' ')
56
+ Libs = ["-L#{EmConfig::Path} -leventmachine", EmConfig::Libs].join(' ')
57
+ end
58
+ CLEAN.include(AppConfig::Compiled)
59
+ CLEAN.include(AppConfig::Appname)
60
+
61
+ rule %r{^.*\.o$} => [proc { |targ|
62
+ targ.sub(%r{^(.*)\.o$}, '\\1.cpp')
63
+ }] do |t|
64
+ Cpp.compile t.source, t.name, AppConfig::Includes, AppConfig::Flags
65
+ end
66
+
67
+ file AppConfig::Appname => ["#{EmConfig::Path}/libeventmachine.a", AppConfig::Compiled] do |t|
68
+ Cpp.link AppConfig::Compiled, t.name, AppConfig::Libs, AppConfig::Flags
69
+ end
70
+
71
+ task :build => AppConfig::Appname
72
+
73
+ task :run => AppConfig::Appname do
74
+ sh "./#{AppConfig::Appname}"
75
+ end
76
+
77
+ end
@@ -0,0 +1,78 @@
1
+ require 'rake/gempackagetask'
2
+ require 'rake/rdoctask'
3
+ require 'rake/clean'
4
+
5
+ # monkey bitchin' for windows stuffs...
6
+ module FileUtils
7
+ # If any of these methods ever clobber, try removing them.
8
+ # Hopefully they'll do something semantically similar.
9
+ abort "Err: #{__FILE__}:#{__LINE__} monkey patch windows? clobbers!" unless instance_methods.grep(/windows\?/).empty?
10
+ abort "Err: #{__FILE__}:#{__LINE__} monkey patch sudo clobbers!" unless instance_methods.grep(/sudo/).empty?
11
+ abort "Err: #{__FILE__}:#{__LINE__} monkey patch gem_cmd clobbers!" unless instance_methods.grep(/gem_cmd/).empty?
12
+ def windows?; RUBY_PLATFORM =~ /mswin|mingw/; end
13
+ def sudo(cmd)
14
+ if windows? || (require 'etc'; Etc.getpwuid.uid == 0)
15
+ sh cmd
16
+ else
17
+ sh "sudo #{cmd}"
18
+ end
19
+ end
20
+ def gem_cmd(action, name, *args)
21
+ rb = Gem.ruby rescue nil
22
+ rb ||= (require 'rbconfig'; File.join(Config::CONFIG['bindir'], Config::CONFIG['ruby_install_name']))
23
+ sudo "#{rb} -r rubygems -e 'require %{rubygems/gem_runner}; Gem::GemRunner.new.run(%w{#{action} #{name} #{args.join(' ')}})'"
24
+ end
25
+ end
26
+
27
+
28
+ # Setup our packaging tasks, we're just jacking the builtins.
29
+ Rake::GemPackageTask.new(Spec) do |pkg|
30
+ pkg.need_tar, pkg.need_tar_gz, pkg.need_zip = true, true, true if Package
31
+ pkg.gem_spec = Spec
32
+ end
33
+ Rake::Task[:clean].enhance [:clobber_package]
34
+
35
+ # Only generate rdoc if the spec says so, again, jack the builtins.
36
+ if Spec.has_rdoc
37
+ Rake::RDocTask.new do |rd|
38
+ rd.title = Spec.name
39
+ rd.rdoc_dir = 'rdoc'
40
+ rd.main = "docs/README" if test ?e, "docs/README"
41
+ rd.rdoc_files.include("lib/**/*.rb", *Spec.extra_rdoc_files)
42
+ end
43
+ Rake::Task[:clean].enhance [:clobber_rdoc]
44
+
45
+ desc 'Generate and open documentation'
46
+ task :docs => :rdoc do
47
+ case RUBY_PLATFORM
48
+ when /darwin/ ; sh 'open rdoc/index.html'
49
+ when /mswin|mingw/ ; sh 'start rdoc\index.html'
50
+ else
51
+ sh 'firefox rdoc/index.html'
52
+ end
53
+ end
54
+ end
55
+
56
+ if Spec.default_executable
57
+ desc "Run #{Spec.default_executable}"
58
+ task :run do ruby File.join(Spec.bindir, Spec.default_executable) end
59
+ end
60
+
61
+ require 'rubygems'
62
+
63
+ desc 'Install gem (and sudo if required)'
64
+ task :install => :package do
65
+ gem_cmd(:install, "pkg/#{Spec.name}-#{Spec.version}.gem")
66
+ end
67
+
68
+ desc 'Uninstall gem (and sudo if required)'
69
+ task :uninstall do
70
+ gem_cmd(:uninstall, "#{Spec.name}", "-v=#{Spec.version}")
71
+ end
72
+
73
+ # Find an scm's store directory, if we do, make a task to commit to it only
74
+ # after running all the tests (successfully).
75
+ if scm = %w(git svn bzr hg).find { |d| File.directory? ".#{d}" }
76
+ desc "Run tests then commit to #{scm}"
77
+ task :commit => :test do sh "#{scm} commit" end
78
+ end
@@ -0,0 +1,193 @@
1
+ # This is used by several rake tasks, that parameterize the
2
+ # behavior so we can use the same tests to test both the
3
+ # extension and non-extension versions.
4
+ def run_tests t, libr = :cascade, test_files="test_*.rb"
5
+ require 'test/unit/testsuite'
6
+ require 'test/unit/ui/console/testrunner'
7
+ require 'tests/testem'
8
+
9
+ base_dir = File.expand_path(File.dirname(__FILE__) + '/../') + '/'
10
+
11
+ runner = Test::Unit::UI::Console::TestRunner
12
+
13
+ $eventmachine_library = libr
14
+ EmTestRunner.run(test_files)
15
+
16
+ suite = Test::Unit::TestSuite.new($name)
17
+
18
+ ObjectSpace.each_object(Class) do |testcase|
19
+ suite << testcase.suite if testcase < Test::Unit::TestCase
20
+ end
21
+
22
+ runner.run(suite)
23
+ end
24
+
25
+ desc "Run tests for #{Spec.name}."
26
+ task :test do |t|
27
+ # run_tests t
28
+ # Rake +/ friends leave threads, etc, less stable test runs.
29
+ ruby "-Ilib -Iext -Iext/fastfilereader -Ijava tests/testem.rb #{'-v' if ENV['VERBOSE']}"
30
+ end
31
+
32
+ namespace :test do
33
+ desc "Run tests for #{Spec.name}."
34
+ task :partial do |t|
35
+ lib = RUBY_PLATFORM =~ /java/ ? :java : :extension
36
+ run_tests t, lib, [
37
+ "test_basic.rb",
38
+ "test_epoll.rb",
39
+ "test_errors.rb",
40
+ "test_error_handler.rb",
41
+ "test_eventables.rb",
42
+ "test_exc.rb",
43
+ "test_futures.rb",
44
+ "test_hc.rb",
45
+ "test_httpclient2.rb",
46
+ "test_httpclient.rb",
47
+ "test_kb.rb",
48
+ "test_ltp2.rb",
49
+ "test_ltp.rb",
50
+ "test_next_tick.rb",
51
+ "test_processes.rb",
52
+ "test_pure.rb",
53
+ "test_running.rb",
54
+ "test_sasl.rb",
55
+ "test_send_file.rb",
56
+ "test_servers.rb",
57
+ "test_smtpclient.rb",
58
+ "test_smtpserver.rb",
59
+ "test_spawn.rb",
60
+ "test_timers.rb",
61
+ "test_ud.rb",
62
+ ].map { |tf| "tests/#{tf}" }
63
+ end
64
+
65
+ desc "Run java tests for #$name."
66
+ task :testjava do |t|
67
+ run_tests t, :java
68
+ end
69
+
70
+ desc "Run pure-ruby tests for #$name."
71
+ task :testpr do |t|
72
+ run_tests t, :pure_ruby
73
+ end
74
+
75
+ desc "Run extension tests for #$name."
76
+ task :testext do |t|
77
+ run_tests t, :extension
78
+ end
79
+
80
+ desc "PROVISIONAL: run tests for user-defined events"
81
+ task :ud do |t|
82
+ run_tests t, :extension, "test_ud.rb"
83
+ end
84
+
85
+ desc "PROVISIONAL: run tests for line/text protocol handler"
86
+ task :ltp do |t|
87
+ run_tests t, :extension, "test_ltp*.rb"
88
+ end
89
+
90
+ desc "PROVISIONAL: run tests for header/content protocol handler"
91
+ task :hc do |t|
92
+ run_tests t, :extension, "test_hc.rb"
93
+ end
94
+
95
+ desc "PROVISIONAL: run tests for exceptions"
96
+ task :exc do |t|
97
+ run_tests t, :extension, "test_exc.rb"
98
+ end
99
+
100
+ desc "Test protocol handlers"
101
+ task :protocols => [ :hc, :ltp ]
102
+
103
+
104
+ desc "Test HTTP client"
105
+ task :httpclient do |t|
106
+ run_tests t, :extension, "test_httpclient.rb"
107
+ end
108
+
109
+ desc "Test HTTP client2"
110
+ task :httpclient2 do |t|
111
+ run_tests t, :extension, "test_httpclient2.rb"
112
+ end
113
+
114
+ desc "Test futures"
115
+ task :futures do |t|
116
+ run_tests t, :extension, "test_future*.rb"
117
+ end
118
+
119
+ desc "Test Timers"
120
+ task :timers do |t|
121
+ run_tests t, :extension, "test_timer*.rb"
122
+ end
123
+
124
+ desc "Test Next Tick"
125
+ task :next_tick do |t|
126
+ run_tests t, :extension, "test_next_tick*.rb"
127
+ end
128
+
129
+ desc "Test Epoll"
130
+ task :epoll do |t|
131
+ run_tests t, :extension, "test_epoll*.rb"
132
+ end
133
+
134
+ desc "Test Servers"
135
+ task :servers do |t|
136
+ run_tests t, :extension, "test_servers*.rb"
137
+ end
138
+
139
+ desc "Test Basic"
140
+ task :basic do |t|
141
+ run_tests t, :extension, "test_basic*.rb"
142
+ end
143
+
144
+ desc "Test Send File"
145
+ task :send_file do |t|
146
+ run_tests t, :extension, "test_send_file*.rb"
147
+ end
148
+
149
+ desc "Test Running"
150
+ task :running do |t|
151
+ run_tests t, :extension, "test_running*.rb"
152
+ end
153
+
154
+ desc "Test Keyboard Events"
155
+ task :keyboard do |t|
156
+ run_tests t, :extension, "test_kb*.rb"
157
+ end
158
+
159
+ desc "Test Spawn"
160
+ task :spawn do |t|
161
+ run_tests t, :extension, "test_spawn*.rb"
162
+ end
163
+
164
+ desc "Test SMTP"
165
+ task :smtp do |t|
166
+ run_tests t, :extension, "test_smtp*.rb"
167
+ end
168
+
169
+ desc "Test Errors"
170
+ task :errors do |t|
171
+ run_tests t, :extension, "test_errors*.rb"
172
+ end
173
+
174
+ desc "Test Pure Ruby"
175
+ task :pure do |t|
176
+ run_tests t, :extension, "test_pure*.rb"
177
+ end
178
+
179
+ desc "Test Processes"
180
+ task :processes do |t|
181
+ run_tests t, :extension, "test_process*.rb"
182
+ end
183
+
184
+ desc "Test SASL"
185
+ task :sasl do |t|
186
+ run_tests t, :java, "test_sasl*.rb"
187
+ end
188
+
189
+ desc "Test Attach"
190
+ task :attach do |t|
191
+ run_tests t, :extension, "test_attach*.rb"
192
+ end
193
+ end
@@ -0,0 +1,83 @@
1
+ # $Id$
2
+ #
3
+ #----------------------------------------------------------------------------
4
+ #
5
+ # Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
6
+ # Gmail: blackhedd
7
+ #
8
+ # This program is free software; you can redistribute it and/or modify
9
+ # it under the terms of either: 1) the GNU General Public License
10
+ # as published by the Free Software Foundation; either version 2 of the
11
+ # License, or (at your option) any later version; or 2) Ruby's License.
12
+ #
13
+ # See the file COPYING for complete licensing information.
14
+ #
15
+ #---------------------------------------------------------------------------
16
+ #
17
+
18
+ $:.unshift "../lib"
19
+ require 'eventmachine'
20
+ require 'socket'
21
+ require 'test/unit'
22
+
23
+
24
+ class TestAttach < Test::Unit::TestCase
25
+
26
+ Host = "127.0.0.1"
27
+ Port = 9550
28
+
29
+ class EchoServer < EM::Connection
30
+ def receive_data data
31
+ send_data data
32
+ end
33
+ end
34
+
35
+ class EchoClient < EM::Connection
36
+ def initialize
37
+ $sock.write("abc\n")
38
+ end
39
+
40
+ def notify_readable
41
+ $read = $sock.readline
42
+ $fd = detach
43
+ end
44
+
45
+ def unbind
46
+ EM.next_tick do
47
+ $sock.write("def\n")
48
+ EM.add_timer(0.5){ EM.stop }
49
+ end
50
+ end
51
+ end
52
+
53
+ def test_attach
54
+ EM.run{
55
+ EM.start_server Host, Port, EchoServer
56
+ $sock = TCPSocket.new Host, Port
57
+ EM.attach $sock, EchoClient
58
+ }
59
+
60
+ assert_equal $read, "abc\n"
61
+ assert_equal $fd, $sock.fileno
62
+ assert_equal false, $sock.closed?
63
+ assert_equal $sock.readline, "def\n"
64
+ end
65
+
66
+
67
+ module PipeWatch
68
+ def notify_readable
69
+ $read = $r.readline
70
+ EM.stop
71
+ end
72
+ end
73
+
74
+ def test_attach_pipe
75
+ EM.run{
76
+ $r, $w = IO.pipe
77
+ EM.attach $r, PipeWatch
78
+ $w.write("ghi\n")
79
+ }
80
+
81
+ assert_equal $read, "ghi\n"
82
+ end
83
+ end
@@ -0,0 +1,231 @@
1
+ # $Id$
2
+ #
3
+ # Author:: Francis Cianfrocca (gmail: blackhedd)
4
+ # Homepage:: http://rubyeventmachine.com
5
+ # Date:: 8 April 2006
6
+ #
7
+ # See EventMachine and EventMachine::Connection for documentation and
8
+ # usage examples.
9
+ #
10
+ #----------------------------------------------------------------------------
11
+ #
12
+ # Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
13
+ # Gmail: blackhedd
14
+ #
15
+ # This program is free software; you can redistribute it and/or modify
16
+ # it under the terms of either: 1) the GNU General Public License
17
+ # as published by the Free Software Foundation; either version 2 of the
18
+ # License, or (at your option) any later version; or 2) Ruby's License.
19
+ #
20
+ # See the file COPYING for complete licensing information.
21
+ #
22
+ #---------------------------------------------------------------------------
23
+ #
24
+ #
25
+ #
26
+
27
+ $:.unshift File.expand_path(File.dirname(__FILE__) + "/../lib")
28
+ require 'eventmachine'
29
+ require 'test/unit'
30
+
31
+ class TestBasic < Test::Unit::TestCase
32
+
33
+ def setup
34
+ assert(!EM.reactor_running?)
35
+ end
36
+
37
+ def teardown
38
+ assert(!EM.reactor_running?)
39
+ end
40
+
41
+ #-------------------------------------
42
+
43
+ def test_libtype
44
+ lt = EventMachine.library_type
45
+ em_lib = (ENV["EVENTMACHINE_LIBRARY"] || $eventmachine_library || :xxx).to_sym
46
+
47
+ # Running from test runner, under jruby.
48
+ if RUBY_PLATFORM == 'java'
49
+ unless em_lib == :pure_ruby
50
+ assert_equal( :java, lt )
51
+ return
52
+ end
53
+ end
54
+
55
+ case em_lib
56
+ when :pure_ruby
57
+ assert_equal( :pure_ruby, lt )
58
+ when :extension
59
+ assert_equal( :extension, lt )
60
+ when :java
61
+ assert_equal( :java, lt )
62
+ else
63
+ # Running from jruby as a standalone test.
64
+ if RUBY_PLATFORM == 'java'
65
+ assert_equal( :java, lt )
66
+ else
67
+ assert_equal( :extension, lt )
68
+ end
69
+ end
70
+ end
71
+
72
+ #-------------------------------------
73
+
74
+
75
+ def test_em
76
+ EventMachine.run {
77
+ EventMachine.add_timer 0 do
78
+ EventMachine.stop
79
+ end
80
+ }
81
+ end
82
+
83
+ #-------------------------------------
84
+
85
+ def test_timer
86
+ n = 0
87
+ EventMachine.run {
88
+ EventMachine.add_periodic_timer(0.1) {
89
+ n += 1
90
+ EventMachine.stop if n == 2
91
+ }
92
+ }
93
+ end
94
+
95
+ #-------------------------------------
96
+
97
+ # This test once threw an already-running exception.
98
+ module Trivial
99
+ def post_init
100
+ EventMachine.stop
101
+ end
102
+ end
103
+
104
+ def test_server
105
+ EventMachine.run {
106
+ EventMachine.start_server "localhost", 9000, Trivial
107
+ EventMachine.connect "localhost", 9000
108
+ }
109
+ assert( true ) # make sure it halts
110
+ end
111
+
112
+ #--------------------------------------
113
+
114
+ # EventMachine#run_block starts the reactor loop, runs the supplied block, and then STOPS
115
+ # the loop automatically. Contrast with EventMachine#run, which keeps running the reactor
116
+ # even after the supplied block completes.
117
+ def test_run_block
118
+ assert !EM.reactor_running?
119
+ a = nil
120
+ EM.run_block { a = "Worked" }
121
+ assert a
122
+ assert !EM.reactor_running?
123
+ end
124
+
125
+
126
+ #--------------------------------------
127
+
128
+ # TODO! This is an unfinished edge case.
129
+ # EM mishandles uncaught Ruby exceptions that fire from within #unbind handlers.
130
+ # A uncaught Ruby exception results in a call to EM::release_machine (which is in an ensure
131
+ # block in EM::run). But if EM is processing an unbind request, the release_machine call
132
+ # will cause a segmentation fault.
133
+ #
134
+
135
+ TestHost = "127.0.0.1"
136
+ TestPort = 9070
137
+
138
+ class UnbindError < EM::Connection
139
+ def initialize *args
140
+ super
141
+ end
142
+ def connection_completed
143
+ close_connection_after_writing
144
+ end
145
+ def unbind
146
+ raise "Blooey"
147
+ end
148
+ end
149
+
150
+ def xxx_test_unbind_error
151
+ assert_raises( RuntimeError ) {
152
+ EM.run {
153
+ EM.start_server TestHost, TestPort
154
+ EM.connect TestHost, TestPort, UnbindError
155
+ }
156
+ }
157
+ end
158
+
159
+ #------------------------------------
160
+ #
161
+ # TODO. This is an unfinished bug fix.
162
+ # This case was originally reported by Dan Aquino. If you throw a Ruby exception
163
+ # in a post_init handler, it gets rethrown as a confusing reactor exception.
164
+ # The problem is in eventmachine.rb, which calls post_init within the private
165
+ # initialize method of the EM::Connection class. This happens in both the EM::connect
166
+ # method and in the code that responds to connection-accepted events.
167
+ # What happens is that we instantiate the new connection object, which calls
168
+ # initialize, and then after initialize returns, we stick the new connection object
169
+ # into EM's @conns hashtable.
170
+ # But the problem is that Connection::initialize calls #post_init before it returns,
171
+ # and this may be user-written code that may throw an uncaught Ruby exception.
172
+ # If that happens, the reactor will abort, and it will then try to run down open
173
+ # connections. Because @conns never got a chance to properly reflect the new connection
174
+ # (because initialize never returned), we throw a ConnectionNotBound error
175
+ # (eventmachine.rb line 1080).
176
+ # When the bug is fixed, activate this test case.
177
+ #
178
+
179
+ class PostInitError < EM::Connection
180
+ def post_init
181
+ aaa bbb # should produce a Ruby exception
182
+ end
183
+ end
184
+ # This test causes issues, the machine becomes unreleasable after
185
+ # release_machine suffers an exception in event_callback.
186
+ def xxx_test_post_init_error
187
+ assert_raises( EventMachine::ConnectionNotBound ) {
188
+ EM.run {
189
+ EM::Timer.new(1) {EM.stop}
190
+ EM.start_server TestHost, TestPort
191
+ EM.connect TestHost, TestPort, PostInitError
192
+ }
193
+ }
194
+ EM.run {
195
+ EM.stop
196
+ }
197
+ assert !EM.reactor_running?
198
+ end
199
+
200
+ module BrsTestSrv
201
+ def receive_data data
202
+ $received << data
203
+ end
204
+ def unbind
205
+ EM.stop
206
+ end
207
+ end
208
+ module BrsTestCli
209
+ def post_init
210
+ send_data $sent
211
+ close_connection_after_writing
212
+ end
213
+ end
214
+
215
+ # From ticket #50
216
+ def test_byte_range_send
217
+ $received = ''
218
+ $sent = (0..255).to_a.pack('C*')
219
+ EM::run {
220
+
221
+ EM::start_server TestHost, TestPort, BrsTestSrv
222
+
223
+ EM::connect TestHost, TestPort, BrsTestCli
224
+
225
+ EM::add_timer(0.5) { assert(false, 'test timed out'); EM.stop; Kernel.warn "test timed out!" }
226
+ }
227
+ assert_equal($sent, $received)
228
+ end
229
+
230
+ end
231
+