async-io 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,47 @@
1
+ # Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require 'async/io'
22
+
23
+ RSpec.describe Async::IO::Generic do
24
+ include_context Async::RSpec::Reactor
25
+
26
+ let(:pipe) {IO.pipe}
27
+ let(:input) {Async::IO::Generic.new(pipe.first)}
28
+ let(:output) {Async::IO::Generic.new(pipe.last)}
29
+
30
+ it "should send and receive data within the same reactor" do
31
+ message = nil
32
+
33
+ output_task = reactor.async do
34
+ message = input.read(1024)
35
+ end
36
+
37
+ reactor.async do
38
+ output.write("Hello World")
39
+ end
40
+
41
+ output_task.wait
42
+ expect(message).to be == "Hello World"
43
+
44
+ input.close
45
+ output.close
46
+ end
47
+ end
@@ -0,0 +1,115 @@
1
+ # Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require 'async/io/tcp_socket'
22
+
23
+ RSpec.describe Async::Reactor do
24
+ include_context Async::RSpec::Leaks
25
+
26
+ # Shared port for localhost network tests.
27
+ let(:server_address) {Async::IO::Address.tcp("localhost", 6779)}
28
+ let(:data) {"The quick brown fox jumped over the lazy dog."}
29
+
30
+ around(:each) do |example|
31
+ # Accept a single incoming connection and then finish.
32
+ subject.async do |task|
33
+ Async::IO::Socket.bind(server_address) do |server|
34
+ server.listen(10)
35
+
36
+ server.accept do |peer, address|
37
+ data = peer.read(512)
38
+ peer.write(data)
39
+ end
40
+ end
41
+ end
42
+
43
+ result = example.run
44
+
45
+ if result.is_a? Exception
46
+ result
47
+ else
48
+ subject.run
49
+ end
50
+ end
51
+
52
+ describe 'basic tcp server' do
53
+ it "should start server and send data" do
54
+ subject.async do
55
+ Async::IO::Socket.connect(server_address) do |client|
56
+ client.write(data)
57
+ expect(client.read(512)).to be == data
58
+ end
59
+ end
60
+ end
61
+ end
62
+
63
+ describe 'non-blocking tcp connect' do
64
+ it "should start server and send data" do
65
+ subject.async do |task|
66
+ Async::IO::Socket.connect(server_address) do |client|
67
+ client.write(data)
68
+ expect(client.read(512)).to be == data
69
+ end
70
+ end
71
+ end
72
+
73
+ it "can connect socket and read/write in a different task" do
74
+ socket = nil
75
+
76
+ subject.async do |task|
77
+ socket = Async::IO::Socket.connect(server_address)
78
+
79
+ # Stop the reactor once the connection was made.
80
+ subject.stop
81
+ end
82
+
83
+ subject.run
84
+
85
+ expect(socket).to_not be_nil
86
+ expect(socket).to be_kind_of Async::Wrapper
87
+
88
+ subject.async do
89
+ socket.write(data)
90
+
91
+ expect(socket.read(512)).to be == data
92
+ end
93
+
94
+ subject.run
95
+
96
+ socket.close
97
+ end
98
+
99
+ it "can't use a socket in nested tasks" do
100
+ subject.async do |task|
101
+ socket = Async::IO::Socket.connect(server_address)
102
+ expect(socket).to be_kind_of Async::Wrapper
103
+
104
+ expect do
105
+ subject.async do
106
+ socket.write(data)
107
+ # expect(socket.read(512)).to be == data
108
+ end
109
+ end.to_not raise_error
110
+
111
+ socket.close
112
+ end
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,73 @@
1
+ # Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require 'async/io/udp_socket'
22
+
23
+ RSpec.describe Async::Reactor do
24
+ include_context Async::RSpec::Leaks
25
+
26
+ # Shared port for localhost network tests.
27
+ let(:server_address) {Async::IO::Address.udp("127.0.0.1", 6778)}
28
+ let(:data) {"The quick brown fox jumped over the lazy dog."}
29
+
30
+ describe 'basic udp server' do
31
+ it "should echo data back to peer" do
32
+ subject.async do
33
+ Async::IO::Socket.bind(server_address) do |server|
34
+ packet, address = server.recvfrom(512)
35
+
36
+ server.send(packet, 0, address)
37
+ end
38
+ end
39
+
40
+ subject.async do
41
+ Async::IO::Socket.connect(server_address) do |client|
42
+ client.send(data)
43
+ response = client.recv(512)
44
+
45
+ expect(response).to be == data
46
+ end
47
+ end
48
+
49
+ subject.run
50
+ end
51
+
52
+ it "should use unconnected socket" do
53
+ subject.async do
54
+ Async::IO::Socket.bind(server_address) do |server|
55
+ packet, address = server.recvfrom(512)
56
+
57
+ server.send(packet, 0, address)
58
+ end
59
+ end
60
+
61
+ subject.async do
62
+ Async::IO::UDPSocket.wrap(server_address.afamily) do |client|
63
+ client.send(data, 0, server_address)
64
+ response, address = client.recvfrom(512)
65
+
66
+ expect(response).to be == data
67
+ end
68
+ end
69
+
70
+ subject.run
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,55 @@
1
+ # Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require 'async/io/unix_socket'
22
+
23
+ RSpec.describe Async::Reactor do
24
+ include_context Async::RSpec::Leaks
25
+
26
+ let(:path) {File.join(__dir__, "unix-socket")}
27
+ let(:data) {"The quick brown fox jumped over the lazy dog."}
28
+
29
+ before(:each) do
30
+ FileUtils.rm_f path
31
+ end
32
+
33
+ describe 'basic unix socket' do
34
+ it "should echo data back to peer" do
35
+ subject.async do
36
+ Async::IO::UNIXServer.wrap(path) do |server|
37
+ server.accept do |peer|
38
+ peer.send(peer.recv(512))
39
+ end
40
+ end
41
+ end
42
+
43
+ subject.async do
44
+ Async::IO::UNIXSocket.wrap(path) do |client|
45
+ client.send(data)
46
+ response = client.recv(512)
47
+
48
+ expect(response).to be == data
49
+ end
50
+ end
51
+
52
+ subject.run
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,40 @@
1
+ # Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require 'net/http'
22
+
23
+ require 'async/io/wrap/tcp'
24
+
25
+ RSpec.describe Async::IO::Wrap::TCPSocket do
26
+ include_context Async::RSpec::Reactor
27
+
28
+ before(:all) do
29
+ # This injects the asynchronous sockets into Net::HTTP.
30
+ Net::HTTP.send(:include, Async::IO::Wrap)
31
+ end
32
+
33
+ it "should fetch page" do
34
+ expect(Async::IO::Wrap::TCPSocket).to receive(:new).and_call_original
35
+
36
+ expect do
37
+ Net::HTTP.get_response('www.google.com', '/')
38
+ end.to_not raise_error
39
+ end
40
+ end
@@ -0,0 +1,32 @@
1
+
2
+ if ENV['COVERAGE'] || ENV['TRAVIS']
3
+ begin
4
+ require 'simplecov'
5
+
6
+ SimpleCov.start do
7
+ add_filter "/spec/"
8
+ end
9
+
10
+ if ENV['TRAVIS']
11
+ require 'coveralls'
12
+ Coveralls.wear!
13
+ end
14
+ rescue LoadError
15
+ warn "Could not load simplecov: #{$!}"
16
+ end
17
+ end
18
+
19
+ require "bundler/setup"
20
+ require "async/io"
21
+
22
+ # Shared rspec helpers:
23
+ require "async/rspec"
24
+
25
+ RSpec.configure do |config|
26
+ # Enable flags like --only-failures and --next-failure
27
+ config.example_status_persistence_file_path = ".rspec_status"
28
+
29
+ config.expect_with :rspec do |c|
30
+ c.syntax = :expect
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: async-io
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Samuel Williams
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-05-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: async
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.14'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: async-rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.13'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.13'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description:
84
+ email:
85
+ - samuel.williams@oriontransfer.co.nz
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - README.md
95
+ - Rakefile
96
+ - async-io.gemspec
97
+ - lib/async/io.rb
98
+ - lib/async/io/address.rb
99
+ - lib/async/io/generic.rb
100
+ - lib/async/io/socket.rb
101
+ - lib/async/io/tcp_socket.rb
102
+ - lib/async/io/udp_socket.rb
103
+ - lib/async/io/unix_socket.rb
104
+ - lib/async/io/version.rb
105
+ - lib/async/io/wrap/tcp.rb
106
+ - spec/async/io/address_spec.rb
107
+ - spec/async/io/echo_spec.rb
108
+ - spec/async/io/generic_spec.rb
109
+ - spec/async/io/tcp_socket_spec.rb
110
+ - spec/async/io/udp_socket_spec.rb
111
+ - spec/async/io/unix_socket_spec.rb
112
+ - spec/async/io/wrap/tcp_spec.rb
113
+ - spec/spec_helper.rb
114
+ homepage: https://github.com/socketry/async-io
115
+ licenses: []
116
+ metadata: {}
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubyforge_project:
133
+ rubygems_version: 2.6.10
134
+ signing_key:
135
+ specification_version: 4
136
+ summary: Provides support for asynchonous TCP, UDP, UNIX and SSL sockets.
137
+ test_files:
138
+ - spec/async/io/address_spec.rb
139
+ - spec/async/io/echo_spec.rb
140
+ - spec/async/io/generic_spec.rb
141
+ - spec/async/io/tcp_socket_spec.rb
142
+ - spec/async/io/udp_socket_spec.rb
143
+ - spec/async/io/unix_socket_spec.rb
144
+ - spec/async/io/wrap/tcp_spec.rb
145
+ - spec/spec_helper.rb