nonnative 1.22.0 → 1.27.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5a6ee4ae9e2629fcae27b82ea0f4db839d6c131ab1687ed286b33eb80fc5a41a
4
- data.tar.gz: 53192dec8216ce696fe9774f77b98904a6f13b041cbbe131bbaacaafc45cffda
3
+ metadata.gz: '0384f1153315ed00bbf2c15405150a102b5fa7c7fac9548eac6ee2c03e6e821e'
4
+ data.tar.gz: d09d8785d1f1a0a81806f3b6740e1b835125884bc1e7b2f192ae8bb8586d8acd
5
5
  SHA512:
6
- metadata.gz: 6da13105ad656ebfe2f5bc5c1ba643e12776b2747da9453c3e1da62729475e2490d67942482b7f0a9de480ccebd245fe798cb57cf5db4ee6ef483781b8657be4
7
- data.tar.gz: 787afe51f6eed81281d439f91824c795a96f4673712302983977c976df949e23eeeee58a3d3c5e71829f4c69b6b8931531488f7bf6a7674c3156322b81d10195
6
+ metadata.gz: b6cf1fad64114a39e7c00090a1dedd00d5c7c4f80344668ef5234e68836c6055a9b5aa839dd6639882effc16d03e142a5ba0db6f18154d05617906ae4492f877
7
+ data.tar.gz: 8050865795030f35bb222314905bb4ae981efce1ee21bcad13a9ead4a6d992163439e8b3900aea566d1244cc2438e3a42f711a909a41b55b849e1379af423cb1
@@ -5,7 +5,7 @@ orbs:
5
5
  jobs:
6
6
  build:
7
7
  docker:
8
- - image: circleci/ruby:2.6
8
+ - image: circleci/ruby:2.7
9
9
  environment:
10
10
  BUNDLE_JOBS: "3"
11
11
  BUNDLE_PATH: vendor/bundle
@@ -9,7 +9,7 @@ Metrics/MethodLength:
9
9
  Max: 20
10
10
 
11
11
  Metrics/BlockLength:
12
- Max: 60
12
+ Max: 80
13
13
 
14
14
  Metrics/AbcSize:
15
15
  Max: 20
@@ -67,3 +67,21 @@ Style/BisectedAttrAccessor:
67
67
 
68
68
  Style/RedundantAssignment:
69
69
  Enabled: true
70
+
71
+ Lint/DuplicateElsifCondition:
72
+ Enabled: true
73
+
74
+ Style/ArrayCoercion:
75
+ Enabled: true
76
+
77
+ Style/CaseLikeIf:
78
+ Enabled: true
79
+
80
+ Style/HashAsLastArrayItem:
81
+ Enabled: true
82
+
83
+ Style/HashLikeCase:
84
+ Enabled: true
85
+
86
+ Style/RedundantFileExtensionInRequire:
87
+ Enabled: true
@@ -1 +1 @@
1
- ruby-2.6.6
1
+ ruby-2.7.1
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- nonnative (1.22.0)
4
+ nonnative (1.27.0)
5
5
  concurrent-ruby (~> 1.0, >= 1.0.5)
6
6
  cucumber (>= 4, < 5)
7
7
  grpc (>= 1, < 2)
@@ -75,7 +75,7 @@ GEM
75
75
  http-accept (1.7.0)
76
76
  http-cookie (1.0.3)
77
77
  domain_name (~> 0.5)
78
- i18n (1.8.3)
78
+ i18n (1.8.4)
79
79
  concurrent-ruby (~> 1.0)
80
80
  jaro_winkler (1.5.4)
81
81
  json (2.3.0)
@@ -135,7 +135,7 @@ GEM
135
135
  diff-lcs (>= 1.2.0, < 2.0)
136
136
  rspec-support (~> 3.9.0)
137
137
  rspec-support (3.9.3)
138
- rubocop (0.87.1)
138
+ rubocop (0.88.0)
139
139
  parallel (~> 1.10)
