Permalink
Browse files

Spec out buffered port

  • Loading branch information...
1 parent 5e6b4eb commit 4fb80b222fbc888d888ce7fbcf564c2f60ec9e85 Kenneth Kan committed Mar 2, 2014
Showing with 49 additions and 6 deletions.
  1. +4 −4 spec/Component.coffee
  2. +43 −0 spec/InPort.coffee
  3. +2 −2 spec/OutPort.coffee
@@ -33,9 +33,9 @@ describe 'Component', ->
optionalPort: new port
c.inPorts.requiredPort.attach s1
c.inPorts.optionalPort.attach s2
- run = ->
+ f = ->
s2.send 'some-data'
- chai.expect(run).to.not.throw()
+ chai.expect(f).to.not.throw()
it 'should simply forward error if error port is attached', (done) ->
s1 = new socket
@@ -53,6 +53,6 @@ describe 'Component', ->
s3.on 'connect', ->
chai.assert true
done()
- run = ->
+ f = ->
s2.send 'some-data'
- chai.expect(run).to.not.throw()
+ chai.expect(f).to.not.throw()
View
@@ -86,3 +86,46 @@ describe 'Inport Port', ->
it 'should be a URL', ->
chai.expect(-> f 'http://schema.org/Person').to.not.throw()
chai.expect(-> f 'not-a-url').to.throw()
+
+ describe 'with buffering', ->
+ it 'should buffer incoming packets until `receive()`d', ->
+ p = new inport
+ buffered: true
+ s = new socket
+ p.attach s
+
+ p.once 'data', (data) ->
+ # We get notified with the packet as the parameter but it is not popped
+ # off the queue. We choose not to handle the packet for now.
+ chai.expect(data).toEqual 'buffered-data-1'
+ s.send 'buffered-data-1'
+
+ p.once 'data', (data) ->
+ # We should still get the queued up value because it doesn't make sense
+ # to "peek" into the latest packet until all preceding packets have
+ # been consumed.
+ chai.expect(data).toEqual 'buffered-data-1'
+ # Now we consume it. Note that the context should the port itself.
+ _data = @receive()
+ chai.expect(data).toEqual _data
+ s.send 'buffered-data-2'
+
+ p.once 'data', (data) ->
+ # Now we see the second packet
+ chai.expect(data).toEqual 'buffered-data-2'
+ s.send 'buffered-data-3'
+
+ it 'should always return the immediate packet even without buffering', ->
+ p = new inport
+ # Specified here simply for illustrative purpose, otherwise implied
+ # `false`
+ buffered: false
+ s = new socket
+ p.attach s
+
+ p.once 'data', (data) ->
+ # `receive()` returns the same thing
+ _data = @receive()
+ chai.expect(data).toEqual 'data'
+ chai.expect(data).toEqual _data
+ s.send 'data'
View
@@ -53,6 +53,6 @@ describe 'Outport Port', ->
p.attach s1
p.attach s2
p.attach s3
- send ->
+ f ->
p.send 'some-data'
- expect(send).toHaveThrown()
+ expect(f).toHaveThrown()

0 comments on commit 4fb80b2

Please sign in to comment.