|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'testing_helper' |
| 4 | +require 'redis_client/cluster/errors' |
| 5 | + |
| 6 | +class RedisClient |
| 7 | + class Cluster |
| 8 | + class TestErrors < Minitest::Test |
| 9 | + DummyError = Struct.new('DummyError', :message) |
| 10 | + |
| 11 | + def test_initial_setup_error |
| 12 | + [ |
| 13 | + { |
| 14 | + errors: [DummyError.new('foo')], |
| 15 | + want: 'Redis client could not fetch cluster information: foo' |
| 16 | + }, |
| 17 | + { |
| 18 | + errors: [DummyError.new('foo'), DummyError.new('bar')], |
| 19 | + want: 'Redis client could not fetch cluster information: foo,bar' |
| 20 | + }, |
| 21 | + { errors: [], want: 'Redis client could not fetch cluster information: ' }, |
| 22 | + { errors: '', want: 'Redis client could not fetch cluster information: ' }, |
| 23 | + { errors: nil, want: 'Redis client could not fetch cluster information: ' } |
| 24 | + ].each_with_index do |c, idx| |
| 25 | + raise ::RedisClient::Cluster::InitialSetupError, c[:errors] |
| 26 | + rescue StandardError => e |
| 27 | + assert_equal(c[:want], e.message, "Case: #{idx}") |
| 28 | + end |
| 29 | + end |
| 30 | + |
| 31 | + def test_orchestration_command_not_supported_error |
| 32 | + [ |
| 33 | + { command: %w[CLUSTER FORGET], want: 'CLUSTER FORGET command should be' }, |
| 34 | + { command: [], want: ' command should be' }, |
| 35 | + { command: '', want: ' command should be' }, |
| 36 | + { command: nil, want: ' command should be' } |
| 37 | + ].each_with_index do |c, idx| |
| 38 | + raise ::RedisClient::Cluster::OrchestrationCommandNotSupported, c[:command] |
| 39 | + rescue StandardError => e |
| 40 | + assert(e.message.start_with?(c[:want]), "Case: #{idx}") |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + def test_command_error_collection_error |
| 45 | + [ |
| 46 | + { |
| 47 | + errors: [DummyError.new('foo')], |
| 48 | + want: { msg: 'Command errors were replied on any node: foo', size: 1 } |
| 49 | + }, |
| 50 | + { |
| 51 | + errors: [DummyError.new('foo'), DummyError.new('bar')], |
| 52 | + want: { msg: 'Command errors were replied on any node: foo,bar', size: 2 } |
| 53 | + }, |
| 54 | + { errors: [], want: { msg: 'Command errors were replied on any node: ', size: 0 } }, |
| 55 | + { errors: '', want: { msg: 'Command errors were replied on any node: ', size: 0 } }, |
| 56 | + { errors: nil, want: { msg: 'Command errors were replied on any node: ', size: 0 } } |
| 57 | + ].each_with_index do |c, idx| |
| 58 | + raise ::RedisClient::Cluster::CommandErrorCollection, c[:errors] |
| 59 | + rescue StandardError => e |
| 60 | + assert_equal(c[:want][:msg], e.message, "Case: #{idx}") |
| 61 | + assert_equal(c[:want][:size], e.errors.size, "Case: #{idx}") |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + def test_ambiguous_node_error |
| 66 | + [ |
| 67 | + { command: 'MULTI', want: "Cluster client doesn't know which node the MULTI command should be sent to." }, |
| 68 | + { command: nil, want: "Cluster client doesn't know which node the command should be sent to." } |
| 69 | + ].each_with_index do |c, idx| |
| 70 | + raise ::RedisClient::Cluster::AmbiguousNodeError, c[:command] |
| 71 | + rescue StandardError => e |
| 72 | + assert_equal(e.message, c[:want], "Case: #{idx}") |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + def test_cross_slot_pipelining_error |
| 77 | + [ |
| 78 | + { keys: %w[foo bar baz], want: 'keys: foo,bar,baz' }, |
| 79 | + { keys: '', want: 'keys: ' }, |
| 80 | + { keys: nil, want: 'keys: ' } |
| 81 | + ].each_with_index do |c, idx| |
| 82 | + raise ::RedisClient::Cluster::CrossSlotPipeliningError, c[:keys] |
| 83 | + rescue StandardError => e |
| 84 | + assert(e.message.end_with?(c[:want]), "Case: #{idx}") |
| 85 | + end |
| 86 | + end |
| 87 | + end |
| 88 | + end |
| 89 | +end |
0 commit comments