13
13
#import " MQTTCFSocketTransport.h"
14
14
#import " MQTTTestHelpers.h"
15
15
16
+ @interface MQTTSessionManager (Tests)
17
+
18
+ - (void )connectWithParameters : (NSDictionary *)parameters clean : (BOOL )clean ;
19
+
20
+ @end
21
+
22
+ @implementation MQTTSessionManager (Tests)
23
+
24
+ - (void )connectWithParameters : (NSDictionary *)parameters clean : (BOOL )clean {
25
+ [self connectTo: parameters[@" host" ]
26
+ port: [parameters[@" port" ] intValue ]
27
+ tls: [parameters[@" tls" ] boolValue ]
28
+ keepalive: 60
29
+ clean: clean
30
+ auth: NO
31
+ user: nil
32
+ pass: nil
33
+ will: NO
34
+ willTopic: nil
35
+ willMsg: nil
36
+ willQos: MQTTQosLevelAtMostOnce
37
+ willRetainFlag: NO
38
+ withClientId: nil
39
+ securityPolicy: [MQTTTestHelpers securityPolicy: parameters]
40
+ certificates: [MQTTTestHelpers clientCerts: parameters]
41
+ protocolLevel: [parameters[@" protocollevel" ] intValue ]];
42
+ }
43
+
44
+ @end
45
+
16
46
@interface MQTTTestSessionManager : MQTTTestHelpers <MQTTSessionManagerDelegate>
17
47
@property (nonatomic ) int step;
18
48
@property (nonatomic ) int sent;
@@ -56,22 +86,7 @@ - (void)testMQTTSessionManager:(BOOL)clean {
56
86
options: NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
57
87
context: nil ];
58
88
manager.subscriptions = [@{TOPIC: @(0 )} mutableCopy];
59
- [manager connectTo: parameters[@" host" ]
60
- port: [parameters[@" port" ] intValue ]
61
- tls: [parameters[@" tls" ] boolValue ]
62
- keepalive: 60
63
- clean: clean
64
- auth: NO
65
- user: nil
66
- pass: nil
67
- will: NO
68
- willTopic: nil
69
- willMsg: nil
70
- willQos: MQTTQosLevelAtMostOnce
71
- willRetainFlag: FALSE
72
- withClientId: nil
73
- securityPolicy: [MQTTTestHelpers securityPolicy: parameters]
74
- certificates: [MQTTTestHelpers clientCerts: parameters]];
89
+ [manager connectWithParameters: parameters clean: clean];
75
90
76
91
while (self.step == -1 && manager.state != MQTTSessionManagerStateConnected) {
77
92
DDLogInfo (@" [testMQTTSessionManager] waiting for connect %d " , manager.state );
@@ -165,22 +180,7 @@ - (void)testMQTTSessionManagerPersistent {
165
180
context: nil ];
166
181
167
182
manager.subscriptions = [@{TOPIC: @(0 )} mutableCopy];
168
- [manager connectTo: parameters[@" host" ]
169
- port: [parameters[@" port" ] intValue ]
170
- tls: [parameters[@" tls" ] boolValue ]
171
- keepalive: 60
172
- clean: TRUE
173
- auth: NO
174
- user: nil
175
- pass: nil
176
- will: NO
177
- willTopic: nil
178
- willMsg: nil
179
- willQos: MQTTQosLevelAtMostOnce
180
- willRetainFlag: FALSE
181
- withClientId: nil
182
- securityPolicy: [MQTTTestHelpers securityPolicy: parameters]
183
- certificates: [MQTTTestHelpers clientCerts: parameters]];
183
+ [manager connectWithParameters: parameters clean: YES ];
184
184
while (self.step == -1 && manager.state != MQTTSessionManagerStateConnected) {
185
185
DDLogInfo (@" [testMQTTSessionManagerPersistent] waiting for connect %d " , manager.state );
186
186
[[NSRunLoop currentRunLoop ] runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 1 ]];
@@ -267,22 +267,7 @@ - (void)testSessionManagerShort {
267
267
268
268
269
269
manager.subscriptions = @{TOPIC: @(MQTTQosLevelExactlyOnce)};
270
- [manager connectTo: parameters[@" host" ]
271
- port: [parameters[@" port" ] intValue ]
272
- tls: [parameters[@" tls" ] boolValue ]
273
- keepalive: 60
274
- clean: TRUE
275
- auth: NO
276
- user: nil
277
- pass: nil
278
- will: NO
279
- willTopic: nil
280
- willMsg: nil
281
- willQos: MQTTQosLevelAtMostOnce
282
- willRetainFlag: FALSE
283
- withClientId: nil
284
- securityPolicy: [MQTTTestHelpers securityPolicy: parameters]
285
- certificates: [MQTTTestHelpers clientCerts: parameters]];
270
+ [manager connectWithParameters: parameters clean: YES ];
286
271
287
272
while (!self.timedout && manager.state != MQTTSessionManagerStateConnected) {
288
273
DDLogInfo (@" waiting for connect %d " , manager.state );
@@ -415,24 +400,8 @@ - (void)testSessionManagerALotSubscriptions {
415
400
selector: @selector (timedout: )
416
401
userInfo: nil
417
402
repeats: false ];
418
-
419
-
420
- [manager connectTo: parameters[@" host" ]
421
- port: [parameters[@" port" ] intValue ]
422
- tls: [parameters[@" tls" ] boolValue ]
423
- keepalive: 60
424
- clean: TRUE
425
- auth: NO
426
- user: nil
427
- pass: nil
428
- will: NO
429
- willTopic: nil
430
- willMsg: nil
431
- willQos: MQTTQosLevelAtMostOnce
432
- willRetainFlag: FALSE
433
- withClientId: nil
434
- securityPolicy: [MQTTTestHelpers securityPolicy: parameters]
435
- certificates: [MQTTTestHelpers clientCerts: parameters]];
403
+
404
+ [manager connectWithParameters: parameters clean: YES ];
436
405
437
406
while (!self.timedout && manager.state != MQTTSessionManagerStateConnected) {
438
407
DDLogInfo (@" waiting for connect %d " , manager.state );
@@ -596,8 +565,7 @@ - (void)testSessionManagerALotSubscriptions {
596
565
selector: @selector (timedout: )
597
566
userInfo: nil
598
567
repeats: false ];
599
-
600
-
568
+
601
569
while (!self.timedout ) {
602
570
[manager sendData: [[NSDate date ].description dataUsingEncoding: NSUTF8StringEncoding]
603
571
topic: TOPIC qos: MQTTQosLevelExactlyOnce retain: FALSE ];
@@ -612,8 +580,7 @@ - (void)testSessionManagerALotSubscriptions {
612
580
selector: @selector (timedout: )
613
581
userInfo: nil
614
582
repeats: false ];
615
-
616
-
583
+
617
584
while (!self.timedout ) {
618
585
[manager sendData: [[NSDate date ].description dataUsingEncoding: NSUTF8StringEncoding]
619
586
topic: TOPIC qos: MQTTQosLevelExactlyOnce retain: FALSE ];
@@ -643,6 +610,53 @@ - (void)testSessionManagerALotSubscriptions {
643
610
}
644
611
}
645
612
613
+ - (void )testMQTTSessionManagerRecconnectionWithConnectToLast {
614
+ for (NSString *broker in self.brokers .allKeys ) {
615
+ DDLogInfo (@" testing broker %@ " , broker);
616
+ NSDictionary *parameters = self.brokers [broker];
617
+ if ([parameters[@" websocket" ] boolValue ]) {
618
+ continue ;
619
+ }
620
+ self.step = -1 ;
621
+ NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: [parameters[@" timeout" ] intValue ]
622
+ target: self
623
+ selector: @selector (stepper: )
624
+ userInfo: nil
625
+ repeats: true ];
626
+
627
+ MQTTSessionManager *manager = [[MQTTSessionManager alloc ] init ];
628
+ manager.delegate = self;
629
+
630
+ [manager connectWithParameters: parameters clean: YES ];
631
+
632
+ while (self.step == -1 && manager.state != MQTTSessionManagerStateConnected) {
633
+ DDLogInfo (@" [testMQTTSessionManager] waiting for connect %d " , manager.state );
634
+ [[NSRunLoop currentRunLoop ] runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 1 ]];
635
+ }
636
+ XCTAssertEqual (manager.state , MQTTSessionManagerStateConnected);
637
+
638
+ [manager disconnect ];
639
+ [[NSRunLoop currentRunLoop ] runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 1 ]];
640
+
641
+ XCTAssertEqual (manager.state , MQTTSessionManagerStateClosed);
642
+
643
+ while (self.step <= 0 ) {
644
+ [[NSRunLoop currentRunLoop ] runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 1 ]];
645
+ }
646
+
647
+ [manager connectToLast ];
648
+ [[NSRunLoop currentRunLoop ] runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 1 ]];
649
+
650
+ XCTAssertEqual (manager.state , MQTTSessionManagerStateConnected);
651
+
652
+ while (self.step <= 1 ) {
653
+ [[NSRunLoop currentRunLoop ] runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 1 ]];
654
+ }
655
+
656
+ [timer invalidate ];
657
+ }
658
+ }
659
+
646
660
#pragma mark - helpers
647
661
648
662
0 commit comments