Skip to content

Commit

Permalink
Darwin: Simplify test setup and teardown (#32061)
Browse files Browse the repository at this point in the history
* Darwin: Simplify test setup and teardown

Avoid relying on dummy tests that are ordered first / last.

* fix unused variables
  • Loading branch information
ksperling-apple authored and pull[bot] committed Apr 23, 2024
1 parent 1893a9f commit 63ba981
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 323 deletions.
86 changes: 21 additions & 65 deletions src/darwin/Framework/CHIPTests/MTRBackwardsCompatTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
// system dependencies
#import <XCTest/XCTest.h>

// Fixture: chip-all-clusters-app --KVS "$(mktemp -t chip-test-kvs)" --interface-id -1

static const uint16_t kPairingTimeoutInSeconds = 10;
static const uint16_t kCASESetupTimeoutInSeconds = 30;
static const uint16_t kTimeoutInSeconds = 3;
Expand Down Expand Up @@ -71,7 +73,7 @@ - (void)onPairingComplete:(NSError *)error
XCTAssertEqual(error.code, 0);

NSError * commissionError = nil;
[sController commissionDevice:kDeviceId commissioningParams:[[MTRCommissioningParameters alloc] init] error:&commissionError];
XCTAssertTrue([sController commissionDevice:kDeviceId commissioningParams:[[MTRCommissioningParameters alloc] init] error:&commissionError]);
XCTAssertNil(commissionError);

// Keep waiting for onCommissioningComplete
Expand All @@ -89,64 +91,27 @@ - (void)onCommissioningComplete:(NSError *)error
@interface MTRBackwardsCompatTests : XCTestCase
@end

static BOOL sStackInitRan = NO;
static BOOL sNeedsStackShutdown = YES;

@implementation MTRBackwardsCompatTests

+ (void)tearDown
+ (void)setUp
{
// Global teardown, runs once
if (sNeedsStackShutdown) {
// We don't need to worry about ResetCommissionee. If we get here,
// we're running only one of our test methods (using
// -only-testing:MatterTests/MTROTAProviderTests/testMethodName), since
// we did not run test999_TearDown.
[self shutdownStack];
}
}

- (void)setUp
{
// Per-test setup, runs before each test.
[super setUp];
[self setContinueAfterFailure:NO];

if (sStackInitRan == NO) {
[self initStack];
}
}

- (void)tearDown
{
// Per-test teardown, runs after each test.
[super tearDown];
}

- (void)initStack
{
sStackInitRan = YES;

XCTestExpectation * expectation = [self expectationWithDescription:@"Pairing Complete"];
XCTestExpectation * expectation = [[XCTestExpectation alloc] initWithDescription:@"Pairing Complete"];

__auto_type * factory = [MTRControllerFactory sharedInstance];
XCTAssertNotNil(factory);

__auto_type * storage = [[MTRTestStorage alloc] init];
__auto_type * factoryParams = [[MTRControllerFactoryParams alloc] initWithStorage:storage];
factoryParams.port = @(kLocalPort);
XCTAssertTrue([factory startup:factoryParams]);

BOOL ok = [factory startup:factoryParams];
XCTAssertTrue(ok);

__auto_type * testKeys = [[MTRTestKeys alloc] init];
XCTAssertNotNil(testKeys);

sTestKeys = testKeys;
XCTAssertNotNil(sTestKeys = [[MTRTestKeys alloc] init]);

// Needs to match what startControllerOnExistingFabric calls elsewhere in
// this file do.
__auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithSigningKeypair:testKeys fabricId:1 ipk:testKeys.ipk];
__auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithSigningKeypair:sTestKeys fabricId:1 ipk:sTestKeys.ipk];
params.vendorId = @(kTestVendorId);

MTRDeviceController * controller = [factory startControllerOnNewFabric:params];
Expand All @@ -164,12 +129,12 @@ - (void)initStack
XCTAssertNotNil(payload);
XCTAssertNil(error);

[controller setupCommissioningSessionWithPayload:payload newNodeID:@(kDeviceId) error:&error];
XCTAssertTrue([controller setupCommissioningSessionWithPayload:payload newNodeID:@(kDeviceId) error:&error]);
XCTAssertNil(error);

[self waitForExpectationsWithTimeout:kPairingTimeoutInSeconds handler:nil];
XCTAssertEqual([XCTWaiter waitForExpectations:@[ expectation ] timeout:kPairingTimeoutInSeconds], XCTWaiterResultCompleted);

