|
15 | 15 | expect(worker.reload.last_heartbeat).to be_within(1.second).of(t)
|
16 | 16 | end
|
17 | 17 | end
|
| 18 | + |
| 19 | + # Iterating by 5 each time to allow enough spacing to be more than 1 second |
| 20 | + # apart when using be_within(x).of(t) |
| 21 | + context "when using file based heartbeating" do |
| 22 | + let!(:first_heartbeat) { Time.now.utc } |
| 23 | + let!(:heartbeat_file) { "/path/to/worker.hb" } |
| 24 | + |
| 25 | + around do |example| |
| 26 | + ENV["WORKER_HEARTBEAT_METHOD"] = "file" |
| 27 | + ENV["WORKER_HEARTBEAT_FILE"] = heartbeat_file |
| 28 | + example.run |
| 29 | + ENV.delete("WORKER_HEARTBEAT_METHOD") |
| 30 | + ENV.delete("WORKER_HEARTBEAT_FILE") |
| 31 | + end |
| 32 | + |
| 33 | + context "with an existing heartbeat file" do |
| 34 | + it "sets initial and subsequent heartbeats" do |
| 35 | + expect(File).to receive(:exist?).with(heartbeat_file).and_return(true, true) |
| 36 | + expect(File).to receive(:mtime).with(heartbeat_file).and_return(first_heartbeat, first_heartbeat + 5) |
| 37 | + |
| 38 | + [0, 5].each do |i| |
| 39 | + Timecop.freeze(first_heartbeat) do |
| 40 | + miq_server.worker_heartbeat(pid) |
| 41 | + miq_server.validate_heartbeat(worker) |
| 42 | + end |
| 43 | + |
| 44 | + expect(worker.reload.last_heartbeat).to be_within(1.second).of(first_heartbeat + i) |
| 45 | + end |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + context "with a missing heartbeat file" do |
| 50 | + it "sets initial heartbeat only" do |
| 51 | + expect(File).to receive(:exist?).with(heartbeat_file).and_return(false).exactly(4).times |
| 52 | + expect(File).to receive(:mtime).with(heartbeat_file).never |
| 53 | + |
| 54 | + # This has different results first iteration of the loop compared to |
| 55 | + # the rest: |
| 56 | + # 1. Sets the initial heartbeat |
| 57 | + # 2. Doesn't update the worker's last_heartbeat value after that |
| 58 | + # |
| 59 | + # So the result from the database should not change after the first |
| 60 | + # iteration of the loop |
| 61 | + [0, 5, 10, 15].each do |i| |
| 62 | + Timecop.freeze(first_heartbeat + i) do |
| 63 | + miq_server.worker_heartbeat(pid) |
| 64 | + miq_server.validate_heartbeat(worker) |
| 65 | + end |
| 66 | + |
| 67 | + expect(worker.reload.last_heartbeat).to be_within(1.second).of(first_heartbeat) |
| 68 | + end |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + context "with a missing heartbeat file on the first validate" do |
| 73 | + it "sets initial heartbeat default, and updates the heartbeat from the file second" do |
| 74 | + expect(File).to receive(:exist?).with(heartbeat_file).and_return(false, true) |
| 75 | + expect(File).to receive(:mtime).with(heartbeat_file).and_return(first_heartbeat + 5) |
| 76 | + |
| 77 | + [0, 5].each do |i| |
| 78 | + Timecop.freeze(first_heartbeat) do |
| 79 | + miq_server.worker_heartbeat(pid) |
| 80 | + miq_server.validate_heartbeat(worker) |
| 81 | + end |
| 82 | + |
| 83 | + expect(worker.reload.last_heartbeat).to be_within(1.second).of(first_heartbeat + i) |
| 84 | + end |
| 85 | + end |
| 86 | + end |
| 87 | + end |
18 | 88 | end
|
19 | 89 | end
|
0 commit comments