File: commands_on_transactions_test.rb

package info (click to toggle)
ruby-redis 5.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,160 kB
  • sloc: ruby: 11,445; makefile: 117; sh: 24
file content (82 lines) | stat: -rw-r--r-- 1,909 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# frozen_string_literal: true

require "helper"

# ruby -w -Itest test/cluster_commands_on_transactions_test.rb
# @see https://redis.io/commands#transactions
class TestClusterCommandsOnTransactions < Minitest::Test
  include Helper::Cluster

  def test_discard
    assert_raises(Redis::Cluster::AmbiguousNodeError) do
      redis.discard
    end
  end

  def test_exec
    assert_raises(Redis::Cluster::AmbiguousNodeError) do
      redis.exec
    end
  end

  def test_multi
    assert_raises(LocalJumpError) do
      redis.multi
    end

    assert_empty(redis.multi {})

    assert_equal([1], redis.multi { |r| r.incr('counter') })
  end

  def test_unwatch
    assert_raises(Redis::Cluster::AmbiguousNodeError) do
      redis.unwatch
    end
  end

  def test_watch
    assert_raises(Redis::Cluster::TransactionConsistencyError) do
      redis.watch('{key}1', '{key}2')
    end

    assert_raises(Redis::Cluster::TransactionConsistencyError) do
      redis.watch('{key}1', '{key}2') {}
    end

    assert_raises(Redis::Cluster::TransactionConsistencyError) do
      redis.watch('{key}1', '{key}2') do |cli|
        cli.watch('{key}3')
      end
    end

    assert_raises(Redis::Cluster::TransactionConsistencyError) do
      redis.watch('key1', 'key2') do |cli|
        cli.multi do |tx|
          tx.set('key1', '1')
          tx.set('key2', '2')
        end
      end
    end

    assert_raises(Redis::Cluster::TransactionConsistencyError) do
      redis.watch('{hey}1', '{hey}2') do |cli|
        cli.multi do |tx|
          tx.set('{key}1', '1')
          tx.set('{key}2', '2')
        end
      end
    end

    assert_equal('hello', redis.watch('{key}1', '{key}2') { |_| 'hello' })

    redis.watch('{key}1', '{key}2') do |cli|
      cli.multi do |tx|
        tx.set('{key}1', '1')
        tx.set('{key}2', '2')
      end
    end

    assert_equal %w[1 2], redis.mget('{key}1', '{key}2')
  end
end