Sha256: da69a46e8e0d824e7f03c4b6336419b8b96e8446d80a596fae15075ac6f426da

Contents?: true

Size: 1.56 KB

Versions: 7

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

require_relative "../../spec_helper"

require_relative "../../../lib/ultravisor/child"
require_relative "../../../lib/ultravisor/error"

describe Ultravisor::Child do
  let(:args) { { id: :bob, klass: Object, method: :to_s } }
  let(:child) { Ultravisor::Child.new(**args) }

  describe "#unsafe_instance" do
    context "by default" do
      it "explodes" do
        expect { child.unsafe_instance }.to raise_error(Ultravisor::ThreadSafetyError)
      end
    end

    context "with access: :unsafe" do
      let(:args) { { id: :bob, klass: Object, method: :to_s, access: :unsafe } }

      context "when there's no instance object" do
        it "waits for the instance object to appear" do
          expect(child.instance_variable_get(:@spawn_cv)).to receive(:wait) do
            child.instance_variable_set(:@instance, "gogogo")
          end

          child.unsafe_instance
        end
      end

      context "when there's an instance object" do
        before(:each) do
          child.instance_variable_set(:@instance, "bob")
        end

        it "returns the instance object" do
          expect(child.unsafe_instance).to eq("bob")
        end
      end
    end

    context "when the child is running" do
      it "only exits once the child has finished" do
        child.instance_variable_set(:@thread, Thread.new {})

        expect(child.instance_variable_get(:@spawn_cv)).to receive(:wait).with(child.instance_variable_get(:@spawn_m)) do
          child.instance_variable_set(:@thread, nil)
        end

        child.wait
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
service_skeleton-2.1.0 ultravisor/spec/ultravisor/child/unsafe_instance_spec.rb
service_skeleton-1.0.5 ultravisor/spec/ultravisor/child/unsafe_instance_spec.rb
service_skeleton-2.0.2 ultravisor/spec/ultravisor/child/unsafe_instance_spec.rb
service_skeleton-2.0.1 ultravisor/spec/ultravisor/child/unsafe_instance_spec.rb
service_skeleton-2.0.0 ultravisor/spec/ultravisor/child/unsafe_instance_spec.rb
service_skeleton-1.0.4 ultravisor/spec/ultravisor/child/unsafe_instance_spec.rb
service_skeleton-0.0.0.1.ENOTAG ultravisor/spec/ultravisor/child/unsafe_instance_spec.rb