/test/protocol/test_binary.rb

http://github.com/mperham/dalli · Ruby · 121 lines · 104 code · 16 blank · 1 comment · 0 complexity · a73bd269b64b4c967e930a49b9d190c0 MD5 · raw file

  1. # frozen_string_literal: true
  2. require 'ostruct'
  3. require_relative '../helper'
  4. describe Dalli::Protocol::Binary do
  5. describe 'hostname parsing' do
  6. it 'handles unix socket with no weight' do
  7. s = Dalli::Protocol::Binary.new('/var/run/memcached/sock')
  8. assert_equal '/var/run/memcached/sock', s.hostname
  9. assert_equal 1, s.weight
  10. assert_equal :unix, s.socket_type
  11. end
  12. it 'handles unix socket with a weight' do
  13. s = Dalli::Protocol::Binary.new('/var/run/memcached/sock:2')
  14. assert_equal '/var/run/memcached/sock', s.hostname
  15. assert_equal 2, s.weight
  16. assert_equal :unix, s.socket_type
  17. end
  18. it 'handles no port or weight' do
  19. s = Dalli::Protocol::Binary.new('localhost')
  20. assert_equal 'localhost', s.hostname
  21. assert_equal 11_211, s.port
  22. assert_equal 1, s.weight
  23. assert_equal :tcp, s.socket_type
  24. end
  25. it 'handles a port, but no weight' do
  26. s = Dalli::Protocol::Binary.new('localhost:11212')
  27. assert_equal 'localhost', s.hostname
  28. assert_equal 11_212, s.port
  29. assert_equal 1, s.weight
  30. assert_equal :tcp, s.socket_type
  31. end
  32. it 'handles a port and a weight' do
  33. s = Dalli::Protocol::Binary.new('localhost:11212:2')
  34. assert_equal 'localhost', s.hostname
  35. assert_equal 11_212, s.port
  36. assert_equal 2, s.weight
  37. assert_equal :tcp, s.socket_type
  38. end
  39. it 'handles ipv4 addresses' do
  40. s = Dalli::Protocol::Binary.new('127.0.0.1')
  41. assert_equal '127.0.0.1', s.hostname
  42. assert_equal 11_211, s.port
  43. assert_equal 1, s.weight
  44. assert_equal :tcp, s.socket_type
  45. end
  46. it 'handles ipv6 addresses' do
  47. s = Dalli::Protocol::Binary.new('[::1]')
  48. assert_equal '::1', s.hostname
  49. assert_equal 11_211, s.port
  50. assert_equal 1, s.weight
  51. assert_equal :tcp, s.socket_type
  52. end
  53. it 'handles ipv6 addresses with port' do
  54. s = Dalli::Protocol::Binary.new('[::1]:11212')
  55. assert_equal '::1', s.hostname
  56. assert_equal 11_212, s.port
  57. assert_equal 1, s.weight
  58. assert_equal :tcp, s.socket_type
  59. end
  60. it 'handles ipv6 addresses with port and weight' do
  61. s = Dalli::Protocol::Binary.new('[::1]:11212:2')
  62. assert_equal '::1', s.hostname
  63. assert_equal 11_212, s.port
  64. assert_equal 2, s.weight
  65. assert_equal :tcp, s.socket_type
  66. end
  67. it 'handles a FQDN' do
  68. s = Dalli::Protocol::Binary.new('my.fqdn.com')
  69. assert_equal 'my.fqdn.com', s.hostname
  70. assert_equal 11_211, s.port
  71. assert_equal 1, s.weight
  72. assert_equal :tcp, s.socket_type
  73. end
  74. it 'handles a FQDN with port and weight' do
  75. s = Dalli::Protocol::Binary.new('my.fqdn.com:11212:2')
  76. assert_equal 'my.fqdn.com', s.hostname
  77. assert_equal 11_212, s.port
  78. assert_equal 2, s.weight
  79. assert_equal :tcp, s.socket_type
  80. end
  81. it 'throws an exception if the hostname cannot be parsed' do
  82. expect(-> { Dalli::Protocol::Binary.new('[]') }).must_raise Dalli::DalliError
  83. expect(-> { Dalli::Protocol::Binary.new('my.fqdn.com:') }).must_raise Dalli::DalliError
  84. expect(-> { Dalli::Protocol::Binary.new('my.fqdn.com:11212,:2') }).must_raise Dalli::DalliError
  85. expect(-> { Dalli::Protocol::Binary.new('my.fqdn.com:11212:abc') }).must_raise Dalli::DalliError
  86. end
  87. end
  88. describe 'pipeline_next_responses' do
  89. subject { Dalli::Protocol::Binary.new('127.0.0.1') }
  90. it 'raises NetworkError when called before pipeline_response_setup' do
  91. assert_raises Dalli::NetworkError do
  92. subject.request(:pipelined_get, %w[a b])
  93. subject.pipeline_next_responses
  94. end
  95. end
  96. it 'raises NetworkError when called after pipeline_abort' do
  97. assert_raises Dalli::NetworkError do
  98. subject.request(:pipelined_get, %w[a b])
  99. subject.pipeline_response_setup
  100. subject.pipeline_abort
  101. subject.pipeline_next_responses
  102. end
  103. end
  104. end
  105. end