Sha256: d20690c4986dfe88130e79bd6d2a5c6d83a9ec8d9d507ee2daba378b7d454ced

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 KB

Contents

require "spec_helper"

describe "Channel of Channels" do

  Request = Struct.new(:args, :resultChan)

  it "should be able to pass channels as first class citizens" do
    server = Proc.new do |reqs|
      2.times do |n|
        res = Request.new(n, channel!(Integer))

        reqs << res
        expect(res.resultChan.receive[0]).to eq(n+1)
        res.resultChan.close
      end
    end

    worker = Proc.new do |reqs|
      loop do
        req, ok = reqs.receive
        break unless ok
        req.resultChan << req.args+1
      end
    end

    clientRequests = channel!(Request)

    s = go!(clientRequests, &server)
    c = go!(clientRequests, &worker)

    s.join
    clientRequests.close
  end

  it "should work with multiple workers" do
    worker = Proc.new do |reqs|
      loop do
        req, _ = reqs.receive
        req.resultChan << req.args+1
      end
    end

    clientRequests = channel!(Request)

    # start multiple workers
    go!(clientRequests, &worker)
    go!(clientRequests, &worker)

    # start server
    s = go! clientRequests do |reqs|
      2.times do |n|
        res = Request.new(n, channel!(Integer))

        reqs << res
        expect(res.resultChan.receive[0]).to eq(n+1)
        res.resultChan.close
      end
    end

    s.join
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
agent-0.12.0 spec/examples/channel_of_channels_spec.rb
agent-0.11.0 spec/examples/channel_of_channels_spec.rb