Class: TestIds::Allocator
- Inherits:
-
Object
- Object
- TestIds::Allocator
- Defined in:
- lib/test_ids/allocator.rb
Overview
The allocator is responsible for assigning new numbers and keeping a record of existing assignments.
There is one allocator instance per configuration, and each has its own database file.
Constant Summary collapse
- STORE_FORMAT_REVISION =
1
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
-
#allocate(instance, options) ⇒ Object
Main method to inject generated bin and test numbers, the given options instance is modified accordingly.
-
#file ⇒ Object
Returns a path to the file that will be used to store the allocated bins/numbers, returns nil if remote storage not enabled.
- #id ⇒ Object
-
#initialize(configuration) ⇒ Allocator
constructor
A new instance of Allocator.
-
#save ⇒ Object
Saves the current allocator state to the repository.
- #store ⇒ Object
Constructor Details
#initialize(configuration) ⇒ Allocator
Returns a new instance of Allocator.
12 13 14 |
# File 'lib/test_ids/allocator.rb', line 12 def initialize(configuration) @config = configuration end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
10 11 12 |
# File 'lib/test_ids/allocator.rb', line 10 def config @config end |
Instance Method Details
#allocate(instance, options) ⇒ Object
Main method to inject generated bin and test numbers, the given options instance is modified accordingly
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/test_ids/allocator.rb', line 18 def allocate(instance, ) clean() @callbacks = [] name = extract_test_name(instance, ) name = "#{name}_#{options[:index]}" if [:index] # First work out the test ID to be used for each of the numbers, and how many numbers # should be reserved if ([:bin].is_a?(Symbol) || [:bin].is_a?(String)) && [:bin] != :none bin_id = [:bin].to_s else bin_id = name end if ([:softbin].is_a?(Symbol) || [:softbin].is_a?(String)) && [:softbin] != :none softbin_id = [:softbin].to_s else softbin_id = name end if ([:number].is_a?(Symbol) || [:number].is_a?(String)) && [:number] != :none number_id = [:number].to_s else number_id = name end bin_size = [:bin_size] || config.bins.size softbin_size = [:softbin_size] || config.softbins.size number_size = [:number_size] || config.numbers.size bin = store['assigned']['bin'][bin_id] ||= {} softbin = store['assigned']['softbin'][softbin_id] ||= {} number = store['assigned']['number'][number_id] ||= {} # If the user has supplied any of these, that number should be used # and reserved so that it is not automatically generated later if [:bin] && [:bin].is_a?(Numeric) bin['number'] = [:bin] bin['size'] = bin_size store['manually_assigned']['bin'][[:bin].to_s] = true # Regenerate the bin if the original allocation has since been applied # manually elsewhere elsif store['manually_assigned']['bin'][bin['number'].to_s] bin['number'] = nil bin['size'] = nil # Also regenerate these as they could be a function of the bin if config.softbins.function? softbin['number'] = nil softbin['size'] = nil end if config.numbers.function? number['number'] = nil number['size'] = nil end end if [:softbin] && [:softbin].is_a?(Numeric) softbin['number'] = [:softbin] softbin['size'] = softbin_size store['manually_assigned']['softbin'][[:softbin].to_s] = true elsif store['manually_assigned']['softbin'][softbin['number'].to_s] softbin['number'] = nil softbin['size'] = nil # Also regenerate the number as it could be a function of the softbin if config.numbers.function? number['number'] = nil number['size'] = nil end end if [:number] && [:number].is_a?(Numeric) number['number'] = [:number] number['size'] = number_size store['manually_assigned']['number'][[:number].to_s] = true elsif store['manually_assigned']['number'][number['number'].to_s] number['number'] = nil number['size'] = nil end # Otherwise generate the missing ones bin['number'] ||= allocate_bin(size: bin_size) bin['size'] ||= bin_size softbin['number'] ||= allocate_softbin(bin: bin['number'], size: softbin_size) softbin['size'] ||= softbin_size number['number'] ||= allocate_number(bin: bin['number'], softbin: softbin['number'], size: number_size) number['size'] ||= number_size # Record that there has been a reference to the final numbers time = Time.now.to_f bin_size.times do |i| store['references']['bin'][(bin['number'] + i).to_s] = time if bin['number'] && [:bin] != :none end softbin_size.times do |i| store['references']['softbin'][(softbin['number'] + i).to_s] = time if softbin['number'] && [:softbin] != :none end number_size.times do |i| store['references']['number'][(number['number'] + i).to_s] = time if number['number'] && [:number] != :none end # Update the supplied options hash that will be forwarded to the program generator unless .delete(:bin) == :none [:bin] = bin['number'] [:bin_size] = bin['size'] end unless .delete(:softbin) == :none [:softbin] = softbin['number'] [:softbin_size] = softbin['size'] end unless .delete(:number) == :none [:number] = number['number'] [:number_size] = number['size'] end end |
#file ⇒ Object
Returns a path to the file that will be used to store the allocated bins/numbers, returns nil if remote storage not enabled
179 180 181 |
# File 'lib/test_ids/allocator.rb', line 179 def file TestIds.database_file(id) end |
#id ⇒ Object
183 184 185 |
# File 'lib/test_ids/allocator.rb', line 183 def id config.id end |
#save ⇒ Object
Saves the current allocator state to the repository
166 167 168 169 170 171 172 173 174 175 |
# File 'lib/test_ids/allocator.rb', line 166 def save if file # Ensure the current store has been loaded before we try to re-write it, this # is necessary if the program generator has crashed before creating a test store p = Pathname.new(file) FileUtils.mkdir_p(p.dirname) File.open(p, 'w') { |f| f.puts JSON.pretty_generate(store) } end end |
#store ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/test_ids/allocator.rb', line 129 def store @store ||= begin s = JSON.load(File.read(file)) if file && File.exist?(file) if s if s['format_revision'] != STORE_FORMAT_REVISION # Upgrade the original store format t = { 'bin' => {}, 'softbin' => {}, 'number' => {} } s['tests'].each do |name, numbers| t['bin'][name] = { 'number' => numbers['bin'], 'size' => 1 } t['softbin'][name] = { 'number' => numbers['softbin'], 'size' => 1 } t['number'][name] = { 'number' => numbers['number'], 'size' => 1 } end s = { 'format_revision' => STORE_FORMAT_REVISION, 'assigned' => t, 'manually_assigned' => s['manually_assigned'], 'pointers' => s['pointers'], 'references' => s['references'] } end @last_bin = s['pointers']['bin'] @last_softbin = s['pointers']['softbin'] @last_number = s['pointers']['number'] s else { 'format_revision' => STORE_FORMAT_REVISION, 'assigned' => { 'bin' => {}, 'softbin' => {}, 'number' => {} }, 'manually_assigned' => { 'bin' => {}, 'softbin' => {}, 'number' => {} }, 'pointers' => { 'bin' => nil, 'softbin' => nil, 'number' => nil }, 'references' => { 'bin' => {}, 'softbin' => {}, 'number' => {} } } end end end |