cuboid 0.0.0 → 0.0.1alpha
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +0 -0
- data/Gemfile +20 -5
- data/LICENSE.md +22 -0
- data/README.md +158 -19
- data/Rakefile +56 -3
- data/config/paths.yml +15 -0
- data/cuboid.gemspec +61 -23
- data/lib/cuboid.rb +96 -4
- data/lib/cuboid/application.rb +326 -0
- data/lib/cuboid/application/parts/data.rb +18 -0
- data/lib/cuboid/application/parts/report.rb +29 -0
- data/lib/cuboid/application/parts/state.rb +274 -0
- data/lib/cuboid/application/runtime.rb +25 -0
- data/lib/cuboid/banner.rb +13 -0
- data/lib/cuboid/data.rb +86 -0
- data/lib/cuboid/data/application.rb +52 -0
- data/lib/cuboid/error.rb +9 -0
- data/lib/cuboid/option_group.rb +129 -0
- data/lib/cuboid/option_groups.rb +8 -0
- data/lib/cuboid/option_groups/datastore.rb +23 -0
- data/lib/cuboid/option_groups/dispatcher.rb +38 -0
- data/lib/cuboid/option_groups/output.rb +14 -0
- data/lib/cuboid/option_groups/paths.rb +184 -0
- data/lib/cuboid/option_groups/report.rb +39 -0
- data/lib/cuboid/option_groups/rpc.rb +105 -0
- data/lib/cuboid/option_groups/scheduler.rb +27 -0
- data/lib/cuboid/option_groups/snapshot.rb +13 -0
- data/lib/cuboid/option_groups/system.rb +10 -0
- data/lib/cuboid/options.rb +254 -0
- data/lib/cuboid/processes.rb +13 -0
- data/lib/cuboid/processes/dispatchers.rb +140 -0
- data/lib/cuboid/processes/executables/base.rb +54 -0
- data/lib/cuboid/processes/executables/dispatcher.rb +5 -0
- data/lib/cuboid/processes/executables/instance.rb +12 -0
- data/lib/cuboid/processes/executables/rest_service.rb +13 -0
- data/lib/cuboid/processes/executables/scheduler.rb +5 -0
- data/lib/cuboid/processes/helpers.rb +4 -0
- data/lib/cuboid/processes/helpers/dispatchers.rb +23 -0
- data/lib/cuboid/processes/helpers/instances.rb +39 -0
- data/lib/cuboid/processes/helpers/processes.rb +23 -0
- data/lib/cuboid/processes/helpers/schedulers.rb +23 -0
- data/lib/cuboid/processes/instances.rb +203 -0
- data/lib/cuboid/processes/manager.rb +262 -0
- data/lib/cuboid/processes/schedulers.rb +128 -0
- data/lib/cuboid/report.rb +220 -0
- data/lib/cuboid/rest/server.rb +165 -0
- data/lib/cuboid/rest/server/instance_helpers.rb +99 -0
- data/lib/cuboid/rest/server/routes/dispatcher.rb +41 -0
- data/lib/cuboid/rest/server/routes/grid.rb +41 -0
- data/lib/cuboid/rest/server/routes/instances.rb +131 -0
- data/lib/cuboid/rest/server/routes/scheduler.rb +140 -0
- data/lib/cuboid/rpc/client.rb +3 -0
- data/lib/cuboid/rpc/client/base.rb +58 -0
- data/lib/cuboid/rpc/client/dispatcher.rb +58 -0
- data/lib/cuboid/rpc/client/instance.rb +100 -0
- data/lib/cuboid/rpc/client/instance/service.rb +37 -0
- data/lib/cuboid/rpc/client/scheduler.rb +46 -0
- data/lib/cuboid/rpc/serializer.rb +92 -0
- data/lib/cuboid/rpc/server/active_options.rb +38 -0
- data/lib/cuboid/rpc/server/application_wrapper.rb +138 -0
- data/lib/cuboid/rpc/server/base.rb +63 -0
- data/lib/cuboid/rpc/server/dispatcher.rb +317 -0
- data/lib/cuboid/rpc/server/dispatcher/node.rb +247 -0
- data/lib/cuboid/rpc/server/dispatcher/service.rb +145 -0
- data/lib/cuboid/rpc/server/instance.rb +338 -0
- data/lib/cuboid/rpc/server/output.rb +92 -0
- data/lib/cuboid/rpc/server/scheduler.rb +482 -0
- data/lib/cuboid/ruby.rb +4 -0
- data/lib/cuboid/ruby/array.rb +17 -0
- data/lib/cuboid/ruby/hash.rb +41 -0
- data/lib/cuboid/ruby/object.rb +32 -0
- data/lib/cuboid/snapshot.rb +186 -0
- data/lib/cuboid/state.rb +94 -0
- data/lib/cuboid/state/application.rb +309 -0
- data/lib/cuboid/state/options.rb +27 -0
- data/lib/cuboid/support.rb +11 -0
- data/lib/cuboid/support/buffer.rb +3 -0
- data/lib/cuboid/support/buffer/autoflush.rb +61 -0
- data/lib/cuboid/support/buffer/base.rb +91 -0
- data/lib/cuboid/support/cache.rb +7 -0
- data/lib/cuboid/support/cache/base.rb +226 -0
- data/lib/cuboid/support/cache/least_cost_replacement.rb +77 -0
- data/lib/cuboid/support/cache/least_recently_pushed.rb +21 -0
- data/lib/cuboid/support/cache/least_recently_used.rb +31 -0
- data/lib/cuboid/support/cache/preference.rb +31 -0
- data/lib/cuboid/support/cache/random_replacement.rb +20 -0
- data/lib/cuboid/support/crypto.rb +2 -0
- data/lib/cuboid/support/crypto/rsa_aes_cbc.rb +86 -0
- data/lib/cuboid/support/database.rb +5 -0
- data/lib/cuboid/support/database/base.rb +177 -0
- data/lib/cuboid/support/database/categorized_queue.rb +195 -0
- data/lib/cuboid/support/database/hash.rb +300 -0
- data/lib/cuboid/support/database/queue.rb +149 -0
- data/lib/cuboid/support/filter.rb +3 -0
- data/lib/cuboid/support/filter/base.rb +110 -0
- data/lib/cuboid/support/filter/set.rb +29 -0
- data/lib/cuboid/support/glob.rb +27 -0
- data/lib/cuboid/support/mixins.rb +8 -0
- data/lib/cuboid/support/mixins/observable.rb +99 -0
- data/lib/cuboid/support/mixins/parts.rb +20 -0
- data/lib/cuboid/support/mixins/profiler.rb +93 -0
- data/lib/cuboid/support/mixins/spec_instances.rb +65 -0
- data/lib/cuboid/support/mixins/terminal.rb +57 -0
- data/lib/cuboid/system.rb +119 -0
- data/lib/cuboid/system/platforms.rb +84 -0
- data/lib/cuboid/system/platforms/linux.rb +26 -0
- data/lib/cuboid/system/platforms/mixins/unix.rb +46 -0
- data/lib/cuboid/system/platforms/osx.rb +25 -0
- data/lib/cuboid/system/platforms/windows.rb +81 -0
- data/lib/cuboid/system/slots.rb +143 -0
- data/lib/cuboid/ui/output.rb +52 -0
- data/lib/cuboid/ui/output_interface.rb +43 -0
- data/lib/cuboid/ui/output_interface/abstract.rb +68 -0
- data/lib/cuboid/ui/output_interface/controls.rb +84 -0
- data/lib/cuboid/ui/output_interface/error_logging.rb +119 -0
- data/lib/cuboid/ui/output_interface/implemented.rb +58 -0
- data/lib/cuboid/ui/output_interface/personalization.rb +62 -0
- data/lib/cuboid/utilities.rb +155 -0
- data/lib/cuboid/version.rb +4 -3
- data/lib/version +1 -0
- data/logs/placeholder +0 -0
- data/spec/cuboid/application/parts/data_spec.rb +12 -0
- data/spec/cuboid/application/parts/report_spec.rb +6 -0
- data/spec/cuboid/application/parts/state_spec.rb +192 -0
- data/spec/cuboid/application/runtime_spec.rb +21 -0
- data/spec/cuboid/application_spec.rb +37 -0
- data/spec/cuboid/data/application_spec.rb +22 -0
- data/spec/cuboid/data_spec.rb +47 -0
- data/spec/cuboid/error_spec.rb +23 -0
- data/spec/cuboid/option_groups/datastore_spec.rb +54 -0
- data/spec/cuboid/option_groups/dispatcher_spec.rb +12 -0
- data/spec/cuboid/option_groups/output_spec.rb +11 -0
- data/spec/cuboid/option_groups/paths_spec.rb +184 -0
- data/spec/cuboid/option_groups/report_spec.rb +26 -0
- data/spec/cuboid/option_groups/rpc_spec.rb +53 -0
- data/spec/cuboid/option_groups/snapshot_spec.rb +26 -0
- data/spec/cuboid/option_groups/system.rb +12 -0
- data/spec/cuboid/options_spec.rb +218 -0
- data/spec/cuboid/report_spec.rb +221 -0
- data/spec/cuboid/rest/server_spec.rb +1205 -0
- data/spec/cuboid/rpc/client/base_spec.rb +151 -0
- data/spec/cuboid/rpc/client/dispatcher_spec.rb +13 -0
- data/spec/cuboid/rpc/client/instance_spec.rb +38 -0
- data/spec/cuboid/rpc/server/active_options_spec.rb +21 -0
- data/spec/cuboid/rpc/server/base_spec.rb +60 -0
- data/spec/cuboid/rpc/server/dispatcher/node_spec.rb +222 -0
- data/spec/cuboid/rpc/server/dispatcher/service_spec.rb +112 -0
- data/spec/cuboid/rpc/server/dispatcher_spec.rb +317 -0
- data/spec/cuboid/rpc/server/instance_spec.rb +307 -0
- data/spec/cuboid/rpc/server/output_spec.rb +32 -0
- data/spec/cuboid/rpc/server/scheduler_spec.rb +400 -0
- data/spec/cuboid/ruby/array_spec.rb +77 -0
- data/spec/cuboid/ruby/hash_spec.rb +63 -0
- data/spec/cuboid/ruby/object_spec.rb +22 -0
- data/spec/cuboid/snapshot_spec.rb +123 -0
- data/spec/cuboid/state/application_spec.rb +538 -0
- data/spec/cuboid/state/options_spec.rb +37 -0
- data/spec/cuboid/state_spec.rb +53 -0
- data/spec/cuboid/support/buffer/autoflush_spec.rb +78 -0
- data/spec/cuboid/support/buffer/base_spec.rb +193 -0
- data/spec/cuboid/support/cache/least_cost_replacement_spec.rb +61 -0
- data/spec/cuboid/support/cache/least_recently_pushed_spec.rb +90 -0
- data/spec/cuboid/support/cache/least_recently_used_spec.rb +80 -0
- data/spec/cuboid/support/cache/preference_spec.rb +37 -0
- data/spec/cuboid/support/cache/random_replacement_spec.rb +42 -0
- data/spec/cuboid/support/crypto/rsa_aes_cbc_spec.rb +28 -0
- data/spec/cuboid/support/database/categorized_queue_spec.rb +327 -0
- data/spec/cuboid/support/database/hash_spec.rb +204 -0
- data/spec/cuboid/support/database/scheduler_spec.rb +325 -0
- data/spec/cuboid/support/filter/set_spec.rb +19 -0
- data/spec/cuboid/support/glob_spec.rb +75 -0
- data/spec/cuboid/support/mixins/observable_spec.rb +95 -0
- data/spec/cuboid/system/platforms/linux_spec.rb +31 -0
- data/spec/cuboid/system/platforms/osx_spec.rb +32 -0
- data/spec/cuboid/system/platforms/windows_spec.rb +41 -0
- data/spec/cuboid/system/slots_spec.rb +202 -0
- data/spec/cuboid/system_spec.rb +105 -0
- data/spec/cuboid/utilities_spec.rb +131 -0
- data/spec/spec_helper.rb +46 -0
- data/spec/support/factories/placeholder +0 -0
- data/spec/support/factories/scan_report.rb +18 -0
- data/spec/support/fixtures/empty/placeholder +0 -0
- data/spec/support/fixtures/executables/node.rb +50 -0
- data/spec/support/fixtures/mock_app.rb +61 -0
- data/spec/support/fixtures/mock_app/test_service.rb +64 -0
- data/spec/support/fixtures/services/echo.rb +64 -0
- data/spec/support/helpers/framework.rb +3 -0
- data/spec/support/helpers/matchers.rb +5 -0
- data/spec/support/helpers/misc.rb +3 -0
- data/spec/support/helpers/paths.rb +15 -0
- data/spec/support/helpers/request_helpers.rb +38 -0
- data/spec/support/helpers/requires.rb +8 -0
- data/spec/support/helpers/resets.rb +52 -0
- data/spec/support/helpers/web_server.rb +15 -0
- data/spec/support/lib/factory.rb +107 -0
- data/spec/support/lib/web_server_client.rb +41 -0
- data/spec/support/lib/web_server_dispatcher.rb +25 -0
- data/spec/support/lib/web_server_manager.rb +118 -0
- data/spec/support/logs/placeholder +0 -0
- data/spec/support/pems/cacert.pem +37 -0
- data/spec/support/pems/client/cert.pem +37 -0
- data/spec/support/pems/client/foo-cert.pem +39 -0
- data/spec/support/pems/client/foo-key.pem +51 -0
- data/spec/support/pems/client/key.pem +51 -0
- data/spec/support/pems/server/cert.pem +37 -0
- data/spec/support/pems/server/key.pem +51 -0
- data/spec/support/reports/placeholder +0 -0
- data/spec/support/shared/application.rb +10 -0
- data/spec/support/shared/component.rb +31 -0
- data/spec/support/shared/component/options/base.rb +187 -0
- data/spec/support/shared/option_group.rb +98 -0
- data/spec/support/shared/support/cache.rb +419 -0
- data/spec/support/shared/support/filter.rb +143 -0
- data/spec/support/shared/system/platforms/base.rb +25 -0
- data/spec/support/shared/system/platforms/mixins/unix.rb +37 -0
- data/spec/support/snapshots/placeholder +0 -0
- metadata +566 -21
- data/.gitignore +0 -8
- data/bin/console +0 -15
- data/bin/setup +0 -8
@@ -0,0 +1,80 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Cuboid::Support::Cache::LeastRecentlyUsed do
|
4
|
+
it_behaves_like 'cache'
|
5
|
+
|
6
|
+
it 'prunes itself by removing Least Recently Used entries' do
|
7
|
+
subject.max_size = 3
|
8
|
+
|
9
|
+
subject[:k] = '1'
|
10
|
+
subject[:k2] = '2'
|
11
|
+
subject[:k]
|
12
|
+
subject[:k3] = '3'
|
13
|
+
subject[:k4] = '4'
|
14
|
+
|
15
|
+
expect(subject.size).to eq(3)
|
16
|
+
|
17
|
+
expect(subject[:k]).to be_truthy
|
18
|
+
expect(subject[:k4]).to be_truthy
|
19
|
+
expect(subject[:k3]).to be_truthy
|
20
|
+
expect(subject[:k2]).to be_nil
|
21
|
+
end
|
22
|
+
|
23
|
+
describe '#[]=' do
|
24
|
+
it 'stores an object' do
|
25
|
+
v = 'val'
|
26
|
+
expect(subject[:key] = v).to eq(v)
|
27
|
+
expect(subject[:key]).to eq(v)
|
28
|
+
end
|
29
|
+
it 'alias of #store' do
|
30
|
+
v = 'val2'
|
31
|
+
expect(subject.store( :key2, v )).to eq(v)
|
32
|
+
expect(subject[:key2]).to eq(v)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe '#[]' do
|
37
|
+
it 'retrieves an object by key' do
|
38
|
+
v = 'val2'
|
39
|
+
subject[:key] = v
|
40
|
+
expect(subject[:key]).to eq(v)
|
41
|
+
expect(subject.empty?).to be_falsey
|
42
|
+
end
|
43
|
+
|
44
|
+
context 'when the key does not exist' do
|
45
|
+
it 'returns nil' do
|
46
|
+
expect(subject[:some_key]).to be_nil
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe '#delete' do
|
52
|
+
context 'when the key exists' do
|
53
|
+
it 'deletes a key and return its value' do
|
54
|
+
v = 'my_val'
|
55
|
+
subject[:my_key] = v
|
56
|
+
expect(subject.delete( :my_key )).to eq(v)
|
57
|
+
expect(subject[:my_key]).to be_nil
|
58
|
+
expect(subject.include?( :my_key )).to be_falsey
|
59
|
+
end
|
60
|
+
end
|
61
|
+
context 'when the key does not exist' do
|
62
|
+
it 'returns nil' do
|
63
|
+
expect(subject.delete( :my_key2 )).to be_nil
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
describe '#clear' do
|
69
|
+
it 'empties the cache' do
|
70
|
+
subject[:my_key2] = 'v'
|
71
|
+
expect(subject.size).to be > 0
|
72
|
+
expect(subject.empty?).to be_falsey
|
73
|
+
subject.clear
|
74
|
+
|
75
|
+
expect(subject.size).to eq(0)
|
76
|
+
expect(subject.empty?).to be_truthy
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Cuboid::Support::Cache::Preference do
|
4
|
+
it_behaves_like 'cache'
|
5
|
+
|
6
|
+
it 'prunes itself by removing entries returned by the given block' do
|
7
|
+
subject.max_size = 3
|
8
|
+
|
9
|
+
subject.prefer { :k2 }
|
10
|
+
|
11
|
+
k = [ :k, :k2, :k3, :k4 ]
|
12
|
+
subject[k[0]] = '1'
|
13
|
+
subject[k[1]] = '2'
|
14
|
+
subject[k[2]] = '3'
|
15
|
+
subject[k[3]] = '4'
|
16
|
+
expect(subject.size).to eq(3)
|
17
|
+
|
18
|
+
expect(k.map { |key| subject[key] }.count( nil )).to eq(1)
|
19
|
+
|
20
|
+
subject.clear
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'does not remove entries which are not preferred even if the max size has been exceeded' do
|
24
|
+
subject.prefer { :k2 }
|
25
|
+
|
26
|
+
k = [ :k, :k2, :k3, :k4 ]
|
27
|
+
|
28
|
+
subject.max_size = 1
|
29
|
+
subject[k[0]] = '1'
|
30
|
+
subject[k[1]] = '3'
|
31
|
+
subject[k[2]] = '4'
|
32
|
+
expect(subject.size).to eq(2)
|
33
|
+
|
34
|
+
expect(k[0...3].map { |key| subject[key] }.count( nil )).to eq(1)
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Cuboid::Support::Cache::RandomReplacement do
|
4
|
+
it_behaves_like 'cache'
|
5
|
+
|
6
|
+
it 'prunes itself by removing random entries (Random Replacement)' do
|
7
|
+
subject.max_size = 3
|
8
|
+
|
9
|
+
k = [ :k, :k2, :k3, :k4 ]
|
10
|
+
subject[k[0]] = '1'
|
11
|
+
subject[k[1]] = '2'
|
12
|
+
subject[k[2]] = '3'
|
13
|
+
subject[k[3]] = '4'
|
14
|
+
expect(subject.size).to eq(3)
|
15
|
+
|
16
|
+
expect(k.map { |key| subject[key] }.count( nil )).to eq(1)
|
17
|
+
|
18
|
+
subject.clear
|
19
|
+
|
20
|
+
subject.max_size = 1
|
21
|
+
subject[k[0]] = '1'
|
22
|
+
subject[k[1]] = '3'
|
23
|
+
subject[k[2]] = '4'
|
24
|
+
expect(subject.size).to eq(1)
|
25
|
+
|
26
|
+
expect(k[0...3].map { |key| subject[key] }.count( nil )).to eq(2)
|
27
|
+
end
|
28
|
+
|
29
|
+
describe '#[]=' do
|
30
|
+
it 'stores an object' do
|
31
|
+
v = 'val'
|
32
|
+
expect(subject[:key] = v).to eq(v)
|
33
|
+
expect(subject[:key]).to eq(v)
|
34
|
+
end
|
35
|
+
it 'alias of #store' do
|
36
|
+
v = 'val2'
|
37
|
+
expect(subject.store( :key2, v )).to eq(v)
|
38
|
+
expect(subject[:key2]).to eq(v)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'tempfile'
|
3
|
+
|
4
|
+
describe Cuboid::Support::Crypto::RSA_AES_CBC do
|
5
|
+
|
6
|
+
SEED = 'seed data'
|
7
|
+
|
8
|
+
let(:public_key_file_path) do
|
9
|
+
key = private_key.public_key
|
10
|
+
file = Tempfile.new( 'public_key.pem' )
|
11
|
+
file.write( key.to_pem )
|
12
|
+
file.close
|
13
|
+
file.path
|
14
|
+
end
|
15
|
+
let(:private_key_file_path) do
|
16
|
+
file = Tempfile.new( 'private_key.pem' )
|
17
|
+
file.write( private_key.to_pem )
|
18
|
+
file.close
|
19
|
+
file.path
|
20
|
+
end
|
21
|
+
let(:private_key) { OpenSSL::PKey::RSA.generate( 1024 ) }
|
22
|
+
subject { described_class.new( public_key_file_path, private_key_file_path ) }
|
23
|
+
|
24
|
+
it 'generates matching encrypted and decrypted data' do
|
25
|
+
expect(subject.decrypt( subject.encrypt( SEED ) )).to eq(SEED)
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
@@ -0,0 +1,327 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Cuboid::Support::Database::CategorizedQueue do
|
4
|
+
|
5
|
+
class Item
|
6
|
+
attr_accessor :category
|
7
|
+
attr_accessor :data
|
8
|
+
|
9
|
+
def initialize( data )
|
10
|
+
@data = data
|
11
|
+
end
|
12
|
+
|
13
|
+
def ==( other )
|
14
|
+
data == other.data
|
15
|
+
end
|
16
|
+
|
17
|
+
def hash
|
18
|
+
data.hash
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
subject { described_class.new {} }
|
23
|
+
|
24
|
+
let(:sample_size) { 2 * subject.max_buffer_size }
|
25
|
+
|
26
|
+
it 'maintains stability and consistency under load' do
|
27
|
+
subject
|
28
|
+
|
29
|
+
entries = 1000
|
30
|
+
poped = Queue.new
|
31
|
+
t = []
|
32
|
+
|
33
|
+
10.times do
|
34
|
+
t << Thread.new do
|
35
|
+
loop do
|
36
|
+
poped << subject.pop
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
entries.times do |i|
|
42
|
+
subject << Item.new( 'a' * i )
|
43
|
+
end
|
44
|
+
|
45
|
+
sleep 0.1 while !subject.empty?
|
46
|
+
|
47
|
+
consumed = []
|
48
|
+
consumed << poped.pop.data while !poped.empty?
|
49
|
+
|
50
|
+
expect(consumed.sort).to eq((0...entries).map { |i| 'a' * i })
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "#{described_class}::DEFAULT_MAX_BUFFER_SIZE" do
|
54
|
+
it 'returns 100' do
|
55
|
+
expect(described_class::DEFAULT_MAX_BUFFER_SIZE).to eq(100)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe '#initialize' do
|
60
|
+
describe ':max_buffer_size' do
|
61
|
+
it 'sets #max_buffer_size' do
|
62
|
+
expect(
|
63
|
+
described_class.new( max_buffer_size: 1 ){}.max_buffer_size
|
64
|
+
).to eq 1
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
describe ':dumper' do
|
69
|
+
it 'defaults to Marshal'
|
70
|
+
|
71
|
+
context 'it responds to :dump' do
|
72
|
+
it 'gets called to serialize the object'
|
73
|
+
end
|
74
|
+
|
75
|
+
context 'it responds to :call' do
|
76
|
+
it 'gets called to serialize the object'
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe ':load' do
|
81
|
+
context 'it responds to :load' do
|
82
|
+
it 'gets called to unserialize the object'
|
83
|
+
end
|
84
|
+
|
85
|
+
context 'it responds to :call' do
|
86
|
+
it 'gets called to serialize the object'
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
describe '&block' do
|
91
|
+
it 'sets the #prefer block' do
|
92
|
+
prefer = proc {}
|
93
|
+
|
94
|
+
expect( described_class.new( &prefer ).prefer ).to be prefer
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
describe '#max_buffer_size' do
|
100
|
+
context 'by default' do
|
101
|
+
it "returns #{described_class}::DEFAULT_MAX_BUFFER_SIZE" do
|
102
|
+
expect(subject.max_buffer_size).to eq(described_class::DEFAULT_MAX_BUFFER_SIZE)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
describe '#max_buffer_size=' do
|
108
|
+
it 'sets #max_buffer_size' do
|
109
|
+
subject.max_buffer_size = 10
|
110
|
+
expect(subject.max_buffer_size).to eq(10)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
describe '#empty?' do
|
115
|
+
context 'when the queue is empty' do
|
116
|
+
it 'returns true' do
|
117
|
+
expect(subject.empty?).to be_truthy
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
context 'when the queue is not empty' do
|
122
|
+
it 'returns false' do
|
123
|
+
subject << Item.new( :stuff )
|
124
|
+
expect(subject.empty?).to be_falsey
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
describe '#<<' do
|
130
|
+
it 'pushes an object' do
|
131
|
+
sample_size.times do |i|
|
132
|
+
subject << Item.new( "stuff #{i}" )
|
133
|
+
end
|
134
|
+
|
135
|
+
sample_size.times do |i|
|
136
|
+
expect(subject.pop).to eq(Item.new( "stuff #{i}" ))
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
context 'when no #prefer block has been set' do
|
141
|
+
it "raises #{ArgumentError}" do
|
142
|
+
expect {
|
143
|
+
described_class.new << Item.new( '' )
|
144
|
+
}.to raise_error ArgumentError
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
describe '#push' do
|
150
|
+
it 'pushes an object' do
|
151
|
+
subject.push Item.new( :stuff )
|
152
|
+
expect(subject.pop).to eq(Item.new( :stuff ))
|
153
|
+
end
|
154
|
+
|
155
|
+
it 'increments .disk_space' do
|
156
|
+
sz = described_class.disk_space
|
157
|
+
|
158
|
+
subject.max_buffer_size = 0
|
159
|
+
subject.push Item.new( :stuff )
|
160
|
+
|
161
|
+
expect(described_class.disk_space).to be > sz
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
describe '#enq' do
|
166
|
+
it 'pushes an object' do
|
167
|
+
subject.enq Item.new( :stuff )
|
168
|
+
expect(subject.pop).to eq(Item.new( :stuff ))
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
describe '#pop' do
|
173
|
+
it 'removes an object' do
|
174
|
+
sample_size.times do |i|
|
175
|
+
subject << Item.new( "stuff #{i}" )
|
176
|
+
end
|
177
|
+
|
178
|
+
sample_size.times do |i|
|
179
|
+
expect(subject.pop).to eq(Item.new( "stuff #{i}" ))
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
it 'blocks until an entry is available' do
|
184
|
+
val = nil
|
185
|
+
|
186
|
+
t = Thread.new { val = subject.pop }
|
187
|
+
sleep 1
|
188
|
+
Thread.new { subject << Item.new( :stuff ) }
|
189
|
+
t.join
|
190
|
+
|
191
|
+
expect(val).to eq(Item.new( :stuff ))
|
192
|
+
end
|
193
|
+
|
194
|
+
it 'gives preference to the category provided by the block' do
|
195
|
+
subject = described_class.new { :stuff2 }
|
196
|
+
|
197
|
+
subject << Item.new( 1 ).tap { |s| s.category = :stuff }
|
198
|
+
subject << Item.new( 2 ).tap { |s| s.category = :stuff2 }
|
199
|
+
subject << Item.new( 3 ).tap { |s| s.category = :stuff3 }
|
200
|
+
|
201
|
+
expect(subject.pop.data).to eq 2
|
202
|
+
expect(subject.pop.data).to eq 3
|
203
|
+
expect(subject.pop.data).to eq 1
|
204
|
+
end
|
205
|
+
|
206
|
+
it 'decrements .disk_space' do
|
207
|
+
subject.max_buffer_size = 0
|
208
|
+
subject.push Item.new( "stuff" )
|
209
|
+
|
210
|
+
sz = described_class.disk_space
|
211
|
+
subject.pop
|
212
|
+
|
213
|
+
expect(described_class.disk_space).to be < sz
|
214
|
+
end
|
215
|
+
|
216
|
+
context 'when the block specified category is empty' do
|
217
|
+
it 'returns an item from the next one' do
|
218
|
+
subject = described_class.new { :stuff4 }
|
219
|
+
|
220
|
+
subject << Item.new( 1 ).tap { |s| s.category = :stuff }
|
221
|
+
subject << Item.new( 2 ).tap { |s| s.category = :stuff2 }
|
222
|
+
subject << Item.new( 3 ).tap { |s| s.category = :stuff3 }
|
223
|
+
|
224
|
+
expect(subject.pop.data).to eq 3
|
225
|
+
expect(subject.pop.data).to eq 2
|
226
|
+
expect(subject.pop.data).to eq 1
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
context 'when no #prefer block has been set' do
|
231
|
+
it "raises #{ArgumentError}" do
|
232
|
+
expect {
|
233
|
+
described_class.new.pop
|
234
|
+
}.to raise_error ArgumentError
|
235
|
+
end
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
describe '#deq' do
|
240
|
+
it 'removes an object' do
|
241
|
+
subject << Item.new( :stuff )
|
242
|
+
expect(subject.deq).to eq(Item.new( :stuff ))
|
243
|
+
end
|
244
|
+
end
|
245
|
+
|
246
|
+
describe '#shift' do
|
247
|
+
it 'removes an object' do
|
248
|
+
subject << Item.new( :stuff )
|
249
|
+
expect(subject.shift).to eq(Item.new( :stuff ))
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
253
|
+
describe '#size' do
|
254
|
+
it 'returns the size of the queue' do
|
255
|
+
sample_size.times { |i| subject << Item.new( i ) }
|
256
|
+
expect(subject.size).to eq(sample_size)
|
257
|
+
end
|
258
|
+
end
|
259
|
+
|
260
|
+
describe '#free_buffer_size' do
|
261
|
+
it 'returns the size of the available buffer' do
|
262
|
+
(subject.max_buffer_size - 2).times { |i| subject << Item.new( i ) }
|
263
|
+
expect(subject.free_buffer_size).to eq(2)
|
264
|
+
end
|
265
|
+
end
|
266
|
+
|
267
|
+
describe '#buffer_size' do
|
268
|
+
it 'returns the size of the in-memory entries' do
|
269
|
+
expect(subject.buffer_size).to eq(0)
|
270
|
+
|
271
|
+
(subject.max_buffer_size - 1).times { |i| subject << Item.new( i ) }
|
272
|
+
expect(subject.buffer_size).to eq(subject.max_buffer_size - 1)
|
273
|
+
|
274
|
+
subject.clear
|
275
|
+
|
276
|
+
sample_size.times { |i| subject << Item.new( i ) }
|
277
|
+
expect(subject.buffer_size).to eq(subject.max_buffer_size)
|
278
|
+
end
|
279
|
+
end
|
280
|
+
|
281
|
+
describe '#disk_size' do
|
282
|
+
it 'returns the size of the disk entries' do
|
283
|
+
expect(subject.buffer_size).to eq(0)
|
284
|
+
|
285
|
+
(subject.max_buffer_size + 1).times { |i| subject << Item.new( i ) }
|
286
|
+
expect(subject.disk_size).to eq(1)
|
287
|
+
|
288
|
+
subject.clear
|
289
|
+
|
290
|
+
sample_size.times { |i| subject << Item.new( i ) }
|
291
|
+
expect(subject.disk_size).to eq(sample_size - subject.max_buffer_size)
|
292
|
+
end
|
293
|
+
end
|
294
|
+
|
295
|
+
describe '#num_waiting' do
|
296
|
+
it 'returns the amount of threads waiting to pop' do
|
297
|
+
expect(subject.num_waiting).to eq(0)
|
298
|
+
|
299
|
+
2.times do
|
300
|
+
Thread.new { subject.pop }
|
301
|
+
end
|
302
|
+
sleep 0.1
|
303
|
+
|
304
|
+
expect(subject.num_waiting).to eq(2)
|
305
|
+
end
|
306
|
+
end
|
307
|
+
|
308
|
+
describe '#clear' do
|
309
|
+
it 'empties the queue' do
|
310
|
+
sample_size.times { |i| subject << Item.new( i ) }
|
311
|
+
subject.clear
|
312
|
+
expect(subject.size).to eq(0)
|
313
|
+
expect((subject.pop( true ) rescue nil)).to eq(nil)
|
314
|
+
end
|
315
|
+
|
316
|
+
it 'decrements .disk_space' do
|
317
|
+
subject.max_buffer_size = 0
|
318
|
+
subject.push Item.new( "stuff" )
|
319
|
+
|
320
|
+
sz = described_class.disk_space
|
321
|
+
subject.clear
|
322
|
+
|
323
|
+
expect(described_class.disk_space).to be < sz
|
324
|
+
end
|
325
|
+
end
|
326
|
+
|
327
|
+
end
|