Skip to content

Async task is appears to be blocking #363

Closed

Description

@stan-stately

I'm trying to run a really simple test:

require "async"
describe "async" do
  it "tests async" do
    task = Async do
      sleep(100)
    end
    task.stop
    puts "done"
  end
end

This should return instantly right? why does the test take 100sec to run? i'm sorry if this is really obvious

im on:

  • ruby: 3.3.4
  • async: 2.21.1
  • io-event: 1.7.4

Activity

  1. ioquatix commented on Nov 29, 2024

    @ioquatix
    Member

    A top level async task is blocking as there is no way the sleep can execute while continuing to execute the code outside the event loop.

    You should run your entire test inside an event, e.g.

    require "async"
    describe "async" do
      it "tests async" do
        Async do
          task = Async do
            sleep(100)
          end
          task.stop
          puts "done"
        end
      end
    end

    If you are using Sus you can use sus-fixtures-async and write it like this:

    require "async"
    require "sus/fixtures/async"
    
    describe "async" do
      include Sus::Fixtures::Async::ReactorContext
      it "tests async" do
        task = Async do
          sleep(100)
        end
        task.stop
        puts "done"
      end
    end

    The reactor context also adds a 60s timeout to the code to ensure your tests aren't hanging.

  2. locked and limited conversation to collaborators on Nov 29, 2024
  3. converted this issue into a discussion #364 on Nov 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions