sequel-pg-locker 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/sequel-pg-locker/version.rb +1 -1
- data/lib/sequel-pg-locker.rb +7 -6
- data/spec/sequel-pg-locker_spec.rb +8 -12
- metadata +1 -1
data/lib/sequel-pg-locker.rb
CHANGED
@@ -6,16 +6,17 @@ module Sequel
|
|
6
6
|
module PG
|
7
7
|
module Locker
|
8
8
|
def self.lock(id)
|
9
|
-
|
9
|
+
db["SELECT pg_try_advisory_lock(#{id})"].get
|
10
10
|
end
|
11
11
|
|
12
12
|
private
|
13
13
|
|
14
|
-
def self.
|
15
|
-
@
|
16
|
-
|
17
|
-
|
18
|
-
|
14
|
+
def self.db
|
15
|
+
@db ||= Sequel.connect(database_url)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.database_url
|
19
|
+
@database_url ||= ENV['DATABASE_URL']
|
19
20
|
end
|
20
21
|
end
|
21
22
|
end
|
@@ -2,27 +2,23 @@ require 'minitest/autorun'
|
|
2
2
|
|
3
3
|
require "sequel-pg-locker"
|
4
4
|
|
5
|
-
|
5
|
+
ENV['DATABASE_URL'] ||= "postgres://localhost/sequel_pg_locker_test"
|
6
6
|
|
7
7
|
describe Sequel::PG::Locker do
|
8
8
|
subject { Sequel::PG::Locker }
|
9
9
|
|
10
|
-
before do
|
11
|
-
ENV['DATABASE_URL'] = "postgres://localhost/sequel_pg_locker_test"
|
12
|
-
end
|
13
|
-
|
14
10
|
describe ".lock" do
|
15
|
-
before do
|
16
|
-
subject.instance_variable_set(:@connection, nil)
|
17
|
-
end
|
18
|
-
|
19
11
|
it "should provide a lock" do
|
20
|
-
|
12
|
+
id = rand(1_000_000)
|
13
|
+
subject.instance_variable_set(:@db, nil)
|
14
|
+
assert subject.lock(id)
|
21
15
|
end
|
22
16
|
|
23
17
|
it "should not allow further locking" do
|
24
|
-
|
25
|
-
assert
|
18
|
+
id = rand(1_000_000)
|
19
|
+
assert subject.lock(id)
|
20
|
+
subject.instance_variable_set(:@db, nil)
|
21
|
+
assert !subject.lock(id)
|
26
22
|
end
|
27
23
|
end
|
28
24
|
end
|