140
140
  parser (>= 2.7.1.1)
141
141
  rainbow (>= 2.2.2, < 4.0)
@@ -144,7 +144,7 @@ GEM
144
144
  rubocop-ast (>= 0.1.0, < 1.0)
145
145
  ruby-progressbar (~> 1.7)
146
146
  unicode-display_width (>= 1.4.0, < 2.0)
147
- rubocop-ast (0.1.0)
147
+ rubocop-ast (0.2.0)
148
148
  parser (>= 2.7.0.1)
149
149
  ruby-progressbar (1.10.1)
150
150
  ruby2_keywords (0.0.2)
@@ -158,7 +158,7 @@ GEM
158
158
  rack (~> 2.0)
159
159
  rack-protection (= 2.0.8.1)
160
160
  tilt (~> 2.0)
161
- solargraph (0.39.11)
161
+ solargraph (0.39.12)
162
162
  backport (~> 1.1)
163
163
  benchmark
164
164
  bundler (>= 1.17.2)
@@ -184,7 +184,7 @@ GEM
184
184
  unf_ext (0.0.7.7)
185
185
  unicode-display_width (1.7.0)
186
186
  yard (0.9.25)
187
- zeitwerk (2.3.1)
187
+ zeitwerk (2.4.0)
188
188
 
189
189
  PLATFORMS
190
190
  ruby
@@ -194,7 +194,7 @@ DEPENDENCIES
194
194
  grpc-tools (>= 1, < 2)
195
195
  nonnative!
196
196
  rake (~> 13.0, >= 13.0.1)
197
- rubocop (~> 0.87.1)
197
+ rubocop (~> 0.88)
198
198
  simplecov (~> 0.17.1)
199
199
  solargraph (~> 0.39.11)
200
200
 
data/README.md CHANGED
@@ -57,7 +57,7 @@ Nonnative.configure do |config|
57
57
  d.command = 'features/support/bin/start 12_321'
58
58
  d.timeout = 0.5
59
59
  d.port = 12_321
60
- d.file = 'features/logs/12_321.log'
60
+ d.log = 'features/logs/12_321.log'
61
61
  d.signal = 'INT' # Possible values are described in Signal.list.keys
62
62
  end
63
63
 
@@ -66,7 +66,7 @@ Nonnative.configure do |config|
66
66
  d.command = 'features/support/bin/start 12_322'
67
67
  d.timeout = 0.5
68
68
  d.port = 12_322
69
- d.file = 'features/logs/12_322.log'
69
+ d.log = 'features/logs/12_322.log'
70
70
  end
71
71
  end
72
72
  ```
@@ -82,14 +82,14 @@ processes:
82
82
  command: features/support/bin/start 12_321
83
83
  timeout: 5
84
84
  port: 12321
85
- file: features/logs/12_321.log
85
+ log: features/logs/12_321.log
86
86
  signal: INT # Possible values are described in Signal.list.keys
87
87
  -
88
88
  name: start_2
89
89
  command: features/support/bin/start 12_322
90
90
  timeout: 5
91
91
  port: 12322
92
- file: features/logs/12_322.log
92
+ log: features/logs/12_322.log
93
93
  ```
94
94
 
95
95
  Then load the file with
@@ -133,18 +133,20 @@ require 'nonnative'
133
133
  Nonnative.configure do |config|
134
134
  config.strategy = :manual
135
135
 
136
- config.server do |d|
137
- d.name = 'server_1'
138
- d.klass = Nonnative::EchoServer
139
- d.timeout = 1
140
- d.port = 12_323
136
+ config.server do |s|
137
+ s.name = 'server_1'
138
+ s.klass = Nonnative::EchoServer
139
+ s.timeout = 1
140
+ s.port = 12_323
141
+ s.log = 'features/logs/server_1.log'
141
142
  end
142
143
 
