shared_timed_stack_spec.rb 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe ConnectionPool::SharedTimedStack do
  4. subject { described_class.new(5) { |site| mini_connection_class.new(site) } }
  5. let(:mini_connection_class) do
  6. Class.new do
  7. attr_reader :site
  8. def initialize(site)
  9. @site = site
  10. end
  11. end
  12. end
  13. describe '#push' do
  14. it 'keeps the connection in the stack' do
  15. subject.push(mini_connection_class.new('foo'))
  16. expect(subject.size).to eq 1
  17. end
  18. end
  19. describe '#pop' do
  20. it 'returns a connection' do
  21. expect(subject.pop('foo')).to be_a mini_connection_class
  22. end
  23. it 'returns the same connection that was pushed in' do
  24. connection = mini_connection_class.new('foo')
  25. subject.push(connection)
  26. expect(subject.pop('foo')).to be connection
  27. end
  28. it 'does not create more than maximum amount of connections' do
  29. expect { 6.times { subject.pop('foo', 0) } }.to raise_error Timeout::Error
  30. end
  31. it 'repurposes a connection for a different site when maximum amount is reached' do
  32. 5.times { subject.push(mini_connection_class.new('foo')) }
  33. expect(subject.pop('bar')).to be_a mini_connection_class
  34. end
  35. end
  36. describe '#empty?' do
  37. it 'returns true when no connections on the stack' do
  38. expect(subject.empty?).to be true
  39. end
  40. it 'returns false when there are connections on the stack' do
  41. subject.push(mini_connection_class.new('foo'))
  42. expect(subject.empty?).to be false
  43. end
  44. end
  45. describe '#size' do
  46. it 'returns the number of connections on the stack' do
  47. 2.times { subject.push(mini_connection_class.new('foo')) }
  48. expect(subject.size).to eq 2
  49. end
  50. end
  51. end