File: GH-1075_fiber_does_not_finalize_properly_spec.rb

package info (click to toggle)
jruby 1.7.26-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 84,572 kB
  • sloc: ruby: 669,910; java: 253,056; xml: 35,152; ansic: 9,187; yacc: 7,267; cpp: 5,244; sh: 1,036; makefile: 345; jsp: 48; tcl: 40
file content (48 lines) | stat: -rw-r--r-- 1,631 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
# We must ensure fibers finalize properly and do not leave a lot of garbage
# around nor leave threads running. This test attempts to brutalize the fiber
# subsystem and the JVM GC in order to prove that fibers are getting cleaned
# up. There may be a better way to do this.

require 'rspec'

describe "A Fiber that has been abandoned" do
  it "cleans itself up properly" do
    begin
      thread_bean = java.lang.management.ManagementFactory.thread_mx_bean
      max_threads = JRuby.runtime.fiber_executor.maximum_pool_size

      # Thread count before fibers
      thread_count = thread_bean.thread_count

      # Attempt to create and abandon 10000 fibers. This should blow up quickly
      # if they are not cleaning up their threads properly.
      100.times do
        100.times do
          Fiber.new { Fiber.yield }.resume
        end
        JRuby.gc
      end

      # Allow GC and other threads plenty of time to clean up
      10.times do
        JRuby.gc
      end

      # Try to force finalizers to run
      java.lang.Runtime.runtime.run_finalization

      # Spin for a while, hoping to make this pass
      1000.times do
        JRuby.runtime.fiber_executor.maximum_pool_size = 1
        break if (thread_bean.thread_count - thread_count) < 10
        Thread.pass
      end

      # Final thread count should be within ~10 threads of original (allowing for
      # JVM GC, finalizer, reference queue, etc threads to have spun up).
      (thread_bean.thread_count - thread_count).should < 10
    ensure
      JRuby.runtime.fiber_executor.maximum_pool_size = max_threads
    end
  end
end if RUBY_VERSION >= "1.9"