/
repository_spec.rb
208 lines (156 loc) · 8.78 KB
/
repository_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# frozen_string_literal: true
require "spec_helper"
module RubyEventStore
module Outbox
::RSpec.describe Repository, db: true do
include SchemaHelper
let(:database_url) { ENV["DATABASE_URL"] }
let(:message_format) { "some_message_format" }
let(:split_key) { "some_split_key" }
let(:some_process_uuid) { SecureRandom.uuid }
let(:other_process_uuid) { SecureRandom.uuid }
let(:clock) { TickingClock.new }
specify "successful obtaining returns Lock structure" do
repository = Repository.new(database_url)
expected_fetch_specification = FetchSpecification.new(message_format, split_key)
lock = repository.obtain_lock_for_process(expected_fetch_specification, some_process_uuid, clock: clock)
expect(lock).to be_a(Repository::Lock)
expect(lock.fetch_specification).to eq(expected_fetch_specification)
expect(lock).to be_locked_by(some_process_uuid)
expect(lock).to be_recently_locked(clock: clock)
end
specify "Lock is not considered locked after some time" do
repository = Repository.new(database_url)
expected_fetch_specification = FetchSpecification.new(message_format, split_key)
result = repository.obtain_lock_for_process(expected_fetch_specification, some_process_uuid, clock: clock)
wait_for_lock_duration
expect(result).not_to be_recently_locked(clock: clock)
end
specify "trying to obtain taken Lock" do
repository = Repository.new(database_url)
expected_fetch_specification = FetchSpecification.new(message_format, split_key)
repository.obtain_lock_for_process(expected_fetch_specification, some_process_uuid, clock: clock)
result = repository.obtain_lock_for_process(expected_fetch_specification, other_process_uuid, clock: clock)
expect(result).to be(:taken)
end
specify "obtains a lock for given fetch specification" do
repository = Repository.new(database_url)
repository.obtain_lock_for_process(
FetchSpecification.new("other_message_format", split_key),
some_process_uuid,
clock: clock
)
repository.obtain_lock_for_process(
FetchSpecification.new(message_format, "other_split_key"),
some_process_uuid,
clock: clock
)
expected_fetch_specification = FetchSpecification.new(message_format, split_key)
lock = repository.obtain_lock_for_process(expected_fetch_specification, some_process_uuid, clock: clock)
expect(lock.fetch_specification).to eq(expected_fetch_specification)
end
specify "successful release" do
repository = Repository.new(database_url)
expected_fetch_specification = FetchSpecification.new(message_format, split_key)
lock = repository.obtain_lock_for_process(expected_fetch_specification, some_process_uuid, clock: clock)
result = repository.release_lock_for_process(expected_fetch_specification, some_process_uuid)
expect(result).to be(:ok)
lock.reload
expect(lock.locked_by).to be_nil
expect(lock.locked_at).to be_nil
end
specify "released lock can be obtained by other process" do
repository = Repository.new(database_url)
expected_fetch_specification = FetchSpecification.new(message_format, split_key)
repository.obtain_lock_for_process(expected_fetch_specification, some_process_uuid, clock: clock)
repository.release_lock_for_process(expected_fetch_specification, some_process_uuid)
result = repository.obtain_lock_for_process(expected_fetch_specification, other_process_uuid, clock: clock)
expect(result).to be_a(Repository::Lock)
end
specify "cant release not obtained lock" do
repository = Repository.new(database_url)
expected_fetch_specification = FetchSpecification.new(message_format, split_key)
result = repository.release_lock_for_process(expected_fetch_specification, some_process_uuid)
expect(result).to be(:not_taken_by_this_process)
end
specify "one process cant release other's lock" do
repository = Repository.new(database_url)
expected_fetch_specification = FetchSpecification.new(message_format, split_key)
repository.obtain_lock_for_process(expected_fetch_specification, some_process_uuid, clock: clock)
result = repository.release_lock_for_process(expected_fetch_specification, other_process_uuid)
expect(result).to be(:not_taken_by_this_process)
end
specify "lock timeout when obtaining Lock" do
repository = Repository.new(database_url)
expected_fetch_specification = FetchSpecification.new(message_format, split_key)
expect(Repository::Lock).to receive(:lock).and_raise(::ActiveRecord::LockWaitTimeout)
result = repository.obtain_lock_for_process(expected_fetch_specification, some_process_uuid, clock: clock)
expect(result).to be(:lock_timeout)
end
specify "deadlock when obtaining Lock" do
repository = Repository.new(database_url)
expected_fetch_specification = FetchSpecification.new(message_format, split_key)
expect(Repository::Lock).to receive(:create!).and_raise(::ActiveRecord::Deadlocked)
result = repository.obtain_lock_for_process(expected_fetch_specification, some_process_uuid, clock: clock)
expect(result).to be(:deadlocked)
end
specify "lock timeout when releasing lock" do
repository = Repository.new(database_url)
expected_fetch_specification = FetchSpecification.new(message_format, split_key)
repository.obtain_lock_for_process(expected_fetch_specification, some_process_uuid, clock: clock)
expect(Repository::Lock).to receive(:lock).and_raise(::ActiveRecord::LockWaitTimeout)
result = repository.release_lock_for_process(expected_fetch_specification, some_process_uuid)
expect(result).to be(:lock_timeout)
end
specify "deadlock when releasing lock" do
repository = Repository.new(database_url)
expected_fetch_specification = FetchSpecification.new(message_format, split_key)
repository.obtain_lock_for_process(expected_fetch_specification, some_process_uuid, clock: clock)
expect(Repository::Lock).to receive(:lock).and_raise(::ActiveRecord::Deadlocked)
result = repository.release_lock_for_process(expected_fetch_specification, some_process_uuid)
expect(result).to be(:deadlocked)
end
specify "refreshing lock" do
repository = Repository.new(database_url)
expected_fetch_specification = FetchSpecification.new(message_format, split_key)
lock = repository.obtain_lock_for_process(expected_fetch_specification, some_process_uuid, clock: clock)
clock.test_travel (Repository::RECENTLY_LOCKED_DURATION / 2)
result = lock.refresh(clock: clock)
clock.test_travel (Repository::RECENTLY_LOCKED_DURATION / 2 + 1.second)
expect(result).to be(:ok)
expect(lock).to be_locked_by(some_process_uuid)
expect(lock).to be_recently_locked(clock: clock)
end
specify "refreshing lock when other process stole it" do
repository = Repository.new(database_url)
expected_fetch_specification = FetchSpecification.new(message_format, split_key)
lock_for_some_process =
repository.obtain_lock_for_process(expected_fetch_specification, some_process_uuid, clock: clock)
wait_for_lock_duration
lock_for_other_process =
repository.obtain_lock_for_process(expected_fetch_specification, other_process_uuid, clock: clock)
result = lock_for_some_process.refresh(clock: clock)
expect(result).to be(:stolen)
end
specify "lock timeout when refreshing lock" do
repository = Repository.new(database_url)
expected_fetch_specification = FetchSpecification.new(message_format, split_key)
lock = repository.obtain_lock_for_process(expected_fetch_specification, some_process_uuid, clock: clock)
expect(lock).to receive(:lock!).and_raise(::ActiveRecord::LockWaitTimeout)
result = lock.refresh(clock: clock)
expect(result).to be(:lock_timeout)
end
specify "deadlocked when refreshing lock" do
repository = Repository.new(database_url)
expected_fetch_specification = FetchSpecification.new(message_format, split_key)
lock = repository.obtain_lock_for_process(expected_fetch_specification, some_process_uuid, clock: clock)
expect(lock).to receive(:lock!).and_raise(::ActiveRecord::Deadlocked)
result = lock.refresh(clock: clock)
expect(result).to be(:deadlocked)
end
def wait_for_lock_duration
clock.test_travel (Repository::RECENTLY_LOCKED_DURATION + 1.second)
end
end
end
end