Class: TestIds::Allocator

Inherits:
Object
  • Object
show all
Includes:
Origen::Callbacks
Defined in:
lib/test_ids/allocator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAllocator

Returns a new instance of Allocator.



6
7
8
9
10
11
12
# File 'lib/test_ids/allocator.rb', line 6

def initialize
  @@allocators ||= 0
  @@allocators += 1
  if @@allocators > 1 && !TestIds.send(:testing?)
    fail 'TestIds::Allocators is a singleton, there can be only one'
  end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



4
5
6
# File 'lib/test_ids/allocator.rb', line 4

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



16
17
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
# File 'lib/test_ids/allocator.rb', line 16

def allocate(instance, options)
  @changes_made = true
  clean(options)
  @callbacks = []
  name = extract_test_name(instance, options)
  name = "#{name}_#{options[:index]}" if options[:index]
  store['tests'][name] ||= {}
  t = store['tests'][name]
  # If the user has supplied any of these, that number should be used
  # and reserved so that it is not automatically generated later
  if options[:bin]
    t['bin'] = options[:bin]
    store['manually_assigned']['bin'][options[:bin].to_s] = true
  # Regenerate the bin if the original allocation has since been applied
  # manually elsewhere
  elsif store['manually_assigned']['bin'][t['bin'].to_s]
    t['bin'] = nil
    # Also regenerate these as they could be a function of the bin
    t['softbin'] = nil if config.softbins.function?
    t['number'] = nil if config.numbers.function?
  end
  if options[:softbin]
    t['softbin'] = options[:softbin]
    store['manually_assigned']['softbin'][options[:softbin].to_s] = true
  elsif store['manually_assigned']['softbin'][t['softbin'].to_s]
    t['softbin'] = nil
    # Also regenerate the number as it could be a function of the softbin
    t['number'] = nil if config.numbers.function?
  end
  if options[:number]
    t['number'] = options[:number]
    store['manually_assigned']['number'][options[:number].to_s] = true
  elsif store['manually_assigned']['number'][t['number'].to_s]
    t['number'] = nil
  end
  # Otherwise generate the missing ones
  t['bin'] ||= allocate_bin
  t['softbin'] ||= allocate_softbin(t['bin'])
  t['number'] ||= allocate_number(t['bin'], t['softbin'])
  # Record that there has been a reference to the final numbers
  time = Time.now.to_f
  store['references']['bin'][t['bin'].to_s] = time if t['bin']
  store['references']['softbin'][t['softbin'].to_s] = time if t['softbin']
  store['references']['number'][t['number'].to_s] = time if t['number']
  # Update the supplied options hash that will be forwarded to the
  # program generator
  options[:bin] = t['bin']
  options[:softbin] = t['softbin']
  options[:number] = t['number']
  options
end

#fileObject

Returns a path to the file that will be used to store the allocated bins/numbers. If config.repo has not been set it returns nil.



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/test_ids/allocator.rb', line 110

def file
  if config.repo
    @file ||= begin
      if git?
        dir = "#{Origen.app.imports_directory}/test_ids/#{Pathname.new(config.repo).basename}"
        FileUtils.mkdir_p(dir)
        "#{dir}/store.json"
      else
        config.repo
      end
    end
  end
end

#gitObject



124
125
126
# File 'lib/test_ids/allocator.rb', line 124

def git
  @git ||= Git.new(local: Pathname.new(file).dirname, remote: config.repo, no_pull: publish?)
end

#on_origen_shutdownObject



99
100
101
102
103
104
105
106
# File 'lib/test_ids/allocator.rb', line 99

def on_origen_shutdown
  unless TestIds.send(:testing?)
    if config.repo && @changes_made && config.on_completion != :discard
      save
      git.publish if publish?
    end
  end
end

#prepareObject



128
129
130
131
132
133
# File 'lib/test_ids/allocator.rb', line 128

def prepare
  if git?
    git # Pulls the latest repo
    git.get_lock if publish?
  end
end

#saveObject

Saves the current allocator state to the repository



91
92
93
94
95
96
97
# File 'lib/test_ids/allocator.rb', line 91

def save
  if file
    p = Pathname.new(file)
    FileUtils.mkdir_p(p.dirname)
    File.open(p, 'w') { |f| f.puts JSON.pretty_generate(store) }
  end
end

#storeObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/test_ids/allocator.rb', line 72

def store
  @store ||= begin
    s = JSON.load(File.read(file)) if file && File.exist?(file)
    if s
      @last_bin = s['pointers']['bin']
      @last_softbin = s['pointers']['softbin']
      @last_number = s['pointers']['number']
      s
    else
      { 'tests'             => {},
        'manually_assigned' => { 'bin' => {}, 'softbin' => {}, 'number' => {} },
        'pointers'          => { 'bin' => nil, 'softbin' => nil, 'number' => nil },
        'references'        => { 'bin' => {}, 'softbin' => {}, 'number' => {} }
      }
    end
  end
end