rspeed 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +16 -0
  3. data/README.md +11 -7
  4. data/lib/generators/rspeed/install_generator.rb +1 -1
  5. data/lib/rspeed.rb +8 -6
  6. data/lib/rspeed/env.rb +43 -0
  7. data/lib/rspeed/extension.rb +3 -2
  8. data/lib/rspeed/observer.rb +17 -3
  9. data/lib/rspeed/redis.rb +55 -0
  10. data/lib/rspeed/runner.rb +3 -18
  11. data/lib/rspeed/splitter.rb +29 -52
  12. data/lib/rspeed/variable.rb +31 -0
  13. data/lib/rspeed/version.rb +1 -1
  14. data/spec/common_helper.rb +10 -0
  15. data/spec/fixtures/empty.rb +4 -0
  16. data/spec/models/rspeed/env/db_spec.rb +17 -0
  17. data/spec/models/rspeed/env/host_spec.rb +17 -0
  18. data/spec/models/rspeed/env/name_spec.rb +17 -0
  19. data/spec/models/rspeed/env/pipe_spec.rb +19 -0
  20. data/spec/models/rspeed/env/pipes_spec.rb +41 -0
  21. data/spec/models/rspeed/env/port_spec.rb +17 -0
  22. data/spec/models/rspeed/env/result_key_spec.rb +19 -0
  23. data/spec/models/rspeed/env/rspeed_spec.rb +43 -0
  24. data/spec/models/rspeed/env/tmp_spec.rb +19 -0
  25. data/spec/models/rspeed/observer/after_spec.rb +0 -2
  26. data/spec/models/rspeed/observer/after_suite_spec.rb +46 -0
  27. data/spec/models/rspeed/observer/before_spec.rb +0 -2
  28. data/spec/models/rspeed/observer/before_suite_spec.rb +24 -2
  29. data/spec/models/rspeed/redis/clean_pipes_flag_spec.rb +14 -0
  30. data/spec/models/rspeed/redis/client_spec.rb +7 -0
  31. data/spec/models/rspeed/redis/destroy_spec.rb +29 -0
  32. data/spec/models/rspeed/redis/get_spec.rb +9 -0
  33. data/spec/models/rspeed/redis/keys_spec.rb +29 -0
  34. data/spec/models/rspeed/redis/result_spec.rb +13 -0
  35. data/spec/models/rspeed/redis/set_spec.rb +9 -0
  36. data/spec/models/rspeed/redis/specs_finished_spec.rb +19 -0
  37. data/spec/models/rspeed/redis/specs_initiated_spec.rb +13 -0
  38. data/spec/models/rspeed/runner/run_spec.rb +30 -0
  39. data/spec/models/rspeed/splitter/actual_examples_spec.rb +6 -2
  40. data/spec/models/rspeed/splitter/append_spec.rb +1 -3
  41. data/spec/models/rspeed/splitter/diff_spec.rb +1 -3
  42. data/spec/models/rspeed/splitter/first_pipe_spec.rb +2 -4
  43. data/spec/models/rspeed/splitter/get_spec.rb +7 -9
  44. data/spec/models/rspeed/splitter/pipe_files_spec.rb +26 -0
  45. data/spec/models/rspeed/splitter/redundant_run_spec.rb +45 -0
  46. data/spec/models/rspeed/splitter/rename_spec.rb +3 -5
  47. data/spec/models/rspeed/splitter/split_spec.rb +70 -34
  48. data/spec/models/rspeed/variable/append_name_spec.rb +19 -0
  49. data/spec/models/rspeed/variable/csv_spec.rb +5 -0
  50. data/spec/models/rspeed/variable/default_partner_spec.rb +5 -0
  51. data/spec/models/rspeed/variable/key_spec.rb +15 -0
  52. data/spec/models/rspeed/variable/pipe_name_spec.rb +15 -0
  53. data/spec/models/rspeed/variable/pipes_pattern_spec.rb +5 -0
  54. data/spec/models/rspeed/variable/result_spec.rb +19 -0
  55. data/spec/models/rspeed/variable/tmp_spec.rb +15 -0
  56. data/spec/{rails_helper.rb → spec_helper.rb} +2 -22
  57. data/spec/support/common.rb +13 -0
  58. data/spec/support/coverage.rb +14 -0
  59. data/spec/support/env_mock.rb +3 -0
  60. data/spec/support/fakeredis.rb +3 -0
  61. metadata +137 -20
  62. data/spec/models/rspeed/splitter/destroy_spec.rb +0 -33
  63. data/spec/models/rspeed/splitter/keys_spec.rb +0 -33
  64. data/spec/models/rspeed/splitter/last_pipe_spec.rb +0 -21
  65. data/spec/models/rspeed/splitter/pipe_spec.rb +0 -21
  66. data/spec/models/rspeed/splitter/pipes_spec.rb +0 -27
  67. data/spec/models/rspeed/splitter/result_spec.rb +0 -19
  68. data/spec/models/rspeed/splitter/save_spec.rb +0 -57
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RSpeed
4
+ module Variable
5
+ module_function
6
+
7
+ CSV = 'rspeed.csv'
8
+ DEFAULT_PATTERN = 'rspeed_*'
9
+ PIPES_PATTERN = 'rspeed_pipe_*'
10
+
11
+ def append_name(value, suffix = nil)
12
+ [value, RSpeed::Env.name, suffix].compact.join('_')
13
+ end
14
+
15
+ def key(number)
16
+ append_name('rspeed', number).to_sym
17
+ end
18
+
19
+ def result
20
+ append_name('rspeed')
21
+ end
22
+
23
+ def tmp
24
+ append_name('rspeed_tmp')
25
+ end
26
+
27
+ def pipe_name
28
+ append_name('rspeed_pipe', RSpeed::Env.pipe)
29
+ end
30
+ end
31
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RSpeed
4
- VERSION = '0.4.0'
4
+ VERSION = '0.5.0'
5
5
  end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ ENV['RAILS_ENV'] ||= 'test'
