Skip to content

Commit 7fea981

Browse files
committed
test: add a case for Ractor
1 parent 424714c commit 7fea981

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

test/test_concurrency.rb

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,75 @@ def test_threading_with_transaction
132132
assert_equal(WANT, @client.call('GET', '{key}1'))
133133
end
134134

135+
def test_ractor
136+
skip('Ractor is not available') unless Object.const_defined?(:Ractor, false)
137+
138+
ractors = Array.new(MAX_THREADS) do |i|
139+
Ractor.new(i) do |i|
140+
c = ::RedisClient.cluster(
141+
nodes: TEST_NODE_URIS,
142+
fixed_hostname: TEST_FIXED_HOSTNAME,
143+
**TEST_GENERIC_OPTIONS
144+
).new_client
145+
c.call('get', "key#{i}")
146+
rescue StandardError => e
147+
e
148+
ensure
149+
c&.close
150+
end
151+
end
152+
153+
ractors.each { |r| assert_equal(WANT, r.take) }
154+
end
155+
156+
def test_ractor_with_pipelining
157+
skip('Ractor is not available') unless Object.const_defined?(:Ractor, false)
158+
159+
ractors = Array.new(MAX_THREADS) do |i|
160+
Ractor.new(i) do |i|
161+
c = ::RedisClient.cluster(
162+
nodes: TEST_NODE_URIS,
163+
fixed_hostname: TEST_FIXED_HOSTNAME,
164+
**TEST_GENERIC_OPTIONS
165+
).new_client
166+
c.pipelined do |pi|
167+
pi.call('get', "key#{i}")
168+
pi.call('get', "key#{i}")
169+
end
170+
rescue StandardError => e
171+
e
172+
ensure
173+
c&.close
174+
end
175+
end
176+
177+
ractors.each { |r| assert_equal([WANT, WANT], r.take) }
178+
end
179+
180+
def test_ractor_with_transaction
181+
skip('Ractor is not available') unless Object.const_defined?(:Ractor, false)
182+
183+
ractors = Array.new(MAX_THREADS) do |i|
184+
Ractor.new(i) do |i|
185+
c = ::RedisClient.cluster(
186+
nodes: TEST_NODE_URIS,
187+
fixed_hostname: TEST_FIXED_HOSTNAME,
188+
**TEST_GENERIC_OPTIONS
189+
).new_client
190+
c.multi(watch: ["key#{i}"]) do |tx|
191+
tx.call('incr', "key#{i}")
192+
tx.call('incr', "key#{i}")
193+
end
194+
rescue StandardError => e
195+
e
196+
ensure
197+
c&.close
198+
end
199+
end
200+
201+
ractors.each { |r| assert_equal([2, 3], r.take) }
202+
end
203+
135204
private
136205

137206
def new_test_client

test/testing_constants.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@
7676
raise NotImplementedError, TEST_REDIS_HOST
7777
end
7878

79+
Ractor.make_shareable(TEST_NODE_URIS) if Object.const_defined?(:Ractor, false) && Ractor.respond_to?(:make_shareable)
80+
7981
TEST_GENERIC_OPTIONS = (TEST_REDIS_SSL ? _base_opts.merge(_ssl_opts) : _base_opts).freeze
8082

8183
_tmp_cli = _new_raw_cli.call(**TEST_GENERIC_OPTIONS)

0 commit comments

Comments
 (0)