闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.6 KiB

  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