4
+
5
+ require 'support/coverage'
6
+
7
+ require 'pry-byebug'
8
+ require 'rspeed'
9
+ require 'support/common'
10
+ require 'support/fakeredis'
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe 'empty' do
4
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'support/env_mock'
4
+
5
+ RSpec.describe RSpeed::Env, '.db' do
6
+ context 'when key is not setted on env' do
7
+ it { expect(described_class.db).to be(nil) }
8
+ end
9
+
10
+ context 'when key is setted on env' do
11
+ it 'returns env value as integer' do
12
+ EnvMock.mock(rspeed_db: '10') do
13
+ expect(described_class.db).to be(10)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'support/env_mock'
4
+
5
+ RSpec.describe RSpeed::Env, '.host' do
6
+ context 'when key is not setted on env' do
7
+ it { expect(described_class.host).to be(nil) }
8
+ end
9
+
10
+ context 'when key is setted on env' do
11
+ it 'returns env value' do
12
+ EnvMock.mock(rspeed_host: 'localhost') do
13
+ expect(described_class.host).to eq('localhost')
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'support/env_mock'
4
+
5
+ RSpec.describe RSpeed::Env, '.name' do
6
+ context 'when env is given' do
7
+ it 'returns the env value' do
8
+ EnvMock.mock(rspeed_name: 'name') do
9
+ expect(described_class.name).to eq('name')
10
+ end
11
+ end
12
+ end
13
+
14
+ context 'when env is not given' do
15
+ it { expect(described_class.name).to be(nil) }
16
+ end
17
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'support/env_mock'
4
+
5
+ RSpec.describe RSpeed::Env, '.pipe' do
6
+ context 'when pipe env is given' do
7
+ it 'returns the number of the current pipe as integer' do
8
+ EnvMock.mock(rspeed_pipe: '2') do
9
+ expect(described_class.pipe).to be(2)
10
+ end
11
+ end
12
+ end
13
+
14
+ context 'when pipe env is not given' do
15
+ it 'returns 1' do
16
+ expect(described_class.pipe).to be(1)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'support/env_mock'
4
+
5
+ RSpec.describe RSpeed::Env, '.pipes' do
6
+ context 'when result is false' do
7
+ before { allow(RSpeed::Redis).to receive(:result?).and_return(false) }
8
+
9
+ context 'when env is setted' do
10
+ it 'returns number 1' do
11
+ EnvMock.mock(rspeed_pipes: '2') do
12
+ expect(described_class.pipes).to be(1)
13
+ end
14
+ end
15
+ end
16
+
17
+ context 'when env is not setted' do
18
+ it 'returns number 1' do
19
+ expect(described_class.pipes).to be(1)
20
+ end
21
+ end
22
+ end
23
+
24
+ context 'when result is true' do
25
+ before { allow(RSpeed::Redis).to receive(:result?).and_return(true) }
26
+
27
+ context 'when env is setted' do
28
+ it 'returns the env value as integer' do
29
+ EnvMock.mock(rspeed_pipes: '2') do
30
+ expect(described_class.pipes).to be(2)
31
+ end
32
+ end
33
+ end
34
+
35
+ context 'when env is not setted' do
36
+ it 'returns number 1' do
37
+ expect(described_class.pipes).to be(1)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'support/env_mock'
4
+
5
+ RSpec.describe RSpeed::Env, '.port' do
6
+ context 'when key is not setted on env' do
7
+ it { expect(described_class.port).to be(nil) }
8
+ end
9
+
10
+ context 'when key is setted on env' do
11
+ it 'returns env value as integer' do
12
+ EnvMock.mock(rspeed_port: '6379') do
13
+ expect(described_class.port).to be(6379)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'support/env_mock'
4
+
5
+ RSpec.describe RSpeed::Env, '.result_key' do
6
+ context 'when key is not setted on env' do
7
+ it 'returns default value' do
8
+ expect(described_class.result_key).to eq 'rspeed'
9
+ end
10
+ end
11
+
12
+ context 'when key is setted on env' do
13
+ it 'returns env value' do
14
+ EnvMock.mock(respeed_result_key: 'result_customer') do
15
+ expect(described_class.result_key).to eq 'result_customer'
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'support/env_mock'
4
+
5
+ RSpec.describe RSpeed::Env, '.rspeed' do
6
+ context 'when env is given' do
7
+ context 'when true as string' do
8
+ it 'true' do
9
+ EnvMock.mock(rspeed: 'true') do
10
+ expect(described_class.rspeed).to be(true)
11
+ end
12
+ end
13
+ end
14
+
15
+ context 'when true' do
16
+ it 'true' do
17
+ EnvMock.mock(rspeed: true) do
18
+ expect(described_class.rspeed).to be(true)
19
+ end
20
+ end
21
+ end
22
+
23
+ context 'when false as string' do
24
+ it 'false' do
25
+ EnvMock.mock(rspeed: 'false') do
26
+ expect(described_class.rspeed).to be(false)
27
+ end
28
+ end
29
+ end
30
+
31
+ context 'when false' do
32
+ it 'false' do
33
+ EnvMock.mock(rspeed: false) do
34
+ expect(described_class.rspeed).to be(false)
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ context 'when env is not given' do
41
+ it { expect(described_class.rspeed).to be(false) }
42
+ end
43
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'support/env_mock'
4
+
5
+ RSpec.describe RSpeed::Env, '.tmp_key' do
6
+ context 'when key is not setted on env' do
7
+ it 'returns default value' do
8
+ expect(described_class.tmp_key).to eq 'rspeed_tmp'
9
+ end
10
+ end
11
+
12
+ context 'when key is setted on env' do
13
+ it 'returns env value' do
14
+ EnvMock.mock(respeed_tmp_key: 'result_customer') do
15
+ expect(described_class.tmp_key).to eq 'result_customer'
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rails_helper'
4
-
5
3
  RSpec.describe RSpeed::Observer, '.after' do
