Sha256: 138650c23622150ea0da1fbaac7b65b166a3d6f0a0b91054a217d8b3ca74565a

Contents?: true

Size: 1.95 KB

Versions: 3

Compression:

Stored size: 1.95 KB

Contents

require "spec_helper"

describe "Producer-Consumer" do
  it "should synchronize by communication" do

    #     func producer(c chan int, N int, s chan bool) {
    #       for i := 0; i < N; i++ {
    #         fmt.Printf("producer: %d\n", i)
    #         c <- i
    #       }
    #       s <- true
    #     }
    #
    #     func consumer(c chan int, N int, s chan bool) {
    #       for i := 0; i < N; i++ {
    #         fmt.Printf("consumer got: %d\n", <- c)
    #       }
    #       s <- true
    #     }
    #
    #     func main() {
    #       runtime.GOMAXPROCS(2)
    #
    #       c := make(chan int)
    #       s := make(chan bool)
    #
    #       go producer(c, 10, s)
    #       go consumer(c, 10, s)
    #
    #       <- s
    #       <- s
    #     }

    producer = Proc.new do |c, n, s|
      # print "producer: starting\n"

      n.times do |i|
        # print "producer: #{i+1} of #{n}\n"
        c << i
        # print "producer sent: #{i}\n"
      end

      # print "producer: finished\n"

      s << "producer finished"
    end

    consumer = Proc.new do |c, n, s|
      # print "consumer: starting\n"
      n.times do |i|
        # print "consumer: #{i+1} of #{n}\n"
        msg = c.receive
        # print "consumer got: #{msg}\n"
      end

      # print "consumer: finished\n"

      s << "consumer finished"
    end

    c = channel!(Integer)
    s = channel!(String)

    go!(c, 3, s, &producer)
    sleep 0.1
    go!(c, 3, s, &consumer)

    messages = [s.pop[0], s.pop[0]]
    messages.should include("producer finished")
    messages.should include("consumer finished")

    c.close
    s.close
  end

  it "should work as generator" do
    producer = Proc.new do |c|
      i = 0
      loop { c.pipe << i+= 1 }
    end

    Generator = Struct.new(:name, :pipe)
    c = channel!(Integer)
    g = Generator.new(:incr, c)

    go!(g, &producer)

    c.receive[0].should == 1
    c.receive[0].should == 2
    c.receive[0].should == 3
    c.close
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
agent-0.10.0 spec/examples/producer_consumer_spec.rb
agent-0.9.1 spec/examples/producer_consumer_spec.rb
agent-0.9.0 spec/examples/producer_consumer_spec.rb