|
4 | 4 | const chai = require('chai') |
5 | 5 | chai.use(require('dirty-chai')) |
6 | 6 | const expect = chai.expect |
| 7 | +const sinon = require('sinon') |
7 | 8 | const signalling = require('libp2p-webrtc-star/src/sig-server') |
8 | 9 | const parallel = require('async/parallel') |
9 | 10 | const crypto = require('crypto') |
@@ -58,6 +59,163 @@ describe('peer discovery', () => { |
58 | 59 | }) |
59 | 60 | } |
60 | 61 |
|
| 62 | + describe('module registration', () => { |
| 63 | + it('should enable module by default', (done) => { |
| 64 | + const mockDiscovery = { |
| 65 | + on: sinon.stub(), |
| 66 | + start: sinon.stub().callsArg(0), |
| 67 | + stop: sinon.stub().callsArg(0) |
| 68 | + } |
| 69 | + |
| 70 | + const options = { modules: { peerDiscovery: [ mockDiscovery ] } } |
| 71 | + |
| 72 | + createNode(['/ip4/0.0.0.0/tcp/0'], options, (err, node) => { |
| 73 | + expect(err).to.not.exist() |
| 74 | + |
| 75 | + node.start((err) => { |
| 76 | + expect(err).to.not.exist() |
| 77 | + expect(mockDiscovery.start.called).to.be.true() |
| 78 | + node.stop(done) |
| 79 | + }) |
| 80 | + }) |
| 81 | + }) |
| 82 | + |
| 83 | + it('should enable module by configutation', (done) => { |
| 84 | + const mockDiscovery = { |
| 85 | + on: sinon.stub(), |
| 86 | + start: sinon.stub().callsArg(0), |
| 87 | + stop: sinon.stub().callsArg(0), |
| 88 | + tag: 'mockDiscovery' |
| 89 | + } |
| 90 | + |
| 91 | + const enabled = sinon.stub().returns(true) |
| 92 | + |
| 93 | + const options = { |
| 94 | + modules: { peerDiscovery: [ mockDiscovery ] }, |
| 95 | + config: { |
| 96 | + peerDiscovery: { |
| 97 | + mockDiscovery: { |
| 98 | + get enabled () { |
| 99 | + return enabled() |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + createNode(['/ip4/0.0.0.0/tcp/0'], options, (err, node) => { |
| 107 | + expect(err).to.not.exist() |
| 108 | + |
| 109 | + node.start((err) => { |
| 110 | + expect(err).to.not.exist() |
| 111 | + expect(mockDiscovery.start.called).to.be.true() |
| 112 | + expect(enabled.called).to.be.true() |
| 113 | + node.stop(done) |
| 114 | + }) |
| 115 | + }) |
| 116 | + }) |
| 117 | + |
| 118 | + it('should disable module by configutation', (done) => { |
| 119 | + const mockDiscovery = { |
| 120 | + on: sinon.stub(), |
| 121 | + start: sinon.stub().callsArg(0), |
| 122 | + stop: sinon.stub().callsArg(0), |
| 123 | + tag: 'mockDiscovery' |
| 124 | + } |
| 125 | + |
| 126 | + const disabled = sinon.stub().returns(false) |
| 127 | + |
| 128 | + const options = { |
| 129 | + modules: { peerDiscovery: [ mockDiscovery ] }, |
| 130 | + config: { |
| 131 | + peerDiscovery: { |
| 132 | + mockDiscovery: { |
| 133 | + get enabled () { |
| 134 | + return disabled() |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + createNode(['/ip4/0.0.0.0/tcp/0'], options, (err, node) => { |
| 142 | + expect(err).to.not.exist() |
| 143 | + |
| 144 | + node.start((err) => { |
| 145 | + expect(err).to.not.exist() |
| 146 | + expect(mockDiscovery.start.called).to.be.false() |
| 147 | + expect(disabled.called).to.be.true() |
| 148 | + node.stop(done) |
| 149 | + }) |
| 150 | + }) |
| 151 | + }) |
| 152 | + |
| 153 | + it('should register module passed as function', (done) => { |
| 154 | + const mockDiscovery = { |
| 155 | + on: sinon.stub(), |
| 156 | + start: sinon.stub().callsArg(0), |
| 157 | + stop: sinon.stub().callsArg(0) |
| 158 | + } |
| 159 | + |
| 160 | + const MockDiscovery = sinon.stub().returns(mockDiscovery) |
| 161 | + MockDiscovery.tag = 'mockDiscovery' |
| 162 | + |
| 163 | + const options = { |
| 164 | + modules: { peerDiscovery: [ MockDiscovery ] }, |
| 165 | + config: { |
| 166 | + peerDiscovery: { |
| 167 | + mockDiscovery: { |
| 168 | + enabled: true, |
| 169 | + time: Date.now() |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + createNode(['/ip4/0.0.0.0/tcp/0'], options, (err, node) => { |
| 176 | + expect(err).to.not.exist() |
| 177 | + |
| 178 | + node.start((err) => { |
| 179 | + expect(err).to.not.exist() |
| 180 | + expect(mockDiscovery.start.called).to.be.true() |
| 181 | + expect(MockDiscovery.called).to.be.true() |
| 182 | + // Ensure configuration was passed |
| 183 | + expect(MockDiscovery.firstCall.args[0]) |
| 184 | + .to.deep.include(options.config.peerDiscovery.mockDiscovery) |
| 185 | + node.stop(done) |
| 186 | + }) |
| 187 | + }) |
| 188 | + }) |
| 189 | + |
| 190 | + it('should register module passed as object', (done) => { |
| 191 | + const mockDiscovery = { |
| 192 | + on: sinon.stub(), |
| 193 | + start: sinon.stub().callsArg(0), |
| 194 | + stop: sinon.stub().callsArg(0), |
| 195 | + tag: 'mockDiscovery' |
| 196 | + } |
| 197 | + |
| 198 | + const options = { |
| 199 | + modules: { peerDiscovery: [ mockDiscovery ] }, |
| 200 | + config: { |
| 201 | + peerDiscovery: { |
| 202 | + mockDiscovery: { enabled: true } |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + createNode(['/ip4/0.0.0.0/tcp/0'], options, (err, node) => { |
| 208 | + expect(err).to.not.exist() |
| 209 | + |
| 210 | + node.start((err) => { |
| 211 | + expect(err).to.not.exist() |
| 212 | + expect(mockDiscovery.start.called).to.be.true() |
| 213 | + node.stop(done) |
| 214 | + }) |
| 215 | + }) |
| 216 | + }) |
| 217 | + }) |
| 218 | + |
61 | 219 | describe('MulticastDNS', () => { |
62 | 220 | setup({ |
63 | 221 | config: { |
|
0 commit comments