This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathpubsub.spec.js
151 lines (132 loc) · 3.9 KB
/
pubsub.spec.js
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
/* eslint max-nested-callbacks: ["error", 8] */
/* eslint-env mocha */
'use strict'
const hat = require('hat')
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const IPFS = require('../../src')
const createTempRepo = require('../utils/create-repo-nodejs')
describe('pubsub disabled', () => {
let ipfs
let repo
before(function (done) {
this.timeout(20 * 1000)
repo = createTempRepo()
ipfs = new IPFS({
repo,
config: {
Addresses: {
Swarm: []
},
Pubsub: {
Enabled: false
}
},
preload: {
enabled: false
}
})
ipfs.on('ready', done)
})
after((done) => ipfs.stop(done))
after((done) => repo.teardown(done))
it('should not allow subscribe if disabled', done => {
const topic = hat()
const handler = () => done(new Error('unexpected message'))
ipfs.pubsub.subscribe(topic, handler, (err) => {
expect(err).to.exist()
expect(err.code).to.equal('ERR_PUBSUB_DISABLED')
done()
})
})
it('should not allow subscribe if disabled (promised)', async () => {
try {
const topic = hat()
const handler = () => { throw new Error('unexpected message') }
await ipfs.pubsub.subscribe(topic, handler)
} catch (err) {
return expect(err.code).to.equal('ERR_PUBSUB_DISABLED')
}
throw new Error('expected error to be thrown')
})
it('should not allow unsubscribe if disabled', done => {
const topic = hat()
const handler = () => done(new Error('unexpected message'))
ipfs.pubsub.unsubscribe(topic, handler, (err) => {
expect(err).to.exist()
expect(err.code).to.equal('ERR_PUBSUB_DISABLED')
done()
})
})
it('should not allow unsubscribe if disabled (promised)', async () => {
try {
const topic = hat()
const handler = () => { throw new Error('unexpected message') }
await ipfs.pubsub.unsubscribe(topic, handler)
} catch (err) {
return expect(err.code).to.equal('ERR_PUBSUB_DISABLED')
}
throw new Error('expected error to be thrown')
})
it('should not allow publish if disabled', done => {
const topic = hat()
const msg = Buffer.from(hat())
ipfs.pubsub.publish(topic, msg, (err) => {
expect(err).to.exist()
expect(err.code).to.equal('ERR_PUBSUB_DISABLED')
done()
})
})
it('should not allow publish if disabled (promised)', async () => {
try {
const topic = hat()
const msg = Buffer.from(hat())
await ipfs.pubsub.publish(topic, msg)
} catch (err) {
return expect(err.code).to.equal('ERR_PUBSUB_DISABLED')
}
throw new Error('expected error to be thrown')
})
it('should not allow ls if disabled', done => {
ipfs.pubsub.ls((err) => {
expect(err).to.exist()
expect(err.code).to.equal('ERR_PUBSUB_DISABLED')
done()
})
})
it('should not allow ls if disabled (promised)', async () => {
try {
await ipfs.pubsub.ls()
} catch (err) {
return expect(err.code).to.equal('ERR_PUBSUB_DISABLED')
}
throw new Error('expected error to be thrown')
})
it('should not allow peers if disabled', done => {
const topic = hat()
ipfs.pubsub.peers(topic, (err) => {
expect(err).to.exist()
expect(err.code).to.equal('ERR_PUBSUB_DISABLED')
done()
})
})
it('should not allow peers if disabled (promised)', async () => {
try {
const topic = hat()
await ipfs.pubsub.peers(topic)
} catch (err) {
return expect(err.code).to.equal('ERR_PUBSUB_DISABLED')
}
throw new Error('expected error to be thrown')
})
it('should not allow setMaxListeners if disabled', async () => {
try {
await ipfs.pubsub.setMaxListeners(100)
} catch (err) {
return expect(err.code).to.equal('ERR_PUBSUB_DISABLED')
}
throw new Error('expected error to be thrown')
})
})