shared_timed_stack_spec.rb 1.6 KB

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