__block XCTestExpectation * connectionExpectation = [self expectationWithDescription:@"CASE established"];
__block XCTestExpectation * connectionExpectation = [[XCTestExpectation alloc] initWithDescription:@"CASE established"];
[controller getBaseDevice:kDeviceId
queue:dispatch_get_main_queue()
completionHandler:^(MTRBaseDevice * _Nullable device, NSError * _Nullable error) {
Expand All @@ -178,27 +143,24 @@ - (void)initStack
sConnectedDevice = device;
connectionExpectation = nil;
}];
[self waitForExpectationsWithTimeout:kCASESetupTimeoutInSeconds handler:nil];
XCTAssertEqual([XCTWaiter waitForExpectations:@[ connectionExpectation ] timeout:kCASESetupTimeoutInSeconds], XCTWaiterResultCompleted);
}

+ (void)shutdownStack
+ (void)tearDown
{
sNeedsStackShutdown = NO;

MTRDeviceController * controller = sController;
XCTAssertNotNil(controller);

[controller shutdown];
XCTAssertFalse([controller isRunning]);
ResetCommissionee(GetConnectedDevice(), dispatch_get_main_queue(), nil, kTimeoutInSeconds);

[sController shutdown];
XCTAssertFalse([sController isRunning]);
[[MTRControllerFactory sharedInstance] shutdown];

[super tearDown];
}

- (void)test000_SetUp
- (void)setUp
{
// Nothing to do here; our setUp method handled this already. This test
// just exists to make the setup not look like it's happening inside other
// tests.
[super setUp];
[self setContinueAfterFailure:NO];
}

#define CHECK_RETURN_TYPE(sig, type) \
Expand Down Expand Up @@ -1210,10 +1172,4 @@ - (void)test048_MTRModeSelectClusterSemanticTagStruct
CHECK_PROPERTY(obj, value, setValue, NSNumber *);
}

- (void)test999_TearDown
{
ResetCommissionee(GetConnectedDevice(), dispatch_get_main_queue(), self, kTimeoutInSeconds);
[[self class] shutdownStack];
}

@end
53 changes: 15 additions & 38 deletions src/darwin/Framework/CHIPTests/MTRCommissionableBrowserTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
#import "MTRTestKeys.h"
#import "MTRTestStorage.h"

// Fixture 1: chip-all-clusters-app --KVS "$(mktemp -t chip-test-kvs)" --interface-id -1
// Fixture 2: chip-all-clusters-app --KVS "$(mktemp -t chip-test-kvs)" --interface-id -1 \
--dac_provider credentials/development/commissioner_dut/struct_cd_origin_pid_vid_correct/test_case_vector.json \
--product-id 32768 --discriminator 3839

static const uint16_t kLocalPort = 5541;
static const uint16_t kTestVendorId = 0xFFF1u;
static const uint16_t kTestProductId1 = 0x8000u;
Expand Down Expand Up @@ -113,39 +118,11 @@ - (void)controller:(MTRDeviceController *)controller didRemoveCommissionableDevi
@interface MTRCommissionableBrowserTests : XCTestCase
@end

static BOOL sStackInitRan = NO;
static BOOL sNeedsStackShutdown = YES;

@implementation MTRCommissionableBrowserTests

+ (void)tearDown
{
// Global teardown, runs once
if (sNeedsStackShutdown) {
[self shutdownStack];
}
}

- (void)setUp
+ (void)setUp
{
// Per-test setup, runs before each test.
[super setUp];
[self setContinueAfterFailure:NO];

if (sStackInitRan == NO) {
[self initStack];
}
}

- (void)tearDown
{
// Per-test teardown, runs after each test.
[super tearDown];
}

- (void)initStack
{
sStackInitRan = YES;

__auto_type * factory = [MTRDeviceControllerFactory sharedInstance];
XCTAssertNotNil(factory);
Expand All @@ -169,17 +146,22 @@ - (void)initStack
sController = controller;
}

+ (void)shutdownStack
+ (void)tearDown
{
sNeedsStackShutdown = NO;

MTRDeviceController * controller = sController;
XCTAssertNotNil(controller);

[controller shutdown];
XCTAssertFalse([controller isRunning]);

[[MTRDeviceControllerFactory sharedInstance] stopControllerFactory];

[super tearDown];
}

- (void)setUp
{
[super setUp];
[self setContinueAfterFailure:NO];
}

- (void)test001_StartBrowseAndStopBrowse
Expand Down Expand Up @@ -248,9 +230,4 @@ - (void)test005_StartBrowseGetCommissionableOverMdns
XCTAssertTrue([sController stopBrowseForCommissionables]);
}

- (void)test999_TearDown
{
[[self class] shutdownStack];
}

@end
76 changes: 16 additions & 60 deletions src/darwin/Framework/CHIPTests/MTRDeviceTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
// system dependencies
#import <XCTest/XCTest.h>

// Fixture: chip-all-clusters-app --KVS "$(mktemp -t chip-test-kvs)" --interface-id -1