143
- config.server do |d|
144
- d.name = 'server_2'
145
- d.klass = Nonnative::EchoServer
146
- d.timeout = 1
147
- d.port = 12_324
144
+ config.server do |s|
145
+ s.name = 'server_2'
146
+ s.klass = Nonnative::EchoServer
147
+ s.timeout = 1
148
+ s.port = 12_324
149
+ s.log = 'features/logs/server_2.log'
148
150
  end
149
151
  end
150
152
  ```
@@ -160,11 +162,13 @@ servers:
160
162
  klass: Nonnative::EchoServer
161
163
  timeout: 1
162
164
  port: 12323
165
+ log: features/logs/server_1.log
163
166
  -
164
167
  name: server_2
165
168
  klass: Nonnative::EchoServer
166
169
  timeout: 1
167
170
  port: 12324
171
+ log: features/logs/server_2.log
168
172
  ```
169
173
 
170
174
  Then load the file with:
@@ -182,7 +186,11 @@ Define your server:
182
186
  ```ruby
183
187
  module Nonnative
184
188
  module Features
185
- class Application < Sinatra::Base
189
+ class Application < Sinatra::Application
190
+ configure do
191
+ set :server_settings, log_requests: true
192
+ end
193
+
186
194
  get '/hello' do
187
195
  'Hello World!'
188
196
  end
@@ -205,11 +213,12 @@ require 'nonnative'
205
213
  Nonnative.configure do |config|
206
214
  config.strategy = :manual
207
215
 
208
- config.server do |d|
209
- d.name = 'http_server_1'
210
- d.klass = Nonnative::Features::HTTPServer
211
- d.timeout = 1
212
- d.port = 4567
216
+ config.server do |s|
217
+ s.name = 'http_server_1'
218
+ s.klass = Nonnative::Features::HTTPServer
219
+ s.timeout = 1
220
+ s.port = 4567
221
+ s.log = 'features/logs/http_server_1.log'
213
222
  end
214
223
  end
215
224
  ```
@@ -225,6 +234,7 @@ servers:
225
234
  klass: Nonnative::Features::HTTPServer
226
235
  timeout: 1
227
236
  port: 4567
237
+ log: features/logs/http_server_1.log
228
238
  ```
229
239
 
230
240
  Then load the file with:
@@ -265,11 +275,12 @@ require 'nonnative'
265
275
  Nonnative.configure do |config|
266
276
  config.strategy = :manual
267
277
 
268
- config.server do |d|
269
- d.name = 'grpc_server_1'
270
- d.klass = Nonnative::Features::GRPCServer
271
- d.timeout = 1
272
- d.port = 9002
278
+ config.server do |s|
279
+ s.name = 'grpc_server_1'
280
+ s.klass = Nonnative::Features::GRPCServer
281
+ s.timeout = 1
282
+ s.port = 9002
283
+ s.log = 'features/logs/grpc_server_1.log'
273
284
  end
274
285
  end
275
286
  ```
@@ -285,6 +296,7 @@ servers:
285
296
  klass: Nonnative::Features::GRPCServer
286
297
  timeout: 1
287
298
  port: 9002
