libs-base/Tests/base/NSURLSession/NSRunLoop+TimeOutAdditions.h
rfm ed4e305026
NSURLSession rewrite (#422)
* clang-format: Do not use tabs

* Ignore clangd cache and compile_commands

* NSBlockOperation: Fix memory leak

* NSHTTPCookie: Fix expires date parsing

* NSHTTPCookie: Release DateFormatter after use

* NSOperation: Remove all objects at end of execution

* Reimplementation of NSURLSession

* NSURLSession: Update expiration dates in test

* Update ChangeLog

* Fix trivial compiler warning caused by missing import

* Import GSDispatch.h for definition of DISPATCH_QUEUE_SERIAL

* Import common.h early to avoid header conflict

* Fix import order to avoid conflicts and ensure we have correct localisation macro

* Check for presence of dispatch_cancel

* Cancel timer using dispatch_source_cancel() if dispatch_cancel() is missing.

* NSURLSession: Replace dispatch_io with dispatch_source in unit test HTTP server

---------

Co-authored-by: hmelder <service@hugomelder.com>
2024-08-16 14:08:41 +02:00

26 lines
No EOL
735 B
Objective-C

#import <Foundation/NSDate.h>
#import <Foundation/NSRunLoop.h>
@interface
NSRunLoop (TimeOutAdditions)
- (void)runForSeconds:(NSTimeInterval)seconds conditionBlock:(BOOL (^)())block;
@end
@implementation
NSRunLoop (TimeOutAdditions)
- (void)runForSeconds:(NSTimeInterval)seconds conditionBlock:(BOOL (^)())block
{
NSDate *startDate = [NSDate date];
NSTimeInterval endTime = [startDate timeIntervalSince1970] + seconds;
NSTimeInterval interval = 0.1; // Interval to check the condition
while (block() && [[NSDate date] timeIntervalSince1970] < endTime)
{
@autoreleasepool
{
[[NSRunLoop currentRunLoop]
runUntilDate:[NSDate dateWithTimeIntervalSinceNow:interval]];
}
}
}
@end