|
5 | 5 |
|
6 | 6 | const chai = require('chai')
|
7 | 7 | chai.use(require('dirty-chai'))
|
| 8 | +chai.use(require('chai-checkmark')) |
8 | 9 | const expect = chai.expect
|
9 | 10 | const parallel = require('async/parallel')
|
10 |
| -const waterfall = require('async/waterfall') |
| 11 | +const series = require('async/series') |
11 | 12 | const _times = require('lodash.times')
|
12 | 13 |
|
13 | 14 | const createNode = require('./utils/create-node')
|
@@ -52,26 +53,37 @@ function stopTwo (nodes, callback) {
|
52 | 53 | // TODO: consider if all or some of those should come here
|
53 | 54 | describe('.pubsub', () => {
|
54 | 55 | describe('.pubsub on (default)', (done) => {
|
55 |
| - it('start two nodes and send one message', (done) => { |
56 |
| - waterfall([ |
57 |
| - (cb) => startTwo(cb), |
58 |
| - (nodes, cb) => { |
59 |
| - const data = Buffer.from('test') |
60 |
| - nodes[0].pubsub.subscribe('pubsub', |
61 |
| - (msg) => { |
62 |
| - expect(msg.data).to.eql(data) |
63 |
| - cb(null, nodes) |
64 |
| - }, |
65 |
| - (err) => { |
66 |
| - expect(err).to.not.exist() |
67 |
| - setTimeout(() => nodes[1].pubsub.publish('pubsub', data, (err) => { |
68 |
| - expect(err).to.not.exist() |
69 |
| - }), 500) |
70 |
| - } |
71 |
| - ) |
72 |
| - }, |
73 |
| - (nodes, cb) => stopTwo(nodes, cb) |
74 |
| - ], done) |
| 56 | + it('start two nodes and send one message, then unsubscribe', (done) => { |
| 57 | + // Check the final series error, and the publish handler |
| 58 | + expect(2).checks(done) |
| 59 | + |
| 60 | + let nodes |
| 61 | + const data = Buffer.from('test') |
| 62 | + const handler = (msg) => { |
| 63 | + // verify the data is correct and mark the expect |
| 64 | + expect(msg.data).to.eql(data).mark() |
| 65 | + } |
| 66 | + |
| 67 | + series([ |
| 68 | + // Start the nodes |
| 69 | + (cb) => startTwo((err, _nodes) => { |
| 70 | + nodes = _nodes |
| 71 | + cb(err) |
| 72 | + }), |
| 73 | + // subscribe on the first |
| 74 | + (cb) => nodes[0].pubsub.subscribe('pubsub', handler, cb), |
| 75 | + // Wait a moment before publishing |
| 76 | + (cb) => setTimeout(cb, 500), |
| 77 | + // publish on the second |
| 78 | + (cb) => nodes[1].pubsub.publish('pubsub', data, cb), |
| 79 | + // unsubscribe on the first |
| 80 | + (cb) => nodes[0].pubsub.unsubscribe('pubsub', handler, cb), |
| 81 | + // Stop both nodes |
| 82 | + (cb) => stopTwo(nodes, cb) |
| 83 | + ], (err) => { |
| 84 | + // Verify there was no error, and mark the expect |
| 85 | + expect(err).to.not.exist().mark() |
| 86 | + }) |
75 | 87 | })
|
76 | 88 | })
|
77 | 89 |
|
|
0 commit comments