abstract_client.js 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667
  1. 'use strict'
  2. /**
  3. * Testing dependencies
  4. */
  5. var should = require('should')
  6. var sinon = require('sinon')
  7. var mqtt = require('../')
  8. var xtend = require('xtend')
  9. var Server = require('./server')
  10. var Store = require('./../lib/store')
  11. var port = 9876
  12. module.exports = function (server, config) {
  13. function connect (opts) {
  14. opts = xtend(config, opts)
  15. return mqtt.connect(opts)
  16. }
  17. describe('closing', function () {
  18. it('should emit close if stream closes', function (done) {
  19. var client = connect()
  20. client.once('connect', function () {
  21. client.stream.end()
  22. })
  23. client.once('close', function () {
  24. client.end()
  25. done()
  26. })
  27. })
  28. it('should mark the client as disconnected', function (done) {
  29. var client = connect()
  30. client.once('close', function () {
  31. client.end()
  32. if (!client.connected) {
  33. done()
  34. } else {
  35. done(new Error('Not marked as disconnected'))
  36. }
  37. })
  38. client.once('connect', function () {
  39. client.stream.end()
  40. })
  41. })
  42. it('should stop ping timer if stream closes', function (done) {
  43. var client = connect()
  44. client.once('close', function () {
  45. should.not.exist(client.pingTimer)
  46. client.end()
  47. done()
  48. })
  49. client.once('connect', function () {
  50. should.exist(client.pingTimer)
  51. client.stream.end()
  52. })
  53. })
  54. it('should emit close after end called', function (done) {
  55. var client = connect()
  56. client.once('close', function () {
  57. done()
  58. })
  59. client.once('connect', function () {
  60. client.end()
  61. })
  62. })
  63. it('should emit end after end called and client must be disconnected', function (done) {
  64. var client = connect()
  65. client.once('end', function () {
  66. if (client.disconnected) {
  67. return done()
  68. }
  69. done(new Error('client must be disconnected'))
  70. })
  71. client.once('connect', function () {
  72. client.end()
  73. })
  74. })
  75. it('should pass store close error to end callback but not to end listeners', function (done) {
  76. var store = new Store()
  77. var client = connect({outgoingStore: store})
  78. store.close = function (cb) {
  79. cb(new Error('test'))
  80. }
  81. client.once('end', function () {
  82. if (arguments.length === 0) {
  83. return done()
  84. }
  85. throw new Error('no argument shoould be passed to event')
  86. })
  87. client.once('connect', function () {
  88. client.end(function (test) {
  89. if (test && test.message === 'test') {
  90. return
  91. }
  92. throw new Error('bad argument passed to callback')
  93. })
  94. })
  95. })
  96. it('should return `this` if end called twice', function (done) {
  97. var client = connect()
  98. client.once('connect', function () {
  99. client.end()
  100. var value = client.end()
  101. if (value === client) {
  102. done()
  103. } else {
  104. done(new Error('Not returning client.'))
  105. }
  106. })
  107. })
  108. it('should emit end only on first client end', function (done) {
  109. var client = connect()
  110. client.once('end', function () {
  111. var timeout = setTimeout(done.bind(null), 200)
  112. client.once('end', function () {
  113. clearTimeout(timeout)
  114. done(new Error('end was emitted twice'))
  115. })
  116. client.end()
  117. })
  118. client.once('connect', client.end.bind(client))
  119. })
  120. it('should stop ping timer after end called', function (done) {
  121. var client = connect()
  122. client.once('connect', function () {
  123. should.exist(client.pingTimer)
  124. client.end()
  125. should.not.exist(client.pingTimer)
  126. done()
  127. })
  128. })
  129. it('should be able to end even on a failed connection', function (done) {
  130. var client = connect({host: 'this_hostname_should_not_exist'})
  131. var timeout = setTimeout(function () {
  132. done(new Error('Failed to end a disconnected client'))
  133. }, 500)
  134. setTimeout(function () {
  135. client.end(function () {
  136. clearTimeout(timeout)
  137. done()
  138. })
  139. }, 200)
  140. })
  141. it('should emit end even on a failed connection', function (done) {
  142. var client = connect({host: 'this_hostname_should_not_exist'})
  143. var timeout = setTimeout(function () {
  144. done(new Error('Disconnected client has failed to emit end'))
  145. }, 500)
  146. client.once('end', function () {
  147. clearTimeout(timeout)
  148. done()
  149. })
  150. setTimeout(client.end.bind(client), 200)
  151. })
  152. it('should emit end only once for a reconnecting client', function (done) {
  153. var client = connect({host: 'this_hostname_should_not_exist', connectTimeout: 10, reconnectPeriod: 10})
  154. client.once('end', function () {
  155. var timeout = setTimeout(done.bind(null))
  156. client.once('end', function () {
  157. clearTimeout(timeout)
  158. done(new Error('end emitted twice'))
  159. })
  160. })
  161. setTimeout(client.end.bind(client), 300)
  162. })
  163. })
  164. describe('connecting', function () {
  165. it('should connect to the broker', function (done) {
  166. var client = connect()
  167. client.on('error', done)
  168. server.once('client', function () {
  169. client.end()
  170. done()
  171. })
  172. })
  173. it('should send a default client id', function (done) {
  174. var client = connect()
  175. client.on('error', done)
  176. server.once('client', function (serverClient) {
  177. serverClient.once('connect', function (packet) {
  178. packet.clientId.should.match(/mqttjs.*/)
  179. serverClient.disconnect()
  180. done()
  181. })
  182. })
  183. })
  184. it('should send be clean by default', function (done) {
  185. var client = connect()
  186. client.on('error', done)
  187. server.once('client', function (serverClient) {
  188. serverClient.once('connect', function (packet) {
  189. packet.clean.should.be.true()
  190. serverClient.disconnect()
  191. done()
  192. })
  193. })
  194. })
  195. it('should connect with the given client id', function (done) {
  196. var client = connect({clientId: 'testclient'})
  197. client.on('error', function (err) {
  198. throw err
  199. })
  200. server.once('client', function (serverClient) {
  201. serverClient.once('connect', function (packet) {
  202. packet.clientId.should.match(/testclient/)
  203. serverClient.disconnect()
  204. done()
  205. })
  206. })
  207. })
  208. it('should connect with the client id and unclean state', function (done) {
  209. var client = connect({clientId: 'testclient', clean: false})
  210. client.on('error', function (err) {
  211. throw err
  212. })
  213. server.once('client', function (serverClient) {
  214. serverClient.once('connect', function (packet) {
  215. packet.clientId.should.match(/testclient/)
  216. packet.clean.should.be.false()
  217. serverClient.disconnect()
  218. done()
  219. })
  220. })
  221. })
  222. it('should require a clientId with clean=false', function (done) {
  223. try {
  224. var client = connect({ clean: false })
  225. client.on('error', function (err) {
  226. done(err)
  227. // done(new Error('should have thrown'));
  228. })
  229. } catch (err) {
  230. done()
  231. }
  232. })
  233. it('should default to localhost', function (done) {
  234. var client = connect({clientId: 'testclient'})
  235. client.on('error', function (err) {
  236. throw err
  237. })
  238. server.once('client', function (serverClient) {
  239. serverClient.once('connect', function (packet) {
  240. packet.clientId.should.match(/testclient/)
  241. serverClient.disconnect()
  242. done()
  243. })
  244. })
  245. })
  246. it('should emit connect', function (done) {
  247. var client = connect()
  248. client.once('connect', function () {
  249. client.end()
  250. done()
  251. })
  252. client.once('error', done)
  253. })
  254. it('should provide connack packet with connect event', function (done) {
  255. server.once('client', function (serverClient) {
  256. serverClient.connack({returnCode: 0, sessionPresent: true})
  257. server.once('client', function (serverClient) {
  258. serverClient.connack({returnCode: 0, sessionPresent: false})
  259. })
  260. })
  261. var client = connect()
  262. client.once('connect', function (packet) {
  263. should(packet.sessionPresent).be.equal(true)
  264. client.once('connect', function (packet) {
  265. should(packet.sessionPresent).be.equal(false)
  266. client.end()
  267. done()
  268. })
  269. })
  270. })
  271. it('should mark the client as connected', function (done) {
  272. var client = connect()
  273. client.once('connect', function () {
  274. client.end()
  275. if (client.connected) {
  276. done()
  277. } else {
  278. done(new Error('Not marked as connected'))
  279. }
  280. })
  281. })
  282. it('should emit error', function (done) {
  283. var client = connect({clientId: 'invalid'})
  284. client.once('connect', function () {
  285. done(new Error('Should not emit connect'))
  286. })
  287. client.once('error', function (error) {
  288. should(error.code).be.equal(2) // code for clientID identifer rejected
  289. client.end()
  290. done()
  291. })
  292. })
  293. it('should have different client ids', function (done) {
  294. var client1 = connect()
  295. var client2 = connect()
  296. client1.options.clientId.should.not.equal(client2.options.clientId)
  297. client1.end(true)
  298. client2.end(true)
  299. setImmediate(done)
  300. })
  301. })
  302. describe('handling offline states', function () {
  303. it('should emit offline events once when the client transitions from connected states to disconnected ones', function (done) {
  304. var client = connect({reconnectPeriod: 20})
  305. client.on('connect', function () {
  306. this.stream.end()
  307. })
  308. client.on('offline', function () {
  309. client.end(true, done)
  310. })
  311. })
  312. it('should emit offline events once when the client (at first) can NOT connect to servers', function (done) {
  313. // fake a port
  314. var client = connect({ reconnectPeriod: 20, port: 4557 })
  315. client.on('offline', function () {
  316. client.end(true, done)
  317. })
  318. })
  319. })
  320. describe('topic validations when subscribing', function () {
  321. it('should be ok for well-formated topics', function (done) {
  322. var client = connect()
  323. client.subscribe(
  324. [
  325. '+', '+/event', 'event/+', '#', 'event/#', 'system/event/+',
  326. 'system/+/event', 'system/registry/event/#', 'system/+/event/#',
  327. 'system/registry/event/new_device', 'system/+/+/new_device'
  328. ],
  329. function (err) {
  330. client.end(function () {
  331. if (err) {
  332. return done(new Error(err))
  333. }
  334. done()
  335. })
  336. }
  337. )
  338. })
  339. it('should return an error (via callbacks) for topic #/event', function (done) {
  340. var client = connect()
  341. client.subscribe(['#/event', 'event#', 'event+'], function (err) {
  342. client.end(false, function () {
  343. if (err) {
  344. return done()
  345. }
  346. done(new Error('Validations do NOT work'))
  347. })
  348. })
  349. })
  350. it('should return an empty array for duplicate subs', function (done) {
  351. var client = connect()
  352. client.subscribe('event', function (err, granted1) {
  353. if (err) {
  354. return done(err)
  355. }
  356. client.subscribe('event', function (err, granted2) {
  357. if (err) {
  358. return done(err)
  359. }
  360. granted2.should.Array()
  361. granted2.should.be.empty()
  362. done()
  363. })
  364. })
  365. })
  366. it('should return an error (via callbacks) for topic #/event', function (done) {
  367. var client = connect()
  368. client.subscribe('#/event', function (err) {
  369. client.end(function () {
  370. if (err) {
  371. return done()
  372. }
  373. done(new Error('Validations do NOT work'))
  374. })
  375. })
  376. })
  377. it('should return an error (via callbacks) for topic event#', function (done) {
  378. var client = connect()
  379. client.subscribe('event#', function (err) {
  380. client.end(function () {
  381. if (err) {
  382. return done()
  383. }
  384. done(new Error('Validations do NOT work'))
  385. })
  386. })
  387. })
  388. it('should return an error (via callbacks) for topic system/#/event', function (done) {
  389. var client = connect()
  390. client.subscribe('system/#/event', function (err) {
  391. client.end(function () {
  392. if (err) {
  393. return done()
  394. }
  395. done(new Error('Validations do NOT work'))
  396. })
  397. })
  398. })
  399. it('should return an error (via callbacks) for empty topic list', function (done) {
  400. var client = connect()
  401. client.subscribe([], function (err) {
  402. client.end()
  403. if (err) {
  404. return done()
  405. }
  406. done(new Error('Validations do NOT work'))
  407. })
  408. })
  409. it('should return an error (via callbacks) for topic system/+/#/event', function (done) {
  410. var client = connect()
  411. client.subscribe('system/+/#/event', function (err) {
  412. client.end(true, function () {
  413. if (err) {
  414. return done()
  415. }
  416. done(new Error('Validations do NOT work'))
  417. })
  418. })
  419. })
  420. })
  421. describe('offline messages', function () {
  422. it('should queue message until connected', function (done) {
  423. var client = connect()
  424. client.publish('test', 'test')
  425. client.subscribe('test')
  426. client.unsubscribe('test')
  427. client.queue.length.should.equal(3)
  428. client.once('connect', function () {
  429. client.queue.length.should.equal(0)
  430. setTimeout(function () {
  431. client.end(true, done)
  432. }, 10)
  433. })
  434. })
  435. it('should not queue qos 0 messages if queueQoSZero is false', function (done) {
  436. var client = connect({queueQoSZero: false})
  437. client.publish('test', 'test', {qos: 0})
  438. client.queue.length.should.equal(0)
  439. client.on('connect', function () {
  440. setTimeout(function () {
  441. client.end(true, done)
  442. }, 10)
  443. })
  444. })
  445. it('should queue qos != 0 messages', function (done) {
  446. var client = connect({queueQoSZero: false})
  447. client.publish('test', 'test', {qos: 1})
  448. client.publish('test', 'test', {qos: 2})
  449. client.subscribe('test')
  450. client.unsubscribe('test')
  451. client.queue.length.should.equal(2)
  452. client.on('connect', function () {
  453. setTimeout(function () {
  454. client.end(true, done)
  455. }, 10)
  456. })
  457. })
  458. it('should call cb if an outgoing QoS 0 message is not sent', function (done) {
  459. var client = connect({queueQoSZero: false})
  460. var called = false
  461. client.publish('test', 'test', {qos: 0}, function () {
  462. called = true
  463. })
  464. client.on('connect', function () {
  465. called.should.equal(true)
  466. setTimeout(function () {
  467. client.end(true, done)
  468. }, 10)
  469. })
  470. })
  471. it('should delay ending up until all inflight messages are delivered', function (done) {
  472. var client = connect()
  473. var subscribeCalled = false
  474. client.on('connect', function () {
  475. client.subscribe('test', function () {
  476. subscribeCalled = true
  477. })
  478. client.publish('test', 'test', function () {
  479. client.end(false, function () {
  480. subscribeCalled.should.be.equal(true)
  481. done()
  482. })
  483. })
  484. })
  485. })
  486. it('wait QoS 1 publish messages', function (done) {
  487. var client = connect()
  488. var messageReceived = false
  489. client.on('connect', function () {
  490. client.subscribe('test')
  491. client.publish('test', 'test', { qos: 1 }, function () {
  492. client.end(false, function () {
  493. messageReceived.should.equal(true)
  494. done()
  495. })
  496. })
  497. client.on('message', function () {
  498. messageReceived = true
  499. })
  500. })
  501. server.once('client', function (serverClient) {
  502. serverClient.on('subscribe', function () {
  503. serverClient.on('publish', function (packet) {
  504. serverClient.publish(packet)
  505. })
  506. })
  507. })
  508. })
  509. it('does not wait acks when force-closing', function (done) {
  510. // non-running broker
  511. var client = connect('mqtt://localhost:8993')
  512. client.publish('test', 'test', { qos: 1 })
  513. client.end(true, done)
  514. })
  515. it('should call cb if store.put fails', function (done) {
  516. const store = new Store()
  517. store.put = function (packet, cb) {
  518. process.nextTick(cb, new Error('oops there is an error'))
  519. }
  520. var client = connect({ incomingStore: store, outgoingStore: store })
  521. client.publish('test', 'test', { qos: 2 }, function (err) {
  522. if (err) {
  523. client.end(true, done)
  524. }
  525. })
  526. })
  527. })
  528. describe('publishing', function () {
  529. it('should publish a message (offline)', function (done) {
  530. var client = connect()
  531. var payload = 'test'
  532. var topic = 'test'
  533. client.publish(topic, payload)
  534. server.on('client', onClient)
  535. function onClient (serverClient) {
  536. serverClient.once('connect', function () {
  537. server.removeListener('client', onClient)
  538. })
  539. serverClient.once('publish', function (packet) {
  540. packet.topic.should.equal(topic)
  541. packet.payload.toString().should.equal(payload)
  542. packet.qos.should.equal(0)
  543. packet.retain.should.equal(false)
  544. client.end(true, done)
  545. })
  546. }
  547. })
  548. it('should publish a message (online)', function (done) {
  549. var client = connect()
  550. var payload = 'test'
  551. var topic = 'test'
  552. client.on('connect', function () {
  553. client.publish(topic, payload)
  554. })
  555. server.once('client', function (serverClient) {
  556. serverClient.once('publish', function (packet) {
  557. packet.topic.should.equal(topic)
  558. packet.payload.toString().should.equal(payload)
  559. packet.qos.should.equal(0)
  560. packet.retain.should.equal(false)
  561. client.end()
  562. done()
  563. })
  564. })
  565. })
  566. it('should publish a message (retain, offline)', function (done) {
  567. var client = connect({ queueQoSZero: true })
  568. var payload = 'test'
  569. var topic = 'test'
  570. var called = false
  571. client.publish(topic, payload, { retain: true }, function () {
  572. called = true
  573. })
  574. server.once('client', function (serverClient) {
  575. serverClient.once('publish', function (packet) {
  576. packet.topic.should.equal(topic)
  577. packet.payload.toString().should.equal(payload)
  578. packet.qos.should.equal(0)
  579. packet.retain.should.equal(true)
  580. called.should.equal(true)
  581. client.end()
  582. done()
  583. })
  584. })
  585. })
  586. it('should emit a packetsend event', function (done) {
  587. var client = connect()
  588. var payload = 'test_payload'
  589. var testTopic = 'testTopic'
  590. client.on('packetsend', function (packet) {
  591. if (packet.cmd === 'publish') {
  592. packet.qos.should.equal(0)
  593. packet.topic.should.equal(testTopic)
  594. packet.payload.should.equal(payload)
  595. packet.retain.should.equal(false)
  596. client.end()
  597. done()
  598. }
  599. })
  600. client.publish(testTopic, payload)
  601. })
  602. it('should accept options', function (done) {
  603. var client = connect()
  604. var payload = 'test'
  605. var topic = 'test'
  606. var opts = {
  607. retain: true,
  608. qos: 1
  609. }
  610. client.once('connect', function () {
  611. client.publish(topic, payload, opts)
  612. })
  613. server.once('client', function (serverClient) {
  614. serverClient.once('publish', function (packet) {
  615. packet.topic.should.equal(topic)
  616. packet.payload.toString().should.equal(payload)
  617. packet.qos.should.equal(opts.qos, 'incorrect qos')
  618. packet.retain.should.equal(opts.retain, 'incorrect ret')
  619. packet.dup.should.equal(false, 'incorrect dup')
  620. client.end()
  621. done()
  622. })
  623. })
  624. })
  625. it('should publish with the default options for an empty parameter', function (done) {
  626. var client = connect()
  627. var payload = 'test'
  628. var topic = 'test'
  629. var defaultOpts = {qos: 0, retain: false, dup: false}
  630. client.once('connect', function () {
  631. client.publish(topic, payload, {})
  632. })
  633. server.once('client', function (serverClient) {
  634. serverClient.once('publish', function (packet) {
  635. packet.topic.should.equal(topic)
  636. packet.payload.toString().should.equal(payload)
  637. packet.qos.should.equal(defaultOpts.qos, 'incorrect qos')
  638. packet.retain.should.equal(defaultOpts.retain, 'incorrect ret')
  639. packet.dup.should.equal(defaultOpts.dup, 'incorrect dup')
  640. client.end()
  641. done()
  642. })
  643. })
  644. })
  645. it('should mark a message as duplicate when "dup" option is set', function (done) {
  646. var client = connect()
  647. var payload = 'duplicated-test'
  648. var topic = 'test'
  649. var opts = {
  650. retain: true,
  651. qos: 1,
  652. dup: true
  653. }
  654. client.once('connect', function () {
  655. client.publish(topic, payload, opts)
  656. })
  657. server.once('client', function (serverClient) {
  658. serverClient.once('publish', function (packet) {
  659. packet.topic.should.equal(topic)
  660. packet.payload.toString().should.equal(payload)
  661. packet.dup.should.equal(opts.dup, 'incorrect dup')
  662. client.end()
  663. done()
  664. })
  665. })
  666. })
  667. it('should fire a callback (qos 0)', function (done) {
  668. var client = connect()
  669. client.once('connect', function () {
  670. client.publish('a', 'b', function () {
  671. client.end()
  672. done()
  673. })
  674. })
  675. })
  676. it('should fire a callback (qos 1)', function (done) {
  677. var client = connect()
  678. var opts = { qos: 1 }
  679. client.once('connect', function () {
  680. client.publish('a', 'b', opts, function () {
  681. client.end()
  682. done()
  683. })
  684. })
  685. })
  686. it('should fire a callback (qos 2)', function (done) {
  687. var client = connect()
  688. var opts = { qos: 2 }
  689. client.once('connect', function () {
  690. client.publish('a', 'b', opts, function () {
  691. client.end()
  692. done()
  693. })
  694. })
  695. })
  696. it('should support UTF-8 characters in topic', function (done) {
  697. var client = connect()
  698. client.once('connect', function () {
  699. client.publish('中国', 'hello', function () {
  700. client.end()
  701. done()
  702. })
  703. })
  704. })
  705. it('should support UTF-8 characters in payload', function (done) {
  706. var client = connect()
  707. client.once('connect', function () {
  708. client.publish('hello', '中国', function () {
  709. client.end()
  710. done()
  711. })
  712. })
  713. })
  714. it('should publish 10 QoS 2 and receive them', function (done) {
  715. var client = connect()
  716. var count = 0
  717. client.on('connect', function () {
  718. client.subscribe('test')
  719. client.publish('test', 'test', { qos: 2 })
  720. })
  721. client.on('message', function () {
  722. if (count >= 10) {
  723. client.end()
  724. done()
  725. } else {
  726. client.publish('test', 'test', { qos: 2 })
  727. }
  728. })
  729. server.once('client', function (serverClient) {
  730. serverClient.on('offline', function () {
  731. client.end()
  732. done('error went offline... didnt see this happen')
  733. })
  734. serverClient.on('subscribe', function () {
  735. serverClient.on('publish', function (packet) {
  736. serverClient.publish(packet)
  737. })
  738. })
  739. serverClient.on('pubrel', function () {
  740. count++
  741. })
  742. })
  743. })
  744. function testQosHandleMessage (qos, done) {
  745. var client = connect()
  746. var messageEventCount = 0
  747. var handleMessageCount = 0
  748. client.handleMessage = function (packet, callback) {
  749. setTimeout(function () {
  750. handleMessageCount++
  751. // next message event should not emit until handleMessage completes
  752. handleMessageCount.should.equal(messageEventCount)
  753. if (handleMessageCount === 10) {
  754. setTimeout(function () {
  755. client.end()
  756. done()
  757. })
  758. }
  759. callback()
  760. }, 100)
  761. }
  762. client.on('message', function (topic, message, packet) {
  763. messageEventCount++
  764. })
  765. client.on('connect', function () {
  766. client.subscribe('test')
  767. })
  768. server.once('client', function (serverClient) {
  769. serverClient.on('offline', function () {
  770. client.end()
  771. done('error went offline... didnt see this happen')
  772. })
  773. serverClient.on('subscribe', function () {
  774. for (var i = 0; i < 10; i++) {
  775. serverClient.publish({
  776. messageId: i,
  777. topic: 'test',
  778. payload: 'test' + i,
  779. qos: qos
  780. })
  781. }
  782. })
  783. })
  784. }
  785. it('should publish 10 QoS 0 and receive them only when `handleMessage` finishes', function (done) {
  786. testQosHandleMessage(0, done)
  787. })
  788. it('should publish 10 QoS 1 and receive them only when `handleMessage` finishes', function (done) {
  789. testQosHandleMessage(1, done)
  790. })
  791. it('should publish 10 QoS 2 and receive them only when `handleMessage` finishes', function (done) {
  792. testQosHandleMessage(2, done)
  793. })
  794. it('should not send a `puback` if the execution of `handleMessage` fails for messages with QoS `1`', function (done) {
  795. var client = connect()
  796. client.handleMessage = function (packet, callback) {
  797. callback(new Error('Error thrown by the application'))
  798. }
  799. client._sendPacket = sinon.spy()
  800. client._handlePublish({
  801. messageId: Math.floor(65535 * Math.random()),
  802. topic: 'test',
  803. payload: 'test',
  804. qos: 1
  805. }, function (err) {
  806. should.exist(err)
  807. })
  808. client._sendPacket.callCount.should.equal(0)
  809. client.end()
  810. client.on('connect', function () { done() })
  811. })
  812. it('should silently ignore errors thrown by `handleMessage` and return when no callback is passed ' +
  813. 'into `handlePublish` method', function (done) {
  814. var client = connect()
  815. client.handleMessage = function (packet, callback) {
  816. callback(new Error('Error thrown by the application'))
  817. }
  818. try {
  819. client._handlePublish({
  820. messageId: Math.floor(65535 * Math.random()),
  821. topic: 'test',
  822. payload: 'test',
  823. qos: 1
  824. })
  825. done()
  826. } catch (err) {
  827. done(err)
  828. } finally {
  829. client.end()
  830. }
  831. })
  832. it('should handle error with async incoming store in QoS 2 `handlePublish` method', function (done) {
  833. function AsyncStore () {
  834. if (!(this instanceof AsyncStore)) {
  835. return new AsyncStore()
  836. }
  837. }
  838. AsyncStore.prototype.put = function (packet, cb) {
  839. process.nextTick(function () {
  840. cb(new Error('Error'))
  841. })
  842. }
  843. var store = new AsyncStore()
  844. var client = connect({incomingStore: store})
  845. client._handlePublish({
  846. messageId: 1,
  847. topic: 'test',
  848. payload: 'test',
  849. qos: 2
  850. }, function () {
  851. done()
  852. client.end()
  853. })
  854. })
  855. it('should handle error with async incoming store in QoS 2 `handlePubrel` method', function (done) {
  856. function AsyncStore () {
  857. if (!(this instanceof AsyncStore)) {
  858. return new AsyncStore()
  859. }
  860. }
  861. AsyncStore.prototype.put = function (packet, cb) {
  862. process.nextTick(function () {
  863. cb(new Error('Error'))
  864. })
  865. }
  866. AsyncStore.prototype.get = function (packet, cb) {
  867. process.nextTick(function () {
  868. cb(null, {cmd: 'publish'})
  869. })
  870. }
  871. var store = new AsyncStore()
  872. var client = connect({incomingStore: store})
  873. client._handlePubrel({
  874. messageId: 1,
  875. qos: 2
  876. }, function () {
  877. done()
  878. client.end()
  879. })
  880. })
  881. it('should handle success with async incoming store in QoS 2 `handlePubrel` method', function (done) {
  882. var putComplete = false
  883. function AsyncStore () {
  884. if (!(this instanceof AsyncStore)) {
  885. return new AsyncStore()
  886. }
  887. }
  888. AsyncStore.prototype.put = function (packet, cb) {
  889. process.nextTick(function () {
  890. putComplete = true
  891. cb(null)
  892. })
  893. }
  894. AsyncStore.prototype.get = function (packet, cb) {
  895. process.nextTick(function () {
  896. cb(null, {cmd: 'publish'})
  897. })
  898. }
  899. var store = new AsyncStore()
  900. var client = connect({incomingStore: store})
  901. client._handlePubrel({
  902. messageId: 1,
  903. qos: 2
  904. }, function () {
  905. putComplete.should.equal(true)
  906. done()
  907. client.end()
  908. })
  909. })
  910. it('should handle error with async incoming store in QoS 1 `handlePublish` method', function (done) {
  911. function AsyncStore () {
  912. if (!(this instanceof AsyncStore)) {
  913. return new AsyncStore()
  914. }
  915. }
  916. AsyncStore.prototype.put = function (packet, cb) {
  917. process.nextTick(function () {
  918. cb(null, 'Error')
  919. })
  920. }
  921. var store = new AsyncStore()
  922. var client = connect({incomingStore: store})
  923. client._handlePublish({
  924. messageId: 1,
  925. topic: 'test',
  926. payload: 'test',
  927. qos: 1
  928. }, function () {
  929. done()
  930. client.end()
  931. })
  932. })
  933. it('should not send a `pubcomp` if the execution of `handleMessage` fails for messages with QoS `2`', function (done) {
  934. var store = new Store()
  935. var client = connect({incomingStore: store})
  936. var messageId = Math.floor(65535 * Math.random())
  937. var topic = 'test'
  938. var payload = 'test'
  939. var qos = 2
  940. client.handleMessage = function (packet, callback) {
  941. callback(new Error('Error thrown by the application'))
  942. }
  943. client.once('connect', function () {
  944. client.subscribe(topic, {qos: 2})
  945. store.put({
  946. messageId: messageId,
  947. topic: topic,
  948. payload: payload,
  949. qos: qos,
  950. cmd: 'publish'
  951. }, function () {
  952. // cleans up the client
  953. client.end()
  954. client._sendPacket = sinon.spy()
  955. client._handlePubrel({cmd: 'pubrel', messageId: messageId}, function (err) {
  956. should.exist(err)
  957. })
  958. client._sendPacket.callCount.should.equal(0)
  959. done()
  960. })
  961. })
  962. })
  963. it('should silently ignore errors thrown by `handleMessage` and return when no callback is passed ' +
  964. 'into `handlePubrel` method', function (done) {
  965. var store = new Store()
  966. var client = connect({incomingStore: store})
  967. var messageId = Math.floor(65535 * Math.random())
  968. var topic = 'test'
  969. var payload = 'test'
  970. var qos = 2
  971. client.handleMessage = function (packet, callback) {
  972. callback(new Error('Error thrown by the application'))
  973. }
  974. client.once('connect', function () {
  975. client.subscribe(topic, {qos: 2})
  976. store.put({
  977. messageId: messageId,
  978. topic: topic,
  979. payload: payload,
  980. qos: qos,
  981. cmd: 'publish'
  982. }, function () {
  983. try {
  984. client._handlePubrel({cmd: 'pubrel', messageId: messageId})
  985. done()
  986. } catch (err) {
  987. done(err)
  988. } finally {
  989. client.end()
  990. }
  991. })
  992. })
  993. })
  994. })
  995. describe('unsubscribing', function () {
  996. it('should send an unsubscribe packet (offline)', function (done) {
  997. var client = connect()
  998. client.unsubscribe('test')
  999. server.once('client', function (serverClient) {
  1000. serverClient.once('unsubscribe', function (packet) {
  1001. packet.unsubscriptions.should.containEql('test')
  1002. client.end()
  1003. done()
  1004. })
  1005. })
  1006. })
  1007. it('should send an unsubscribe packet', function (done) {
  1008. var client = connect()
  1009. var topic = 'topic'
  1010. client.once('connect', function () {
  1011. client.unsubscribe(topic)
  1012. })
  1013. server.once('client', function (serverClient) {
  1014. serverClient.once('unsubscribe', function (packet) {
  1015. packet.unsubscriptions.should.containEql(topic)
  1016. client.end()
  1017. done()
  1018. })
  1019. })
  1020. })
  1021. it('should emit a packetsend event', function (done) {
  1022. var client = connect()
  1023. var testTopic = 'testTopic'
  1024. client.once('connect', function () {
  1025. client.subscribe(testTopic)
  1026. })
  1027. client.on('packetsend', function (packet) {
  1028. if (packet.cmd === 'subscribe') {
  1029. client.end()
  1030. done()
  1031. }
  1032. })
  1033. })
  1034. it('should emit a packetreceive event', function (done) {
  1035. var client = connect()
  1036. var testTopic = 'testTopic'
  1037. client.once('connect', function () {
  1038. client.subscribe(testTopic)
  1039. })
  1040. client.on('packetreceive', function (packet) {
  1041. if (packet.cmd === 'suback') {
  1042. client.end()
  1043. done()
  1044. }
  1045. })
  1046. })
  1047. it('should accept an array of unsubs', function (done) {
  1048. var client = connect()
  1049. var topics = ['topic1', 'topic2']
  1050. client.once('connect', function () {
  1051. client.unsubscribe(topics)
  1052. })
  1053. server.once('client', function (serverClient) {
  1054. serverClient.once('unsubscribe', function (packet) {
  1055. packet.unsubscriptions.should.eql(topics)
  1056. done()
  1057. })
  1058. })
  1059. })
  1060. it('should fire a callback on unsuback', function (done) {
  1061. var client = connect()
  1062. var topic = 'topic'
  1063. client.once('connect', function () {
  1064. client.unsubscribe(topic, done)
  1065. })
  1066. server.once('client', function (serverClient) {
  1067. serverClient.once('unsubscribe', function (packet) {
  1068. serverClient.unsuback(packet)
  1069. client.end()
  1070. })
  1071. })
  1072. })
  1073. it('should unsubscribe from a chinese topic', function (done) {
  1074. var client = connect()
  1075. var topic = '中国'
  1076. client.once('connect', function () {
  1077. client.unsubscribe(topic)
  1078. })
  1079. server.once('client', function (serverClient) {
  1080. serverClient.once('unsubscribe', function (packet) {
  1081. packet.unsubscriptions.should.containEql(topic)
  1082. client.end()
  1083. done()
  1084. })
  1085. })
  1086. })
  1087. })
  1088. describe('keepalive', function () {
  1089. var clock
  1090. beforeEach(function () {
  1091. clock = sinon.useFakeTimers()
  1092. })
  1093. afterEach(function () {
  1094. clock.restore()
  1095. })
  1096. it('should checkPing at keepalive interval', function (done) {
  1097. var interval = 3
  1098. var client = connect({ keepalive: interval })
  1099. client._checkPing = sinon.spy()
  1100. client.once('connect', function () {
  1101. clock.tick(interval * 1000)
  1102. client._checkPing.callCount.should.equal(1)
  1103. clock.tick(interval * 1000)
  1104. client._checkPing.callCount.should.equal(2)
  1105. clock.tick(interval * 1000)
  1106. client._checkPing.callCount.should.equal(3)
  1107. client.end()
  1108. done()
  1109. })
  1110. })
  1111. it('should not checkPing if publishing at a higher rate than keepalive', function (done) {
  1112. var intervalMs = 3000
  1113. var client = connect({keepalive: intervalMs / 1000})
  1114. client._checkPing = sinon.spy()
  1115. client.once('connect', function () {
  1116. client.publish('foo', 'bar')
  1117. clock.tick(intervalMs - 1)
  1118. client.publish('foo', 'bar')
  1119. clock.tick(2)
  1120. client._checkPing.callCount.should.equal(0)
  1121. client.end()
  1122. done()
  1123. })
  1124. })
  1125. it('should checkPing if publishing at a higher rate than keepalive and reschedulePings===false', function (done) {
  1126. var intervalMs = 3000
  1127. var client = connect({
  1128. keepalive: intervalMs / 1000,
  1129. reschedulePings: false
  1130. })
  1131. client._checkPing = sinon.spy()
  1132. client.once('connect', function () {
  1133. client.publish('foo', 'bar')
  1134. clock.tick(intervalMs - 1)
  1135. client.publish('foo', 'bar')
  1136. clock.tick(2)
  1137. client._checkPing.callCount.should.equal(1)
  1138. client.end()
  1139. done()
  1140. })
  1141. })
  1142. })
  1143. describe('pinging', function () {
  1144. it('should set a ping timer', function (done) {
  1145. var client = connect({keepalive: 3})
  1146. client.once('connect', function () {
  1147. should.exist(client.pingTimer)
  1148. client.end()
  1149. done()
  1150. })
  1151. })
  1152. it('should not set a ping timer keepalive=0', function (done) {
  1153. var client = connect({keepalive: 0})
  1154. client.on('connect', function () {
  1155. should.not.exist(client.pingTimer)
  1156. client.end()
  1157. done()
  1158. })
  1159. })
  1160. it('should reconnect if pingresp is not sent', function (done) {
  1161. var client = connect({keepalive: 1, reconnectPeriod: 100})
  1162. // Fake no pingresp being send by stubbing the _handlePingresp function
  1163. client._handlePingresp = function () {}
  1164. client.once('connect', function () {
  1165. client.once('connect', function () {
  1166. client.end()
  1167. done()
  1168. })
  1169. })
  1170. })
  1171. it('should not reconnect if pingresp is successful', function (done) {
  1172. var client = connect({keepalive: 100})
  1173. client.once('close', function () {
  1174. done(new Error('Client closed connection'))
  1175. })
  1176. setTimeout(done, 1000)
  1177. })
  1178. it('should defer the next ping when sending a control packet', function (done) {
  1179. var client = connect({keepalive: 1})
  1180. client.once('connect', function () {
  1181. client._checkPing = sinon.spy()
  1182. client.publish('foo', 'bar')
  1183. setTimeout(function () {
  1184. client._checkPing.callCount.should.equal(0)
  1185. client.publish('foo', 'bar')
  1186. setTimeout(function () {
  1187. client._checkPing.callCount.should.equal(0)
  1188. client.publish('foo', 'bar')
  1189. setTimeout(function () {
  1190. client._checkPing.callCount.should.equal(0)
  1191. done()
  1192. }, 75)
  1193. }, 75)
  1194. }, 75)
  1195. })
  1196. })
  1197. })
  1198. describe('subscribing', function () {
  1199. it('should send a subscribe message (offline)', function (done) {
  1200. var client = connect()
  1201. client.subscribe('test')
  1202. server.once('client', function (serverClient) {
  1203. serverClient.once('subscribe', function () {
  1204. done()
  1205. })
  1206. })
  1207. })
  1208. it('should send a subscribe message', function (done) {
  1209. var client = connect()
  1210. var topic = 'test'
  1211. client.once('connect', function () {
  1212. client.subscribe(topic)
  1213. })
  1214. server.once('client', function (serverClient) {
  1215. serverClient.once('subscribe', function (packet) {
  1216. packet.subscriptions.should.containEql({
  1217. topic: topic,
  1218. qos: 0
  1219. })
  1220. done()
  1221. })
  1222. })
  1223. })
  1224. it('should emit a packetsend event', function (done) {
  1225. var client = connect()
  1226. var testTopic = 'testTopic'
  1227. client.once('connect', function () {
  1228. client.subscribe(testTopic)
  1229. })
  1230. client.on('packetsend', function (packet) {
  1231. if (packet.cmd === 'subscribe') {
  1232. done()
  1233. }
  1234. })
  1235. })
  1236. it('should emit a packetreceive event', function (done) {
  1237. var client = connect()
  1238. var testTopic = 'testTopic'
  1239. client.once('connect', function () {
  1240. client.subscribe(testTopic)
  1241. })
  1242. client.on('packetreceive', function (packet) {
  1243. if (packet.cmd === 'suback') {
  1244. done()
  1245. }
  1246. })
  1247. })
  1248. it('should accept an array of subscriptions', function (done) {
  1249. var client = connect()
  1250. var subs = ['test1', 'test2']
  1251. client.once('connect', function () {
  1252. client.subscribe(subs)
  1253. })
  1254. server.once('client', function (serverClient) {
  1255. serverClient.once('subscribe', function (packet) {
  1256. // i.e. [{topic: 'a', qos: 0}, {topic: 'b', qos: 0}]
  1257. var expected = subs.map(function (i) {
  1258. return {topic: i, qos: 0}
  1259. })
  1260. packet.subscriptions.should.eql(expected)
  1261. done()
  1262. })
  1263. })
  1264. })
  1265. it('should accept an hash of subscriptions', function (done) {
  1266. var client = connect()
  1267. var topics = {
  1268. test1: 0,
  1269. test2: 1
  1270. }
  1271. client.once('connect', function () {
  1272. client.subscribe(topics)
  1273. })
  1274. server.once('client', function (serverClient) {
  1275. serverClient.once('subscribe', function (packet) {
  1276. var k
  1277. var expected = []
  1278. for (k in topics) {
  1279. if (topics.hasOwnProperty(k)) {
  1280. expected.push({
  1281. topic: k,
  1282. qos: topics[k]
  1283. })
  1284. }
  1285. }
  1286. packet.subscriptions.should.eql(expected)
  1287. done()
  1288. })
  1289. })
  1290. })
  1291. it('should accept an options parameter', function (done) {
  1292. var client = connect()
  1293. var topic = 'test'
  1294. var opts = {qos: 1}
  1295. client.once('connect', function () {
  1296. client.subscribe(topic, opts)
  1297. })
  1298. server.once('client', function (serverClient) {
  1299. serverClient.once('subscribe', function (packet) {
  1300. var expected = [{
  1301. topic: topic,
  1302. qos: 1
  1303. }]
  1304. packet.subscriptions.should.eql(expected)
  1305. done()
  1306. })
  1307. })
  1308. })
  1309. it('should subscribe with the default options for an empty options parameter', function (done) {
  1310. var client = connect()
  1311. var topic = 'test'
  1312. var defaultOpts = {qos: 0}
  1313. client.once('connect', function () {
  1314. client.subscribe(topic, {})
  1315. })
  1316. server.once('client', function (serverClient) {
  1317. serverClient.once('subscribe', function (packet) {
  1318. packet.subscriptions.should.containEql({
  1319. topic: topic,
  1320. qos: defaultOpts.qos
  1321. })
  1322. done()
  1323. })
  1324. })
  1325. })
  1326. it('should fire a callback on suback', function (done) {
  1327. var client = connect()
  1328. var topic = 'test'
  1329. client.once('connect', function () {
  1330. client.subscribe(topic, { qos: 2 }, function (err, granted) {
  1331. if (err) {
  1332. done(err)
  1333. } else {
  1334. should.exist(granted, 'granted not given')
  1335. granted.should.containEql({topic: 'test', qos: 2})
  1336. done()
  1337. }
  1338. })
  1339. })
  1340. })
  1341. it('should fire a callback with error if disconnected (options provided)', function (done) {
  1342. var client = connect()
  1343. var topic = 'test'
  1344. client.once('connect', function () {
  1345. client.end(true, function () {
  1346. client.subscribe(topic, {qos: 2}, function (err, granted) {
  1347. should.not.exist(granted, 'granted given')
  1348. should.exist(err, 'no error given')
  1349. done()
  1350. })
  1351. })
  1352. })
  1353. })
  1354. it('should fire a callback with error if disconnected (options not provided)', function (done) {
  1355. var client = connect()
  1356. var topic = 'test'
  1357. client.once('connect', function () {
  1358. client.end(true, function () {
  1359. client.subscribe(topic, function (err, granted) {
  1360. should.not.exist(granted, 'granted given')
  1361. should.exist(err, 'no error given')
  1362. done()
  1363. })
  1364. })
  1365. })
  1366. })
  1367. it('should subscribe with a chinese topic', function (done) {
  1368. var client = connect()
  1369. var topic = '中国'
  1370. client.once('connect', function () {
  1371. client.subscribe(topic)
  1372. })
  1373. server.once('client', function (serverClient) {
  1374. serverClient.once('subscribe', function (packet) {
  1375. packet.subscriptions.should.containEql({
  1376. topic: topic,
  1377. qos: 0
  1378. })
  1379. done()
  1380. })
  1381. })
  1382. })
  1383. })
  1384. describe('receiving messages', function () {
  1385. it('should fire the message event', function (done) {
  1386. var client = connect()
  1387. var testPacket = {
  1388. topic: 'test',
  1389. payload: 'message',
  1390. retain: true,
  1391. qos: 1,
  1392. messageId: 5
  1393. }
  1394. client.subscribe(testPacket.topic)
  1395. client.once('message', function (topic, message, packet) {
  1396. topic.should.equal(testPacket.topic)
  1397. message.toString().should.equal(testPacket.payload)
  1398. packet.should.equal(packet)
  1399. client.end()
  1400. done()
  1401. })
  1402. server.once('client', function (serverClient) {
  1403. serverClient.on('subscribe', function () {
  1404. serverClient.publish(testPacket)
  1405. })
  1406. })
  1407. })
  1408. it('should emit a packetreceive event', function (done) {
  1409. var client = connect()
  1410. var testPacket = {
  1411. topic: 'test',
  1412. payload: 'message',
  1413. retain: true,
  1414. qos: 1,
  1415. messageId: 5
  1416. }
  1417. client.subscribe(testPacket.topic)
  1418. client.on('packetreceive', function (packet) {
  1419. if (packet.cmd === 'publish') {
  1420. packet.qos.should.equal(1)
  1421. packet.topic.should.equal(testPacket.topic)
  1422. packet.payload.toString().should.equal(testPacket.payload)
  1423. packet.retain.should.equal(true)
  1424. client.end()
  1425. done()
  1426. }
  1427. })
  1428. server.once('client', function (serverClient) {
  1429. serverClient.on('subscribe', function () {
  1430. serverClient.publish(testPacket)
  1431. })
  1432. })
  1433. })
  1434. it('should support binary data', function (done) {
  1435. var client = connect({ encoding: 'binary' })
  1436. var testPacket = {
  1437. topic: 'test',
  1438. payload: 'message',
  1439. retain: true,
  1440. qos: 1,
  1441. messageId: 5
  1442. }
  1443. client.subscribe(testPacket.topic)
  1444. client.once('message', function (topic, message, packet) {
  1445. topic.should.equal(testPacket.topic)
  1446. message.should.be.an.instanceOf(Buffer)
  1447. message.toString().should.equal(testPacket.payload)
  1448. packet.should.equal(packet)
  1449. done()
  1450. })
  1451. server.once('client', function (serverClient) {
  1452. serverClient.on('subscribe', function () {
  1453. serverClient.publish(testPacket)
  1454. })
  1455. })
  1456. })
  1457. it('should emit a message event (qos=2)', function (done) {
  1458. var client = connect()
  1459. var testPacket = {
  1460. topic: 'test',
  1461. payload: 'message',
  1462. retain: true,
  1463. qos: 2,
  1464. messageId: 5
  1465. }
  1466. server.testPublish = testPacket
  1467. client.subscribe(testPacket.topic)
  1468. client.once('message', function (topic, message, packet) {
  1469. topic.should.equal(testPacket.topic)
  1470. message.toString().should.equal(testPacket.payload)
  1471. packet.should.equal(packet)
  1472. done()
  1473. })
  1474. server.once('client', function (serverClient) {
  1475. serverClient.on('subscribe', function () {
  1476. serverClient.publish(testPacket)
  1477. })
  1478. })
  1479. })
  1480. it('should emit a message event (qos=2) - repeated publish', function (done) {
  1481. var client = connect()
  1482. var testPacket = {
  1483. topic: 'test',
  1484. payload: 'message',
  1485. retain: true,
  1486. qos: 2,
  1487. messageId: 5
  1488. }
  1489. server.testPublish = testPacket
  1490. client.subscribe(testPacket.topic)
  1491. client.on('message', function (topic, message, packet) {
  1492. topic.should.equal(testPacket.topic)
  1493. message.toString().should.equal(testPacket.payload)
  1494. packet.should.equal(packet)
  1495. done()
  1496. })
  1497. server.once('client', function (serverClient) {
  1498. serverClient.on('subscribe', function () {
  1499. serverClient.publish(testPacket)
  1500. // twice, should be ignored
  1501. serverClient.publish(testPacket)
  1502. })
  1503. })
  1504. })
  1505. it('should support chinese topic', function (done) {
  1506. var client = connect({ encoding: 'binary' })
  1507. var testPacket = {
  1508. topic: '国',
  1509. payload: 'message',
  1510. retain: true,
  1511. qos: 1,
  1512. messageId: 5
  1513. }
  1514. client.subscribe(testPacket.topic)
  1515. client.once('message', function (topic, message, packet) {
  1516. topic.should.equal(testPacket.topic)
  1517. message.should.be.an.instanceOf(Buffer)
  1518. message.toString().should.equal(testPacket.payload)
  1519. packet.should.equal(packet)
  1520. done()
  1521. })
  1522. server.once('client', function (serverClient) {
  1523. serverClient.on('subscribe', function () {
  1524. serverClient.publish(testPacket)
  1525. })
  1526. })
  1527. })
  1528. })
  1529. describe('qos handling', function () {
  1530. it('should follow qos 0 semantics (trivial)', function (done) {
  1531. var client = connect()
  1532. var testTopic = 'test'
  1533. var testMessage = 'message'
  1534. client.once('connect', function () {
  1535. client.subscribe(testTopic, {qos: 0})
  1536. })
  1537. server.once('client', function (serverClient) {
  1538. serverClient.once('subscribe', function () {
  1539. serverClient.publish({
  1540. topic: testTopic,
  1541. payload: testMessage,
  1542. qos: 0,
  1543. retain: false
  1544. })
  1545. done()
  1546. })
  1547. })
  1548. })
  1549. it('should follow qos 1 semantics', function (done) {
  1550. var client = connect()
  1551. var testTopic = 'test'
  1552. var testMessage = 'message'
  1553. var mid = 50
  1554. client.once('connect', function () {
  1555. client.subscribe(testTopic, {qos: 1})
  1556. })
  1557. server.once('client', function (serverClient) {
  1558. serverClient.once('subscribe', function () {
  1559. serverClient.publish({
  1560. topic: testTopic,
  1561. payload: testMessage,
  1562. messageId: mid,
  1563. qos: 1
  1564. })
  1565. })
  1566. serverClient.once('puback', function (packet) {
  1567. packet.messageId.should.equal(mid)
  1568. done()
  1569. })
  1570. })
  1571. })
  1572. it('should follow qos 2 semantics', function (done) {
  1573. var client = connect()
  1574. var testTopic = 'test'
  1575. var testMessage = 'message'
  1576. var mid = 253
  1577. client.once('connect', function () {
  1578. client.subscribe(testTopic, {qos: 2})
  1579. })
  1580. server.once('client', function (serverClient) {
  1581. serverClient.once('subscribe', function () {
  1582. serverClient.publish({
  1583. topic: testTopic,
  1584. payload: testMessage,
  1585. qos: 2,
  1586. messageId: mid
  1587. })
  1588. })
  1589. serverClient.once('pubcomp', function () {
  1590. done()
  1591. })
  1592. })
  1593. })
  1594. })
  1595. describe('auto reconnect', function () {
  1596. it('should mark the client disconnecting if #end called', function () {
  1597. var client = connect()
  1598. client.end()
  1599. client.disconnecting.should.eql(true)
  1600. })
  1601. it('should reconnect after stream disconnect', function (done) {
  1602. var client = connect()
  1603. var tryReconnect = true
  1604. client.on('connect', function () {
  1605. if (tryReconnect) {
  1606. client.stream.end()
  1607. tryReconnect = false
  1608. } else {
  1609. client.end()
  1610. done()
  1611. }
  1612. })
  1613. })
  1614. it('should emit \'reconnect\' when reconnecting', function (done) {
  1615. var client = connect()
  1616. var tryReconnect = true
  1617. var reconnectEvent = false
  1618. client.on('reconnect', function () {
  1619. reconnectEvent = true
  1620. })
  1621. client.on('connect', function () {
  1622. if (tryReconnect) {
  1623. client.stream.end()
  1624. tryReconnect = false
  1625. } else {
  1626. reconnectEvent.should.equal(true)
  1627. client.end()
  1628. done()
  1629. }
  1630. })
  1631. })
  1632. it('should emit \'offline\' after going offline', function (done) {
  1633. var client = connect()
  1634. var tryReconnect = true
  1635. var offlineEvent = false
  1636. client.on('offline', function () {
  1637. offlineEvent = true
  1638. })
  1639. client.on('connect', function () {
  1640. if (tryReconnect) {
  1641. client.stream.end()
  1642. tryReconnect = false
  1643. } else {
  1644. offlineEvent.should.equal(true)
  1645. client.end()
  1646. done()
  1647. }
  1648. })
  1649. })
  1650. it('should not reconnect if it was ended by the user', function (done) {
  1651. var client = connect()
  1652. client.on('connect', function () {
  1653. client.end()
  1654. done() // it will raise an exception if called two times
  1655. })
  1656. })
  1657. it('should setup a reconnect timer on disconnect', function (done) {
  1658. var client = connect()
  1659. client.once('connect', function () {
  1660. should.not.exist(client.reconnectTimer)
  1661. client.stream.end()
  1662. })
  1663. client.once('close', function () {
  1664. should.exist(client.reconnectTimer)
  1665. client.end()
  1666. done()
  1667. })
  1668. })
  1669. it('should allow specification of a reconnect period', function (done) {
  1670. var end
  1671. var period = 200
  1672. var client = connect({reconnectPeriod: period})
  1673. var reconnect = false
  1674. var start = Date.now()
  1675. client.on('connect', function () {
  1676. if (!reconnect) {
  1677. client.stream.end()
  1678. reconnect = true
  1679. } else {
  1680. client.end()
  1681. end = Date.now()
  1682. if (end - start >= period) {
  1683. // Connected in about 2 seconds, that's good enough
  1684. done()
  1685. } else {
  1686. done(new Error('Strange reconnect period'))
  1687. }
  1688. }
  1689. })
  1690. })
  1691. it('should always cleanup successfully on reconnection', function (done) {
  1692. var client = connect({host: 'this_hostname_should_not_exist', connectTimeout: 0, reconnectPeriod: 1})
  1693. setTimeout(client.end.bind(client, done), 50)
  1694. })
  1695. it('should resend in-flight QoS 1 publish messages from the client', function (done) {
  1696. var client = connect({reconnectPeriod: 200})
  1697. var serverPublished = false
  1698. var clientCalledBack = false
  1699. server.once('client', function (serverClient) {
  1700. serverClient.on('connect', function () {
  1701. setImmediate(function () {
  1702. serverClient.stream.destroy()
  1703. })
  1704. })
  1705. server.once('client', function (serverClientNew) {
  1706. serverClientNew.on('publish', function () {
  1707. serverPublished = true
  1708. check()
  1709. })
  1710. })
  1711. })
  1712. client.publish('hello', 'world', { qos: 1 }, function () {
  1713. clientCalledBack = true
  1714. check()
  1715. })
  1716. function check () {
  1717. if (serverPublished && clientCalledBack) {
  1718. client.end()
  1719. done()
  1720. }
  1721. }
  1722. })
  1723. it('should not resend in-flight publish messages if disconnecting', function (done) {
  1724. var client = connect({reconnectPeriod: 200})
  1725. var serverPublished = false
  1726. var clientCalledBack = false
  1727. server.once('client', function (serverClient) {
  1728. serverClient.on('connect', function () {
  1729. setImmediate(function () {
  1730. serverClient.stream.destroy()
  1731. client.end()
  1732. serverPublished.should.be.false()
  1733. clientCalledBack.should.be.false()
  1734. done()
  1735. })
  1736. })
  1737. server.once('client', function (serverClientNew) {
  1738. serverClientNew.on('publish', function () {
  1739. serverPublished = true
  1740. })
  1741. })
  1742. })
  1743. client.publish('hello', 'world', { qos: 1 }, function () {
  1744. clientCalledBack = true
  1745. })
  1746. })
  1747. it('should resend in-flight QoS 2 publish messages from the client', function (done) {
  1748. var client = connect({reconnectPeriod: 200})
  1749. var serverPublished = false
  1750. var clientCalledBack = false
  1751. server.once('client', function (serverClient) {
  1752. // ignore errors
  1753. serverClient.on('error', function () {})
  1754. serverClient.on('publish', function () {
  1755. setImmediate(function () {
  1756. serverClient.stream.destroy()
  1757. })
  1758. })
  1759. server.once('client', function (serverClientNew) {
  1760. serverClientNew.on('pubrel', function () {
  1761. serverPublished = true
  1762. check()
  1763. })
  1764. })
  1765. })
  1766. client.publish('hello', 'world', { qos: 2 }, function () {
  1767. clientCalledBack = true
  1768. check()
  1769. })
  1770. function check () {
  1771. if (serverPublished && clientCalledBack) {
  1772. client.end()
  1773. done()
  1774. }
  1775. }
  1776. })
  1777. it('should not resend in-flight QoS 1 removed publish messages from the client', function (done) {
  1778. var client = connect({reconnectPeriod: 200})
  1779. var clientCalledBack = false
  1780. server.once('client', function (serverClient) {
  1781. serverClient.on('connect', function () {
  1782. setImmediate(function () {
  1783. serverClient.stream.destroy()
  1784. })
  1785. })
  1786. server.once('client', function (serverClientNew) {
  1787. serverClientNew.on('publish', function () {
  1788. should.fail()
  1789. done()
  1790. })
  1791. })
  1792. })
  1793. client.publish('hello', 'world', { qos: 1 }, function (err) {
  1794. clientCalledBack = true
  1795. should(err.message).be.equal('Message removed')
  1796. })
  1797. should(Object.keys(client.outgoing).length).be.equal(1)
  1798. should(client.outgoingStore._inflights.size).be.equal(1)
  1799. client.removeOutgoingMessage(client.getLastMessageId())
  1800. should(Object.keys(client.outgoing).length).be.equal(0)
  1801. should(client.outgoingStore._inflights.size).be.equal(0)
  1802. clientCalledBack.should.be.true()
  1803. client.end()
  1804. done()
  1805. })
  1806. it('should not resend in-flight QoS 2 removed publish messages from the client', function (done) {
  1807. var client = connect({reconnectPeriod: 200})
  1808. var clientCalledBack = false
  1809. server.once('client', function (serverClient) {
  1810. serverClient.on('connect', function () {
  1811. setImmediate(function () {
  1812. serverClient.stream.destroy()
  1813. })
  1814. })
  1815. server.once('client', function (serverClientNew) {
  1816. serverClientNew.on('publish', function () {
  1817. should.fail()
  1818. done()
  1819. })
  1820. })
  1821. })
  1822. client.publish('hello', 'world', { qos: 2 }, function (err) {
  1823. clientCalledBack = true
  1824. should(err.message).be.equal('Message removed')
  1825. })
  1826. should(Object.keys(client.outgoing).length).be.equal(1)
  1827. should(client.outgoingStore._inflights.size).be.equal(1)
  1828. client.removeOutgoingMessage(client.getLastMessageId())
  1829. should(Object.keys(client.outgoing).length).be.equal(0)
  1830. should(client.outgoingStore._inflights.size).be.equal(0)
  1831. clientCalledBack.should.be.true()
  1832. client.end()
  1833. done()
  1834. })
  1835. it('should resubscribe when reconnecting', function (done) {
  1836. var client = connect({ reconnectPeriod: 100 })
  1837. var tryReconnect = true
  1838. var reconnectEvent = false
  1839. client.on('reconnect', function () {
  1840. reconnectEvent = true
  1841. })
  1842. client.on('connect', function () {
  1843. if (tryReconnect) {
  1844. client.subscribe('hello', function () {
  1845. client.stream.end()
  1846. server.once('client', function (serverClient) {
  1847. serverClient.on('subscribe', function () {
  1848. client.end()
  1849. done()
  1850. })
  1851. })
  1852. })
  1853. tryReconnect = false
  1854. } else {
  1855. reconnectEvent.should.equal(true)
  1856. }
  1857. })
  1858. })
  1859. it('should not resubscribe when reconnecting if resubscribe is disabled', function (done) {
  1860. var client = connect({ reconnectPeriod: 100, resubscribe: false })
  1861. var tryReconnect = true
  1862. var reconnectEvent = false
  1863. client.on('reconnect', function () {
  1864. reconnectEvent = true
  1865. })
  1866. client.on('connect', function () {
  1867. if (tryReconnect) {
  1868. client.subscribe('hello', function () {
  1869. client.stream.end()
  1870. server.once('client', function (serverClient) {
  1871. serverClient.on('subscribe', function () {
  1872. should.fail()
  1873. })
  1874. })
  1875. })
  1876. tryReconnect = false
  1877. } else {
  1878. reconnectEvent.should.equal(true)
  1879. should(Object.keys(client._resubscribeTopics).length).be.equal(0)
  1880. done()
  1881. }
  1882. })
  1883. })
  1884. it('should not resubscribe when reconnecting if suback is error', function (done) {
  1885. var tryReconnect = true
  1886. var reconnectEvent = false
  1887. var server2 = new Server(function (c) {
  1888. c.on('connect', function (packet) {
  1889. c.connack({returnCode: 0})
  1890. })
  1891. c.on('subscribe', function (packet) {
  1892. c.suback({
  1893. messageId: packet.messageId,
  1894. granted: packet.subscriptions.map(function (e) {
  1895. return e.qos | 0x80
  1896. })
  1897. })
  1898. c.pubrel({ messageId: Math.floor(Math.random() * 9000) + 1000 })
  1899. })
  1900. })
  1901. server2.listen(port + 49, function () {
  1902. var client = mqtt.connect({
  1903. port: port + 49,
  1904. host: 'localhost',
  1905. reconnectPeriod: 100
  1906. })
  1907. client.on('reconnect', function () {
  1908. reconnectEvent = true
  1909. })
  1910. client.on('connect', function () {
  1911. if (tryReconnect) {
  1912. client.subscribe('hello', function () {
  1913. client.stream.end()
  1914. server.once('client', function (serverClient) {
  1915. serverClient.on('subscribe', function () {
  1916. should.fail()
  1917. })
  1918. })
  1919. })
  1920. tryReconnect = false
  1921. } else {
  1922. reconnectEvent.should.equal(true)
  1923. should(Object.keys(client._resubscribeTopics).length).be.equal(0)
  1924. server2.close()
  1925. done()
  1926. }
  1927. })
  1928. })
  1929. })
  1930. it('should preserved incomingStore after disconnecting if clean is false', function (done) {
  1931. var reconnect = false
  1932. var client = {}
  1933. var incomingStore = new mqtt.Store({ clean: false })
  1934. var outgoingStore = new mqtt.Store({ clean: false })
  1935. var server2 = new Server(function (c) {
  1936. c.on('connect', function (packet) {
  1937. c.connack({returnCode: 0})
  1938. if (reconnect) {
  1939. c.pubrel({ messageId: 1 })
  1940. }
  1941. })
  1942. c.on('subscribe', function (packet) {
  1943. c.suback({
  1944. messageId: packet.messageId,
  1945. granted: packet.subscriptions.map(function (e) {
  1946. return e.qos
  1947. })
  1948. })
  1949. c.publish({ topic: 'topic', payload: 'payload', qos: 2, messageId: 1, retain: false })
  1950. })
  1951. c.on('pubrec', function (packet) {
  1952. client.end(false, function () {
  1953. client.reconnect({
  1954. incomingStore: incomingStore,
  1955. outgoingStore: outgoingStore
  1956. })
  1957. })
  1958. })
  1959. c.on('pubcomp', function (packet) {
  1960. client.end()
  1961. server2.close()
  1962. done()
  1963. })
  1964. })
  1965. server2.listen(port + 50, function () {
  1966. client = mqtt.connect({
  1967. port: port + 50,
  1968. host: 'localhost',
  1969. clean: false,
  1970. clientId: 'cid1',
  1971. reconnectPeriod: 0,
  1972. incomingStore: incomingStore,
  1973. outgoingStore: outgoingStore
  1974. })
  1975. client.on('connect', function () {
  1976. if (!reconnect) {
  1977. client.subscribe('test', {qos: 2}, function () {
  1978. })
  1979. reconnect = true
  1980. }
  1981. })
  1982. client.on('message', function (topic, message) {
  1983. topic.should.equal('topic')
  1984. message.toString().should.equal('payload')
  1985. })
  1986. })
  1987. })
  1988. it('should resend in-flight QoS 1 publish messages from the client if clean is false', function (done) {
  1989. var reconnect = false
  1990. var client = {}
  1991. var incomingStore = new mqtt.Store({ clean: false })
  1992. var outgoingStore = new mqtt.Store({ clean: false })
  1993. var server2 = new Server(function (c) {
  1994. c.on('connect', function (packet) {
  1995. c.connack({returnCode: 0})
  1996. })
  1997. c.on('publish', function (packet) {
  1998. if (reconnect) {
  1999. server2.close()
  2000. done()
  2001. } else {
  2002. client.end(true, function () {
  2003. client.reconnect({
  2004. incomingStore: incomingStore,
  2005. outgoingStore: outgoingStore
  2006. })
  2007. reconnect = true
  2008. })
  2009. }
  2010. })
  2011. })
  2012. server2.listen(port + 50, function () {
  2013. client = mqtt.connect({
  2014. port: port + 50,
  2015. host: 'localhost',
  2016. clean: false,
  2017. clientId: 'cid1',
  2018. reconnectPeriod: 0,
  2019. incomingStore: incomingStore,
  2020. outgoingStore: outgoingStore
  2021. })
  2022. client.on('connect', function () {
  2023. if (!reconnect) {
  2024. client.publish('topic', 'payload', {qos: 1})
  2025. }
  2026. })
  2027. client.on('error', function () {})
  2028. })
  2029. })
  2030. it('should resend in-flight QoS 2 publish messages from the client if clean is false', function (done) {
  2031. var reconnect = false
  2032. var client = {}
  2033. var incomingStore = new mqtt.Store({ clean: false })
  2034. var outgoingStore = new mqtt.Store({ clean: false })
  2035. var server2 = new Server(function (c) {
  2036. c.on('connect', function (packet) {
  2037. c.connack({returnCode: 0})
  2038. })
  2039. c.on('publish', function (packet) {
  2040. if (reconnect) {
  2041. server2.close()
  2042. done()
  2043. } else {
  2044. client.end(true, function () {
  2045. client.reconnect({
  2046. incomingStore: incomingStore,
  2047. outgoingStore: outgoingStore
  2048. })
  2049. reconnect = true
  2050. })
  2051. }
  2052. })
  2053. })
  2054. server2.listen(port + 50, function () {
  2055. client = mqtt.connect({
  2056. port: port + 50,
  2057. host: 'localhost',
  2058. clean: false,
  2059. clientId: 'cid1',
  2060. reconnectPeriod: 0,
  2061. incomingStore: incomingStore,
  2062. outgoingStore: outgoingStore
  2063. })
  2064. client.on('connect', function () {
  2065. if (!reconnect) {
  2066. client.publish('topic', 'payload', {qos: 2})
  2067. }
  2068. })
  2069. client.on('error', function () {})
  2070. })
  2071. })
  2072. it('should resend in-flight QoS 2 pubrel messages from the client if clean is false', function (done) {
  2073. var reconnect = false
  2074. var client = {}
  2075. var incomingStore = new mqtt.Store({ clean: false })
  2076. var outgoingStore = new mqtt.Store({ clean: false })
  2077. var server2 = new Server(function (c) {
  2078. c.on('connect', function (packet) {
  2079. c.connack({returnCode: 0})
  2080. })
  2081. c.on('publish', function (packet) {
  2082. if (!reconnect) {
  2083. c.pubrec({messageId: packet.messageId})
  2084. }
  2085. })
  2086. c.on('pubrel', function () {
  2087. if (reconnect) {
  2088. server2.close()
  2089. done()
  2090. } else {
  2091. client.end(true, function () {
  2092. client.reconnect({
  2093. incomingStore: incomingStore,
  2094. outgoingStore: outgoingStore
  2095. })
  2096. reconnect = true
  2097. })
  2098. }
  2099. })
  2100. })
  2101. server2.listen(port + 50, function () {
  2102. client = mqtt.connect({
  2103. port: port + 50,
  2104. host: 'localhost',
  2105. clean: false,
  2106. clientId: 'cid1',
  2107. reconnectPeriod: 0,
  2108. incomingStore: incomingStore,
  2109. outgoingStore: outgoingStore
  2110. })
  2111. client.on('connect', function () {
  2112. if (!reconnect) {
  2113. client.publish('topic', 'payload', {qos: 2})
  2114. }
  2115. })
  2116. client.on('error', function () {})
  2117. })
  2118. })
  2119. it('should resend in-flight publish messages by published order', function (done) {
  2120. var publishCount = 0
  2121. var reconnect = false
  2122. var disconnectOnce = true
  2123. var client = {}
  2124. var incomingStore = new mqtt.Store({ clean: false })
  2125. var outgoingStore = new mqtt.Store({ clean: false })
  2126. var server2 = new Server(function (c) {
  2127. // errors are not interesting for this test
  2128. // but they might happen on some platforms
  2129. c.on('error', function () {})
  2130. c.on('connect', function (packet) {
  2131. c.connack({returnCode: 0})
  2132. })
  2133. c.on('publish', function (packet) {
  2134. c.puback({messageId: packet.messageId})
  2135. if (reconnect) {
  2136. switch (publishCount++) {
  2137. case 0:
  2138. packet.payload.toString().should.equal('payload1')
  2139. break
  2140. case 1:
  2141. packet.payload.toString().should.equal('payload2')
  2142. break
  2143. case 2:
  2144. packet.payload.toString().should.equal('payload3')
  2145. server2.close()
  2146. done()
  2147. break
  2148. }
  2149. } else {
  2150. if (disconnectOnce) {
  2151. client.end(true, function () {
  2152. reconnect = true
  2153. client.reconnect({
  2154. incomingStore: incomingStore,
  2155. outgoingStore: outgoingStore
  2156. })
  2157. })
  2158. disconnectOnce = false
  2159. }
  2160. }
  2161. })
  2162. })
  2163. server2.listen(port + 50, function () {
  2164. client = mqtt.connect({
  2165. port: port + 50,
  2166. host: 'localhost',
  2167. clean: false,
  2168. clientId: 'cid1',
  2169. reconnectPeriod: 0,
  2170. incomingStore: incomingStore,
  2171. outgoingStore: outgoingStore
  2172. })
  2173. client.nextId = 65535
  2174. client.on('connect', function () {
  2175. if (!reconnect) {
  2176. client.publish('topic', 'payload1', {qos: 1})
  2177. client.publish('topic', 'payload2', {qos: 1})
  2178. client.publish('topic', 'payload3', {qos: 1})
  2179. }
  2180. })
  2181. client.on('error', function () {})
  2182. })
  2183. })
  2184. it('should be able to pub/sub if reconnect() is called at close handler', function (done) {
  2185. var client = connect({ reconnectPeriod: 0 })
  2186. var tryReconnect = true
  2187. var reconnectEvent = false
  2188. client.on('close', function () {
  2189. if (tryReconnect) {
  2190. tryReconnect = false
  2191. client.reconnect()
  2192. } else {
  2193. reconnectEvent.should.equal(true)
  2194. done()
  2195. }
  2196. })
  2197. client.on('reconnect', function () {
  2198. reconnectEvent = true
  2199. })
  2200. client.on('connect', function () {
  2201. if (tryReconnect) {
  2202. client.end()
  2203. } else {
  2204. client.subscribe('hello', function () {
  2205. client.end()
  2206. })
  2207. }
  2208. })
  2209. })
  2210. it('should be able to pub/sub if reconnect() is called at out of close handler', function (done) {
  2211. var client = connect({ reconnectPeriod: 0 })
  2212. var tryReconnect = true
  2213. var reconnectEvent = false
  2214. client.on('close', function () {
  2215. if (tryReconnect) {
  2216. tryReconnect = false
  2217. setTimeout(function () {
  2218. client.reconnect()
  2219. }, 100)
  2220. } else {
  2221. reconnectEvent.should.equal(true)
  2222. done()
  2223. }
  2224. })
  2225. client.on('reconnect', function () {
  2226. reconnectEvent = true
  2227. })
  2228. client.on('connect', function () {
  2229. if (tryReconnect) {
  2230. client.end()
  2231. } else {
  2232. client.subscribe('hello', function () {
  2233. client.end()
  2234. })
  2235. }
  2236. })
  2237. })
  2238. context('with alternate server client', function () {
  2239. var cachedClientListeners
  2240. beforeEach(function () {
  2241. cachedClientListeners = server.listeners('client')
  2242. server.removeAllListeners('client')
  2243. })
  2244. afterEach(function () {
  2245. server.removeAllListeners('client')
  2246. cachedClientListeners.forEach(function (listener) {
  2247. server.on('client', listener)
  2248. })
  2249. })
  2250. it('should resubscribe even if disconnect is before suback', function (done) {
  2251. var client = mqtt.connect(Object.assign({ reconnectPeriod: 100 }, config))
  2252. var subscribeCount = 0
  2253. var connectCount = 0
  2254. server.on('client', function (serverClient) {
  2255. serverClient.on('connect', function () {
  2256. connectCount++
  2257. serverClient.connack({returnCode: 0})
  2258. })
  2259. serverClient.on('subscribe', function () {
  2260. subscribeCount++
  2261. // disconnect before sending the suback on the first subscribe
  2262. if (subscribeCount === 1) {
  2263. client.stream.end()
  2264. }
  2265. // after the second connection, confirm that the only two
  2266. // subscribes have taken place, then cleanup and exit
  2267. if (connectCount >= 2) {
  2268. subscribeCount.should.equal(2)
  2269. client.end(true, done)
  2270. }
  2271. })
  2272. })
  2273. client.subscribe('hello')
  2274. })
  2275. it('should resubscribe exactly once', function (done) {
  2276. var client = mqtt.connect(Object.assign({ reconnectPeriod: 100 }, config))
  2277. var subscribeCount = 0
  2278. server.on('client', function (serverClient) {
  2279. serverClient.on('connect', function () {
  2280. serverClient.connack({returnCode: 0})
  2281. })
  2282. serverClient.on('subscribe', function () {
  2283. subscribeCount++
  2284. // disconnect before sending the suback on the first subscribe
  2285. if (subscribeCount === 1) {
  2286. client.stream.end()
  2287. }
  2288. // after the second connection, only two subs
  2289. // subscribes have taken place, then cleanup and exit
  2290. if (subscribeCount === 2) {
  2291. client.end(true, done)
  2292. }
  2293. })
  2294. })
  2295. client.subscribe('hello')
  2296. })
  2297. })
  2298. })
  2299. }