Skip to content

Commit

Permalink
Fix race condition in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ffried committed Apr 16, 2024
1 parent 2907d80 commit b6c1239
Showing 1 changed file with 10 additions and 25 deletions.
35 changes: 10 additions & 25 deletions Tests/CocoaLumberjackTests/DDAtomicCounterTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,13 @@ - (void)testMultithreadAtomicCounter {
__auto_type expectation = [self expectationWithDescription:@"Multithread atomic counter"];
__auto_type globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

NSInteger numberOfThreads = 5;
__block NSInteger executedCount = 0;
for (NSInteger i=0; i<numberOfThreads; i++) {
static NSInteger numberOfThreads = 5;
expectation.expectedFulfillmentCount = numberOfThreads;
for (NSInteger i = 0; i < numberOfThreads; i++) {
dispatch_async(globalQueue, ^{
[atomicCounter increment];
XCTAssertGreaterThanOrEqual([atomicCounter value], 1);
dispatch_async(dispatch_get_main_queue(), ^{
executedCount++;
if (executedCount == numberOfThreads) {
[expectation fulfill];
}
});
[expectation fulfill];
});
}

Expand All @@ -71,27 +66,17 @@ - (void)testMultithreadAtomicCounterWithIncrementAndDecrement {
__auto_type expectation = [self expectationWithDescription:@"Multithread atomic counter inc and dec"];
__auto_type globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

NSInteger numberOfThreads = 5;
__block NSInteger executedCount = 0;
for (NSInteger i=0; i<numberOfThreads; i++) {
static NSInteger numberOfThreads = 5;
expectation.expectedFulfillmentCount = numberOfThreads * 2;

for (NSInteger i = 0; i < numberOfThreads; i++) {
dispatch_async(globalQueue, ^{
[atomicCounter increment];

dispatch_async(dispatch_get_main_queue(), ^{
executedCount++;
if (executedCount == 2 * numberOfThreads) {
[expectation fulfill];
}
});
[expectation fulfill];
});
dispatch_async(globalQueue, ^{
[atomicCounter decrement];
dispatch_async(dispatch_get_main_queue(), ^{
executedCount++;
if (executedCount == 2 * numberOfThreads) {
[expectation fulfill];
}
});
[expectation fulfill];
});
}

Expand Down

0 comments on commit b6c1239

Please sign in to comment.