This is designed to handle race conditions, where the strand is deleted between when the object is retrieved and when incr_* is called. One caller of Semaphore.incr, SemSnap.incr, relies on the Semaphore being returned. Update this method handle the case where nil is returned by Semaphore.incr. An alternative approach would be raising an exception for this case. That can potentially alert us to cases that pass invalid strand ids, but it would require more code to handle race conditions. We couple have separate methods, one that raised by default, and another than can be used in cases where we know the strand id is correct, but want to avoid a race condition. However, I'm not sure the complexity is worth it. While here, omit unnecessary hash values, switch from create_with_id to create, and explicit dataset call, and unnecessary explicit receiver.
18 lines
546 B
Ruby
18 lines
546 B
Ruby
# frozen_string_literal: true
|
|
|
|
require_relative "spec_helper"
|
|
|
|
RSpec.describe Semaphore do
|
|
let(:st) { Strand.create(prog: "Test", label: "start") }
|
|
|
|
it ".incr returns nil and does not add Semaphore if there is no related strand" do
|
|
expect(described_class.all).to be_empty
|
|
expect(described_class.incr(Vm.generate_uuid, "foo")).to be_nil
|
|
expect(described_class.all).to be_empty
|
|
end
|
|
|
|
it ".incr raises if invalid name is given" do
|
|
expect { described_class.incr(st.id, nil) }.to raise_error(Sequel::ValidationFailed)
|
|
end
|
|
end
|