2 require 'crunch_dispatch'
4 class CrunchDispatchTest < ActiveSupport::TestCase
5 test 'choose cheaper nodes first' do
7 # Replace test fixtures with a set suitable for testing dispatch
10 # Idle nodes with different prices
11 [['compute1', 3.20, 32],
12 ['compute2', 1.60, 16],
13 ['compute3', 0.80, 8]].each do |hostname, price, cores|
14 Node.create!(hostname: hostname,
16 'slurm_state' => 'idle',
22 'total_cpu_cores' => cores,
23 'total_ram_mb' => cores*1024,
24 'total_scratch_mb' => cores*10000,
28 # Node with no price information
29 Node.create!(hostname: 'compute4',
31 'slurm_state' => 'idle',
34 'total_cpu_cores' => 8,
35 'total_ram_mb' => 8192,
36 'total_scratch_mb' => 80000,
40 Node.create!(hostname: 'compute5',
42 'slurm_state' => 'alloc',
48 'total_cpu_cores' => 32,
49 'total_ram_mb' => 32768,
50 'total_scratch_mb' => 320000,
54 dispatch = CrunchDispatch.new
55 [[1, 16384, ['compute2']],
56 [2, 16384, ['compute2', 'compute1']],
57 [2, 8000, ['compute4', 'compute3']],
58 ].each do |min_nodes, min_ram, expect_nodes|
59 job = Job.new(runtime_constraints: {
60 'min_nodes' => min_nodes,
61 'min_ram_mb_per_node' => min_ram,
63 nodes = dispatch.nodes_available_for_job_now job
64 assert_equal expect_nodes, nodes
68 test 'respond to TERM' do
69 lockfile = Rails.root.join 'tmp', 'dispatch.lock'
70 ENV['CRUNCH_DISPATCH_LOCKFILE'] = lockfile.to_s
74 # Abandon database connections inherited from parent
76 # https://github.com/kstephens/rails_is_forked
77 ActiveRecord::Base.connection_handler.connection_pools.each_value do |pool|
79 @reserved_connections = {}
83 ActiveRecord::Base.establish_connection
84 CrunchDispatch.new.run []
89 assert_with_timeout 5, "Dispatch did not lock #{lockfile}" do
93 Process.kill("TERM", pid)
95 assert_with_timeout 5, "Dispatch did not unlock #{lockfile}" do
100 def assert_with_timeout timeout, message
102 while (t += 0.1) < timeout
108 assert false, message + " (waited #{timeout} seconds)"
111 def can_lock lockfile
112 lockfile.open(File::RDWR|File::CREAT, 0644) do |f|
113 return f.flock(File::LOCK_EX|File::LOCK_NB)