tcp-client 0.5.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,67 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../test_helper'
4
-
5
- class AddressTest < MiniTest::Test
6
- parallelize_me!
7
-
8
- def test_create_from_integer
9
- subject = TCPClient::Address.new(42)
10
- assert_equal('localhost:42', subject.to_s)
11
- assert_equal('localhost', subject.hostname)
12
- assert(subject.addrinfo.ip?)
13
- assert(subject.addrinfo.ipv6?)
14
- assert_same(42, subject.addrinfo.ip_port)
15
- end
16
-
17
- def test_create_from_addrinfo
18
- addrinfo = Addrinfo.tcp('localhost', 42)
19
- subject = TCPClient::Address.new(addrinfo)
20
- assert_equal(addrinfo.getnameinfo[0], subject.hostname)
21
- assert_equal(addrinfo, subject.addrinfo)
22
- end
23
-
24
- def test_create_from_str
25
- subject = TCPClient::Address.new('localhost:42')
26
- assert_equal('localhost:42', subject.to_s)
27
- assert_equal('localhost', subject.hostname)
28
- assert(subject.addrinfo.ip?)
29
- assert(subject.addrinfo.ipv6?)
30
- assert_same(42, subject.addrinfo.ip_port)
31
- end
32
-
33
- def test_create_from_str_short
34
- subject = TCPClient::Address.new(':42')
35
- assert_equal(':42', subject.to_s)
36
- assert_empty(subject.hostname)
37
- assert_same(42, subject.addrinfo.ip_port)
38
- assert(subject.addrinfo.ip?)
39
- assert(subject.addrinfo.ipv4?)
40
- end
41
-
42
- def test_create_from_str_ip6
43
- subject = TCPClient::Address.new('[::1]:42')
44
- assert_equal('[::1]:42', subject.to_s)
45
- assert_equal('::1', subject.hostname)
46
- assert_same(42, subject.addrinfo.ip_port)
47
- assert(subject.addrinfo.ip?)
48
- assert(subject.addrinfo.ipv6?)
49
- end
50
-
51
- def test_create_from_empty_str
52
- subject = TCPClient::Address.new('')
53
- assert_equal('localhost:0', subject.to_s)
54
- assert_equal('localhost', subject.hostname)
55
- assert_same(0, subject.addrinfo.ip_port)
56
- assert(subject.addrinfo.ip?)
57
- assert(subject.addrinfo.ipv6?)
58
- end
59
-
60
- def test_compare
61
- a = TCPClient::Address.new('localhost:42')
62
- b = TCPClient::Address.new('localhost:42')
63
- assert_equal(a, b)
64
- assert(a == b)
65
- assert(a === b)
66
- end
67
- end
@@ -1,143 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../test_helper'
4
-
5
- class ConfigurationTest < MiniTest::Test
6
- parallelize_me!
7
-
8
- def test_defaults
9
- subject = TCPClient::Configuration.new
10
- assert(subject.buffered)
11
- assert(subject.keep_alive)
12
- assert(subject.reverse_lookup)
13
- refute(subject.ssl?)
14
- assert_nil(subject.connect_timeout)
15
- assert_nil(subject.read_timeout)
16
- assert_nil(subject.write_timeout)
17
- end
18
-
19
- def test_configure
20
- subject =
21
- TCPClient::Configuration.create do |cfg|
22
- cfg.buffered = cfg.keep_alive = cfg.reverse_lookup = false
23
- cfg.timeout = 42
24
- cfg.ssl = true
25
- end
26
- refute(subject.buffered)
27
- refute(subject.keep_alive)
28
- refute(subject.reverse_lookup)
29
- assert_same(42, subject.connect_timeout)
30
- assert_same(42, subject.read_timeout)
31
- assert_same(42, subject.write_timeout)
32
- assert(subject.ssl?)
33
- end
34
-
35
- def test_options
36
- subject =
37
- TCPClient::Configuration.new(
38
- buffered: false,
39
- keep_alive: false,
40
- reverse_lookup: false,
41
- connect_timeout: 1,
42
- read_timeout: 2,
43
- write_timeout: 3,
44
- ssl: true,
45
- connect_timeout_error: IOError
46
- )
47
- refute(subject.buffered)
48
- refute(subject.keep_alive)
49
- refute(subject.reverse_lookup)
50
- assert_same(1, subject.connect_timeout)
51
- assert_same(2, subject.read_timeout)
52
- assert_same(3, subject.write_timeout)
53
- assert(subject.ssl?)
54
- assert_same(IOError, subject.connect_timeout_error)
55
- assert_same(TCPClient::ReadTimeoutError, subject.read_timeout_error)
56
- end
57
-
58
- def test_invalid_option
59
- err =
60
- assert_raises(ArgumentError) do
61
- TCPClient::Configuration.new(unknown_attr: :argument)
62
- end
63
- assert_includes(err.message, 'attribute')
64
- assert_includes(err.message, 'unknown_attr')
65
- end
66
-
67
- def test_ssl_params
68
- subject = TCPClient::Configuration.new
69
- refute(subject.ssl?)
70
- assert_nil(subject.ssl_params)
71
- subject.ssl = true
72
- assert(subject.ssl?)
73
- assert_empty(subject.ssl_params)
74
- subject.ssl_params[:ssl_version] = :TLSv1_2
75
- subject.ssl = false
76
- assert_nil(subject.ssl_params)
77
- end
78
-
79
- def test_timeout_overwrite
80
- subject =
81
- TCPClient::Configuration.create do |cfg|
82
- cfg.connect_timeout = 1
83
- cfg.read_timeout = 2
84
- cfg.write_timeout = 3
85
- end
86
- assert_same(1, subject.connect_timeout)
87
- assert_same(2, subject.read_timeout)
88
- assert_same(3, subject.write_timeout)
89
-
90
- subject.timeout = 42
91
- assert_same(42, subject.connect_timeout)
92
- assert_same(42, subject.read_timeout)
93
- assert_same(42, subject.write_timeout)
94
- end
95
-
96
- def test_timeout_error_overwrite
97
- subject = TCPClient::Configuration.new
98
- assert_same(TCPClient::ConnectTimeoutError, subject.connect_timeout_error)
99
- assert_same(TCPClient::ReadTimeoutError, subject.read_timeout_error)
100
- assert_same(TCPClient::WriteTimeoutError, subject.write_timeout_error)
101
-
102
- subject.timeout_error = IOError
103
- assert_same(IOError, subject.connect_timeout_error)
104
- assert_same(IOError, subject.read_timeout_error)
105
- assert_same(IOError, subject.write_timeout_error)
106
- end
107
-
108
- def test_compare
109
- a = TCPClient::Configuration.new
110
- b = TCPClient::Configuration.new
111
- assert_equal(a, b)
112
- assert(a == b)
113
- assert(a === b)
114
- end
115
-
116
- def test_dup
117
- source =
118
- TCPClient::Configuration.new(
119
- buffered: false,
120
- keep_alive: false,
121
- reverse_lookup: false,
122
- connect_timeout: 1,
123
- read_timeout: 2,
124
- write_timeout: 3,
125
- ssl: {
126
- ssl_version: :TLSv1_2
127
- }
128
- )
129
- shadow = source.dup.freeze
130
-
131
- # some changes
132
- source.buffered = true
133
- source.write_timeout = 5
134
- source.ssl_params[:err] = true
135
- source.timeout_error = IOError
136
-
137
- refute_equal(source.__id__, shadow.__id__)
138
- refute(shadow.buffered)
139
- assert_equal(3, shadow.write_timeout)
140
- assert_equal({ ssl_version: :TLSv1_2 }, shadow.ssl_params)
141
- assert_same(TCPClient::ReadTimeoutError, shadow.read_timeout_error)
142
- end
143
- end
@@ -1,26 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../test_helper'
4
-
5
- class Deadlineest < MiniTest::Test
6
- parallelize_me!
7
-
8
- def test_validity
9
- assert(TCPClient::Deadline.new(1).valid?)
10
- assert(TCPClient::Deadline.new(0.0001).valid?)
11
-
12
- refute(TCPClient::Deadline.new(0).valid?)
13
- refute(TCPClient::Deadline.new(nil).valid?)
14
- end
15
-
16
- def test_remaining_time
17
- assert(TCPClient::Deadline.new(1).remaining_time > 0)
18
-
19
- assert_nil(TCPClient::Deadline.new(0).remaining_time)
20
- assert_nil(TCPClient::Deadline.new(nil).remaining_time)
21
-
22
- deadline = TCPClient::Deadline.new(0.2)
23
- sleep(0.2)
24
- assert_nil(deadline.remaining_time)
25
- end
26
- end
@@ -1,59 +0,0 @@
1
- require_relative '../test_helper'
2
-
3
- class DefauktConfigurationTest < MiniTest::Test
4
- def test_default
5
- subject = TCPClient.configure # reset to defaults
6
-
7
- assert_same(
8
- TCPClient.default_configuration,
9
- TCPClient::Configuration.default
10
- )
11
- assert(subject.buffered)
12
- assert(subject.keep_alive)
13
- assert(subject.reverse_lookup)
14
- refute(subject.ssl?)
15
- assert_nil(subject.connect_timeout)
16
- assert_nil(subject.read_timeout)
17
- assert_nil(subject.write_timeout)
18
- end
19
-
20
- def test_configure_options
21
- TCPClient.configure(
22
- buffered: false,
23
- keep_alive: false,
24
- reverse_lookup: false,
25
- ssl: true,
26
- connect_timeout: 1,
27
- read_timeout: 2,
28
- write_timeout: 3
29
- )
30
- subject = TCPClient.default_configuration
31
- refute(subject.buffered)
32
- refute(subject.keep_alive)
33
- refute(subject.reverse_lookup)
34
- assert(subject.ssl?)
35
- assert_same(1, subject.connect_timeout)
36
- assert_same(2, subject.read_timeout)
37
- assert_same(3, subject.write_timeout)
38
- end
39
-
40
- def test_configure_block
41
- TCPClient.configure do |cfg|
42
- cfg.buffered = false
43
- cfg.keep_alive = false
44
- cfg.reverse_lookup = false
45
- cfg.ssl = true
46
- cfg.connect_timeout = 1
47
- cfg.read_timeout = 2
48
- cfg.write_timeout = 3
49
- end
50
- subject = TCPClient.default_configuration
51
- refute(subject.buffered)
52
- refute(subject.keep_alive)
53
- refute(subject.reverse_lookup)
54
- assert(subject.ssl?)
55
- assert_same(1, subject.connect_timeout)
56
- assert_same(2, subject.read_timeout)
57
- assert_same(3, subject.write_timeout)
58
- end
59
- end
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../test_helper'
4
-
5
- class VersionTest < MiniTest::Test
6
- parallelize_me!
7
-
8
- def test_format
9
- assert_match(/\d+\.\d+\.\d+/, TCPClient::VERSION)
10
- end
11
- end
@@ -1,163 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'test_helper'
4
-
5
- class TCPClientTest < MiniTest::Test
6
- parallelize_me!
7
-
8
- HUGE_AMOUNT_OF_DATA = Array.new(2024, '?' * 1024).freeze
9
-
10
- attr_reader :config
11
-
12
- def setup
13
- @config = TCPClient::Configuration.create(buffered: false)
14
- end
15
-
16
- def port
17
- PseudoServer.local_address.ip_port
18
- end
19
-
20
- def test_defaults
21
- subject = TCPClient.new
22
- assert(subject.closed?)
23
- assert_equal('', subject.to_s)
24
- assert_nil(subject.address)
25
- subject.close
26
- assert_raises(TCPClient::NotConnected) { subject.write('hello world!') }
27
- assert_raises(TCPClient::NotConnected) { subject.read(42) }
28
- end
29
-
30
- def create_nonconnected_client
31
- client = TCPClient.new
32
- client.connect('', config)
33
- client
34
- rescue Errno::EADDRNOTAVAIL
35
- client
36
- end
37
-
38
- def test_failed_state
39
- subject = create_nonconnected_client
40
- assert(subject.closed?)
41
- assert_equal('localhost:0', subject.to_s)
42
- refute_nil(subject.address)
43
- assert_equal('localhost:0', subject.address.to_s)
44
- assert_equal('localhost', subject.address.hostname)
45
- assert_instance_of(Addrinfo, subject.address.addrinfo)
46
- assert_same(0, subject.address.addrinfo.ip_port)
47
- assert_raises(TCPClient::NotConnected) { subject.write('hello world!') }
48
- assert_raises(TCPClient::NotConnected) { subject.read(42) }
49
- end
50
-
51
- def test_connected_state
52
- TCPClient.open("localhost:#{port}", config) do |subject|
53
- refute(subject.closed?)
54
- assert_equal("localhost:#{port}", subject.to_s)
55
- refute_nil(subject.address)
56
- address_when_opened = subject.address
57
- assert_equal("localhost:#{port}", subject.address.to_s)
58
- assert_equal('localhost', subject.address.hostname)
59
- assert_instance_of(Addrinfo, subject.address.addrinfo)
60
- assert_same(port, subject.address.addrinfo.ip_port)
61
-
62
- subject.close
63
- assert(subject.closed?)
64
- assert_same(address_when_opened, subject.address)
65
- end
66
- end
67
-
68
- def check_read_timeout(timeout)
69
- TCPClient.open("localhost:#{port}", config) do |subject|
70
- refute(subject.closed?)
71
- start_time = nil
72
- assert_raises(TCPClient::ReadTimeoutError) do
73
- start_time = Time.now
74
- subject.read(42, timeout: timeout)
75
- end
76
- assert_in_delta(timeout, Time.now - start_time, 0.15)
77
- end
78
- end
79
-
80
- def test_read_timeout
81
- check_read_timeout(0.5)
82
- check_read_timeout(1)
83
- check_read_timeout(1.5)
84
- end
85
-
86
- def check_write_timeout(timeout)
87
- TCPClient.open("localhost:#{port}", config) do |subject|
88
- refute(subject.closed?)
89
- start_time = nil
90
- assert_raises(TCPClient::WriteTimeoutError) do
91
- start_time = Time.now
92
- subject.write(*HUGE_AMOUNT_OF_DATA, timeout: timeout)
93
- end
94
- assert_in_delta(timeout, Time.now - start_time, 0.15)
95
- end
96
- end
97
-
98
- def test_write_timeout
99
- check_write_timeout(0.01)
100
- check_write_timeout(0.25)
101
- end
102
-
103
- def test_write_deadline
104
- TCPClient.open("localhost:#{port}", config) do |subject|
105
- refute(subject.closed?)
106
- assert_raises(TCPClient::WriteTimeoutError) do
107
- subject.with_deadline(0.25) do |*args|
108
- assert_equal([subject], args)
109
- loop { subject.write('some data here') }
110
- end
111
- end
112
- end
113
- end
114
-
115
- def test_read_deadline
116
- TCPClient.open("localhost:#{port}", config) do |subject|
117
- refute(subject.closed?)
118
- assert_raises(TCPClient::ReadTimeoutError) do
119
- subject.with_deadline(0.25) do |*args|
120
- assert_equal([subject], args)
121
- loop { subject.read(0) }
122
- end
123
- end
124
- end
125
- end
126
-
127
- def test_read_write_deadline
128
- TCPClient.open("localhost:#{port}", config) do |subject|
129
- refute(subject.closed?)
130
- assert_raises(TCPClient::TimeoutError) do
131
- subject.with_deadline(0.25) do |*args|
132
- assert_equal([subject], args)
133
- loop do
134
- subject.write('HUGE_AMOUNT_OF_DATA')
135
- subject.read(0)
136
- end
137
- end
138
- end
139
- end
140
- end
141
-
142
- def check_connect_timeout(ssl_config)
143
- start_time = nil
144
- assert_raises(TCPClient::ConnectTimeoutError) do
145
- start_time = Time.now
146
- TCPClient.new.connect("localhost:#{port}", ssl_config)
147
- end
148
- assert_in_delta(ssl_config.connect_timeout, Time.now - start_time, 0.25)
149
- end
150
-
151
- def test_connect_ssl_timeout
152
- ssl_config = TCPClient::Configuration.new(ssl: true)
153
-
154
- ssl_config.connect_timeout = 0.5
155
- check_connect_timeout(ssl_config)
156
-
157
- ssl_config.connect_timeout = 1
158
- check_connect_timeout(ssl_config)
159
-
160
- ssl_config.connect_timeout = 1.5
161
- check_connect_timeout(ssl_config)
162
- end
163
- end
data/test/test_helper.rb DELETED
@@ -1,9 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'minitest/autorun'
4
- require 'minitest/parallel'
5
- require_relative '../lib/tcp-client'
6
-
7
- # this pseudo-server never reads or writes anything
8
- PseudoServer = TCPServer.new('localhost', 0)
9
- Minitest.after_run { PseudoServer.close }