File: cleanup_spec.rb

package info (click to toggle)
ruby-mongo 2.21.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 14,764 kB
  • sloc: ruby: 108,806; makefile: 5; sh: 2
file content (61 lines) | stat: -rw-r--r-- 1,536 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# frozen_string_literal: true
# rubocop:todo all

require 'spec_helper'

describe 'Cleanup stress test' do
  require_stress

  let(:options) do
    SpecConfig.instance.all_test_options
  end

  before(:all) do
    # load if necessary
    ClusterConfig.instance.primary_address
    ClientRegistry.instance.close_all_clients
  end

  context 'single client disconnect/reconnect' do
    let(:client) do
      new_local_client([ClusterConfig.instance.primary_address.seed], options)
    end

    it 'cleans up' do
      client
      client.cluster.servers_list.map(&:scan!)

      sleep 1
      GC.start

      start_resources = resources

      500.times do
        client.close
        client.reconnect
      end

      sleep 1
      GC.start

      end_resources = resources

      # There seem to be a temporary file descriptor leak in CI,
      # where we start with 75 fds and end with 77 fds.
      # Allow a few to be leaked, run more iterations to ensure the leak
      # is not a real one.
      # Sometimes we end with fewer fds than we started with also...
      end_resources[:open_file_count].should >= start_resources[:open_file_count] - 3
      end_resources[:open_file_count].should <= start_resources[:open_file_count] + 3

      end_resources[:running_thread_count].should == start_resources[:running_thread_count]
    end
  end

  def resources
    {
      open_file_count: Dir["/proc/#{Process.pid}/fd/*"].count,
      running_thread_count: Thread.list.select { |thread| thread.status == 'run' }.count,
    }
  end
end