Added test case `testSDCallbackQueue`

This commit is contained in:
DreamPiggy 2023-01-09 18:31:56 +08:00
parent 90eeb4d830
commit 43d74211c3
2 changed files with 41 additions and 2 deletions

View File

@ -15,7 +15,7 @@ typedef NS_ENUM(NSUInteger, SDCallbackPolicy) {
SDCallbackPolicySafeExecute = 0,
/// Follow async/sync using the correspond `dispatch_async`/`dispatch_sync` to dispatch block on queue
SDCallbackPolicyDispatch = 1,
/// Ignore any async/sync and just directly invoke `block` in current queue (without `dispatch_async/dispatch_sync`)
/// Ignore any async/sync and just directly invoke `block` in current queue (without `dispatch_async`/`dispatch_sync`)
SDCallbackPolicyInvoke = 2
};
@ -35,9 +35,11 @@ typedef NS_ENUM(NSUInteger, SDCallbackPolicy) {
/// The current queue's callback policy, defaults to `SDCallbackPolicySafeExecute`, which behaves like the old macro `dispatch_main_async_safe`
@property (assign, readwrite) SDCallbackPolicy policy;
- (nonnull instancetype)init NS_UNAVAILABLE;
+ (nonnull instancetype)new NS_UNAVAILABLE;
/// Create the callback queue with a GCD queue
/// - Parameter queue: The GCD queue, should not be NULL
- (nonnull instancetype)initWithDispatchQueue:(nonnull dispatch_queue_t)queue;
- (nonnull instancetype)initWithDispatchQueue:(nonnull dispatch_queue_t)queue NS_DESIGNATED_INITIALIZER;
#pragma mark - Execution Entry

View File

@ -154,6 +154,43 @@
expect(scaledImage.scale).equal(2);
}
- (void)testSDCallbackQueue {
XCTestExpectation *expectation1 = [self expectationWithDescription:@"SDCallbackQueue SafeExecute works"];
XCTestExpectation *expectation2 = [self expectationWithDescription:@"SDCallbackQueue Dispatch works"];
dispatch_queue_t queue = dispatch_queue_create("testSDCallbackQueue", NULL);
SDCallbackQueue *callbackQueue = [[SDCallbackQueue alloc] initWithDispatchQueue:queue];
__block BOOL called1 = NO;
[callbackQueue sync:^{
called1 = YES;
}];
expect(called1).beTruthy();
__block BOOL called2 = NO;
callbackQueue.policy = SDCallbackPolicySafeExecute;
dispatch_async(queue, ^{
// Should execute in sync
[callbackQueue async:^{
called2 = YES;
[expectation1 fulfill];
}];
expect(called2).beTruthy();
});
SDCallbackQueue *callbackQueue2 = [[SDCallbackQueue alloc] initWithDispatchQueue:queue];
__block BOOL called3 = NO;
callbackQueue2.policy = SDCallbackPolicyDispatch;
dispatch_async(queue, ^{
// Should execute in async
[callbackQueue2 async:^{
called3 = YES;
[expectation2 fulfill];
}];
expect(called3).beFalsy();
});
[self waitForExpectationsWithCommonTimeout];
}
- (void)testInternalMacro {
@weakify(self);
@onExit {