Skip to content

Commit

Permalink
feat(operation-queue): Add runSynchronized method to operationQueue
Browse files Browse the repository at this point in the history
SUITEDEV-36925

Co-authored-by: davidSchuppa <[email protected]>
  • Loading branch information
LasOri and davidSchuppa committed Nov 7, 2024
1 parent 930aed8 commit 972257b
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 25 deletions.
8 changes: 8 additions & 0 deletions EmarsysSDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@
5D3394D62C50E83A00E617E1 /* NSString+EMSCore.h in Headers */ = {isa = PBXBuildFile; fileRef = 5D3394D32C50E83A00E617E1 /* NSString+EMSCore.h */; };
5D3E118A2C77265B00D26BB1 /* EMSCompletionBlockProvider.m in Sources */ = {isa = PBXBuildFile; fileRef = 5D3E11892C77265B00D26BB1 /* EMSCompletionBlockProvider.m */; };
5D3E118B2C77265B00D26BB1 /* EMSCompletionBlockProvider.h in Headers */ = {isa = PBXBuildFile; fileRef = 5D3E11882C77265B00D26BB1 /* EMSCompletionBlockProvider.h */; };
5D7CF8FB2CDCEAEF00CC91C5 /* NSOperationQueue+EMSCore.m in Sources */ = {isa = PBXBuildFile; fileRef = 5D7CF8FA2CDCEAEF00CC91C5 /* NSOperationQueue+EMSCore.m */; };
5D7CF8FC2CDCEAEF00CC91C5 /* NSOperationQueue+EMSCore.h in Headers */ = {isa = PBXBuildFile; fileRef = 5D7CF8F92CDCEAEF00CC91C5 /* NSOperationQueue+EMSCore.h */; };
5D7EEE092BF4B4AB000E1558 /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = 5D7EEE082BF4B4AB000E1558 /* PrivacyInfo.xcprivacy */; };
5DF6FD1A2C6F392C0038790E /* EMSTestColumnInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 5DF6FD192C6F392C0038790E /* EMSTestColumnInfo.m */; };
5DF6FD1D2C6F39710038790E /* EMSTestColumnInfoMapper.m in Sources */ = {isa = PBXBuildFile; fileRef = 5DF6FD1C2C6F39710038790E /* EMSTestColumnInfoMapper.m */; };
Expand Down Expand Up @@ -898,6 +900,8 @@
5D3394D42C50E83A00E617E1 /* NSString+EMSCore.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "NSString+EMSCore.m"; sourceTree = "<group>"; };
5D3E11882C77265B00D26BB1 /* EMSCompletionBlockProvider.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = EMSCompletionBlockProvider.h; sourceTree = "<group>"; };
5D3E11892C77265B00D26BB1 /* EMSCompletionBlockProvider.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = EMSCompletionBlockProvider.m; sourceTree = "<group>"; };
5D7CF8F92CDCEAEF00CC91C5 /* NSOperationQueue+EMSCore.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "NSOperationQueue+EMSCore.h"; sourceTree = "<group>"; };
5D7CF8FA2CDCEAEF00CC91C5 /* NSOperationQueue+EMSCore.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "NSOperationQueue+EMSCore.m"; sourceTree = "<group>"; };
5D7EEE082BF4B4AB000E1558 /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = PrivacyInfo.xcprivacy; sourceTree = "<group>"; };
5DF6FD182C6F392C0038790E /* EMSTestColumnInfo.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = EMSTestColumnInfo.h; sourceTree = "<group>"; };
5DF6FD192C6F392C0038790E /* EMSTestColumnInfo.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = EMSTestColumnInfo.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1899,6 +1903,7 @@
5DF894CA2A812AED00803F2C /* Private */ = {
isa = PBXGroup;
children = (
5D7CF8F92CDCEAEF00CC91C5 /* NSOperationQueue+EMSCore.h */,
5D3394D32C50E83A00E617E1 /* NSString+EMSCore.h */,
8A6BE8AE23E86B480066B13C /* EMSOperationQueue.h */,
32C6F4D808CD54C437521E7B /* EMSQueueDelegator.h */,
Expand Down Expand Up @@ -2477,6 +2482,7 @@
8A6BE94923E86B480066B13C /* NSURLRequest+EMSCore.m */,
FB8A35934808E998DA538E1C /* NSHTTPURLResponse+EMSCore.m */,
5D3394D42C50E83A00E617E1 /* NSString+EMSCore.m */,
5D7CF8FA2CDCEAEF00CC91C5 /* NSOperationQueue+EMSCore.m */,
);
path = Categories;
sourceTree = "<group>";
Expand Down Expand Up @@ -3481,6 +3487,7 @@
32C6F38F9483E284AFBBEE82 /* EMSGeofenceResponse.h in Headers */,
32C6FCEEADE304159925A1D3 /* EMSGeofenceGroup.h in Headers */,
32C6F9B42C12B269BB77B1A0 /* EMSGeofenceResponseMapper.h in Headers */,
5D7CF8FC2CDCEAEF00CC91C5 /* NSOperationQueue+EMSCore.h in Headers */,
64259CC38D5008984B2BAC84 /* EMSGeofenceInternal.h in Headers */,
32C6F02AF6D5DDBBD4B8A739 /* EMSGeofenceProtocol.h in Headers */,
32C6F4C02D790A1A37384336 /* EMSLoggingGeofenceInternal.h in Headers */,
Expand Down Expand Up @@ -4047,6 +4054,7 @@
8A6BEADA23E86B480066B13C /* EMSScheduler.m in Sources */,
8A6BEB5923E86B490066B13C /* MEIAMTriggerMEEvent.m in Sources */,
8A6BEB3A23E86B490066B13C /* MERequestTools.m in Sources */,
5D7CF8FB2CDCEAEF00CC91C5 /* NSOperationQueue+EMSCore.m in Sources */,
8A6BEB8323E86B490066B13C /* EMSNotificationService+PushToInApp.m in Sources */,
8A6BEA7923E86B480066B13C /* EMSAbstractResponseHandler.m in Sources */,
8A6BEB7623E86B490066B13C /* MEInAppMessage.m in Sources */,
Expand Down
16 changes: 16 additions & 0 deletions Sources/Core/Categories/NSOperationQueue+EMSCore.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
////
//
// Copyright © 2024 Emarsys-Technologies Kft. All rights reserved.
//

#import "NSOperationQueue+EMSCore.h"

@implementation NSOperationQueue (EMSCore)

- (void)runSynchronized:(EMSRunnerBlock)runnerBlock {
NSOperation *operation = [NSBlockOperation blockOperationWithBlock:runnerBlock];
[self addOperations:@[operation]
waitUntilFinished:YES];
}

@end
19 changes: 7 additions & 12 deletions Sources/Core/Database/EMSSQLiteHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import "EMSSQLiteHelperSchemaHandlerProtocol.h"
#import "EMSStatusLog.h"
#import "EMSMacros.h"
#import "NSOperationQueue+EMSCore.h"

typedef BOOL (^EMSSQLLiteTransactionBlock)(void);

Expand Down Expand Up @@ -83,7 +84,7 @@ - (int)version {

- (void)open {
__weak typeof(self) weakSelf = self;
[self.operationQueue addOperationWithBlock:^{
[self.operationQueue runSynchronized:^{
sqlite3_initialize();
int sqlResult = sqlite3_open_v2([weakSelf.dbPath UTF8String], &self->_db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
if (sqlResult == SQLITE_OK) {
Expand All @@ -100,16 +101,14 @@ - (void)open {
}
}
}];
[self.operationQueue waitUntilAllOperationsAreFinished];
}

- (void)close {
__weak typeof(self) weakSelf = self;
[self.operationQueue addOperationWithBlock:^{
[self.operationQueue runSynchronized:^{
sqlite3_close_v2(weakSelf.db);
sqlite3_shutdown();
}];
[self.operationQueue waitUntilAllOperationsAreFinished];
}

- (void)registerTriggerWithTableName:(NSString *)tableName
Expand Down Expand Up @@ -167,7 +166,7 @@ - (BOOL)removeFromTable:(NSString *)tableName

__block BOOL success;
__weak typeof(self) weakSelf = self;
[self.operationQueue addOperationWithBlock:^{
[self.operationQueue runSynchronized:^{
success = [weakSelf executeTransaction:^BOOL{
BOOL result = YES;
sqlite3_stmt *statement;
Expand Down Expand Up @@ -203,7 +202,6 @@ - (BOOL)removeFromTable:(NSString *)tableName
return result;
}];
}];
[self.operationQueue waitUntilAllOperationsAreFinished];

[self runTriggerWithTableName:tableName
event:[EMSDBTriggerEvent deleteEvent]
Expand All @@ -225,7 +223,7 @@ - (NSArray *)queryWithTable:(NSString *)tableName
limit:limit];
__block NSMutableArray *models = [NSMutableArray new];
__weak typeof(self) weakSelf = self;
[self.operationQueue addOperationWithBlock:^{
[self.operationQueue runSynchronized:^{
[weakSelf executeTransaction:^BOOL{
BOOL result = YES;
sqlite3_stmt *statement;
Expand Down Expand Up @@ -266,7 +264,6 @@ - (NSArray *)queryWithTable:(NSString *)tableName
return result;
}];
}];
[self.operationQueue waitUntilAllOperationsAreFinished];
return [NSArray arrayWithArray:models];
}

Expand All @@ -275,7 +272,7 @@ - (BOOL)insertModel:(id)model
mapper:(id <EMSModelMapperProtocol>)mapper {
__block BOOL success;
__weak typeof(self) weakSelf = self;
[self.operationQueue addOperationWithBlock:^{
[self.operationQueue runSynchronized:^{
success = [weakSelf executeTransaction:^BOOL{
BOOL result = YES;
sqlite3_stmt *statement;
Expand Down Expand Up @@ -310,7 +307,6 @@ - (BOOL)insertModel:(id)model
return result;
}];
}];
[self.operationQueue waitUntilAllOperationsAreFinished];
return success;
}

Expand All @@ -336,7 +332,7 @@ - (NSArray *)executeQuery:(NSString *)query
mapper:(id <EMSModelMapperProtocol>)mapper {
__block NSMutableArray *models = [NSMutableArray new];
__weak typeof(self) weakSelf = self;
[self.operationQueue addOperationWithBlock:^{
[self.operationQueue runSynchronized:^{
[weakSelf executeTransaction:^BOOL{
BOOL result = YES;
sqlite3_stmt *statement;
Expand Down Expand Up @@ -372,7 +368,6 @@ - (NSArray *)executeQuery:(NSString *)query
return result;
}];
}];
[self.operationQueue waitUntilAllOperationsAreFinished];
return [NSArray arrayWithArray:models];
}

Expand Down
18 changes: 18 additions & 0 deletions Sources/Private/NSOperationQueue+EMSCore.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
////
//
// Copyright © 2024 Emarsys-Technologies Kft. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

typedef void (^EMSRunnerBlock)(void);

@interface NSOperationQueue (EMSCore)

- (void)runSynchronized:(EMSRunnerBlock)runnerBlock;

@end

NS_ASSUME_NONNULL_END
51 changes: 38 additions & 13 deletions Tests/CoreTests/Database/EMSSQLiteHelperTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#import "EMSTestColumnInfo.h"
#import "EMSTestColumnInfoMapper.h"
#import "XCTestCase+Helper.h"
#import "NSOperationQueue+EMSCore.h"

#define TEST_DB_PATH [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"TestDB.db"]

Expand All @@ -43,29 +44,42 @@ - (void)setUp {
}

- (void)tearDown {
[self.dbHelper executeCommand:@"PRAGMA user_version=1;"];
[EmarsysTestUtils clearDb:self.dbHelper];
__weak typeof(self) weakSelf = self;
[self.operationQueue runSynchronized:^{
[weakSelf.dbHelper executeCommand:@"PRAGMA user_version=1;"];
[EmarsysTestUtils clearDb:weakSelf.dbHelper];
}];
[super tearDown];
}

- (void)testGetVersion {
[self.dbHelper executeCommand:@"PRAGMA user_version=1;"];
XCTAssertEqual([self.dbHelper version], 1);
__weak typeof(self) weakSelf = self;
[self.operationQueue runSynchronized:^{
[weakSelf.dbHelper executeCommand:@"PRAGMA user_version=1;"];
XCTAssertEqual([weakSelf.dbHelper version], 1);
}];
}

- (void)testOpenDatabase {
[self.dbHelper executeCommand:@"PRAGMA user_version=0;"];
EMSSqliteSchemaHandler *schemaDelegate = OCMClassMock([EMSSqliteSchemaHandler class]);
__weak typeof(self) weakSelf = self;
[self.operationQueue runSynchronized:^{
[weakSelf.dbHelper executeCommand:@"PRAGMA user_version=0;"];
}];
self.dbHelper.schemaHandler = schemaDelegate;
OCMExpect([schemaDelegate onCreateWithDbHelper:OCMOCK_ANY]);


[self.dbHelper open];

OCMVerifyAll(schemaDelegate);
}

- (void)testOpenDatabaseWithUpgrade {
[self.dbHelper executeCommand:@"PRAGMA user_version=2;"];
__weak typeof(self) weakSelf = self;
[self.operationQueue runSynchronized:^{
[weakSelf.dbHelper executeCommand:@"PRAGMA user_version=2;"];
}];
EMSSqliteSchemaHandler *schemaDelegate = OCMClassMock([EMSSqliteSchemaHandler class]);
self.dbHelper.schemaHandler = schemaDelegate;
OCMStub([schemaDelegate schemaVersion]).andReturn(100);
Expand All @@ -77,8 +91,11 @@ - (void)testOpenDatabaseWithUpgrade {
}

- (void)testExecuteCommandFailure {
BOOL returnedValue = [self.dbHelper executeCommand:@"invalid sql;"];
XCTAssertFalse(returnedValue);
__weak typeof(self) weakSelf = self;
[self.operationQueue runSynchronized:^{
BOOL returnedValue = [weakSelf.dbHelper executeCommand:@"invalid sql;"];
XCTAssertFalse(returnedValue);
}];
}

- (void)testRemoveFromTableWithoutWhereClause {
Expand Down Expand Up @@ -221,14 +238,20 @@ - (void)testSchemaHandlerOnCreate {
[self isEqualArrays:expectedRequestColumnInfos currentArray:currentRequestColumnInfos];
[self isEqualArrays:expectedShardColumnInfos currentArray:currentShardColumnInfos];

XCTAssertEqual([self.dbHelper version], 1);
__weak typeof(self) weakSelf = self;
[self.operationQueue runSynchronized:^{
XCTAssertEqual([weakSelf.dbHelper version], 1);
}];
}

- (void)testSchemaMigration {
[self tearDown];
[self.dbHelper executeCommand:@"DROP TABLE shard;"];
[self.dbHelper executeCommand:@"DROP TABLE request;"];
[self.dbHelper executeCommand:@"PRAGMA user_version=0;"];
__weak typeof(self) weakSelf = self;
[self.operationQueue runSynchronized:^{
[weakSelf.dbHelper executeCommand:@"DROP TABLE shard;"];
[weakSelf.dbHelper executeCommand:@"DROP TABLE request;"];
[weakSelf.dbHelper executeCommand:@"PRAGMA user_version=0;"];
}];

NSArray<EMSTestColumnInfo *> *expectedRequestColumnInfos = @[
[[EMSTestColumnInfo alloc] initWithColumnName:@"request_id" columnType:@"TEXT"],
Expand Down Expand Up @@ -261,7 +284,9 @@ - (void)testSchemaMigration {
XCTAssertEqualObjects(expectedRequestColumnInfos, currentRequestColumnInfos);
XCTAssertEqualObjects(expectedShardColumnInfos, currentShardColumnInfos);

XCTAssertEqual([self.dbHelper version], 1);
[self.operationQueue runSynchronized:^{
XCTAssertEqual([weakSelf.dbHelper version], 1);
}];

}

Expand Down

0 comments on commit 972257b

Please sign in to comment.