Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Thread#fetch #5034

Closed
wants to merge 1 commit into from

Conversation

nomadium
Copy link
Contributor

@nomadium nomadium commented Feb 6, 2018

Hi folks,

This is another feature targeting Ruby 2.5 [1]: Thread#fetch (feature #13009).

Note: The tests are copied from MRI.

Thanks for your review and feedback.

  1. Ruby 2.5 Support #4876

@nomadium
Copy link
Contributor Author

nomadium commented Mar 4, 2018

I modified the PR because updated MRI 2.5.0 tests were merged in ruby-2.5 branch with 14a2aca.

I reverified the associated test with this new method and it works as expected:

  def test_thread_local_fetch
    t = Thread.new { sleep }

    assert_equal(false, t.key?(:foo))

    t["foo"] = "foo"
    t["bar"] = "bar"
    t["baz"] = "baz"

    x = nil
    assert_equal("foo", t.fetch(:foo, 0))
    assert_equal("foo", t.fetch(:foo) {x = true})
    assert_nil(x)
    assert_equal("foo", t.fetch("foo", 0))
    assert_equal("foo", t.fetch("foo") {x = true})
    assert_nil(x)

    x = nil
    assert_equal(0, t.fetch(:qux, 0))
    assert_equal(1, t.fetch(:qux) {x = 1})
    assert_equal(1, x)
    assert_equal(2, t.fetch("qux", 2))
    assert_equal(3, t.fetch("qux") {x = 3})
    assert_equal(3, x)

    assert_raise(KeyError) {t.fetch(:qux)}
  ensure
    t.kill if t
  end
miguel@alice:~/jruby$ PATH=$PWD/bin:$PATH jruby test/mri/runner.rb -v --color=never --tty=no --excludes=test/mri/excludes -q -n test_thread_local_fetch -- ruby/test_thread.rb
Run options: -v --color=never --tty=no --excludes=test/mri/excludes -q -n test_thread_local_fetch --

# Running tests:

[1/1] TestThread#test_thread_local_fetch = 0.01 s
Finished tests in 0.085849s, 11.6484 tests/s, 186.3737 assertions/s.
1 tests, 16 assertions, 0 failures, 0 errors, 0 skips

ruby -v: jruby 9.3.0.0-SNAPSHOT (2.5.0) 2018-03-04 4e258ca Java HotSpot(TM) 64-Bit Server VM 25.102-b14 on 1.8.0_102-b14 +jit [darwin-x86_64]

@nomadium
Copy link
Contributor Author

Closing it in favor of #5089.

@nomadium nomadium closed this Mar 17, 2018
@nomadium nomadium deleted the add-method-thread-fetch branch March 18, 2018 12:27
@enebo enebo added this to the Invalid or Duplicate milestone Apr 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants