Skip to content

Commit b37414d

Browse files
Fix intermittent falure in Darwin browsing test.
We are running the test against Matter applications that are starting up, and during startup we do some silly unregistering/re-registering of DNS-SD advertisements. The test was counting all the registrations, not subtracting the un-registrations, and asserting that the resulting count was not too high. But sometimes it is, if we get a "unregister/re-register" pair in there. The fix: 1) Fixes the test to better track un-registration bits. 2) Fixes the OnBleScanRemove code to call the right delegate method. 3) Fixes OnBrowseRemove to not call the delegate if we have not notified about the add for the relevant result. We could have just worked around this in the test, but notifying "removed" about an object with all fields set to nil just doesn't make much sense.
1 parent 9533925 commit b37414d

File tree

2 files changed

+55
-10
lines changed

2 files changed

+55
-10
lines changed

src/darwin/Framework/CHIP/MTRCommissionableBrowser.mm

+9-4
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,14 @@ void OnBrowseRemove(DnssdService service) override
268268
// If there is nothing else to resolve for the given instance name, just remove it
269269
// too and informs the delegate that it is gone.
270270
if ([interfaces count] == 0) {
271-
dispatch_async(mDispatchQueue, ^{
272-
[mDelegate controller:mController didRemoveCommissionableDevice:result];
273-
});
271+
// If result.instanceName is nil, that means we never notified our
272+
// delegate about this result (because we did not get that far in
273+
// resolving it), so don't bother notifying about the removal either.
274+
if (result.instanceName != nil) {
275+
dispatch_async(mDispatchQueue, ^{
276+
[mDelegate controller:mController didRemoveCommissionableDevice:result];
277+
});
278+
}
274279

275280
mDiscoveredResults[key] = nil;
276281
}
@@ -325,7 +330,7 @@ void OnBleScanRemove(BLE_CONNECTION_OBJECT connObj) override
325330
MATTER_LOG_METRIC(kMetricBLEDevicesRemoved, ++mBLEDevicesRemoved);
326331

327332
dispatch_async(mDispatchQueue, ^{
328-
[mDelegate controller:mController didFindCommissionableDevice:result];
333+
[mDelegate controller:mController didRemoveCommissionableDevice:result];
329334
});
330335
}
331336

src/darwin/Framework/CHIPTests/MTRCommissionableBrowserTests.m

+46-6
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,27 @@
3636
// Singleton controller we use.
3737
static MTRDeviceController * sController = nil;
3838

39+
static NSString * kInstanceNameKey = @"instanceName";
40+
static NSString * kVendorIDKey = @"vendorID";
41+
static NSString * kProductIDKey = @"productID";
42+
static NSString * kDiscriminatorKey = @"discriminator";
43+
static NSString * kCommissioningModeKey = @"commissioningMode";
44+
45+
static NSDictionary<NSString *, id> * ResultSnapshot(MTRCommissionableBrowserResult * result)
46+
{
47+
return @{
48+
kInstanceNameKey : result.instanceName,
49+
kVendorIDKey : result.vendorID,
50+
kProductIDKey : result.productID,
51+
kDiscriminatorKey : result.discriminator,
52+
kCommissioningModeKey : @(result.commissioningMode),
53+
};
54+
}
55+
3956
@interface DeviceScannerDelegate : NSObject <MTRCommissionableBrowserDelegate>
4057
@property (nonatomic, nullable) XCTestExpectation * expectation;
41-
@property (nonatomic) NSNumber * resultsCount;
58+
@property (nonatomic) NSMutableArray<NSDictionary<NSString *, id> *> * results;
59+
@property (nonatomic) NSMutableArray<NSDictionary<NSString *, id> *> * removedResults;
4260

4361
- (instancetype)initWithExpectation:(XCTestExpectation *)expectation;
4462
- (void)controller:(MTRDeviceController *)controller didFindCommissionableDevice:(MTRCommissionableBrowserResult *)device;
@@ -52,8 +70,9 @@ - (instancetype)initWithExpectation:(XCTestExpectation *)expectation
5270
return nil;
5371
}
5472

55-
_resultsCount = 0;
5673
_expectation = expectation;
74+
_results = [[NSMutableArray alloc] init];
75+
_removedResults = [[NSMutableArray alloc] init];
5776
return self;
5877
}
5978

@@ -63,8 +82,9 @@ - (instancetype)init
6382
return nil;
6483
}
6584

66-
_resultsCount = 0;
6785
_expectation = nil;
86+
_results = [[NSMutableArray alloc] init];
87+
_removedResults = [[NSMutableArray alloc] init];
6888
return self;
6989
}
7090

@@ -77,12 +97,26 @@ - (void)controller:(MTRDeviceController *)controller didFindCommissionableDevice
7797
return;
7898
}
7999

80-
_resultsCount = @(_resultsCount.unsignedLongValue + 1);
81-
if ([_resultsCount isEqual:@(kExpectedDiscoveredDevicesCount)]) {
100+
__auto_type * snapshot = ResultSnapshot(device);
101+
102+
XCTAssertFalse([_results containsObject:snapshot], @"Newly discovered device %@ should not be in results already.", snapshot);
103+
104+
[_results addObject:snapshot];
105+
106+
if (_results.count == kExpectedDiscoveredDevicesCount) {
107+
// Do some sanity checking on our results and removedResults to make
108+
// sure we really only saw the relevant set of things.
109+
NSSet<NSDictionary<NSString *, id> *> * finalResultsSet = [NSSet setWithArray:_results];
110+
NSSet<NSDictionary<NSString *, id> *> * allResultsSet = [finalResultsSet copy];
111+
allResultsSet = [allResultsSet setByAddingObjectsFromArray:_removedResults];
112+
113+
// Ensure that we just saw the same results as our final set popping in and out if things
114+
// ever got removed here.
115+
XCTAssertEqualObjects(finalResultsSet, allResultsSet);
82116
[self.expectation fulfill];
83117
}
84118

85-
XCTAssertLessThanOrEqual(_resultsCount.unsignedLongValue, kExpectedDiscoveredDevicesCount);
119+
XCTAssertLessThanOrEqual(_results.count, kExpectedDiscoveredDevicesCount);
86120

87121
__auto_type instanceName = device.instanceName;
88122
__auto_type vendorId = device.vendorID;
@@ -108,6 +142,12 @@ - (void)controller:(MTRDeviceController *)controller didRemoveCommissionableDevi
108142

109143
NSLog(
110144
@"Removed Device (%@) with discriminator: %@ (vendor: %@, product: %@)", instanceName, discriminator, vendorId, productId);
145+
146+
__auto_type * snapshot = ResultSnapshot(device);
147+
XCTAssertTrue([_results containsObject:snapshot], @"Removed device %@ is not something we found before", snapshot);
148+
149+
[_removedResults addObject:snapshot];
150+
[_results removeObject:snapshot];
111151
}
112152
@end
113153

0 commit comments

Comments
 (0)