File: resume_spec.rb

package info (click to toggle)
ruby2.7 2.7.4-1%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 112,576 kB
  • sloc: ruby: 849,454; ansic: 697,834; yacc: 45,100; xml: 25,367; pascal: 10,051; javascript: 6,575; sh: 3,848; makefile: 759; cpp: 713; asm: 333; python: 295; lisp: 97; sed: 94; perl: 62; awk: 36
file content (48 lines) | stat: -rw-r--r-- 1,428 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
require_relative '../../spec_helper'
require_relative '../../shared/fiber/resume'

describe "Fiber#resume" do
  it_behaves_like :fiber_resume, :resume
end

describe "Fiber#resume" do
  it "raises a FiberError if the Fiber tries to resume itself" do
    fiber = Fiber.new { fiber.resume }
    -> { fiber.resume }.should raise_error(FiberError, /double resume/)
  end

  it "returns control to the calling Fiber if called from one" do
    fiber1 = Fiber.new { :fiber1 }
    fiber2 = Fiber.new { fiber1.resume; :fiber2 }
    fiber2.resume.should == :fiber2
  end

  # Redmine #595
  it "executes the ensure clause" do
    code = <<-RUBY
      f = Fiber.new do
        begin
          Fiber.yield
        ensure
          puts "ensure executed"
        end
      end

      # The apparent issue is that when Fiber.yield executes, control
      # "leaves" the "ensure block" and so the ensure clause should run. But
      # control really does NOT leave the ensure block when Fiber.yield
      # executes. It merely pauses there. To require ensure to run when a
      # Fiber is suspended then makes ensure-in-a-Fiber-context different
      # than ensure-in-a-Thread-context and this would be very confusing.
      f.resume

      # When we execute the second #resume call, the ensure block DOES exit,
      # the ensure clause runs.
      f.resume

      exit 0
    RUBY

    ruby_exe(code).should == "ensure executed\n"
  end
end