static const uint16_t kPairingTimeoutInSeconds = 10;
static const uint16_t kTimeoutInSeconds = 3;
static const uint64_t kDeviceId = 0x12344321;
Expand Down Expand Up @@ -125,46 +127,11 @@ - (void)controller:(MTRDeviceController *)controller commissioningComplete:(NSEr
@interface MTRDeviceTests : XCTestCase
@end

static BOOL sStackInitRan = NO;
static BOOL sNeedsStackShutdown = YES;

@implementation MTRDeviceTests

+ (void)tearDown
{
// Global teardown, runs once
if (sNeedsStackShutdown) {
// We don't need to worry about ResetCommissionee. If we get here,
// we're running only one of our test methods (using
// -only-testing:MatterTests/MTROTAProviderTests/testMethodName), since
// we did not run test999_TearDown.
[self shutdownStack];
}
}

- (void)setUp
{
// Per-test setup, runs before each test.
[super setUp];
[self setContinueAfterFailure:NO];

if (sStackInitRan == NO) {
[self initStack];
[self waitForCommissionee];
}
}

- (void)tearDown
+ (void)setUp
{
// Per-test teardown, runs after each test.
[super tearDown];
}

- (void)initStack
{
sStackInitRan = YES;

XCTestExpectation * expectation = [self expectationWithDescription:@"Pairing Complete"];
XCTestExpectation * pairingExpectation = [[XCTestExpectation alloc] initWithDescription:@"Pairing Complete"];

__auto_type * factory = [MTRDeviceControllerFactory sharedInstance];
XCTAssertNotNil(factory);
Expand Down Expand Up @@ -192,7 +159,7 @@ - (void)initStack
sController = controller;

MTRDeviceTestDeviceControllerDelegate * deviceControllerDelegate =
[[MTRDeviceTestDeviceControllerDelegate alloc] initWithExpectation:expectation];
[[MTRDeviceTestDeviceControllerDelegate alloc] initWithExpectation:pairingExpectation];
dispatch_queue_t callbackQueue = dispatch_queue_create("com.chip.device_controller_delegate", DISPATCH_QUEUE_SERIAL);

[controller setDeviceControllerDelegate:deviceControllerDelegate queue:callbackQueue];
Expand All @@ -202,38 +169,33 @@ - (void)initStack
XCTAssertNotNil(payload);
XCTAssertNil(error);

[controller setupCommissioningSessionWithPayload:payload newNodeID:@(kDeviceId) error:&error];
XCTAssertTrue([controller setupCommissioningSessionWithPayload:payload newNodeID:@(kDeviceId) error:&error]);
XCTAssertNil(error);
XCTAssertEqual([XCTWaiter waitForExpectations:@[ pairingExpectation ] timeout:kPairingTimeoutInSeconds], XCTWaiterResultCompleted);

[self waitForExpectationsWithTimeout:kPairingTimeoutInSeconds handler:nil];
XCTestExpectation * expectation = [[XCTestExpectation alloc] initWithDescription:@"Wait for the commissioned device to be retrieved"];
WaitForCommissionee(expectation);
XCTAssertEqual([XCTWaiter waitForExpectations:@[ expectation ] timeout:kTimeoutInSeconds], XCTWaiterResultCompleted);
}

+ (void)shutdownStack
+ (void)tearDown
{
sNeedsStackShutdown = NO;
ResetCommissionee(GetConnectedDevice(), dispatch_get_main_queue(), nil, kTimeoutInSeconds);

MTRDeviceController * controller = sController;
XCTAssertNotNil(controller);

[controller shutdown];
XCTAssertFalse([controller isRunning]);

[[MTRDeviceControllerFactory sharedInstance] stopControllerFactory];
}

- (void)waitForCommissionee
{
XCTestExpectation * expectation = [self expectationWithDescription:@"Wait for the commissioned device to be retrieved"];

WaitForCommissionee(expectation);
[self waitForExpectationsWithTimeout:kTimeoutInSeconds handler:nil];
[super tearDown];
}

- (void)test000_SetUp
- (void)setUp
{
// Nothing to do here; our setUp method handled this already. This test
// just exists to make the setup not look like it's happening inside other
// tests.
[super setUp];
[self setContinueAfterFailure:NO];
}

- (void)test001_ReadAttribute
Expand Down Expand Up @@ -2864,12 +2826,6 @@ - (void)test030_DeviceAndClusterProperties
XCTAssertEqualObjects(cluster.endpointID, @(0));
}

- (void)test999_TearDown
{
ResetCommissionee(GetConnectedDevice(), dispatch_get_main_queue(), self, kTimeoutInSeconds);
[[self class] shutdownStack];
}

@end

@interface MTRDeviceEncoderTests : XCTestCase
Expand Down
Loading

0 comments on commit 63ba981

Please sign in to comment.