From 9edae80c5c2fc93b01558ce05521d17a5c2a82e3 Mon Sep 17 00:00:00 2001 From: Bryan Powell Date: Sun, 17 Sep 2023 10:56:59 -0700 Subject: [PATCH] Close the io object when the routine is finished --- lib/goru/routines/io.rb | 7 +++++++ spec/features/io/closing_spec.rb | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 spec/features/io/closing_spec.rb diff --git a/lib/goru/routines/io.rb b/lib/goru/routines/io.rb index bf1dd90..4d640fa 100644 --- a/lib/goru/routines/io.rb +++ b/lib/goru/routines/io.rb @@ -161,6 +161,13 @@ def unbridge @reactor.wakeup end + # [public] + # + def finished(...) + @io.close + super + end + private def status_changed case @status when Routine::STATUS_FINISHED diff --git a/spec/features/io/closing_spec.rb b/spec/features/io/closing_spec.rb new file mode 100644 index 0000000..b1f299c --- /dev/null +++ b/spec/features/io/closing_spec.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +require "goru/scheduler" + +RSpec.describe "closing the io object" do + let(:scheduler) { + Goru::Scheduler.new + } + + let(:io) { + double(:io) + } + + it "closes the io when the routine is finished" do + expect(io).to receive(:close) + + scheduler.go(intent: :read, io: io) { |routine| + routine.finished + } + + scheduler.wait + end +end