299
+ log: features/logs/grpc_server_1.log
288
300
  ```
289
301
 
290
302
  Then load the file with:
@@ -297,8 +309,8 @@ Nonnative.load_configuration('configuration.yml')
297
309
  #### Proxies
298
310
 
299
311
  We allow different proxies to be configured. These proxies can be used to simulate all kind of situations. The proxies that can be configured are:
300
- - none (this is the default)
301
- - chaos
312
+ - `none` (this is the default)
313
+ - `fault_injection`
302
314
 
303
315
  Setup it up programmatically:
304
316
 
@@ -310,8 +322,12 @@ Nonnative.configure do |config|
310
322
 
311
323
  config.server do |d|
312
324
  d.proxy = {
313
- type: 'chaos',
314
- port: 20_000
325
+ type: 'fault_injection',
326
+ port: 20_000,
327
+ log: 'features/logs/proxy_server.log',
328
+ options: {
329
+ delay: 5
330
+ }
315
331
  }
316
332
  end
317
333
  end
@@ -325,6 +341,26 @@ strategy: manual
325
341
  servers:
326
342
  -
327
343
  proxy:
328
- type: chaos
344
+ type: fault_injection
329
345
  port: 20000
346
+ log: features/logs/proxy_server.log
347
+ options:
348
+ delay: 5
349
+ ```
350
+
351
+ ##### Fault Injection
352
+
353
+ The `fault_injection` proxy allows you to simulate failures by injecting them. We currently support the following:
354
+ - `close_all` - Closes the socket as soon as it connects.
355
+ - `delay` - This delays the communication between the connection. Default is 2 secs can be configured through options.
356
+ - `invalid_data` - This takes the input and rearranges it to produce invalid data.
357
+
358
+ Setup it up programmatically:
359
+
360
+ ```ruby
361
+ name = 'name of server in configuration'
362
+ server = Nonnative.pool.server_by_name(name)
363
+
364
+ server.proxy.close_all # To use close_all.
365
+ server.proxy.reset # To reset it back to a good state.
330
366
  ```
@@ -2,7 +2,6 @@
2
2
 
3
3
  require 'socket'
4
4
  require 'timeout'
5
- require 'thwait'
6
5
  require 'yaml'
7
6
 
8
7
  require 'grpc'
@@ -34,7 +33,12 @@ require 'nonnative/observability'
34
33
  require 'nonnative/proxy_factory'
35
34
  require 'nonnative/proxy'
36
35
  require 'nonnative/no_proxy'
37
- require 'nonnative/chaos_proxy'
36
+ require 'nonnative/fault_injection_proxy'
37
+ require 'nonnative/socket_pair'
38
+ require 'nonnative/close_all_socket_pair'
39
+ require 'nonnative/delay_socket_pair'
40
+ require 'nonnative/invalid_data_socket_pair'
41
+ require 'nonnative/socket_pair_factory'
38
42
 
39
43
  module Nonnative
40
44
  class << self
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nonnative
4
+ class CloseAllSocketPair < SocketPair
5
+ def connect(local_socket)
6
+ local_socket.close
7
+ end
8
+ end
9
+ end
@@ -38,7 +38,7 @@ module Nonnative
38
38
  end
39
39
 
40
40
  def command_spawn
41
- spawn(service.command, %i[out err] => [service.file, 'a'])
41
+ spawn(service.command, %i[out err] => [service.log, 'a'])
42
42
  end
43
43
 
44
44
  def command_exists?
@@ -24,7 +24,7 @@ module Nonnative
24
24
  d.command = fd['command']
25
25
  d.timeout = fd['timeout']
26
26
  d.port = fd['port']
27
- d.file = fd['file']
27
+ d.log = fd['log']
28
28
  d.signal = fd['signal']
29
29
  end
30
30
  end
@@ -38,13 +38,16 @@ module Nonnative
38
38
  s.klass = Object.const_get(fd['klass'])
39
39
  s.timeout = fd['timeout']
40
40
  s.port = fd['port']
41
+ s.log = fd['log']
41
42
 
42
43
  proxy = fd['proxy']
43
44
 
44
45
  if proxy
45
46
  s.proxy = {
46
47
  type: proxy['type'],
47
- port: proxy['port']
48
+ port: proxy['port'],
49
+ log: proxy['log'],
50
+ options: proxy['options']
48
51
  }
49
52
  end
50
53
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Nonnative
4
4
  class ConfigurationProcess
5
- attr_accessor :name, :command, :timeout, :port, :file, :signal
5
+ attr_accessor :name, :command, :timeout, :port, :log, :signal
6
6
  end
7
7
  end
@@ -2,11 +2,12 @@
2
2
 
3
3
  module Nonnative
4
4
  class ConfigurationProxy
5
- attr_accessor :type, :port
5
+ attr_accessor :type, :port, :log, :options
6
6
 
7
7
  def initialize
8
8
  self.type = 'none'
9
9
  self.port = 0
10
+ self.options = {}
10
11
  end
11
12
  end
12
13
  end
@@ -2,8 +2,7 @@
2
2
 
3
3
  module Nonnative
4
4
  class ConfigurationServer
5
- attr_accessor :name, :klass, :timeout, :port
6
-
5
+ attr_accessor :name, :klass, :timeout, :port, :log
7
6
  attr_reader :proxy
8
7
 
9
8
  def initialize
@@ -13,6 +12,8 @@ module Nonnative
13
12
  def proxy=(value)
14
13
  proxy.type = value[:type]
15
14
  proxy.port = value[:port]
15
+ proxy.log = value[:log]
16
+ proxy.options = value[:options]
16
17
  end
17
18
  end
18
19
  end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nonnative
4
+ class DelaySocketPair < SocketPair
5
+ def read(socket)
6
+ duration = proxy.options.dig(:delay) || 2
7
+ sleep duration
8
+
9
+ super socket
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nonnative
4
+ class FaultInjectionProxy < Nonnative::Proxy
5
+ def initialize(service)
6
+ @connections = Concurrent::Hash.new
7
+ @logger = Logger.new(service.proxy.log)
8
+ @mutex = Mutex.new
9
+ @state = :none
10
+
11
+ super service
12
+ end
13
+
14
+ def start
15
+ @tcp_server = ::TCPServer.new('0.0.0.0', service.port)
16
+ @thread = Thread.new { perform_start }
17
+ end
18
+
19
+ def stop
20
+ thread.terminate
21
+ tcp_server.close
22
+ end
23
+
24
+ def close_all
25
+ apply_state :close_all
26
+ end
27
+
28
+ def delay
29
+ apply_state :delay
30
+ end
31
+
32
+ def invalid_data
33
+ apply_state :invalid_data
34
+ end
35
+
36
+ def reset
37
+ apply_state :none
38
+ end
39
+
40
+ def port
41
+ service.proxy.port
42
+ end
43
+
44
+ private
45
+
46
+ attr_reader :tcp_server, :thread, :connections, :mutex, :state, :logger
47
+
48
+ def perform_start
49
+ loop do
50
+ thread = Thread.start(tcp_server.accept) do |local_socket|
51
+ id = Thread.current.object_id
52
+
53
+ logger.info "started connection for #{id} with socket #{local_socket.inspect}"
54
+
55
+ connect local_socket
56
+ connections.delete(id)
57
+
58
+ logger.info "finished connection for #{id} with socket #{local_socket.inspect}"
59
+ end
60
+
61
+ thread.report_on_exception = false
62
+ connections[thread.object_id] = thread
63
+ end
64
+ end
65
+
66
+ def connect(local_socket)
67
+ SocketPairFactory.create(read_state, service.proxy, logger).connect(local_socket)
68
+ end
69
+
70
+ def apply_state(state)
71
+ mutex.synchronize { @state = state }
72
+ end
73
+
74
+ def read_state
75
+ mutex.synchronize { state }
76
+ end
77
+ end
78
+ end
@@ -6,6 +6,11 @@ module Nonnative
6
6
  @server = GRPC::RpcServer.new
7
7
  server.handle(svc)
8
8
 
9
+ # Unfortunately gRPC has only one logger so the first server wins.
10
+ GRPC.define_singleton_method(:logger) do
11
+ @logger ||= Logger.new(service.log)
12
+ end
13
+
9
14
  super service
10
15
  end
11
16
 
@@ -8,31 +8,33 @@ module Nonnative
8
8
 
9
9
  protected
10
10
 
11
- def get(pathname, headers = {})
11
+ def get(pathname, headers = {}, timeout = 60)
12
12
  with_exception do
13
13
  uri = URI.join(host, pathname)
14
- RestClient.get(uri.to_s, headers)
14
+ RestClient::Request.execute(method: :get, url: uri.to_s, headers: headers, timeout: timeout)
15
15
  end
16
16
  end
17
17
 
18
- def post(pathname, payload, headers = {})
18
+ def post(pathname, payload, headers = {}, timeout = 60)
19
19
  with_exception do
20
20
  uri = URI.join(host, pathname)
21
- RestClient.post(uri.to_s, payload.to_json, headers)
21
+ RestClient::Request.execute(method: :post, url: uri.to_s, payload: payload.to_json, headers: headers,
22
+ timeout: timeout)
22
23
  end
23
24
  end
24
25
 
25
- def delete(pathname, headers = {})
26
+ def delete(pathname, headers = {}, timeout = 60)
26
27
  with_exception do
27
28
  uri = URI.join(host, pathname)
28
- RestClient.delete(uri.to_s, headers)
29
+ RestClient::Request.execute(method: :delete, url: uri.to_s, headers: headers, timeout: timeout)
29
30
  end
30
31
  end
31
32
 
32
- def put(pathname, payload, headers = {})
33
+ def put(pathname, payload, headers = {}, timeout = 60)
33
34
  with_exception do
34
35
  uri = URI.join(host, pathname)
35
- RestClient.put(uri.to_s, payload.to_json, headers)
36
+ RestClient::Request.execute(method: :put, url: uri.to_s, payload: payload.to_json, headers: headers,
37
+ timeout: timeout)
36
38
  end
37
39
  end
38
40
 
@@ -42,6 +44,8 @@ module Nonnative
42
44
 
43
45
  def with_exception
44
46
  yield
47
+ rescue RestClient::Exceptions::ReadTimeout => e
48
+ raise e
45
49
  rescue RestClient::ExceptionWithResponse => e
46
50
  e.response
47
51
  end
@@ -3,7 +3,9 @@
3
3
  module Nonnative
4
4
  class HTTPServer < Nonnative::Server
5
5
  def initialize(service)
6
- @server = Puma::Server.new(app, Puma::Events.strings)
6
+ log = File.open(service.log, 'a')
7
+ events = Puma::Events.new(log, log)
8
+ @server = Puma::Server.new(app, events)
7
9
 
8
10
  super service
9
11
  end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nonnative
4
+ class InvalidDataSocketPair < SocketPair
5
+ def write(socket, data)
6
+ data = data.chars.shuffle.join
7
+
8
+ super socket, data
9
+ end
10
+ end
11
+ end
@@ -46,8 +46,6 @@ module Nonnative
46
46
  threads << Thread.new { port.send(port_method) }
47
47
  end
48
48
 
49
- ThreadsWait.all_waits(*threads)
50
-
51
49
  ports = threads.map(&:value)
52
50
 
53
51
  yield_results(types, pids, ports, &block)
@@ -4,11 +4,10 @@ module Nonnative
4
4
  class Proxy
5
5
  def initialize(service)
6
6
  @service = service
7
- @timeout = Nonnative::Timeout.new(service.timeout)
8
7
  end
9
8
 
10
9
  protected
11
10
 
12
- attr_reader :service, :timeout
11
+ attr_reader :service
13
12
  end
14
13
  end
@@ -4,13 +4,14 @@ module Nonnative
4
4
  class ProxyFactory
5
5
  class << self
6
6
  def create(service)
7
- case service.proxy.type
8
- when 'chaos'
9
- ChaosProxy.new(service)
10
- else
11
- # By default we want no proxy.
12
- NoProxy.new(service)
13
- end
7
+ proxy = case service.proxy.type
8
+ when 'fault_injection'
9
+ FaultInjectionProxy
10
+ else
11
+ NoProxy
12
+ end
13
+
14
+ proxy.new(service)
14
15
  end
15
16
  end
16
17
  end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nonnative
4
+ class SocketPair
5
+ def initialize(proxy, logger)
6
+ @proxy = proxy
7
+ @logger = logger
8
+ end
9
+
10
+ def connect(local_socket)
11
+ remote_socket = create_remote_socket
12
+
13
+ loop do
14
+ ready = select([local_socket, remote_socket], nil, nil)
15
+
16
+ break if pipe(ready, local_socket, remote_socket)
17
+ break if pipe(ready, remote_socket, local_socket)
18
+ end
19
+ rescue StandardError => e
20
+ logger.error e
21
+ ensure
22
+ local_socket.close
23
+ remote_socket&.close
24
+ end
25
+
26
+ protected
27
+
28
+ attr_reader :proxy, :logger
29
+
30
+ def create_remote_socket
31
+ ::TCPSocket.new('0.0.0.0', proxy.port)
32
+ end
33
+
34
+ def pipe(ready, socket1, socket2)
35
+ if ready[0].include?(socket1)
36
+ data = read(socket1)
37
+ return true if data.empty?
38
+
39
+ write socket2, data
40
+ end
41
+
42
+ false
43
+ end
44
+
45
+ def read(socket)
46
+ socket.recv(1024)
47
+ end
48
+
49
+ def write(socket, data)
50
+ socket.write(data)
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nonnative
4
+ class SocketPairFactory
5
+ class << self
6
+ def create(type, proxy, logger)
7
+ pair = case type
8
+ when :close_all
9
+ CloseAllSocketPair
10
+ when :delay
11
+ DelaySocketPair
12
+ when :invalid_data
13
+ InvalidDataSocketPair
14
+ else
15
+ SocketPair
16
+ end
17
+
18
+ pair.new(proxy, logger)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Nonnative
4
- VERSION = '1.22.0'
4
+ VERSION = '1.27.0'
5
5
  end
@@ -36,7 +36,7 @@ Gem::Specification.new do |spec|
36
36
  spec.add_development_dependency 'bundler', '~> 2.1', '>= 2.1.4'
37
37
  spec.add_development_dependency 'grpc-tools', ['>= 1', '< 2']
38
38
  spec.add_development_dependency 'rake', '~> 13.0', '>= 13.0.1'
39
- spec.add_development_dependency 'rubocop', '~> 0.87.1'
39
+ spec.add_development_dependency 'rubocop', '~> 0.88'
40
40
  spec.add_development_dependency 'simplecov', '~> 0.17.1'
41
41
  spec.add_development_dependency 'solargraph', '~> 0.39.11'
42
42
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nonnative
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.22.0
4
+ version: 1.27.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Falkowski
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-07-10 00:00:00.000000000 Z
11
+ date: 2020-07-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -224,14 +224,14 @@ dependencies:
224
224
  requirements:
225
225
  - - "~>"
226
226
  - !ruby/object:Gem::Version
227
- version: 0.87.1
227
+ version: '0.88'
228
228
  type: :development
229
229
  prerelease: false
230
230
  version_requirements: !ruby/object:Gem::Requirement
231
231
  requirements:
232
232
  - - "~>"
233
233
  - !ruby/object:Gem::Version
234
- version: 0.87.1
234
+ version: '0.88'
235
235
  - !ruby/object:Gem::Dependency
236
236
  name: simplecov
237
237
  requirement: !ruby/object:Gem::Requirement
@@ -283,16 +283,19 @@ files:
283
283
  - bin/setup
284
284
  - lib/nonnative.rb
285
285
  - lib/nonnative/before.rb
286
- - lib/nonnative/chaos_proxy.rb
286
+ - lib/nonnative/close_all_socket_pair.rb
287
287
  - lib/nonnative/command.rb
288
288
  - lib/nonnative/configuration.rb
289
289
  - lib/nonnative/configuration_process.rb
