cuboid 0.0.0 → 0.0.1alpha

Sign up to get free protection for your applications and to get access to all the features.
Files changed (221) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +0 -0
  3. data/Gemfile +20 -5
  4. data/LICENSE.md +22 -0
  5. data/README.md +158 -19
  6. data/Rakefile +56 -3
  7. data/config/paths.yml +15 -0
  8. data/cuboid.gemspec +61 -23
  9. data/lib/cuboid.rb +96 -4
  10. data/lib/cuboid/application.rb +326 -0
  11. data/lib/cuboid/application/parts/data.rb +18 -0
  12. data/lib/cuboid/application/parts/report.rb +29 -0
  13. data/lib/cuboid/application/parts/state.rb +274 -0
  14. data/lib/cuboid/application/runtime.rb +25 -0
  15. data/lib/cuboid/banner.rb +13 -0
  16. data/lib/cuboid/data.rb +86 -0
  17. data/lib/cuboid/data/application.rb +52 -0
  18. data/lib/cuboid/error.rb +9 -0
  19. data/lib/cuboid/option_group.rb +129 -0
  20. data/lib/cuboid/option_groups.rb +8 -0
  21. data/lib/cuboid/option_groups/datastore.rb +23 -0
  22. data/lib/cuboid/option_groups/dispatcher.rb +38 -0
  23. data/lib/cuboid/option_groups/output.rb +14 -0
  24. data/lib/cuboid/option_groups/paths.rb +184 -0
  25. data/lib/cuboid/option_groups/report.rb +39 -0
  26. data/lib/cuboid/option_groups/rpc.rb +105 -0
  27. data/lib/cuboid/option_groups/scheduler.rb +27 -0
  28. data/lib/cuboid/option_groups/snapshot.rb +13 -0
  29. data/lib/cuboid/option_groups/system.rb +10 -0
  30. data/lib/cuboid/options.rb +254 -0
  31. data/lib/cuboid/processes.rb +13 -0
  32. data/lib/cuboid/processes/dispatchers.rb +140 -0
  33. data/lib/cuboid/processes/executables/base.rb +54 -0
  34. data/lib/cuboid/processes/executables/dispatcher.rb +5 -0
  35. data/lib/cuboid/processes/executables/instance.rb +12 -0
  36. data/lib/cuboid/processes/executables/rest_service.rb +13 -0
  37. data/lib/cuboid/processes/executables/scheduler.rb +5 -0
  38. data/lib/cuboid/processes/helpers.rb +4 -0
  39. data/lib/cuboid/processes/helpers/dispatchers.rb +23 -0
  40. data/lib/cuboid/processes/helpers/instances.rb +39 -0
  41. data/lib/cuboid/processes/helpers/processes.rb +23 -0
  42. data/lib/cuboid/processes/helpers/schedulers.rb +23 -0
  43. data/lib/cuboid/processes/instances.rb +203 -0
  44. data/lib/cuboid/processes/manager.rb +262 -0
  45. data/lib/cuboid/processes/schedulers.rb +128 -0
  46. data/lib/cuboid/report.rb +220 -0
  47. data/lib/cuboid/rest/server.rb +165 -0
  48. data/lib/cuboid/rest/server/instance_helpers.rb +99 -0
  49. data/lib/cuboid/rest/server/routes/dispatcher.rb +41 -0
  50. data/lib/cuboid/rest/server/routes/grid.rb +41 -0
  51. data/lib/cuboid/rest/server/routes/instances.rb +131 -0
  52. data/lib/cuboid/rest/server/routes/scheduler.rb +140 -0
  53. data/lib/cuboid/rpc/client.rb +3 -0
  54. data/lib/cuboid/rpc/client/base.rb +58 -0
  55. data/lib/cuboid/rpc/client/dispatcher.rb +58 -0
  56. data/lib/cuboid/rpc/client/instance.rb +100 -0
  57. data/lib/cuboid/rpc/client/instance/service.rb +37 -0
  58. data/lib/cuboid/rpc/client/scheduler.rb +46 -0
  59. data/lib/cuboid/rpc/serializer.rb +92 -0
  60. data/lib/cuboid/rpc/server/active_options.rb +38 -0
  61. data/lib/cuboid/rpc/server/application_wrapper.rb +138 -0
  62. data/lib/cuboid/rpc/server/base.rb +63 -0
  63. data/lib/cuboid/rpc/server/dispatcher.rb +317 -0
  64. data/lib/cuboid/rpc/server/dispatcher/node.rb +247 -0
  65. data/lib/cuboid/rpc/server/dispatcher/service.rb +145 -0
  66. data/lib/cuboid/rpc/server/instance.rb +338 -0
  67. data/lib/cuboid/rpc/server/output.rb +92 -0
  68. data/lib/cuboid/rpc/server/scheduler.rb +482 -0
  69. data/lib/cuboid/ruby.rb +4 -0
  70. data/lib/cuboid/ruby/array.rb +17 -0
  71. data/lib/cuboid/ruby/hash.rb +41 -0
  72. data/lib/cuboid/ruby/object.rb +32 -0
  73. data/lib/cuboid/snapshot.rb +186 -0
  74. data/lib/cuboid/state.rb +94 -0
  75. data/lib/cuboid/state/application.rb +309 -0
  76. data/lib/cuboid/state/options.rb +27 -0
  77. data/lib/cuboid/support.rb +11 -0
  78. data/lib/cuboid/support/buffer.rb +3 -0
  79. data/lib/cuboid/support/buffer/autoflush.rb +61 -0
  80. data/lib/cuboid/support/buffer/base.rb +91 -0
  81. data/lib/cuboid/support/cache.rb +7 -0
  82. data/lib/cuboid/support/cache/base.rb +226 -0
  83. data/lib/cuboid/support/cache/least_cost_replacement.rb +77 -0
  84. data/lib/cuboid/support/cache/least_recently_pushed.rb +21 -0
  85. data/lib/cuboid/support/cache/least_recently_used.rb +31 -0
  86. data/lib/cuboid/support/cache/preference.rb +31 -0
  87. data/lib/cuboid/support/cache/random_replacement.rb +20 -0
  88. data/lib/cuboid/support/crypto.rb +2 -0
  89. data/lib/cuboid/support/crypto/rsa_aes_cbc.rb +86 -0
  90. data/lib/cuboid/support/database.rb +5 -0
  91. data/lib/cuboid/support/database/base.rb +177 -0
  92. data/lib/cuboid/support/database/categorized_queue.rb +195 -0
  93. data/lib/cuboid/support/database/hash.rb +300 -0
  94. data/lib/cuboid/support/database/queue.rb +149 -0
  95. data/lib/cuboid/support/filter.rb +3 -0
  96. data/lib/cuboid/support/filter/base.rb +110 -0
  97. data/lib/cuboid/support/filter/set.rb +29 -0
  98. data/lib/cuboid/support/glob.rb +27 -0
  99. data/lib/cuboid/support/mixins.rb +8 -0
  100. data/lib/cuboid/support/mixins/observable.rb +99 -0
  101. data/lib/cuboid/support/mixins/parts.rb +20 -0
  102. data/lib/cuboid/support/mixins/profiler.rb +93 -0
  103. data/lib/cuboid/support/mixins/spec_instances.rb +65 -0
  104. data/lib/cuboid/support/mixins/terminal.rb +57 -0
  105. data/lib/cuboid/system.rb +119 -0
  106. data/lib/cuboid/system/platforms.rb +84 -0
  107. data/lib/cuboid/system/platforms/linux.rb +26 -0
  108. data/lib/cuboid/system/platforms/mixins/unix.rb +46 -0
  109. data/lib/cuboid/system/platforms/osx.rb +25 -0
  110. data/lib/cuboid/system/platforms/windows.rb +81 -0
  111. data/lib/cuboid/system/slots.rb +143 -0
  112. data/lib/cuboid/ui/output.rb +52 -0
  113. data/lib/cuboid/ui/output_interface.rb +43 -0
  114. data/lib/cuboid/ui/output_interface/abstract.rb +68 -0
  115. data/lib/cuboid/ui/output_interface/controls.rb +84 -0
  116. data/lib/cuboid/ui/output_interface/error_logging.rb +119 -0
  117. data/lib/cuboid/ui/output_interface/implemented.rb +58 -0
  118. data/lib/cuboid/ui/output_interface/personalization.rb +62 -0
  119. data/lib/cuboid/utilities.rb +155 -0
  120. data/lib/cuboid/version.rb +4 -3
  121. data/lib/version +1 -0
  122. data/logs/placeholder +0 -0
  123. data/spec/cuboid/application/parts/data_spec.rb +12 -0
  124. data/spec/cuboid/application/parts/report_spec.rb +6 -0
  125. data/spec/cuboid/application/parts/state_spec.rb +192 -0
  126. data/spec/cuboid/application/runtime_spec.rb +21 -0
  127. data/spec/cuboid/application_spec.rb +37 -0
  128. data/spec/cuboid/data/application_spec.rb +22 -0
  129. data/spec/cuboid/data_spec.rb +47 -0
  130. data/spec/cuboid/error_spec.rb +23 -0
  131. data/spec/cuboid/option_groups/datastore_spec.rb +54 -0
  132. data/spec/cuboid/option_groups/dispatcher_spec.rb +12 -0
  133. data/spec/cuboid/option_groups/output_spec.rb +11 -0
  134. data/spec/cuboid/option_groups/paths_spec.rb +184 -0
  135. data/spec/cuboid/option_groups/report_spec.rb +26 -0
  136. data/spec/cuboid/option_groups/rpc_spec.rb +53 -0
  137. data/spec/cuboid/option_groups/snapshot_spec.rb +26 -0
  138. data/spec/cuboid/option_groups/system.rb +12 -0
  139. data/spec/cuboid/options_spec.rb +218 -0
  140. data/spec/cuboid/report_spec.rb +221 -0
  141. data/spec/cuboid/rest/server_spec.rb +1205 -0
  142. data/spec/cuboid/rpc/client/base_spec.rb +151 -0
  143. data/spec/cuboid/rpc/client/dispatcher_spec.rb +13 -0
  144. data/spec/cuboid/rpc/client/instance_spec.rb +38 -0
  145. data/spec/cuboid/rpc/server/active_options_spec.rb +21 -0
  146. data/spec/cuboid/rpc/server/base_spec.rb +60 -0
  147. data/spec/cuboid/rpc/server/dispatcher/node_spec.rb +222 -0
  148. data/spec/cuboid/rpc/server/dispatcher/service_spec.rb +112 -0
  149. data/spec/cuboid/rpc/server/dispatcher_spec.rb +317 -0
  150. data/spec/cuboid/rpc/server/instance_spec.rb +307 -0
  151. data/spec/cuboid/rpc/server/output_spec.rb +32 -0
  152. data/spec/cuboid/rpc/server/scheduler_spec.rb +400 -0
  153. data/spec/cuboid/ruby/array_spec.rb +77 -0
  154. data/spec/cuboid/ruby/hash_spec.rb +63 -0
  155. data/spec/cuboid/ruby/object_spec.rb +22 -0
  156. data/spec/cuboid/snapshot_spec.rb +123 -0
  157. data/spec/cuboid/state/application_spec.rb +538 -0
  158. data/spec/cuboid/state/options_spec.rb +37 -0
  159. data/spec/cuboid/state_spec.rb +53 -0
  160. data/spec/cuboid/support/buffer/autoflush_spec.rb +78 -0
  161. data/spec/cuboid/support/buffer/base_spec.rb +193 -0
  162. data/spec/cuboid/support/cache/least_cost_replacement_spec.rb +61 -0
  163. data/spec/cuboid/support/cache/least_recently_pushed_spec.rb +90 -0
  164. data/spec/cuboid/support/cache/least_recently_used_spec.rb +80 -0
  165. data/spec/cuboid/support/cache/preference_spec.rb +37 -0
  166. data/spec/cuboid/support/cache/random_replacement_spec.rb +42 -0
  167. data/spec/cuboid/support/crypto/rsa_aes_cbc_spec.rb +28 -0
  168. data/spec/cuboid/support/database/categorized_queue_spec.rb +327 -0
  169. data/spec/cuboid/support/database/hash_spec.rb +204 -0
  170. data/spec/cuboid/support/database/scheduler_spec.rb +325 -0
  171. data/spec/cuboid/support/filter/set_spec.rb +19 -0
  172. data/spec/cuboid/support/glob_spec.rb +75 -0
  173. data/spec/cuboid/support/mixins/observable_spec.rb +95 -0
  174. data/spec/cuboid/system/platforms/linux_spec.rb +31 -0
  175. data/spec/cuboid/system/platforms/osx_spec.rb +32 -0
  176. data/spec/cuboid/system/platforms/windows_spec.rb +41 -0
  177. data/spec/cuboid/system/slots_spec.rb +202 -0
  178. data/spec/cuboid/system_spec.rb +105 -0
  179. data/spec/cuboid/utilities_spec.rb +131 -0
  180. data/spec/spec_helper.rb +46 -0
  181. data/spec/support/factories/placeholder +0 -0
  182. data/spec/support/factories/scan_report.rb +18 -0
  183. data/spec/support/fixtures/empty/placeholder +0 -0
  184. data/spec/support/fixtures/executables/node.rb +50 -0
  185. data/spec/support/fixtures/mock_app.rb +61 -0
  186. data/spec/support/fixtures/mock_app/test_service.rb +64 -0
  187. data/spec/support/fixtures/services/echo.rb +64 -0
  188. data/spec/support/helpers/framework.rb +3 -0
  189. data/spec/support/helpers/matchers.rb +5 -0
  190. data/spec/support/helpers/misc.rb +3 -0
  191. data/spec/support/helpers/paths.rb +15 -0
  192. data/spec/support/helpers/request_helpers.rb +38 -0
  193. data/spec/support/helpers/requires.rb +8 -0
  194. data/spec/support/helpers/resets.rb +52 -0
  195. data/spec/support/helpers/web_server.rb +15 -0
  196. data/spec/support/lib/factory.rb +107 -0
  197. data/spec/support/lib/web_server_client.rb +41 -0
  198. data/spec/support/lib/web_server_dispatcher.rb +25 -0
  199. data/spec/support/lib/web_server_manager.rb +118 -0
  200. data/spec/support/logs/placeholder +0 -0
  201. data/spec/support/pems/cacert.pem +37 -0
  202. data/spec/support/pems/client/cert.pem +37 -0
  203. data/spec/support/pems/client/foo-cert.pem +39 -0
  204. data/spec/support/pems/client/foo-key.pem +51 -0
  205. data/spec/support/pems/client/key.pem +51 -0
  206. data/spec/support/pems/server/cert.pem +37 -0
  207. data/spec/support/pems/server/key.pem +51 -0
  208. data/spec/support/reports/placeholder +0 -0
  209. data/spec/support/shared/application.rb +10 -0
  210. data/spec/support/shared/component.rb +31 -0
  211. data/spec/support/shared/component/options/base.rb +187 -0
  212. data/spec/support/shared/option_group.rb +98 -0
  213. data/spec/support/shared/support/cache.rb +419 -0
  214. data/spec/support/shared/support/filter.rb +143 -0
  215. data/spec/support/shared/system/platforms/base.rb +25 -0
  216. data/spec/support/shared/system/platforms/mixins/unix.rb +37 -0
  217. data/spec/support/snapshots/placeholder +0 -0
  218. metadata +566 -21
  219. data/.gitignore +0 -8
  220. data/bin/console +0 -15
  221. data/bin/setup +0 -8
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cuboid::State::Options do
4
+
5
+ subject { described_class.new }
6
+ let(:dump_directory) do
7
+ "#{Dir.tmpdir}/options-#{Cuboid::Utilities.generate_token}"
8
+ end
9
+
10
+ it { is_expected.to respond_to :clear}
11
+
12
+ describe '#statistics' do
13
+ let(:statistics) { subject.statistics }
14
+ end
15
+
16
+ describe '#dump' do
17
+ it 'stores to disk' do
18
+ Cuboid::Options.datastore.my_custom_option = 'my value'
19
+ subject.dump( dump_directory )
20
+
21
+ expect(Cuboid::Options.load( "#{dump_directory}/options" ).
22
+ datastore.my_custom_option).to eq('my value')
23
+ end
24
+ end
25
+
26
+ describe '.load' do
27
+ it 'restores from disk' do
28
+ Cuboid::Options.datastore.my_custom_option = 'my value'
29
+ subject.dump( dump_directory )
30
+
31
+ described_class.load( dump_directory )
32
+
33
+ expect(Cuboid::Options.datastore.my_custom_option).to eq('my value')
34
+ end
35
+ end
36
+
37
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cuboid::State do
4
+
5
+ subject { described_class }
6
+ let(:dump_directory) do
7
+ "#{Dir.tmpdir}/state-#{Cuboid::Utilities.generate_token}"
8
+ end
9
+
10
+ describe '#application' do
11
+ it "returns an instance of #{described_class::Application}" do
12
+ expect(subject.application).to be_kind_of described_class::Application
13
+ end
14
+ end
15
+
16
+ describe '#options' do
17
+ it "returns an instance of #{described_class::Options}" do
18
+ expect(subject.options).to be_kind_of described_class::Options
19
+ end
20
+ end
21
+
22
+ describe '#statistics' do
23
+ %w(options application).each do |name|
24
+ it "includes :#{name} statistics" do
25
+ expect(subject.statistics[name.to_sym]).to eq(subject.send(name).statistics)
26
+ end
27
+ end
28
+ end
29
+
30
+ describe '.dump' do
31
+ %w(options application).each do |name|
32
+ it "stores ##{name} to disk" do
33
+ previous_instance = subject.send(name)
34
+
35
+ subject.dump( dump_directory )
36
+
37
+ new_instance = subject.load( dump_directory ).send(name)
38
+
39
+ expect(new_instance).to be_kind_of subject.send(name).class
40
+ expect(new_instance.object_id).not_to eq(previous_instance.object_id)
41
+ end
42
+ end
43
+ end
44
+
45
+ describe '#clear' do
46
+ %w(options application).each do |method|
47
+ it "clears ##{method}" do
48
+ expect(subject.send(method)).to receive(:clear).at_least(:once)
49
+ subject.clear
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,78 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cuboid::Support::Buffer::AutoFlush do
4
+ describe '#initialize' do
5
+ context 'when passed a max_size' do
6
+ context 'when the buffer reaches that size' do
7
+ it 'forces the buffer to #flush itself' do
8
+ b = described_class.new( 10 )
9
+
10
+ buffers = []
11
+ b.on_flush do |buffer|
12
+ buffers << buffer
13
+ end
14
+
15
+ 20.times { |i| b << i }
16
+
17
+ expect(buffers.size).to eq(2)
18
+ expect(buffers.shift).to eq((0..9).to_a)
19
+ expect(buffers.shift).to eq((10...20).to_a)
20
+
21
+ expect(b).to be_empty
22
+ end
23
+ end
24
+ end
25
+
26
+ context 'when passed a max_pushes' do
27
+ context 'when the amount of pushes reaches that size' do
28
+ it 'forces the buffer to #flush itself' do
29
+ b = described_class.new( 99999, 10 )
30
+
31
+ buffers = []
32
+ b.on_flush do |buffer|
33
+ buffers << buffer
34
+ end
35
+
36
+ 20.times { |i| b << i }
37
+
38
+ expect(buffers.size).to eq(2)
39
+ expect(buffers.shift).to eq((0..9).to_a)
40
+ expect(buffers.shift).to eq((10...20).to_a)
41
+ expect(b).to be_empty
42
+
43
+ b = described_class.new( 99999, 10 )
44
+
45
+ buffers = []
46
+ b.on_flush do |buffer|
47
+ buffers << buffer
48
+ end
49
+
50
+ 20.times { |i| b.batch_push (0..1000).to_a }
51
+
52
+ expect(buffers.size).to eq(2)
53
+ expect(buffers.shift).to eq((0..1000).to_a)
54
+ expect(buffers.shift).to eq((0..1000).to_a)
55
+ expect(b).to be_empty
56
+ end
57
+ end
58
+ end
59
+
60
+ context 'when passed a type' do
61
+ it 'should be used for internal storage' do
62
+ b = described_class.new( 10, 999, Set )
63
+ b << 'test'
64
+ b << 'test'
65
+ expect(b.size).to eq(1)
66
+ expect(b.flush.class).to eq(Set)
67
+
68
+ b = described_class.new
69
+ b << 'test'
70
+ b << 'test'
71
+ expect(b.size).to eq(2)
72
+
73
+ expect(b.flush.class).to eq(Array)
74
+ end
75
+ end
76
+ end
77
+
78
+ end
@@ -0,0 +1,193 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cuboid::Support::Buffer::Base do
4
+
5
+ describe '#initialize' do
6
+ context 'when passed a max_size' do
7
+ it 'determines whether or not the buffer is full' do
8
+ b = described_class.new( 10 )
9
+ 20.times { |i| b << i }
10
+ expect(b.full?).to be_truthy
11
+ end
12
+ end
13
+
14
+ context 'when passed a type' do
15
+ it 'determines the type to use for internal storage' do
16
+ b = described_class.new( 10, Set )
17
+ b << 'test'
18
+ b << 'test'
19
+ expect(b.size).to eq(1)
20
+ expect(b.flush.class).to eq(Set)
21
+
22
+ b = described_class.new
23
+ b << 'test'
24
+ b << 'test'
25
+ expect(b.size).to eq(2)
26
+
27
+ expect(b.flush.class).to eq(Array)
28
+ end
29
+ end
30
+ end
31
+
32
+ describe '#<<' do
33
+ it 'adds an element to the buffer' do
34
+ b = described_class.new
35
+ b << 'test'
36
+ b << 'test'
37
+ expect(b.size).to eq(2)
38
+ end
39
+ it 'aliased to #push' do
40
+ b = described_class.new
41
+ b.push 'test'
42
+ b.push 'test'
43
+ expect(b.size).to eq(2)
44
+ end
45
+ end
46
+
47
+ describe '#batch_push' do
48
+ it 'pushes a batch of entries' do
49
+ b = described_class.new
50
+ b.batch_push [ 'test', 'test2' ]
51
+ expect(b.size).to eq(2)
52
+ end
53
+ end
54
+
55
+ describe '#size' do
56
+ it 'returns the number of entries in the buffer' do
57
+ b = described_class.new
58
+ b.batch_push [ 'test', 'test2', 'test3' ]
59
+ expect(b.size).to eq(3)
60
+ end
61
+ end
62
+
63
+ describe '#empty?' do
64
+ context 'when the buffer' do
65
+ context 'is empty' do
66
+ it 'returns true' do
67
+ b = described_class.new( 10 )
68
+ expect(b.empty?).to be_truthy
69
+ end
70
+ end
71
+ context 'is not empty' do
72
+ it 'returns false' do
73
+ b = described_class.new( 10 )
74
+ b << 1
75
+ expect(b.empty?).to be_falsey
76
+ end
77
+ end
78
+ end
79
+ end
80
+
81
+ describe '#full?' do
82
+ context 'when the buffer has' do
83
+ context 'reached its maximum size' do
84
+ it 'returns true' do
85
+ b = described_class.new( 10 )
86
+ 20.times { |i| b << i }
87
+ expect(b.full?).to be_truthy
88
+ end
89
+ end
90
+ context 'not reached its maximum size' do
91
+ it 'returns false' do
92
+ b = described_class.new( 100 )
93
+ 20.times { |i| b << i }
94
+ expect(b.full?).to be_falsey
95
+ end
96
+ end
97
+ end
98
+ end
99
+
100
+ describe '#flush' do
101
+ it 'returns buffer contents' do
102
+ b = described_class.new
103
+ b.batch_push [ 'test', 'test2', 'test3' ]
104
+ expect(b.size).to eq(3)
105
+
106
+ expect(b.flush).to eq([ 'test', 'test2', 'test3' ])
107
+ expect(b.size).to eq(0)
108
+ end
109
+ it 'empties the buffer' do
110
+ b = described_class.new
111
+ b.batch_push [ 'test', 'test2', 'test3' ]
112
+ expect(b.size).to eq(3)
113
+
114
+ expect(b.flush).to eq([ 'test', 'test2', 'test3' ])
115
+ expect(b.size).to eq(0)
116
+ end
117
+ end
118
+
119
+ describe '#on_push' do
120
+ it 'adds blocks to be called every time #<< (or #push) is called' do
121
+ item = :ya
122
+
123
+ b = described_class.new
124
+
125
+ call_args = []
126
+ expect(b.on_push do |buffer|
127
+ call_args << buffer
128
+ end).to eq(b)
129
+
130
+ expect(b.on_push do |buffer|
131
+ call_args << buffer
132
+ end).to eq(b)
133
+
134
+ b << item
135
+ expect(call_args).to eq([ item, item])
136
+
137
+ b = described_class.new
138
+
139
+ call_args = []
140
+ expect(b.on_push do |buffer|
141
+ call_args << buffer
142
+ end).to eq(b)
143
+
144
+ expect(b.on_push do |buffer|
145
+ call_args << buffer
146
+ end).to eq(b)
147
+
148
+ b.push item
149
+ expect(call_args).to eq([ item, item])
150
+ end
151
+ end
152
+
153
+ describe '#on_batch_push' do
154
+ it 'adds blocks to be called every time #batch_push is called' do
155
+ item = [:ya, :ya1]
156
+
157
+ b = described_class.new
158
+
159
+ call_args = []
160
+ expect(b.on_batch_push do |buffer|
161
+ call_args << buffer
162
+ end).to eq(b)
163
+
164
+ expect(b.on_batch_push do |buffer|
165
+ call_args << buffer
166
+ end).to eq(b)
167
+
168
+ b.batch_push item
169
+ expect(call_args).to eq([ item, item])
170
+ end
171
+ end
172
+
173
+ describe '#on_flush' do
174
+ it 'adds blocks to be called every time #flush is called' do
175
+ item = :ya
176
+
177
+ b = described_class.new
178
+ b << item
179
+
180
+ call_args = []
181
+ expect(b.on_flush do |buffer|
182
+ call_args << buffer
183
+ end).to eq(b)
184
+
185
+ expect(b.on_flush do |buffer|
186
+ call_args << buffer
187
+ end).to eq(b)
188
+
189
+ b.flush
190
+ expect(call_args).to eq([ [item], [item]])
191
+ end
192
+ end
193
+ end
@@ -0,0 +1,61 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cuboid::Support::Cache::LeastCostReplacement do
4
+ it_behaves_like 'cache'
5
+
6
+ it 'prunes itself by removing the least costly entries' do
7
+ subject.max_size = 3
8
+
9
+ subject.store( :k, '1', :high )
10
+ subject.store( :k2, '2', :high )
11
+ subject.store( :k3, '3', :medium )
12
+ subject.store( :k4, '4', :low )
13
+ expect(subject.size).to eq(3)
14
+
15
+ expect(subject[:k4]).to be_truthy
16
+ expect(subject[:k3]).to be_nil
17
+ expect(subject[:k2]).to be_truthy
18
+ expect(subject[:k]).to be_truthy
19
+
20
+ subject.clear
21
+
22
+ subject.max_size = 1
23
+
24
+ subject.store( :k, '1', :medium )
25
+ subject.store( :k2, '2', :low )
26
+ subject.store( :k3, '3', :low )
27
+ subject.store( :k4, '4', :low )
28
+ expect(subject.size).to eq(1)
29
+
30
+ expect(subject[:k4]).to be_truthy
31
+ expect(subject[:k3]).to be_nil
32
+ expect(subject[:k2]).to be_nil
33
+ expect(subject[:k]).to be_nil
34
+ end
35
+
36
+ describe '#store' do
37
+ it 'stores an object by key' do
38
+ v = 'val'
39
+ expect(subject.store( :key, v, :low )).to eq(v)
40
+ expect(subject[:key]).to eq(v)
41
+ end
42
+ it 'assigns cost to object' do
43
+ v = 'val'
44
+ expect(subject.store( :key, v, :low )).to eq(v)
45
+ expect(subject[:key]).to eq(v)
46
+ end
47
+ end
48
+
49
+ describe '#[]=' do
50
+ it 'stores an object' do
51
+ v = 'val'
52
+ expect(subject[:key] = v).to eq(v)
53
+ expect(subject[:key]).to eq(v)
54
+ end
55
+ it 'alias of #store' do
56
+ v = 'val2'
57
+ expect(subject.store( :key2, v )).to eq(v)
58
+ expect(subject[:key2]).to eq(v)
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,90 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cuboid::Support::Cache::LeastRecentlyPushed do
4
+ it_behaves_like 'cache'
5
+
6
+ it 'prunes itself by removing Least Recently Pushed entries' do
7
+ subject.max_size = 3
8
+
9
+ subject[:k] = '1'
10
+ subject[:k2] = '2'
11
+ subject[:k3] = '3'
12
+ subject[:k4] = '4'
13
+ expect(subject.size).to eq(3)
14
+
15
+ expect(subject[:k4]).to be_truthy
16
+ expect(subject[:k3]).to be_truthy
17
+ expect(subject[:k2]).to be_truthy
18
+ expect(subject[:k]).to be_nil
19
+
20
+ subject.clear
21
+
22
+ subject.max_size = 1
23
+ subject[:k] = '1'
24
+ subject[:k2] = '3'
25
+ subject[:k3] = '4'
26
+ expect(subject.size).to eq(1)
27
+
28
+ expect(subject[:k3]).to be_truthy
29
+ expect(subject[:k2]).to be_nil
30
+ expect(subject[:k]).to be_nil
31
+ end
32
+
33
+ describe '#[]=' do
34
+ it 'stores an object' do
35
+ v = 'val'
36
+ expect(subject[:key] = v).to eq(v)
37
+ expect(subject[:key]).to eq(v)
38
+ end
39
+ it 'alias of #store' do
40
+ v = 'val2'
41
+ expect(subject.store( :key2, v )).to eq(v)
42
+ expect(subject[:key2]).to eq(v)
43
+ end
44
+ end
45
+
46
+ describe '#[]' do
47
+ it 'retrieves an object by key' do
48
+ v = 'val2'
49
+ subject[:key] = v
50
+ expect(subject[:key]).to eq(v)
51
+ expect(subject.empty?).to be_falsey
52
+ end
53
+
54
+ context 'when the key does not exist' do
55
+ it 'returns nil' do
56
+ expect(subject[:some_key]).to be_nil
57
+ end
58
+ end
59
+ end
60
+
61
+ describe '#delete' do
62
+ context 'when the key exists' do
63
+ it 'deletes a key and return its value' do
64
+ v = 'my_val'
65
+ subject[:my_key] = v
66
+ expect(subject.delete( :my_key )).to eq(v)
67
+ expect(subject[:my_key]).to be_nil
68
+ expect(subject.include?( :my_key )).to be_falsey
69
+ end
70
+ end
71
+ context 'when the key does not exist' do
72
+ it 'returns nil' do
73
+ expect(subject.delete( :my_key2 )).to be_nil
74
+ end
75
+ end
76
+ end
77
+
78
+ describe '#clear' do
79
+ it 'empties the cache' do
80
+ subject[:my_key2] = 'v'
81
+ expect(subject.size).to be > 0
82
+ expect(subject.empty?).to be_falsey
83
+ subject.clear
84
+
85
+ expect(subject.size).to eq(0)
86
+ expect(subject.empty?).to be_truthy
87
+ end
88
+ end
89
+
90
+ end