Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: ec282393ad
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 151 lines (129 sloc) 4.287 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
{nullmq} = require('../nullmq.js')
{Stomp} = require('../lib/stomp.js')
Stomp.WebSocket = require('./server.mock.js').ReflectorServerMock

describe "nullmq with reflector", ->
  it "round robins and fans-in for PUSH-PULL pipelining", ->
    url = "ws://endpoint"
    received = 0
    messages = []
    sink = null
    message = "Foobar"
    ctx = new nullmq.Context url, ->
      worker1in = ctx.socket(nullmq.PULL)
      worker1out = ctx.socket(nullmq.PUSH)
      worker1in.connect('/source')
      worker1out.connect('/sink')
      worker1in.recvall (msg) ->
        worker1out.send "worker1: #{msg}"
      
      worker2in = ctx.socket(nullmq.PULL)
      worker2out = ctx.socket(nullmq.PUSH)
      worker2in.connect('/source')
      worker2out.connect('/sink')
      worker2in.recvall (msg) ->
        worker2out.send "worker2: #{msg}"
      
      sink = ctx.socket(nullmq.PULL)
      sink.bind('/sink')
      sink.recvall (msg) ->
        messages.push msg
      
      source = ctx.socket(nullmq.PUSH)
      source.bind('/source')
      source.send message
      source.send message
      source.send message
      
    waitsFor -> messages.length > 2
    runs ->
      expect(messages).toContain "worker1: #{message}"
      expect(messages).toContain "worker2: #{message}"
      expect(messages.length).toBe 3
      ctx.term()
      
  it "round robin to REP sockets over several destinations from REQ socket", ->
    url = "ws://endpoint"
    received = 0
    messages = []
    req = null
    ctx = new nullmq.Context url, ->
      rep1 = ctx.socket(nullmq.REP)
      rep1.connect('/destination1')
      rep1.recvall (msg) ->
        rep1.send "rep1"
  
      rep2 = ctx.socket(nullmq.REP)
      rep2.bind('/destination2')
      rep2.recvall (msg) ->
        rep2.send "rep2"
  
      req = ctx.socket(nullmq.REQ)
      req.connect('/destination1')
      req.connect('/destination2')
      req.send("foo")
      req.recv (reply) ->
        messages.push reply
        received++
      
    waitsFor -> received > 0
    runs ->
      expect(messages).toContain "rep1"
      req.send("bar")
      req.recv (reply) ->
        messages.push reply
        received++
      
    waitsFor -> received > 1
    runs ->
      expect(messages).toContain "rep2"
      ctx.term()
      
  it "does fanout when using PUB and SUB over several destinations", ->
    url = "ws://endpoint"
    received = 0
    [messages1, messages2, messages3] = [[],[],[]]
    msg = "Hello world"
    ctx = new nullmq.Context url, ->
    
      sub1 = ctx.socket(nullmq.SUB)
      sub1.connect('/destination1')
      sub1.setsockopt(nullmq.SUBSCRIBE, '')
      sub1.recvall (msg) ->
        messages1.push(msg)
        received++
      
      sub2 = ctx.socket(nullmq.SUB)
      sub2.connect('/destination2')
      sub2.setsockopt(nullmq.SUBSCRIBE, '')
      sub2.recvall (msg) ->
        messages2.push(msg)
        received++
      
      sub3 = ctx.socket(nullmq.SUB)
      sub3.connect('/destination1')
      sub3.connect('/destination2')
      sub3.setsockopt(nullmq.SUBSCRIBE, '')
      sub3.recvall (msg) ->
        messages3.push(msg)
        received++
      
      pub = ctx.socket(nullmq.PUB)
      pub.connect('/destination1')
      pub.connect('/destination2')
      pub.send(msg)
    
    waitsFor -> received >= 4
    runs ->
      expect(messages1).toContain msg
      expect(messages2).toContain msg
      expect(messages3).toContain msg
      expect(messages3.length).toEqual 2
      ctx.term()


describe "nullmq namespace", ->
  it "can create a Context", ->
    url = "ws://endpoint"
    ctx = new nullmq.Context url, ->
      expect(ctx.url).toEqual url
      expect(ctx.sockets.length).toEqual 0
      ctx.term()

describe "nullmq.Context", ->
  url = "ws://endpoint"
  
  it "can create a Socket", ->
    ctx = new nullmq.Context url, ->
      socket = ctx.socket(nullmq.REQ)
      expect(socket.type).toEqual nullmq.REQ
      ctx.term()
  
  it "closes sockets on termination", ->
    ctx = new nullmq.Context url, ->
      socket1 = ctx.socket(nullmq.REQ)
      socket2 = ctx.socket(nullmq.REQ)
      expect(socket1.closed).toEqual false
      expect(socket2.closed).toEqual false
      ctx.term()
      expect(socket1.closed).toEqual true
      expect(socket2.closed).toEqual true
  
Something went wrong with that request. Please try again.