Previously, it would usually use 2 queries if inside a transaction, or 4 queries if outside a transaction. This replaces the UPDATE then INSERT query approach with an INSERT SELECT (UPDATE RETURNING) approach in a single query. As this is a single query and not multiple queries, it does not need a transaction. If the UPDATE does not update a row, then no rows will be inserted. This approach also opens up the ability to create multiple semaphores in a single query, if the first argument is an array or a dataset. That will be used in an upcoming commit. This changes the return value of Semaphore.incr when provided a single id from returning a Semaphore instance to returning the Semaphore uuid. Only SemSnap#incr cares about the return value, so update that to fetch the Semaphore.
18 lines
534 B
Ruby
18 lines
534 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(RuntimeError)
|
|
end
|
|
end
|