Sha256: ed8594a8e38bdadc5dbac0b0f5826ab958958dda40b4a9a575015c74ce490388

Contents?: true

Size: 1.39 KB

Versions: 16

Compression:

Stored size: 1.39 KB

Contents

require 'test_helper'

class ThreadedContext
  extend Surrounded::Context

  def initialize(leader, members)
    role_names = [:leader, :members]
    role_players = [leader, members]

    role_names.concat(members.map{|member| :"member_#{member.object_id}" })
    role_players.concat(members)

    map_roles(role_names.zip(role_players))

  end
  private_attr_reader :leader, :members

  trigger :meet do
    result = []
    result << leader.greet
    result << members.concurrent_map do |member|
      result << member.greet
    end
    result.flatten.join(' ')
  end

  module Leader
    def greet
      "Hello everyone. I am #{name}"
    end
  end

  module Member
    def greet
      "Hello #{leader.name}, I am #{name}"
    end
  end

  module Members
    include Surrounded

    def concurrent_map
      map do |member|
        Thread.new do
          yield member
        end
      end.each(&:join)
    end
  end
end

describe ThreadedContext do
  let(:jim){ User.new('Jim') }
  let(:amy){ User.new('Amy') }
  let(:guille){ User.new('Guille') }
  let(:jason){ User.new('Jason') }
  let(:dave){ User.new('Dave') }

  let(:greeter){ jim }
  let(:members){ [amy, guille, jason, dave] }

  it 'works in multi-threaded environments' do
    meeting = ThreadedContext.new(jim, members)

    result = meeting.meet

    assert_includes result, 'Hello everyone. I am Jim'
    assert_includes result, 'Hello Jim, I am Amy'
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
surrounded-0.8.2 test/example_threaded_test.rb
surrounded-0.8.1 test/example_threaded_test.rb
surrounded-0.8.0 test/example_threaded_test.rb
surrounded-0.7.3 test/example_threaded_test.rb
surrounded-0.7.2 test/example_threaded_test.rb
surrounded-0.7.1 test/example_threaded_test.rb
surrounded-0.7.0 test/example_threaded_test.rb
surrounded-0.6.0 test/example_threaded_test.rb
surrounded-0.5.1 test/example_threaded_test.rb
surrounded-0.5.0 test/example_threaded_test.rb
surrounded-0.4.1 test/example_threaded_test.rb
surrounded-0.4.0 test/example_threaded_test.rb
surrounded-0.3.1 test/example_threaded_test.rb
surrounded-0.3.0 test/example_threaded_test.rb
surrounded-0.2.1 test/example_threaded_test.rb
surrounded-0.2.0 test/example_threaded_test.rb