6
4
  let!(:now) { Time.local(2020, 1, 1, 0, 0, 1) }
7
5
  let!(:clock) { class_double(RSpec::Core::Time, now: now) }
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe RSpeed::Observer, '.after' do
4
+ let!(:splitter) { instance_double('RSpeed::Splitter') }
5
+
6
+ before do
7
+ allow(RSpeed::Splitter).to receive(:new).and_return(splitter)
8
+ allow(splitter).to receive(:append)
9
+ end
10
+
11
+ context 'when all specs is not finished' do
12
+ before { allow(RSpeed::Redis).to receive(:specs_finished?).and_return(false) }
13
+
14
+ it 'sets true on pipe key to indicates that its finished' do
15
+ described_class.after_suite
16
+
17
+ expect(RSpeed::Redis.get('rspeed_pipe_1')).to eq('true')
18
+ end
19
+
20
+ it 'appends the time result' do
21
+ described_class.after_suite
22
+
23
+ expect(splitter).to have_received(:append)
24
+ end
25
+ end
26
+
27
+ context 'when all specs finished' do
28
+ before do
29
+ allow(RSpeed::Redis).to receive(:specs_finished?).and_return(true)
30
+ allow(splitter).to receive(:rename)
31
+ allow(RSpeed::Redis).to receive(:clean_pipes_flag)
32
+ end
33
+
34
+ it 'renames the tmp data to the permanent key result' do
35
+ described_class.after_suite
36
+
37
+ expect(splitter).to have_received(:rename)
38
+ end
39
+
40
+ it 'destroyes pipe finished flag keys' do
41
+ described_class.after_suite
42
+
43
+ expect(RSpeed::Redis).to have_received(:clean_pipes_flag)
44
+ end
45
+ end
46
+ end
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rails_helper'
4
-
5
3
  RSpec.describe RSpeed::Observer, '.before' do
6
4
  let!(:now) { Time.local(2020) }
7
5
  let!(:clock) { class_double(RSpec::Core::Time, now: now) }
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rails_helper'
4
-
5
3
  RSpec.describe RSpeed::Observer, '.before_suite' do
6
4
  before { truncate_file }
7
5
 
@@ -12,4 +10,28 @@ RSpec.describe RSpeed::Observer, '.before_suite' do
12
10
 
13
11
  expect(File.open('rspeed.csv').read).to eq ''
14
12
  end
13
+
14
+ context 'when specs are not initiated yet' do
15
+ before { allow(RSpeed::Redis).to receive(:specs_initiated?).and_return(false) }
16
+
17
+ it 'destroyes the tmp result key' do
18
+ RSpeed::Redis.client.lpush(RSpeed::Env.tmp_key, { file: 'file', time: 1.0 }.to_json)
19
+
20
+ described_class.before_suite
21
+
22
+ expect(RSpeed::Splitter.new.get(RSpeed::Variable.tmp)).to eq([])
23
+ end
24
+ end
25
+
26
+ context 'when specs are already initiated' do
27
+ before { allow(RSpeed::Redis).to receive(:specs_initiated?).and_return(true) }
28
+
29
+ it 'does not destroy the tmp result key' do
30
+ RSpeed::Redis.client.lpush(RSpeed::Env.tmp_key, { file: 'file', time: 1.0 }.to_json)
31
+
32
+ described_class.before_suite
33
+
34
+ expect(RSpeed::Splitter.new.get(RSpeed::Variable.tmp)).to eq(['{"file":"file","time":1.0}'])
35
+ end
36
+ end
15
37
  end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe RSpeed::Redis, '.clean_pipes_flag' do
4
+ before do
5
+ described_class.set('rspeed_pipe_1', true)
6
+ described_class.set('rspeed_pipe_2', true)
7
+ end
8
+
9
+ it 'destroy all keys that keeps the pipe finished' do
10
+ described_class.clean_pipes_flag
11
+
12
+ expect(described_class.keys('*')).to eq([])
13
+ end
14
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe RSpeed::Redis, '.client' do
4
+ it 'returns a redis client' do
5
+ expect(described_class.client.class).to eq(Redis)
6
+ end
7
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe RSpeed::Redis, '#destroy' do
4
+ let!(:redis) { described_class }
5
+
6
+ before do
7
+ redis.set('rspeed', '{}')
8
+ redis.set('rspeed_1', '{}')
9
+ redis.set('rspeed_2', '{}')
10
+ end
11
+
12
+ it 'destroys via wildcard' do
13
+ redis.destroy('rspeed_*')
14
+
15
+ expect(redis.keys('*')).to eq %w[rspeed]
16
+ end
17
+
18
+ it 'destroys via single name' do
19
+ redis.destroy('rspeed')
20
+
21
+ expect(redis.keys('*')).to eq %w[rspeed_1 rspeed_2]
22
+ end
23
+
24
+ it 'destroys default partner when no pattern is given' do
25
+ redis.destroy
26
+
27
+ expect(redis.keys('*')).to eq %w[rspeed]
28
+ end
29
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe RSpeed::Redis, '.set' do
4
+ it 'sets a key on redis' do
5
+ described_class.client.set('key', 'value')
6
+
7
+ expect(described_class.get('key')).to eq('value')
8
+ end
9
+ end