Skip to content

Commit 61a4ad8

Browse files
Fix Tsan for eDO channel tests.
PiperOrigin-RevId: 323894097
1 parent 6bcc078 commit 61a4ad8

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

Channel/Tests/EDOBlockingQueueTest.m

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ - (void)testAddAndFetchObjectsInOrderInDifferentQueue {
7878
}
7979

8080
- (void)testAddAndFetchObjectsConcurrently {
81+
dispatch_queue_t testQueue = dispatch_queue_create("com.google.edo.test", DISPATCH_QUEUE_SERIAL);
8182
EDOBlockingQueue<NSObject *> *blockingQueue = [[EDOBlockingQueue alloc] init];
8283
NSArray<NSObject *> *objects = [self objects];
8384

@@ -88,15 +89,20 @@ - (void)testAddAndFetchObjectsConcurrently {
8889
}
8990

9091
XCTestExpectation *expectFetchAll = [self expectationWithDescription:@"All objects are fetched."];
92+
expectFetchAll.expectedFulfillmentCount = objects.count;
9193
dispatch_async(dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0), ^{
9294
dispatch_apply(objects.count, dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0), ^(size_t idx) {
95+
NSObject *object;
9396
if (idx % 2) {
94-
fetchedObjects[idx] = [blockingQueue firstObjectWithTimeout:DISPATCH_TIME_FOREVER];
97+
object = [blockingQueue firstObjectWithTimeout:DISPATCH_TIME_FOREVER];
9598
} else {
96-
fetchedObjects[idx] = [blockingQueue lastObjectWithTimeout:DISPATCH_TIME_FOREVER];
99+
object = [blockingQueue lastObjectWithTimeout:DISPATCH_TIME_FOREVER];
97100
}
101+
dispatch_async(testQueue, ^{
102+
fetchedObjects[idx] = object;
103+
[expectFetchAll fulfill];
104+
});
98105
});
99-
[expectFetchAll fulfill];
100106
});
101107

102108
dispatch_apply(objects.count, dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0), ^(size_t idx) {

Channel/Tests/EDOChannelForwarderTest.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ - (void)testNoChannelAfterForwarderStop {
171171
}
172172

173173
- (void)testMultipleForwarderCanConnectMultiplexerAndForward {
174+
dispatch_queue_t testQueue = dispatch_queue_create("com.google.edo.test", DISPATCH_QUEUE_SERIAL);
174175
int numOfForwarders = 20;
175176
NS_VALID_UNTIL_END_OF_SCOPE NSMutableArray<id> *savedForwarders = [[NSMutableArray alloc] init];
176177
// Add the placeholders first.
@@ -187,7 +188,9 @@ - (void)testMultipleForwarderCanConnectMultiplexerAndForward {
187188

188189
XCTAssertNotNil([forwarder startWithErrorHandler:^(EDOForwarderError errorCode){
189190
}]);
190-
savedForwarders[idx] = forwarder;
191+
dispatch_async(testQueue, ^{
192+
savedForwarders[idx] = forwarder;
193+
});
191194
});
192195
});
193196

Channel/Tests/EDOChannelMultiplexerTest.m

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ - (void)testMultiplexerCanFetchForwardedChannelWithHostPort {
134134
}
135135

136136
- (void)testCanFetchChannelWithHostPortConcurrently {
137+
dispatch_queue_t testQueue = dispatch_queue_create("com.google.edo.test", DISPATCH_QUEUE_SERIAL);
137138
int numOfChannels = 200;
138139
EDOChannelMultiplexer *multiplexer = [[EDOChannelMultiplexer alloc] init];
139140
[multiplexer start:nil error:nil];
@@ -158,7 +159,9 @@ - (void)testCanFetchChannelWithHostPortConcurrently {
158159
XCTAssertEqualObjects(port, receivedPort);
159160
[channel sendData:deviceIdentifier withCompletionHandler:nil];
160161
}];
161-
savedChannels[idx] = channel;
162+
dispatch_async(testQueue, ^{
163+
savedChannels[idx] = channel;
164+
});
162165
});
163166
});
164167

@@ -175,6 +178,7 @@ - (void)testCanFetchChannelWithHostPortConcurrently {
175178
}
176179

177180
- (void)testCanFetchChannelWithBothCorrectAndIncorrectAckConcurrently {
181+
dispatch_queue_t testQueue = dispatch_queue_create("com.google.edo.test", DISPATCH_QUEUE_SERIAL);
178182
int numOfChannels = 30;
179183
EDOChannelMultiplexer *multiplexer = [[EDOChannelMultiplexer alloc] init];
180184
[multiplexer start:nil error:nil];
@@ -184,6 +188,7 @@ - (void)testCanFetchChannelWithBothCorrectAndIncorrectAckConcurrently {
184188
NSData *deviceInfo = [@"info" dataUsingEncoding:NSUTF8StringEncoding];
185189
NSData *incorrectAck = [@"deadbeef" dataUsingEncoding:NSUTF8StringEncoding];
186190
XCTestExpectation *expectChannelCreated = [self expectationWithDescription:@"Channels created."];
191+
expectChannelCreated.expectedFulfillmentCount = numOfChannels;
187192

188193
NS_VALID_UNTIL_END_OF_SCOPE NSMutableArray<id> *savedChannels = [[NSMutableArray alloc] init];
189194
// Add the placeholders first.
@@ -221,9 +226,11 @@ - (void)testCanFetchChannelWithBothCorrectAndIncorrectAckConcurrently {
221226
[channel sendData:deviceIdentifier withCompletionHandler:nil];
222227
}];
223228
}
224-
savedChannels[idx] = channel;
229+
dispatch_async(testQueue, ^{
230+
savedChannels[idx] = channel;
231+
[expectChannelCreated fulfill];
232+
});
225233
});
226-
[expectChannelCreated fulfill];
227234
});
228235

229236
int numOfBadChannels = (numOfChannels + 1) / 3;

0 commit comments

Comments
 (0)