290
290
  - lib/nonnative/configuration_proxy.rb
291
291
  - lib/nonnative/configuration_server.rb
292
+ - lib/nonnative/delay_socket_pair.rb
292
293
  - lib/nonnative/error.rb
294
+ - lib/nonnative/fault_injection_proxy.rb
293
295
  - lib/nonnative/grpc_server.rb
294
296
  - lib/nonnative/http_client.rb
295
297
  - lib/nonnative/http_server.rb
298
+ - lib/nonnative/invalid_data_socket_pair.rb
296
299
  - lib/nonnative/manual.rb
297
300
  - lib/nonnative/no_proxy.rb
298
301
  - lib/nonnative/observability.rb
@@ -302,6 +305,8 @@ files:
302
305
  - lib/nonnative/proxy_factory.rb
303
306
  - lib/nonnative/server.rb
304
307
  - lib/nonnative/service.rb
308
+ - lib/nonnative/socket_pair.rb
309
+ - lib/nonnative/socket_pair_factory.rb
305
310
  - lib/nonnative/start_error.rb
306
311
  - lib/nonnative/startup.rb
307
312
  - lib/nonnative/stop_error.rb
@@ -328,7 +333,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
328
333
  - !ruby/object:Gem::Version
329
334
  version: '0'
330
335
  requirements: []
331
- rubygems_version: 3.0.3
336
+ rubygems_version: 3.1.2
332
337
  signing_key:
333
338
  specification_version: 4
334
339
  summary: Allows you to keep using the power of ruby to test other systems
@@ -1,94 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Nonnative
4
- class ChaosProxy < Nonnative::Proxy
5
- def initialize(service)
6
- @connections = Concurrent::Hash.new
7
- @mutex = Mutex.new
8
- @state = :none
9
-
10
- super service
11
- end
12
-
13
- def start
14
- @tcp_server = ::TCPServer.new('0.0.0.0', service.port)
15
- @thread = Thread.new { perform_start }
16
- end
17
-
18
- def stop
19
- thread.terminate
20
- tcp_server.close
21
- end
22
-
23
- def close_all
24
- apply_state :close_all
25
- end
26
-
27
- def reset
28
- apply_state :none
29
- end
30
-
31
- def port
32
- service.proxy.port
33
- end
34
-
35
- private
36
-
37
- attr_reader :tcp_server, :thread, :connections, :mutex, :state
38
-
39
- def perform_start
40
- loop do
41
- thread = Thread.start(tcp_server.accept) { |local_socket| connect(local_socket) }
42
- connections[thread.object_id] = thread
43
- end
44
- end
45
-
46
- def connect(local_socket)
47
- return local_socket.close if state?(:close_all)
48
-
49
- remote_socket = create_remote_socket
50
- return unless remote_socket
51
-
52
- loop do
53
- ready = select([local_socket, remote_socket], nil, nil)
54
-
55
- break if write(ready, local_socket, remote_socket)
56
- break if write(ready, remote_socket, local_socket)
57
- end
58
- rescue Errno::ECONNRESET
59
- # Just ignore it.
60
- ensure
61
- local_socket.close
62
- remote_socket&.close
63
- connections.delete(Thread.current.object_id)
64
- end
65
-
66
- def create_remote_socket
67
- timeout.perform do
68
- ::TCPSocket.new('0.0.0.0', port)
69
- rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
70
- sleep 0.01
71
- retry
72
- end
73
- end
74
-
75
- def write(ready, socket1, socket2)
76
- if ready[0].include?(socket1)
77
- data = socket1.recv(1024)
78
- return true if data.empty?
79
-
80
- socket2.write(data)
81
- end
82
-
83
- false
84
- end
85
-
86
- def apply_state(state)
87
- mutex.synchronize { @state = state }
88
- end
89
-
90
- def state?(state)
91
- mutex.synchronize { @state == state }
92
- end
93
- end
94
- end