@@ -4,123 +4,133 @@ const isUndefined = require('../../common/lib/is-undefined.js');
44
55function mockMQTTClient ( wrapper , options ) {
66
7- var that = this ;
8- this . options = { 'reconnectPeriod' :0 } ;
9-
10- // Record list indicating whether the corresponding method is called
11- this . commandCalled = { 'publish' :0 , 'subscribe' :0 , 'unsubscribe' :0 , 'end' :0 , 'handleMessage' :0 } ;
12- this . lastPublishedMessage = 'Empty' ; // Keep track of the last published message
13- this . subscriptions = new Array ;
14- this . subscriptions . length = 0 ;
15- this . publishes = new Array ; // for all topics
16- this . publishes . length = 0 ;
17- this . subscribeQosValues = new Array ;
18- this . subscribeQosValues . length = 0 ;
19- this . publishQosValues = new Array ;
20- this . publishQosValues . length = 0 ;
21-
22- // Reinit the record list
23- this . reInitCommandCalled = function ( ) {
24- this . commandCalled [ 'publish' ] = 0 ;
25- this . commandCalled [ 'subscribe' ] = 0 ;
26- this . commandCalled [ 'unsubscribe' ] = 0 ;
27- this . commandCalled [ 'end' ] = 0 ;
28- this . commandCalled [ 'handleMessage' ] = 0 ;
29-
30- var topic = this . subscriptions . shift ( ) ;
31- while ( ! isUndefined ( topic ) ) {
32- topic = this . subscriptions . shift ( ) ;
33- }
34- var message = this . publishes . shift ( ) ;
35- while ( ! isUndefined ( message ) ) {
36- message = this . publishes . shift ( ) ;
37- }
38- var qos = this . subscribeQosValues . shift ( ) ;
39- while ( ! isUndefined ( qos ) ) {
40- qos = this . subscribeQosValues . shift ( ) ;
41- }
42- var qos = this . publishQosValues . shift ( ) ;
43- while ( ! isUndefined ( qos ) ) {
44- qos = this . publishQosValues . shift ( ) ;
45- }
46- } ;
47-
48- // Reset publishedMessage
49- this . resetPublishedMessage = function ( ) {
50- this . lastPublishedMessage = 'Empty' ;
51- }
7+ var that = this ;
8+ this . options = { 'reconnectPeriod' :0 } ;
9+
10+ // Record list indicating whether the corresponding method is called
11+ this . commandCalled = { 'publish' :0 , 'subscribe' :0 , 'unsubscribe' :0 , 'end' :0 , 'handleMessage' :0 } ;
12+ this . lastPublishedMessage = 'Empty' ; // Keep track of the last published message
13+ this . subscriptions = new Array ;
14+ this . subscriptions . length = 0 ;
15+ this . unsubscriptions = new Array ;
16+ this . unsubscriptions . length = 0 ;
17+ this . publishes = new Array ; // for all topics
18+ this . publishes . length = 0 ;
19+ this . subscribeQosValues = new Array ;
20+ this . subscribeQosValues . length = 0 ;
21+ this . publishQosValues = new Array ;
22+ this . publishQosValues . length = 0 ;
23+
24+ // Reinit the record list
25+ this . reInitCommandCalled = function ( ) {
26+ this . commandCalled [ 'publish' ] = 0 ;
27+ this . commandCalled [ 'subscribe' ] = 0 ;
28+ this . commandCalled [ 'unsubscribe' ] = 0 ;
29+ this . commandCalled [ 'end' ] = 0 ;
30+ this . commandCalled [ 'handleMessage' ] = 0 ;
31+
32+ var topic = this . subscriptions . shift ( ) ;
33+ while ( ! isUndefined ( topic ) ) {
34+ topic = this . subscriptions . shift ( ) ;
35+ }
36+ var message = this . publishes . shift ( ) ;
37+ while ( ! isUndefined ( message ) ) {
38+ message = this . publishes . shift ( ) ;
39+ }
40+ var qos = this . subscribeQosValues . shift ( ) ;
41+ while ( ! isUndefined ( qos ) ) {
42+ qos = this . subscribeQosValues . shift ( ) ;
43+ }
44+ var qos = this . publishQosValues . shift ( ) ;
45+ while ( ! isUndefined ( qos ) ) {
46+ qos = this . publishQosValues . shift ( ) ;
47+ }
48+ } ;
49+
50+ // Reset publishedMessage
51+ this . resetPublishedMessage = function ( ) {
52+ this . lastPublishedMessage = 'Empty' ;
53+ }
5254
5355 // This is the module mocking the client returned by mqtt.connect, APIs are:
54- this . publish = function ( topic , message , options , callback ) {
55- options = options || '' ;
56- callback = callback || '' ;
57- this . lastPublishedMessage = message ;
58- this . commandCalled [ 'publish' ] += 1 ;
59- this . publishes . push ( message ) ;
60-
61- if ( ! isUndefined ( options . qos ) ) {
62- this . publishQosValues . push ( options . qos ) ;
63- }
64- } ;
65-
66- this . subscribe = function ( topic , options , callback ) {
67- options = options || '' ;
68- callback = callback || '' ;
69- this . commandCalled [ 'subscribe' ] += 1 ;
70-
71- var granted = [ ] ;
72- if ( Object . prototype . toString . call ( topic ) === '[object Array]' ) {
73- topic . forEach ( function ( item , index , array ) {
74- var grantedTopic = { topic : item , qos : 0 }
75- that . subscriptions . push ( item ) ;
76- if ( ! isUndefined ( options . qos ) ) {
77- that . subscribeQosValues . push ( options . qos ) ;
78- grantedTopic . qos = options . qos ;
79- }
80-
81- if ( mockMQTTClient . triggerError ( ) ) {
82- grantedTopic . qos = 128 ;
83- }
84-
85- granted . push ( grantedTopic ) ;
86- } ) ;
87- }
88- else {
89- var grantedTopic = { topic : topic , qos : 0 }
90- this . subscriptions . push ( topic ) ;
91- if ( ! isUndefined ( options . qos ) ) {
92- that . subscribeQosValues . push ( options . qos ) ;
93- grantedTopic . qos = options . qos ;
94- }
95- if ( mockMQTTClient . triggerError ( ) ) {
96- grantedTopic . qos = 128 ;
97- }
98- granted . push ( grantedTopic ) ;
99- }
100- if ( callback !== '' ) {
101- callback ( null , granted ) ; // call callback
102- }
103- } ;
104-
105- this . unsubscribe = function ( topic , callback ) {
106- callback = callback || '' ;
107- this . commandCalled [ 'unsubscribe' ] += 1 ;
108- if ( callback !== '' ) {
109- callback ( null ) ; // call callback
110- }
111- } ;
112-
113- this . end = function ( force , cb ) {
114- force = force || false ;
115- cb = cb || '' ;
116- this . commandCalled [ 'end' ] += 1 ;
117- } ;
118-
119- this . handleMessage = function ( packet , callback ) {
120- this . commandCalled [ 'handleMessage' ] += 1 ;
121- } ;
122-
123- EventEmitter . call ( this ) ;
56+ this . publish = function ( topic , message , options , callback ) {
57+ options = options || '' ;
58+ callback = callback || '' ;
59+ this . lastPublishedMessage = message ;
60+ this . commandCalled [ 'publish' ] += 1 ;
61+ this . publishes . push ( message ) ;
62+
63+ if ( ! isUndefined ( options . qos ) ) {
64+ this . publishQosValues . push ( options . qos ) ;
65+ }
66+ } ;
67+
68+ this . subscribe = function ( topic , options , callback ) {
69+ options = options || '' ;
70+ callback = callback || '' ;
71+ this . commandCalled [ 'subscribe' ] += 1 ;
72+
73+ var granted = [ ] ;
74+ if ( Object . prototype . toString . call ( topic ) === '[object Array]' ) {
75+ topic . forEach ( function ( item , index , array ) {
76+ var grantedTopic = { topic : item , qos : 0 }
77+ that . subscriptions . push ( item ) ;
78+ if ( ! isUndefined ( options . qos ) ) {
79+ that . subscribeQosValues . push ( options . qos ) ;
80+ grantedTopic . qos = options . qos ;
81+ }
82+
83+ if ( mockMQTTClient . triggerError ( ) ) {
84+ grantedTopic . qos = 128 ;
85+ }
86+
87+ granted . push ( grantedTopic ) ;
88+ } ) ;
89+ }
90+ else {
91+ var grantedTopic = { topic : topic , qos : 0 }
92+ this . subscriptions . push ( topic ) ;
93+ if ( ! isUndefined ( options . qos ) ) {
94+ that . subscribeQosValues . push ( options . qos ) ;
95+ grantedTopic . qos = options . qos ;
96+ }
97+ if ( mockMQTTClient . triggerError ( ) ) {
98+ grantedTopic . qos = 128 ;
99+ }
100+ granted . push ( grantedTopic ) ;
101+ }
102+ if ( callback !== '' ) {
103+ callback ( null , granted ) ; // call callback
104+ }
105+ } ;
106+
107+ this . unsubscribe = function ( topic , callback ) {
108+ callback = callback || '' ;
109+ this . commandCalled [ 'unsubscribe' ] += 1 ;
110+ if ( Object . prototype . toString . call ( topic ) === '[object Array]' ) {
111+ topic . forEach ( function ( item , index , array ) {
112+ that . unsubscriptions . push ( item ) ;
113+ } ) ;
114+ }
115+ else {
116+ this . unsubscriptions . push ( topic ) ;
117+ }
118+ if ( callback !== '' ) {
119+ callback ( null ) ; // call callback
120+ }
121+ } ;
122+
123+ this . end = function ( force , cb ) {
124+ force = force || false ;
125+ cb = cb || '' ;
126+ this . commandCalled [ 'end' ] += 1 ;
127+ } ;
128+
129+ this . handleMessage = function ( packet , callback ) {
130+ this . commandCalled [ 'handleMessage' ] += 1 ;
131+ } ;
132+
133+ EventEmitter . call ( this ) ;
124134}
125135
126136util . inherits ( mockMQTTClient , EventEmitter ) ;
0 commit comments