File: ractor_test.rb

package info (click to toggle)
ruby-redis-client 0.28.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,116 kB
  • sloc: ansic: 7,517; ruby: 5,801; makefile: 246; sh: 123
file content (63 lines) | stat: -rw-r--r-- 1,775 bytes parent folder | download
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
# frozen_string_literal: true

require "test_helper"

class RactorTest < RedisClientTestCase
  tag isolated: true

  def setup
    skip("Ractors are not supported on this Ruby version") unless defined?(::Ractor)
    skip("Hiredis is not Ractor safe") if RedisClient.default_driver.name == "RedisClient::HiredisConnection"
    begin
      ractor_value(Ractor.new { RedisClient.default_driver.name })
    rescue Ractor::RemoteError
      skip("Ractor implementation is too limited (MRI 3.0?)")
    end
    super
  end

  def test_get_and_set_within_ractor
    ractor = Ractor.new do
      config = Ractor.receive
      within_ractor_redis = RedisClient.new(**config)
      within_ractor_redis.call("SET", "foo", "bar")
      within_ractor_redis.call("GET", "foo")
    end
    ractor.send(ClientTestHelper.tcp_config.freeze)

    assert_equal("bar", ractor_value(ractor))
  end

  def test_multiple_ractors
    ractor1 = Ractor.new do
      config = Ractor.receive
      within_ractor_redis = RedisClient.new(**config)
      within_ractor_redis.call("SET", "foo", "bar")
      within_ractor_redis.call("GET", "foo")
    end
    ractor1.send(ClientTestHelper.tcp_config.freeze)

    ractor_value(ractor1) # We do this to ensure that the SET has been processed

    ractor2 = Ractor.new do
      config = Ractor.receive
      within_ractor_redis = RedisClient.new(**config)
      key = Ractor.receive
      within_ractor_redis.call("GET", key)
    end
    ractor2.send(ClientTestHelper.tcp_config.freeze)
    ractor2.send("foo")

    assert_equal("bar", ractor_value(ractor2))
  end

  if defined?(Ractor) && Ractor.method_defined?(:value) # Ruby 3.5+
    def ractor_value(ractor)
      ractor.value
    end
  else
    def ractor_value(ractor)
      ractor.take
    end
  end
end