Sha256: b493db0f4896283525c5bcb1101302d2cd016eba5239d11e70d2a9a0c4029dad

Contents?: true

Size: 1.18 KB

Versions: 14

Compression:

Stored size: 1.18 KB

Contents

require 'test_helper'

class ThreadedContext
  extend Surrounded::Context

  initialize :leader, :members

  trigger :meet do
    leader.welcome
  end

  module Leader
    def welcome
      result = []
      result << leader.greet
      result << members.concurrent_map do |member|
        result << member.greet
      end
      result.flatten.join(' ')
    end

    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

14 entries across 14 versions & 1 rubygems

Version Path
surrounded-0.9.11 test/example_threaded_test.rb
surrounded-0.9.10 test/example_threaded_test.rb
surrounded-0.9.9 test/example_threaded_test.rb
surrounded-0.9.8 test/example_threaded_test.rb
surrounded-0.9.7 test/example_threaded_test.rb
surrounded-0.9.6 test/example_threaded_test.rb
surrounded-0.9.5 test/example_threaded_test.rb
surrounded-0.9.4 test/example_threaded_test.rb
surrounded-0.9.3 test/example_threaded_test.rb
surrounded-0.9.2 test/example_threaded_test.rb
surrounded-0.9.1 test/example_threaded_test.rb
surrounded-0.9.0 test/example_threaded_test.rb
surrounded-0.8.4 test/example_threaded_test.rb
surrounded-0.8.3 test/example_threaded_test.rb