Skip to content

Commit da8d550

Browse files
Address review comment.
Switch to just having app-starting functionality as a category on MTRTestCase. This is much easier to use.
1 parent 763487d commit da8d550

8 files changed

+68
-96
lines changed

src/darwin/Framework/CHIPTests/MTRCommissionableBrowserTests.m

+4-5
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,10 @@ + (void)setUp
184184
@"MT:Y.K90SO527JA0648G00",
185185
@"MT:-24J0AFN00I40648G00",
186186
]) {
187-
__auto_type * appRunner = [[MTRTestServerAppRunner alloc] initCrossTestWithAppName:@"all-clusters"
188-
arguments:@[]
189-
payload:payload
190-
testsuite:self];
191-
XCTAssertNotNil(appRunner);
187+
BOOL started = [self startAppWithName:@"all-clusters"
188+
arguments:@[]
189+
payload:payload];
190+
XCTAssertTrue(started);
192191
}
193192
}
194193

src/darwin/Framework/CHIPTests/MTROTAProviderTests.m

+4-3
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ - (NSString *)createImageFromRawImage:(NSString *)rawImage withVersion:(NSNumber
8080
- (MTRDevice *)commissionDeviceWithPayload:(NSString *)payloadString nodeID:(NSNumber *)nodeID;
8181
@end
8282

83-
@interface MTROTARequestorAppRunner : MTRTestServerAppRunner
83+
@interface MTROTARequestorAppRunner : NSObject
8484
@property (nonatomic, copy) NSString * downloadFilePath;
8585

8686
- (instancetype)initWithPayload:(NSString *)payload testcase:(MTROTAProviderTests *)testcase;
@@ -99,14 +99,15 @@ - (MTRDevice *)commissionWithNodeID:(NSNumber *)nodeID
9999

100100
- (instancetype)initWithPayload:(NSString *)payload testcase:(MTROTAProviderTests *)testcase
101101
{
102-
__auto_type * downloadFilePath = [NSString stringWithFormat:@"/tmp/chip-ota-requestor-downloaded-image%u", [MTRTestServerAppRunner nextUniqueIndex]];
102+
__auto_type * downloadFilePath = [NSString stringWithFormat:@"/tmp/chip-ota-requestor-downloaded-image%u", [MTROTAProviderTests nextUniqueIndex]];
103103
__auto_type * extraArguments = @[
104104
@"--otaDownloadPath",
105105
downloadFilePath,
106106
@"--autoApplyImage",
107107
];
108108

109-
if (!(self = [super initWithAppName:@"ota-requestor" arguments:extraArguments payload:payload testcase:testcase])) {
109+
BOOL started = [testcase startAppWithName:@"ota-requestor" arguments:extraArguments payload:payload];
110+
if (!started) {
110111
return nil;
111112
}
112113

src/darwin/Framework/CHIPTests/MTRPairingTests.m

+7-8
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,13 @@ - (void)tearDown
186186
- (void)startServerApp
187187
{
188188
// For manual testing, CASE retry code paths can be tested by adding --faults chip_CASEServerBusy_f1 (or similar)
189-
__auto_type * appRunner = [[MTRTestServerAppRunner alloc] initWithAppName:@"all-clusters"
190-
arguments:@[
191-
@"--dac_provider",
192-
[self absolutePathFor:@"credentials/development/commissioner_dut/struct_cd_origin_pid_vid_correct/test_case_vector.json"],
193-
]
194-
payload:kOnboardingPayload
195-
testcase:self];
196-
XCTAssertNotNil(appRunner);
189+
BOOL started = [self startAppWithName:@"all-clusters"
190+
arguments:@[
191+
@"--dac_provider",
192+
[self absolutePathFor:@"credentials/development/commissioner_dut/struct_cd_origin_pid_vid_correct/test_case_vector.json"],
193+
]
194+
payload:kOnboardingPayload];
195+
XCTAssertTrue(started);
197196
}
198197

199198
// attestationDelegate and failSafeExtension can both be nil

src/darwin/Framework/CHIPTests/MTRPerControllerStorageTests.m

+4-5
Original file line numberDiff line numberDiff line change
@@ -2437,11 +2437,10 @@ - (void)testSubscriptionPool
24372437
// Start our helper apps.
24382438
__auto_type * sortedKeys = [[deviceOnboardingPayloads allKeys] sortedArrayUsingSelector:@selector(compare:)];
24392439
for (NSNumber * deviceID in sortedKeys) {
2440-
__auto_type * appRunner = [[MTRTestServerAppRunner alloc] initWithAppName:@"all-clusters"
2441-
arguments:@[]
2442-
payload:deviceOnboardingPayloads[deviceID]
2443-
testcase:self];
2444-
XCTAssertNotNil(appRunner);
2440+
BOOL started = [self startAppWithName:@"all-clusters"
2441+
arguments:@[]
2442+
payload:deviceOnboardingPayloads[deviceID]];
2443+
XCTAssertTrue(started);
24452444
}
24462445

24472446
[self doTestSubscriptionPoolWithSize:1 deviceOnboardingPayloads:deviceOnboardingPayloads];

src/darwin/Framework/CHIPTests/TestHelpers/MTRTestCase.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ NS_ASSUME_NONNULL_BEGIN
5757
* Launch a cross-test task. The task will be automatically terminated when the testsuite
5858
* tearDown happens.
5959
*/
60-
+ (void)launchCrossTestTask:(NSTask *)task;
60+
+ (void)launchTask:(NSTask *)task;
6161
#endif // HAVE_NSTASK
6262

6363
/**

src/darwin/Framework/CHIPTests/TestHelpers/MTRTestCase.mm

+11-10
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,18 @@ - (void)tearDown
8585
}
8686

8787
#if HAVE_NSTASK
88+
- (NSTask *)createTaskForPath:(NSString *)path
89+
{
90+
return [self.class createTaskForPath:path];
91+
}
92+
8893
+ (NSTask *)createTaskForPath:(NSString *)path
8994
{
9095
NSTask * task = [[NSTask alloc] init];
9196
[task setLaunchPath:[self absolutePathFor:path]];
9297
return task;
9398
}
9499

95-
- (NSTask *)createTaskForPath:(NSString *)path
96-
{
97-
return [self.class createTaskForPath:path];
98-
}
99-
100100
- (void)runTask:(NSTask *)task
101101
{
102102
NSError * launchError;
@@ -121,14 +121,19 @@ - (void)launchTask:(NSTask *)task
121121
[_runningTasks addObject:task];
122122
}
123123

124-
+ (void)launchCrossTestTask:(NSTask *)task
124+
+ (void)launchTask:(NSTask *)task
125125
{
126126
[self doLaunchTask:task];
127127

128128
[runningCrossTestTasks addObject:task];
129129
}
130130
#endif // HAVE_NSTASK
131131

132+
- (NSString *)absolutePathFor:(NSString *)matterRootRelativePath
133+
{
134+
return [self.class absolutePathFor:matterRootRelativePath];
135+
}
136+
132137
+ (NSString *)absolutePathFor:(NSString *)matterRootRelativePath
133138
{
134139
// Start with the absolute path to our file, then remove the suffix that
@@ -140,8 +145,4 @@ + (NSString *)absolutePathFor:(NSString *)matterRootRelativePath
140145
return [NSString pathWithComponents:pathComponents];
141146
}
142147

143-
- (NSString *)absolutePathFor:(NSString *)matterRootRelativePath
144-
{
145-
return [self.class absolutePathFor:matterRootRelativePath];
146-
}
147148
@end

src/darwin/Framework/CHIPTests/TestHelpers/MTRTestServerAppRunner.h

+7-16
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,14 @@
1616

1717
#import <Foundation/Foundation.h>
1818

19-
@class MTRTestCase;
19+
#import "MTRTestCase.h"
2020

2121
NS_ASSUME_NONNULL_BEGIN
2222

23-
/**
24-
* A representation of a server application instance.
25-
*
26-
* Server applications are assumed to be compiled into out/debug/${APPNAME}-app,
27-
* with the binary being out/debug/${APPNAME}-app/chip-${APPNAME}-app.
28-
*/
29-
@interface MTRTestServerAppRunner : NSObject
23+
@interface MTRTestCase (ServerAppRunner)
3024

3125
/**
32-
* Initialize the app runner with the given app name, arguments, setup payload, and testcase
33-
* instance.
26+
* Start a server app with the given app name, arguments, and setup payload.
3427
*
3528
* The payload will be used to determine the discriminator and passcode
3629
* arguments the app should use, in addition to the provided arguments.
@@ -43,15 +36,13 @@ NS_ASSUME_NONNULL_BEGIN
4336
* subtracting 1111 from the discriminator and adding 5542 (so as not to collide
4437
* with any existing Matter things running on 5540/5541).
4538
*/
46-
- (instancetype)initWithAppName:(NSString *)name arguments:(NSArray<NSString *> *)arguments payload:(NSString *)payload testcase:(MTRTestCase *)testcase;
39+
- (BOOL)startAppWithName:(NSString *)name arguments:(NSArray<NSString *> *)arguments payload:(NSString *)payload;
4740

4841
/**
49-
* Same thing, but initialize as a "cross test" helper, which is not killed at
50-
* the end of the current test (but is killed at the end of the current suite).
51-
*
52-
* The passed-in testsuite must be a subclass of MTRTestCase.
42+
* Same thing, but the server will be killed at the end of the current suite,
43+
* and is not bound to a particular test in the suite.
5344
*/
54-
- (instancetype)initCrossTestWithAppName:(NSString *)name arguments:(NSArray<NSString *> *)arguments payload:(NSString *)payload testsuite:(Class)testsuite;
45+
+ (BOOL)startAppWithName:(NSString *)name arguments:(NSArray<NSString *> *)arguments payload:(NSString *)payload;
5546

5647
/**
5748
* Get the unique index that will be used for the next initialization. This

src/darwin/Framework/CHIPTests/TestHelpers/MTRTestServerAppRunner.m

+30-48
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
#import <Matter/Matter.h>
1818

19-
#import "MTRTestCase.h"
2019
#import "MTRTestServerAppRunner.h"
2120

2221
static unsigned sAppRunnerIndex = 1;
@@ -29,24 +28,12 @@
2928
static const uint16_t kBasePort = 5542 - kMinDiscriminator;
3029
#endif // HAVE_NSTASK
3130

32-
@implementation MTRTestServerAppRunner {
33-
unsigned _uniqueIndex;
34-
#if HAVE_NSTASK
35-
NSTask * _appTask;
36-
#endif
37-
}
31+
@implementation MTRTestCase (ServerAppRunner)
3832

39-
- (instancetype)initInternalWithAppName:(NSString *)name arguments:(NSArray<NSString *> *)arguments payload:(NSString *)payload
33+
#if HAVE_NSTASK
34+
+ (NSTask *)doStartAppWithName:(NSString *)name arguments:(NSArray<NSString *> *)arguments payload:(NSString *)payload
4035
{
41-
#if !HAVE_NSTASK
42-
XCTFail("Unable to start server app when we do not have NSTask");
43-
return nil;
44-
#else // HAVE_NSTASK
45-
if (!(self = [super init])) {
46-
return nil;
47-
}
48-
49-
_uniqueIndex = sAppRunnerIndex++;
36+
__auto_type uniqueIndex = sAppRunnerIndex++;
5037

5138
NSError * error;
5239
__auto_type * parsedPayload = [MTRSetupPayload setupPayloadWithOnboardingPayload:payload error:&error];
@@ -61,7 +48,7 @@ - (instancetype)initInternalWithAppName:(NSString *)name arguments:(NSArray<NSSt
6148
NSNumber * passcode = parsedPayload.setupPasscode;
6249

6350
__auto_type * executable = [NSString stringWithFormat:@"out/debug/%@-app/chip-%@-app", name, name];
64-
_appTask = [MTRTestCase createTaskForPath:executable];
51+
__auto_type * appTask = [self createTaskForPath:executable];
6552

6653
__auto_type * forcedArguments = @[
6754
// Make sure we only advertise on the local interface.
@@ -74,55 +61,50 @@ - (instancetype)initInternalWithAppName:(NSString *)name arguments:(NSArray<NSSt
7461
@"--passcode",
7562
[NSString stringWithFormat:@"%llu", passcode.unsignedLongLongValue],
7663
@"--KVS",
77-
[NSString stringWithFormat:@"/tmp/chip-%@-kvs%u", name, _uniqueIndex],
64+
[NSString stringWithFormat:@"/tmp/chip-%@-kvs%u", name, uniqueIndex],
7865
@"--product-id",
7966
[NSString stringWithFormat:@"%u", parsedPayload.productID.unsignedShortValue],
8067
];
8168

8269
__auto_type * allArguments = [forcedArguments arrayByAddingObjectsFromArray:arguments];
83-
[_appTask setArguments:allArguments];
70+
[appTask setArguments:allArguments];
8471

85-
NSString * outFile = [NSString stringWithFormat:@"/tmp/darwin/framework-tests/%@-app-%u.log", name, _uniqueIndex];
86-
NSString * errorFile = [NSString stringWithFormat:@"/tmp/darwin/framework-tests/%@-app-err-%u.log", name, _uniqueIndex];
72+
NSString * outFile = [NSString stringWithFormat:@"/tmp/darwin/framework-tests/%@-app-%u.log", name, uniqueIndex];
73+
NSString * errorFile = [NSString stringWithFormat:@"/tmp/darwin/framework-tests/%@-app-err-%u.log", name, uniqueIndex];
8774

8875
// Make sure the files exist.
8976
[[NSFileManager defaultManager] createFileAtPath:outFile contents:nil attributes:nil];
9077
[[NSFileManager defaultManager] createFileAtPath:errorFile contents:nil attributes:nil];
9178

92-
_appTask.standardOutput = [NSFileHandle fileHandleForWritingAtPath:outFile];
93-
_appTask.standardError = [NSFileHandle fileHandleForWritingAtPath:errorFile];
79+
appTask.standardOutput = [NSFileHandle fileHandleForWritingAtPath:outFile];
80+
appTask.standardError = [NSFileHandle fileHandleForWritingAtPath:errorFile];
9481

95-
NSLog(@"Started chip-%@-app (%@) with arguments %@ stdout=%@ and stderr=%@", name, _appTask, allArguments, outFile, errorFile);
82+
NSLog(@"Started chip-%@-app (%@) with arguments %@ stdout=%@ and stderr=%@", name, appTask, allArguments, outFile, errorFile);
9683

97-
return self;
98-
#endif // HAVE_NSTASK
84+
return appTask;
9985
}
86+
#endif // HAVE_NSTASK
10087

101-
- (instancetype)initWithAppName:(NSString *)name arguments:(NSArray<NSString *> *)arguments payload:(NSString *)payload testcase:(MTRTestCase *)testcase
88+
- (BOOL)startAppWithName:(NSString *)name arguments:(NSArray<NSString *> *)arguments payload:(NSString *)payload
10289
{
103-
if (!(self = [self initInternalWithAppName:name arguments:arguments payload:payload])) {
104-
return nil;
105-
}
106-
107-
[testcase launchTask:_appTask];
108-
109-
return self;
90+
#if !HAVE_NSTASK
91+
XCTFail("Unable to start server app when we do not have NSTask");
92+
return NO;
93+
#else
94+
[self launchTask:[self.class doStartAppWithName:name arguments:arguments payload:payload]];
95+
return YES;
96+
#endif // HAVE_NSTASK
11097
}
11198

112-
- (instancetype)initCrossTestWithAppName:(NSString *)name arguments:(NSArray<NSString *> *)arguments payload:(NSString *)payload testsuite:(Class)testsuite
99+
+ (BOOL)startAppWithName:(NSString *)name arguments:(NSArray<NSString *> *)arguments payload:(NSString *)payload
113100
{
114-
if (![testsuite isSubclassOfClass:MTRTestCase.class]) {
115-
NSLog(@"%@ is not a subclass of MTRTestCase", testsuite);
116-
return nil;
117-
}
118-
119-
if (!(self = [self initInternalWithAppName:name arguments:arguments payload:payload])) {
120-
return nil;
121-
}
122-
123-
[MTRTestCase launchCrossTestTask:_appTask];
124-
125-
return self;
101+
#if !HAVE_NSTASK
102+
XCTFail("Unable to start server app when we do not have NSTask");
103+
return NO;
104+
#else
105+
[self launchTask:[self doStartAppWithName:name arguments:arguments payload:payload]];
106+
return YES;
107+
#endif // HAVE_NSTASK
126108
}
127109

128110
+ (unsigned)nextUniqueIndex

0 commit comments

Comments
 (0)