File: async_callback_spec.rb

package info (click to toggle)
jruby 9.4.12.0%2Bds-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 89,468 kB
  • sloc: ruby: 550,612; java: 276,882; yacc: 25,873; ansic: 6,285; xml: 6,172; sh: 1,775; sed: 94; makefile: 76; jsp: 48; tcl: 40; exp: 12
file content (53 lines) | stat: -rw-r--r-- 1,677 bytes parent folder | download | duplicates (4)
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
#
# This file is part of ruby-ffi.
# For licensing, see LICENSE.SPECS
#

require File.expand_path(File.join(File.dirname(__FILE__), "spec_helper"))

describe "async callback" do
  module LibTest
    extend FFI::Library
    ffi_lib TestLibrary::PATH
    AsyncIntCallback = callback [ :int ], :void

    @blocking = true
    attach_function :testAsyncCallback, [ AsyncIntCallback, :int ], :void
  end

  it ":int (0x7fffffff) argument" do
    skip "not yet supported on TruffleRuby" if RUBY_ENGINE == "truffleruby"
    v = 0xdeadbeef
    called = false
    cb = Proc.new {|i| v = i; called = Thread.current }
    LibTest.testAsyncCallback(cb, 0x7fffffff)
    expect(called).to be_kind_of(Thread)
    expect(called).to_not eq(Thread.current)
    expect(v).to eq(0x7fffffff)
  end

  it "called a second time" do
    skip "not yet supported on TruffleRuby" if RUBY_ENGINE == "truffleruby"
    v = 1
    th1 = th2 = false
    LibTest.testAsyncCallback(2) { |i| v += i; th1 = Thread.current }
    LibTest.testAsyncCallback(3) { |i| v += i; th2 = Thread.current }
    expect(th1).to be_kind_of(Thread)
    expect(th2).to be_kind_of(Thread)
    expect(th1).to_not eq(Thread.current)
    expect(th2).to_not eq(Thread.current)
    expect(th1).to_not eq(th2)
    expect(v).to eq(6)
  end

  it "sets the name of the thread that runs the callback" do
    skip "not yet supported on TruffleRuby" if RUBY_ENGINE == "truffleruby"
    skip "not yet supported on JRuby" if RUBY_ENGINE == "jruby"

    callback_runner_thread = nil

    LibTest.testAsyncCallback(proc { callback_runner_thread = Thread.current }, 0)

    expect(callback_runner_thread.name).to eq("FFI Callback Runner")
  end
end