Skip to content

Instantly share code, notes, and snippets.

@levantAJ
Created December 11, 2017 01:19
Show Gist options
  • Save levantAJ/0c76bff8f48eb490910d2e45e6e99cb8 to your computer and use it in GitHub Desktop.
Save levantAJ/0c76bff8f48eb490910d2e45e6e99cb8 to your computer and use it in GitHub Desktop.
GCD queues and dispatch method were extracted to properties to facilitate unit testing
#import <XCTest/XCTest.h>
#import "Dispatcher.h"
#import <ReactiveObjC/ReactiveObjC.h>
@interface Dispatcher ()
@property(nonatomic, assign) void (*dispatchMethod)(dispatch_queue_t, dispatch_block_t);
@property(nonatomic, strong) dispatch_queue_t queue;
@end
@implementation Dispatcher
- (instancetype)initWithQueue:(dispatch_queue_t)queue dispatchMethod:(void(*)(dispatch_queue_t, dispatch_block_t))dispatchMethod {
self = [super init];
if (self) {
_queue = queue;
_dispatchMethod = dispatchMethod;
}
return self;
}
- (void)dispatch:(void(^)(void))block {
self.dispatchMethod(self.queue, block);
}
@end
@interface DispatcherTest : XCTestCase
@property (nonatomic, strong) Dispatcher *dispatcher;
@property (nonatomic, strong) dispatch_queue_t queue;
@property (nonatomic, assign) void (*dispatchMethod)(dispatch_queue_t, dispatch_block_t);
@end
@implementation DispatcherTest
static BOOL dispatchGetsCalled = NO;
void dispatchMethod(dispatch_queue_t queue, DISPATCH_NOESCAPE dispatch_block_t block) {
dispatchGetsCalled = YES;
block();
}
- (void)setUp {
[super setUp];
_queue = dispatch_queue_create("mock queue", NULL);
XCTAssertFalse(dispatchGetsCalled);
}
- (void)tearDown {
_dispatcher = nil;
_queue = nil;
_dispatchMethod = nil;
dispatchGetsCalled = NO;
[super tearDown];
}
- (void)testDispatchSync {
_dispatchMethod = &dispatch_sync;
_dispatcher = [[Dispatcher alloc] initWithQueue:self.queue dispatchMethod:self.dispatchMethod];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
@weakify(self);
[self.dispatcher dispatch:^{
@strongify(self);
[self assertQueue];
[expectation fulfill];
}];
[self waitForExpectations:@[expectation] timeout:0.1];
}
- (void)testDispatchAsync {
_dispatchMethod = &dispatch_async;
_dispatcher = [[Dispatcher alloc] initWithQueue:self.queue dispatchMethod:self.dispatchMethod];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
@weakify(self);
[self.dispatcher dispatch:^{
@strongify(self);
[self assertQueue];
[expectation fulfill];
}];
[self waitForExpectations:@[expectation] timeout:0.1];
}
- (void)testDispatcherUsesDispatchMethod {
_dispatchMethod = &dispatchMethod;
_dispatcher = [[Dispatcher alloc] initWithQueue:self.queue dispatchMethod:self.dispatchMethod];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
@weakify(self);
[self.dispatcher dispatch:^{
@strongify(self);
XCTAssertTrue(dispatchGetsCalled);
[expectation fulfill];
}];
[self waitForExpectations:@[expectation] timeout:0.1];
}
#pragma mark - Privates
- (void)assertQueue {
const char *dispatchQueueName = dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL);
const char *queueName = dispatch_queue_get_label(self.queue);
int comparison = 1; //default int value is 0
comparison = strcmp(dispatchQueueName, queueName);
XCTAssertTrue(comparison == 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment