From 00d1a80b316287eafb1610dd7310c512e5008041 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Tue, 24 May 2022 17:48:55 +0200 Subject: [PATCH 01/13] Failing tests for number of threads created by DatabasePool --- GRDB.xcodeproj/project.pbxproj | 38 ++++++++- GRDBCustom.xcodeproj/project.pbxproj | 26 +++++- Package.swift | 1 + .../GRDBTests.xcodeproj/project.pbxproj | 22 ++++- .../GRDBTests.xcodeproj/project.pbxproj | 22 ++++- Tests/GRDBTests/DatabasePoolTests.swift | 84 +++++++++++++++++++ Tests/GRDBTests/GRDBTests-Bridging-Header.h | 5 ++ Tests/GRDBTests/getThreadsCount.c | 23 +++++ Tests/GRDBTests/getThreadsCount.h | 7 ++ 9 files changed, 216 insertions(+), 12 deletions(-) create mode 100644 Tests/GRDBTests/GRDBTests-Bridging-Header.h create mode 100644 Tests/GRDBTests/getThreadsCount.c create mode 100644 Tests/GRDBTests/getThreadsCount.h diff --git a/GRDB.xcodeproj/project.pbxproj b/GRDB.xcodeproj/project.pbxproj index 683b990e3..d0586f79c 100755 --- a/GRDB.xcodeproj/project.pbxproj +++ b/GRDB.xcodeproj/project.pbxproj @@ -924,6 +924,9 @@ 56F5ABDA1D814330001F60CB /* NSData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5657AAB81D107001006283EF /* NSData.swift */; }; 56F5ABDC1D814330001F60CB /* URL.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5657AB0E1D10899D006283EF /* URL.swift */; }; 56F5ABDD1D814330001F60CB /* UUID.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A8C22F1D1914540096E9D4 /* UUID.swift */; }; + 56F61DD5283D344E00AF9884 /* getThreadsCount.c in Sources */ = {isa = PBXBuildFile; fileRef = 56F61DD4283D344E00AF9884 /* getThreadsCount.c */; }; + 56F61DD6283D344E00AF9884 /* getThreadsCount.c in Sources */ = {isa = PBXBuildFile; fileRef = 56F61DD4283D344E00AF9884 /* getThreadsCount.c */; }; + 56F61DD7283D344E00AF9884 /* getThreadsCount.c in Sources */ = {isa = PBXBuildFile; fileRef = 56F61DD4283D344E00AF9884 /* getThreadsCount.c */; }; 56FBFED92210731A00945324 /* SQLRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56FBFED82210731A00945324 /* SQLRequest.swift */; }; 56FBFEDA2210731A00945324 /* SQLRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56FBFED82210731A00945324 /* SQLRequest.swift */; }; 56FBFEDB2210731A00945324 /* SQLRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56FBFED82210731A00945324 /* SQLRequest.swift */; }; @@ -1646,6 +1649,9 @@ 56F34FB924B094B6007513FC /* SQLExpressionIsConstantTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SQLExpressionIsConstantTests.swift; sourceTree = ""; }; 56F34FC124B0A0B7007513FC /* SQLIdentifyingColumnsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SQLIdentifyingColumnsTests.swift; sourceTree = ""; }; 56F3E7481E66F83A00BF0F01 /* ResultCodeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ResultCodeTests.swift; sourceTree = ""; }; + 56F61DD0283D344D00AF9884 /* GRDBTests-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "GRDBTests-Bridging-Header.h"; path = "GRDBTests/GRDBTests-Bridging-Header.h"; sourceTree = ""; }; + 56F61DD3283D344E00AF9884 /* getThreadsCount.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = getThreadsCount.h; sourceTree = ""; }; + 56F61DD4283D344E00AF9884 /* getThreadsCount.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = getThreadsCount.c; sourceTree = ""; }; 56FBFED82210731A00945324 /* SQLRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SQLRequest.swift; sourceTree = ""; }; 56FDECE11BB32DFD009AD709 /* RowFromStatementTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowFromStatementTests.swift; sourceTree = ""; }; 56FEB8F6248402E10081AF83 /* DatabaseTraceTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DatabaseTraceTests.swift; sourceTree = ""; }; @@ -1796,6 +1802,8 @@ 569531361C919DF700CF1A2B /* DatabasePoolFunctionTests.swift */, 56EA869D1C932597002BB4DF /* DatabasePoolReadOnlyTests.swift */, 563C67B224628BEA00E94EDC /* DatabasePoolTests.swift */, + 56F61DD3283D344E00AF9884 /* getThreadsCount.h */, + 56F61DD4283D344E00AF9884 /* getThreadsCount.c */, ); name = DatabasePool; sourceTree = ""; @@ -2502,6 +2510,7 @@ DC37740319C8CBB3004FCF85 /* Supporting Files */ = { isa = PBXGroup; children = ( + 56F61DD0283D344D00AF9884 /* GRDBTests-Bridging-Header.h */, DC37740419C8CBB3004FCF85 /* Info.plist */, ); name = "Supporting Files"; @@ -2752,17 +2761,18 @@ }; 56E5D7D21B4D3FEE00430942 = { CreatedOnToolsVersion = 7.0; - LastSwiftMigration = 1130; + LastSwiftMigration = 1340; ProvisioningStyle = Manual; }; 56E5D7F81B4D422D00430942 = { CreatedOnToolsVersion = 7.0; - LastSwiftMigration = 1020; + LastSwiftMigration = 1340; }; AAA4DC75230F1E0600C74B15 = { ProvisioningStyle = Manual; }; AAA4DD07230F262000C74B15 = { + LastSwiftMigration = 1340; ProvisioningStyle = Manual; }; DC3773F219C8CBB3004FCF85 = { @@ -3432,6 +3442,7 @@ 56A2383A1B9C74A90082EB20 /* DatabaseQueueInMemoryTests.swift in Sources */, 56A5E40A1BA2BCF900707640 /* RecordWithColumnNameManglingTests.swift in Sources */, 56677C1A241D217F0050755D /* ValueObservationRecorderTests.swift in Sources */, + 56F61DD6283D344E00AF9884 /* getThreadsCount.c in Sources */, 5653EAF520944B4F00F46237 /* AssociationParallelSQLTests.swift in Sources */, 563B06BE2185CCD300B38F35 /* ValueObservationTests.swift in Sources */, 56FF455A1D2CDA5200F21EF9 /* RecordUniqueIndexTests.swift in Sources */, @@ -3667,6 +3678,7 @@ 56DF001D228DDBA300D611F3 /* AssociationPrefetchingCodableRecordTests.swift in Sources */, 5653EAD820944B4F00F46237 /* AssociationBelongsToRowScopeTests.swift in Sources */, 562205F11E420E47005860AC /* DatabasePoolReleaseMemoryTests.swift in Sources */, + 56F61DD5283D344E00AF9884 /* getThreadsCount.c in Sources */, 56677C19241D217F0050755D /* ValueObservationRecorderTests.swift in Sources */, 5653EAF420944B4F00F46237 /* AssociationParallelSQLTests.swift in Sources */, 563B06BD2185CCD300B38F35 /* ValueObservationTests.swift in Sources */, @@ -4038,6 +4050,7 @@ AAA4DDC0230F262000C74B15 /* DatabaseQueueInMemoryTests.swift in Sources */, AAA4DDC1230F262000C74B15 /* RecordWithColumnNameManglingTests.swift in Sources */, 56677C1B241D217F0050755D /* ValueObservationRecorderTests.swift in Sources */, + 56F61DD7283D344E00AF9884 /* getThreadsCount.c in Sources */, AAA4DDC2230F262000C74B15 /* AssociationParallelSQLTests.swift in Sources */, AAA4DDC3230F262000C74B15 /* ValueObservationTests.swift in Sources */, AAA4DDC4230F262000C74B15 /* RecordUniqueIndexTests.swift in Sources */, @@ -4382,6 +4395,7 @@ isa = XCBuildConfiguration; baseConfigurationReference = 56C494401ED7255500CC72AF /* GRDBDeploymentTarget.xcconfig */; buildSettings = { + CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_WEAK = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; CODE_SIGN_STYLE = Manual; @@ -4400,6 +4414,9 @@ PROVISIONING_PROFILE_SPECIFIER = ""; "PROVISIONING_PROFILE_SPECIFIER[sdk=macosx*]" = ""; SDKROOT = iphoneos; + SWIFT_OBJC_BRIDGING_HEADER = "Tests/GRDBTests/GRDBTests-Bridging-Header.h"; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 5.0; }; name = Debug; }; @@ -4407,6 +4424,7 @@ isa = XCBuildConfiguration; baseConfigurationReference = 56C494401ED7255500CC72AF /* GRDBDeploymentTarget.xcconfig */; buildSettings = { + CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_WEAK = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; CODE_SIGN_STYLE = Manual; @@ -4427,7 +4445,9 @@ "PROVISIONING_PROFILE_SPECIFIER[sdk=macosx*]" = ""; SDKROOT = iphoneos; SWIFT_COMPILATION_MODE = wholemodule; + SWIFT_OBJC_BRIDGING_HEADER = "Tests/GRDBTests/GRDBTests-Bridging-Header.h"; SWIFT_OPTIMIZATION_LEVEL = "-O"; + SWIFT_VERSION = 5.0; }; name = Release; }; @@ -4435,6 +4455,7 @@ isa = XCBuildConfiguration; baseConfigurationReference = 56C494401ED7255500CC72AF /* GRDBDeploymentTarget.xcconfig */; buildSettings = { + CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_WEAK = YES; COMBINE_HIDPI_IMAGES = YES; DEBUG_INFORMATION_FORMAT = dwarf; @@ -4449,6 +4470,9 @@ PRODUCT_BUNDLE_IDENTIFIER = com.github.groue.GRDBOSXTests; PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = macosx; + SWIFT_OBJC_BRIDGING_HEADER = "Tests/GRDBTests/GRDBTests-Bridging-Header.h"; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 5.0; }; name = Debug; }; @@ -4456,6 +4480,7 @@ isa = XCBuildConfiguration; baseConfigurationReference = 56C494401ED7255500CC72AF /* GRDBDeploymentTarget.xcconfig */; buildSettings = { + CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_WEAK = YES; COMBINE_HIDPI_IMAGES = YES; COPY_PHASE_STRIP = NO; @@ -4472,7 +4497,9 @@ PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = macosx; SWIFT_COMPILATION_MODE = wholemodule; + SWIFT_OBJC_BRIDGING_HEADER = "Tests/GRDBTests/GRDBTests-Bridging-Header.h"; SWIFT_OPTIMIZATION_LEVEL = "-O"; + SWIFT_VERSION = 5.0; }; name = Release; }; @@ -4544,6 +4571,7 @@ isa = XCBuildConfiguration; baseConfigurationReference = 56C494401ED7255500CC72AF /* GRDBDeploymentTarget.xcconfig */; buildSettings = { + CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_WEAK = YES; CODE_SIGN_IDENTITY = "iPhone Developer"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; @@ -4562,6 +4590,9 @@ PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = ""; SDKROOT = appletvos; + SWIFT_OBJC_BRIDGING_HEADER = "Tests/GRDBTests/GRDBTests-Bridging-Header.h"; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 5.0; }; name = Debug; }; @@ -4569,6 +4600,7 @@ isa = XCBuildConfiguration; baseConfigurationReference = 56C494401ED7255500CC72AF /* GRDBDeploymentTarget.xcconfig */; buildSettings = { + CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_WEAK = YES; CODE_SIGN_IDENTITY = "iPhone Developer"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; @@ -4589,7 +4621,9 @@ PROVISIONING_PROFILE_SPECIFIER = ""; SDKROOT = appletvos; SWIFT_COMPILATION_MODE = wholemodule; + SWIFT_OBJC_BRIDGING_HEADER = "Tests/GRDBTests/GRDBTests-Bridging-Header.h"; SWIFT_OPTIMIZATION_LEVEL = "-O"; + SWIFT_VERSION = 5.0; }; name = Release; }; diff --git a/GRDBCustom.xcodeproj/project.pbxproj b/GRDBCustom.xcodeproj/project.pbxproj index fbc0e9cd4..f93bf87f7 100755 --- a/GRDBCustom.xcodeproj/project.pbxproj +++ b/GRDBCustom.xcodeproj/project.pbxproj @@ -535,6 +535,8 @@ 56F34FC724B0A0C9007513FC /* SQLIdentifyingColumnsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56F34FC524B0A0C8007513FC /* SQLIdentifyingColumnsTests.swift */; }; 56F3E74C1E66F83A00BF0F01 /* ResultCodeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56F3E7481E66F83A00BF0F01 /* ResultCodeTests.swift */; }; 56F3E7501E66F83A00BF0F01 /* ResultCodeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56F3E7481E66F83A00BF0F01 /* ResultCodeTests.swift */; }; + 56F61DEA283D469F00AF9884 /* getThreadsCount.c in Sources */ = {isa = PBXBuildFile; fileRef = 56F61DE8283D469F00AF9884 /* getThreadsCount.c */; }; + 56F61DEB283D469F00AF9884 /* getThreadsCount.c in Sources */ = {isa = PBXBuildFile; fileRef = 56F61DE8283D469F00AF9884 /* getThreadsCount.c */; }; 56FBFED62210731100945324 /* SQLRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56FBFED52210731000945324 /* SQLRequest.swift */; }; 56FBFED72210731100945324 /* SQLRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56FBFED52210731000945324 /* SQLRequest.swift */; }; 56FEB8FE248403270081AF83 /* DatabaseTraceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56FEB8FC248403270081AF83 /* DatabaseTraceTests.swift */; }; @@ -1133,6 +1135,8 @@ 56F34FBD24B094D1007513FC /* SQLExpressionIsConstantTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SQLExpressionIsConstantTests.swift; sourceTree = ""; }; 56F34FC524B0A0C8007513FC /* SQLIdentifyingColumnsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLIdentifyingColumnsTests.swift; sourceTree = ""; }; 56F3E7481E66F83A00BF0F01 /* ResultCodeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ResultCodeTests.swift; sourceTree = ""; }; + 56F61DE8283D469F00AF9884 /* getThreadsCount.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = getThreadsCount.c; sourceTree = ""; }; + 56F61DE9283D469F00AF9884 /* getThreadsCount.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = getThreadsCount.h; sourceTree = ""; }; 56FBFED52210731000945324 /* SQLRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SQLRequest.swift; sourceTree = ""; }; 56FDECE11BB32DFD009AD709 /* RowFromStatementTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowFromStatementTests.swift; sourceTree = ""; }; 56FEB8FC248403270081AF83 /* DatabaseTraceTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseTraceTests.swift; sourceTree = ""; }; @@ -1262,6 +1266,8 @@ 569531361C919DF700CF1A2B /* DatabasePoolFunctionTests.swift */, 56EA869D1C932597002BB4DF /* DatabasePoolReadOnlyTests.swift */, 563C67B624628C0C00E94EDC /* DatabasePoolTests.swift */, + 56F61DE8283D469F00AF9884 /* getThreadsCount.c */, + 56F61DE9283D469F00AF9884 /* getThreadsCount.h */, ); name = DatabasePool; sourceTree = ""; @@ -2138,7 +2144,7 @@ }; F3BA80401CFB2AD7003DC1BA = { CreatedOnToolsVersion = 7.3.1; - LastSwiftMigration = 1020; + LastSwiftMigration = 1340; }; F3BA80591CFB2BB2003DC1BA = { CreatedOnToolsVersion = 7.3.1; @@ -2146,7 +2152,7 @@ }; F3BA809A1CFB2F6F003DC1BA = { CreatedOnToolsVersion = 7.3.1; - LastSwiftMigration = 1020; + LastSwiftMigration = 1340; }; }; }; @@ -2558,6 +2564,7 @@ 56419C8724A51D6F004967E1 /* ValueObservationPublisherTests.swift in Sources */, 560432A7228F167A009D3FE2 /* AssociationPrefetchingObservationTests.swift in Sources */, 5690C33E1D23E7D200E59934 /* FoundationDateTests.swift in Sources */, + 56F61DEB283D469F00AF9884 /* getThreadsCount.c in Sources */, 567A805A1D41350C00C7DCEC /* IndexInfoTests.swift in Sources */, 5674A7291F30A9090095F066 /* FetchableRecordDecodableTests.swift in Sources */, 56057C5A2291B18E00A7CB10 /* AssociationHasManyRowScopeTests.swift in Sources */, @@ -2928,6 +2935,7 @@ 56419C8224A51D6E004967E1 /* ValueObservationPublisherTests.swift in Sources */, 560432A6228F167A009D3FE2 /* AssociationPrefetchingObservationTests.swift in Sources */, 5698AC061D9B9FCF0056AF8C /* QueryInterfaceExtensibilityTests.swift in Sources */, + 56F61DEA283D469F00AF9884 /* getThreadsCount.c in Sources */, F3BA80FA1CFB3021003DC1BA /* SelectStatementTests.swift in Sources */, 5674A7271F30A9090095F066 /* FetchableRecordDecodableTests.swift in Sources */, 56057C592291B18E00A7CB10 /* AssociationHasManyRowScopeTests.swift in Sources */, @@ -3237,6 +3245,7 @@ baseConfigurationReference = F3BA813B1CFB3770003DC1BA /* GRDBCustomSQLite-Testing.xcconfig */; buildSettings = { CLANG_ANALYZER_NONNULL = YES; + CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_WEAK = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; DEBUG_INFORMATION_FORMAT = dwarf; @@ -3250,6 +3259,9 @@ PRODUCT_BUNDLE_IDENTIFIER = com.github.groue.GRDBCustomSQLite.GRDBCustomSQLiteiOSTests; PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = iphoneos; + SWIFT_OBJC_BRIDGING_HEADER = "Tests/GRDBTests/GRDBTests-Bridging-Header.h"; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 5.0; }; name = Debug; }; @@ -3258,6 +3270,7 @@ baseConfigurationReference = F3BA813B1CFB3770003DC1BA /* GRDBCustomSQLite-Testing.xcconfig */; buildSettings = { CLANG_ANALYZER_NONNULL = YES; + CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_WEAK = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; COPY_PHASE_STRIP = NO; @@ -3273,7 +3286,9 @@ PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = iphoneos; SWIFT_COMPILATION_MODE = wholemodule; + SWIFT_OBJC_BRIDGING_HEADER = "Tests/GRDBTests/GRDBTests-Bridging-Header.h"; SWIFT_OPTIMIZATION_LEVEL = "-O"; + SWIFT_VERSION = 5.0; }; name = Release; }; @@ -3339,6 +3354,7 @@ baseConfigurationReference = F3BA813B1CFB3770003DC1BA /* GRDBCustomSQLite-Testing.xcconfig */; buildSettings = { CLANG_ANALYZER_NONNULL = YES; + CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_WEAK = YES; CODE_SIGN_IDENTITY = "-"; COMBINE_HIDPI_IMAGES = YES; @@ -3353,6 +3369,9 @@ PRODUCT_BUNDLE_IDENTIFIER = com.github.groue.GRDBCustomSQLiteOSXTests; PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = macosx; + SWIFT_OBJC_BRIDGING_HEADER = "Tests/GRDBTests/GRDBTests-Bridging-Header.h"; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 5.0; }; name = Debug; }; @@ -3361,6 +3380,7 @@ baseConfigurationReference = F3BA813B1CFB3770003DC1BA /* GRDBCustomSQLite-Testing.xcconfig */; buildSettings = { CLANG_ANALYZER_NONNULL = YES; + CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_WEAK = YES; CODE_SIGN_IDENTITY = "-"; COMBINE_HIDPI_IMAGES = YES; @@ -3377,7 +3397,9 @@ PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = macosx; SWIFT_COMPILATION_MODE = wholemodule; + SWIFT_OBJC_BRIDGING_HEADER = "Tests/GRDBTests/GRDBTests-Bridging-Header.h"; SWIFT_OPTIMIZATION_LEVEL = "-O"; + SWIFT_VERSION = 5.0; }; name = Release; }; diff --git a/Package.swift b/Package.swift index 3a95b85ba..c93a39597 100644 --- a/Package.swift +++ b/Package.swift @@ -35,6 +35,7 @@ let package = Package( "Crash", "Performance", "SPM", + "GRDBTests/getThreadsCount.c", ]) ], swiftLanguageVersions: [.v5] diff --git a/Tests/CocoaPods/SQLCipher3/GRDBTests.xcodeproj/project.pbxproj b/Tests/CocoaPods/SQLCipher3/GRDBTests.xcodeproj/project.pbxproj index ae433327c..239d10277 100644 --- a/Tests/CocoaPods/SQLCipher3/GRDBTests.xcodeproj/project.pbxproj +++ b/Tests/CocoaPods/SQLCipher3/GRDBTests.xcodeproj/project.pbxproj @@ -447,6 +447,8 @@ 565A27CD27871FFF00659A62 /* BackupTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 565A27CB27871FFF00659A62 /* BackupTestCase.swift */; }; 5691D97527257CE40021D540 /* AvailableElements.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5691D97427257CE40021D540 /* AvailableElements.swift */; }; 5691D97627257CE40021D540 /* AvailableElements.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5691D97427257CE40021D540 /* AvailableElements.swift */; }; + 56F61DF0283D484700AF9884 /* getThreadsCount.c in Sources */ = {isa = PBXBuildFile; fileRef = 56F61DEE283D484700AF9884 /* getThreadsCount.c */; }; + 56F61DF1283D484700AF9884 /* getThreadsCount.c in Sources */ = {isa = PBXBuildFile; fileRef = 56F61DEE283D484700AF9884 /* getThreadsCount.c */; }; 5B33E6E34F941B4C839A714F /* (null) in Frameworks */ = {isa = PBXBuildFile; }; 98AB0B01EB11B33719AE412E /* Pods_GRDBTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FE2436BF42B9FCD6552E7076 /* Pods_GRDBTests.framework */; }; F2B3C4250D67969FF3948955 /* Pods_GRDBTestsEncrypted.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7D5C7999E7D9CE7145687F5D /* Pods_GRDBTestsEncrypted.framework */; }; @@ -678,6 +680,9 @@ 564A2156226B8E18001F64F1 /* GRDBTestsEncrypted.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = GRDBTestsEncrypted.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 565A27CB27871FFF00659A62 /* BackupTestCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BackupTestCase.swift; sourceTree = ""; }; 5691D97427257CE40021D540 /* AvailableElements.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AvailableElements.swift; sourceTree = ""; }; + 56F61DEC283D484700AF9884 /* GRDBTests-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "GRDBTests-Bridging-Header.h"; sourceTree = ""; }; + 56F61DEE283D484700AF9884 /* getThreadsCount.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = getThreadsCount.c; sourceTree = ""; }; + 56F61DEF283D484700AF9884 /* getThreadsCount.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = getThreadsCount.h; sourceTree = ""; }; 6A4788C0F815F6C5E4EBDE12 /* Pods-GRDBTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-GRDBTests.debug.xcconfig"; path = "Target Support Files/Pods-GRDBTests/Pods-GRDBTests.debug.xcconfig"; sourceTree = ""; }; 7D5C7999E7D9CE7145687F5D /* Pods_GRDBTestsEncrypted.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_GRDBTestsEncrypted.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 83BFB5733A86DAA3D0BEE684 /* Pods-GRDBTestsEncrypted.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-GRDBTestsEncrypted.debug.xcconfig"; path = "Target Support Files/Pods-GRDBTestsEncrypted/Pods-GRDBTestsEncrypted.debug.xcconfig"; sourceTree = ""; }; @@ -768,6 +773,9 @@ 564A1F1F226B876D001F64F1 /* GRDBTests */ = { isa = PBXGroup; children = ( + 56F61DEE283D484700AF9884 /* getThreadsCount.c */, + 56F61DEF283D484700AF9884 /* getThreadsCount.h */, + 56F61DEC283D484700AF9884 /* GRDBTests-Bridging-Header.h */, 564A1F6F226B89D6001F64F1 /* Betty.jpeg */, 56419CDA24A54058004967E1 /* InflectionsTests.json */, 56419CE724A54058004967E1 /* AnyCursorTests.swift */, @@ -900,11 +908,9 @@ 56419D0024A5405A004967E1 /* JoinSupportTests.swift */, 56419CDC24A54058004967E1 /* MapCursorTests.swift */, 56419D0224A5405A004967E1 /* MutablePersistableRecordChangesTests.swift */, - 56419D3A24A5405F004967E1 /* TableRecordDeleteTests.swift */, 56419D2F24A5405E004967E1 /* MutablePersistableRecordEncodableTests.swift */, 56419D4424A5405F004967E1 /* MutablePersistableRecordPersistenceConflictPolicyTests.swift */, 56419D3B24A5405F004967E1 /* MutablePersistableRecordTests.swift */, - 56419CD224A54057004967E1 /* TableRecordUpdateTests.swift */, 56419CFD24A5405A004967E1 /* NumericOverflowTests.swift */, 56419D4F24A54060004967E1 /* OrderedDictionaryTests.swift */, 56419D4524A5405F004967E1 /* PersistableRecordTests.swift */, @@ -952,7 +958,9 @@ 56419D6124A54062004967E1 /* StatementColumnConvertibleFetchTests.swift */, 56419D5724A54061004967E1 /* TableDefinitionTests.swift */, 56419D4A24A54060004967E1 /* TableRecord+QueryInterfaceRequestTests.swift */, + 56419D3A24A5405F004967E1 /* TableRecordDeleteTests.swift */, 56419CA224A54054004967E1 /* TableRecordTests.swift */, + 56419CD224A54057004967E1 /* TableRecordUpdateTests.swift */, 56419CB624A54055004967E1 /* TransactionObserverSavepointsTests.swift */, 56419D0924A5405B004967E1 /* TransactionObserverTests.swift */, 56419D2224A5405D004967E1 /* TruncateOptimizationTests.swift */, @@ -1051,10 +1059,10 @@ TargetAttributes = { 564A1F1C226B876D001F64F1 = { CreatedOnToolsVersion = 10.2.1; - LastSwiftMigration = 1150; + LastSwiftMigration = 1340; }; 564A2095226B8E18001F64F1 = { - LastSwiftMigration = 1150; + LastSwiftMigration = 1340; }; }; }; @@ -1243,6 +1251,7 @@ 56419EAB24A54063004967E1 /* DatabaseValueTests.swift in Sources */, 56419D7724A54062004967E1 /* DatabaseCursorTests.swift in Sources */, 56419E1524A54062004967E1 /* AssociationChainRowScopesTests.swift in Sources */, + 56F61DF0283D484700AF9884 /* getThreadsCount.c in Sources */, 56419EC724A54063004967E1 /* RecordPrimaryKeyMultipleTests.swift in Sources */, 56419DF724A54062004967E1 /* DatabaseDateEncodingStrategyTests.swift in Sources */, 56419EB724A54063004967E1 /* MutablePersistableRecordPersistenceConflictPolicyTests.swift in Sources */, @@ -1468,6 +1477,7 @@ 56419EAC24A54063004967E1 /* DatabaseValueTests.swift in Sources */, 56419D7824A54062004967E1 /* DatabaseCursorTests.swift in Sources */, 56419E1624A54062004967E1 /* AssociationChainRowScopesTests.swift in Sources */, + 56F61DF1283D484700AF9884 /* getThreadsCount.c in Sources */, 56419EC824A54063004967E1 /* RecordPrimaryKeyMultipleTests.swift in Sources */, 56419DF824A54062004967E1 /* DatabaseDateEncodingStrategyTests.swift in Sources */, 56419EB824A54063004967E1 /* MutablePersistableRecordPersistenceConflictPolicyTests.swift in Sources */, @@ -1766,6 +1776,7 @@ PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = macosx; SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; + SWIFT_OBJC_BRIDGING_HEADER = "../../GRDBTests/GRDBTests-Bridging-Header.h"; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; SWIFT_VERSION = 5.0; }; @@ -1829,6 +1840,7 @@ PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = macosx; SWIFT_COMPILATION_MODE = wholemodule; + SWIFT_OBJC_BRIDGING_HEADER = "../../GRDBTests/GRDBTests-Bridging-Header.h"; SWIFT_OPTIMIZATION_LEVEL = "-O"; SWIFT_VERSION = 5.0; }; @@ -1900,6 +1912,7 @@ PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = macosx; SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; + SWIFT_OBJC_BRIDGING_HEADER = "../../GRDBTests/GRDBTests-Bridging-Header.h"; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; SWIFT_VERSION = 5.0; }; @@ -1964,6 +1977,7 @@ PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = macosx; SWIFT_COMPILATION_MODE = wholemodule; + SWIFT_OBJC_BRIDGING_HEADER = "../../GRDBTests/GRDBTests-Bridging-Header.h"; SWIFT_OPTIMIZATION_LEVEL = "-O"; SWIFT_VERSION = 5.0; }; diff --git a/Tests/CocoaPods/SQLCipher4/GRDBTests.xcodeproj/project.pbxproj b/Tests/CocoaPods/SQLCipher4/GRDBTests.xcodeproj/project.pbxproj index 1d12cd7c6..dc6a20213 100644 --- a/Tests/CocoaPods/SQLCipher4/GRDBTests.xcodeproj/project.pbxproj +++ b/Tests/CocoaPods/SQLCipher4/GRDBTests.xcodeproj/project.pbxproj @@ -449,6 +449,8 @@ 565A27CA27871FE500659A62 /* BackupTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 565A27C827871FE500659A62 /* BackupTestCase.swift */; }; 5691D97227257C930021D540 /* AvailableElements.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5691D97127257C930021D540 /* AvailableElements.swift */; }; 5691D97327257C930021D540 /* AvailableElements.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5691D97127257C930021D540 /* AvailableElements.swift */; }; + 56F61DF6283D4AB100AF9884 /* getThreadsCount.c in Sources */ = {isa = PBXBuildFile; fileRef = 56F61DF4283D4AB100AF9884 /* getThreadsCount.c */; }; + 56F61DF7283D4AB100AF9884 /* getThreadsCount.c in Sources */ = {isa = PBXBuildFile; fileRef = 56F61DF4283D4AB100AF9884 /* getThreadsCount.c */; }; 5B33E6E34F941B4C839A714F /* (null) in Frameworks */ = {isa = PBXBuildFile; }; E158370AAEED49ECD53CE24A /* Pods_GRDBTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 67377FE4CDAD675809F93AD4 /* Pods_GRDBTests.framework */; }; F2B3C4250D67969FF3948955 /* Pods_GRDBTestsEncrypted.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7D5C7999E7D9CE7145687F5D /* Pods_GRDBTestsEncrypted.framework */; }; @@ -681,6 +683,9 @@ 564A2158226C8F24001F64F1 /* db.SQLCipher3 */ = {isa = PBXFileReference; lastKnownFileType = file; path = db.SQLCipher3; sourceTree = SOURCE_ROOT; }; 565A27C827871FE500659A62 /* BackupTestCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BackupTestCase.swift; sourceTree = ""; }; 5691D97127257C930021D540 /* AvailableElements.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AvailableElements.swift; sourceTree = ""; }; + 56F61DF2283D4AB100AF9884 /* GRDBTests-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "GRDBTests-Bridging-Header.h"; sourceTree = ""; }; + 56F61DF4283D4AB100AF9884 /* getThreadsCount.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = getThreadsCount.c; sourceTree = ""; }; + 56F61DF5283D4AB100AF9884 /* getThreadsCount.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = getThreadsCount.h; sourceTree = ""; }; 67377FE4CDAD675809F93AD4 /* Pods_GRDBTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_GRDBTests.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 6A4788C0F815F6C5E4EBDE12 /* Pods-GRDBTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-GRDBTests.debug.xcconfig"; path = "Target Support Files/Pods-GRDBTests/Pods-GRDBTests.debug.xcconfig"; sourceTree = ""; }; 7D5C7999E7D9CE7145687F5D /* Pods_GRDBTestsEncrypted.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_GRDBTestsEncrypted.framework; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -771,6 +776,9 @@ 564A1F1F226B876D001F64F1 /* GRDBTests */ = { isa = PBXGroup; children = ( + 56F61DF4283D4AB100AF9884 /* getThreadsCount.c */, + 56F61DF5283D4AB100AF9884 /* getThreadsCount.h */, + 56F61DF2283D4AB100AF9884 /* GRDBTests-Bridging-Header.h */, 564A1F6F226B89D6001F64F1 /* Betty.jpeg */, 56419F4E24A54098004967E1 /* InflectionsTests.json */, 564A2158226C8F24001F64F1 /* db.SQLCipher3 */, @@ -904,11 +912,9 @@ 56419F6D24A5409A004967E1 /* JoinSupportTests.swift */, 56419F7B24A5409B004967E1 /* MapCursorTests.swift */, 56419F4524A54097004967E1 /* MutablePersistableRecordChangesTests.swift */, - 56419EFF24A54093004967E1 /* TableRecordDeleteTests.swift */, 56419F0224A54093004967E1 /* MutablePersistableRecordEncodableTests.swift */, 56419F3124A54096004967E1 /* MutablePersistableRecordPersistenceConflictPolicyTests.swift */, 56419F0124A54093004967E1 /* MutablePersistableRecordTests.swift */, - 56419F0624A54093004967E1 /* TableRecordUpdateTests.swift */, 56419F0824A54093004967E1 /* NumericOverflowTests.swift */, 56419F0324A54093004967E1 /* OrderedDictionaryTests.swift */, 56419F1A24A54094004967E1 /* PersistableRecordTests.swift */, @@ -956,7 +962,9 @@ 56419FA624A5409E004967E1 /* StatementColumnConvertibleFetchTests.swift */, 56419F5F24A54099004967E1 /* TableDefinitionTests.swift */, 56419F9C24A5409D004967E1 /* TableRecord+QueryInterfaceRequestTests.swift */, + 56419EFF24A54093004967E1 /* TableRecordDeleteTests.swift */, 56419F9024A5409C004967E1 /* TableRecordTests.swift */, + 56419F0624A54093004967E1 /* TableRecordUpdateTests.swift */, 56419F8B24A5409C004967E1 /* TransactionObserverSavepointsTests.swift */, 56419F7A24A5409B004967E1 /* TransactionObserverTests.swift */, 56419FB624A5409F004967E1 /* TruncateOptimizationTests.swift */, @@ -1055,10 +1063,10 @@ TargetAttributes = { 564A1F1C226B876D001F64F1 = { CreatedOnToolsVersion = 10.2.1; - LastSwiftMigration = 1150; + LastSwiftMigration = 1340; }; 564A2095226B8E18001F64F1 = { - LastSwiftMigration = 1150; + LastSwiftMigration = 1340; }; }; }; @@ -1249,6 +1257,7 @@ 5641A01824A540A1004967E1 /* FoundationDateComponentsTests.swift in Sources */, 5641A01E24A540A1004967E1 /* DatabaseDateEncodingStrategyTests.swift in Sources */, 56419FCE24A540A1004967E1 /* AssociationBelongsToSQLDerivationTests.swift in Sources */, + 56F61DF6283D4AB100AF9884 /* getThreadsCount.c in Sources */, 5641A0A024A540A1004967E1 /* SQLRequestTests.swift in Sources */, 5641A02224A540A1004967E1 /* ValueObservationFetchTests.swift in Sources */, 5641A12624A540A1004967E1 /* DatabasePoolSchemaCacheTests.swift in Sources */, @@ -1474,6 +1483,7 @@ 5641A01924A540A1004967E1 /* FoundationDateComponentsTests.swift in Sources */, 5641A01F24A540A1004967E1 /* DatabaseDateEncodingStrategyTests.swift in Sources */, 56419FCF24A540A1004967E1 /* AssociationBelongsToSQLDerivationTests.swift in Sources */, + 56F61DF7283D4AB100AF9884 /* getThreadsCount.c in Sources */, 5641A0A124A540A1004967E1 /* SQLRequestTests.swift in Sources */, 5641A02324A540A1004967E1 /* ValueObservationFetchTests.swift in Sources */, 5641A12724A540A1004967E1 /* DatabasePoolSchemaCacheTests.swift in Sources */, @@ -1775,6 +1785,7 @@ PROVISIONING_PROFILE_SPECIFIER = ""; SDKROOT = macosx; SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; + SWIFT_OBJC_BRIDGING_HEADER = "../../GRDBTests/GRDBTests-Bridging-Header.h"; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; SWIFT_VERSION = 5.0; }; @@ -1841,6 +1852,7 @@ PROVISIONING_PROFILE_SPECIFIER = ""; SDKROOT = macosx; SWIFT_COMPILATION_MODE = wholemodule; + SWIFT_OBJC_BRIDGING_HEADER = "../../GRDBTests/GRDBTests-Bridging-Header.h"; SWIFT_OPTIMIZATION_LEVEL = "-O"; SWIFT_VERSION = 5.0; }; @@ -1915,6 +1927,7 @@ PROVISIONING_PROFILE_SPECIFIER = ""; SDKROOT = macosx; SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; + SWIFT_OBJC_BRIDGING_HEADER = "../../GRDBTests/GRDBTests-Bridging-Header.h"; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; SWIFT_VERSION = 5.0; }; @@ -1982,6 +1995,7 @@ PROVISIONING_PROFILE_SPECIFIER = ""; SDKROOT = macosx; SWIFT_COMPILATION_MODE = wholemodule; + SWIFT_OBJC_BRIDGING_HEADER = "../../GRDBTests/GRDBTests-Bridging-Header.h"; SWIFT_OPTIMIZATION_LEVEL = "-O"; SWIFT_VERSION = 5.0; }; diff --git a/Tests/GRDBTests/DatabasePoolTests.swift b/Tests/GRDBTests/DatabasePoolTests.swift index a85a24de9..22813d232 100644 --- a/Tests/GRDBTests/DatabasePoolTests.swift +++ b/Tests/GRDBTests/DatabasePoolTests.swift @@ -76,6 +76,90 @@ class DatabasePoolTests: GRDBTestCase { try migrator.migrate(dbQueue) } + func testNumberOfThreads_asyncUnsafeRead() throws { +#if SWIFT_PACKAGE + // Can't access getThreadsCount() C function + throw XCTSkip("Thread count is not available") +#else + if getThreadsCount() < 0 { + throw XCTSkip("Thread count is not available") + } + + let pool = try makeDatabasePool() + + // Keep this number big, so that we have a good chance to detect + // thread explosion. + let numberOfConcurrentReads = 10000 + + // Wait for all concurrent reads to end + let group = DispatchGroup() + + // The maximum number of threads we could witness + var maxThreadCount: CInt = 0 + let lock = NSLock() + + for _ in (0.. +#include +#include "getThreadsCount.h" + +// https://stackoverflow.com/a/21571172/525656 +int getThreadsCount(void) { + thread_array_t threadList; + mach_msg_type_number_t threadCount; + task_t task; + + kern_return_t kernReturn = task_for_pid(mach_task_self(), getpid(), &task); + if (kernReturn != KERN_SUCCESS) { + return -1; + } + + kernReturn = task_threads(task, &threadList, &threadCount); + if (kernReturn != KERN_SUCCESS) { + return -1; + } + vm_deallocate (mach_task_self(), (vm_address_t)threadList, threadCount * sizeof(thread_act_t)); + + return threadCount; +} diff --git a/Tests/GRDBTests/getThreadsCount.h b/Tests/GRDBTests/getThreadsCount.h new file mode 100644 index 000000000..db2c0b4a2 --- /dev/null +++ b/Tests/GRDBTests/getThreadsCount.h @@ -0,0 +1,7 @@ +#ifndef getThreadsCount_h +#define getThreadsCount_h + +/// Returns the number of mach threads, or -1 in case of error. +int getThreadsCount(void); + +#endif /* getThreadsCount_h */ From 9ed309b470caf0785b15eaab1575c797ad07b553 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Tue, 24 May 2022 17:49:14 +0200 Subject: [PATCH 02/13] Fix failing tests for number of threads created by DatabasePool --- GRDB/Core/DatabasePool.swift | 50 +++++++++++++++--------------------- GRDB/Utils/Pool.swift | 21 +++++++++++++++ 2 files changed, 41 insertions(+), 30 deletions(-) diff --git a/GRDB/Core/DatabasePool.swift b/GRDB/Core/DatabasePool.swift index 0958e661d..9af640d11 100644 --- a/GRDB/Core/DatabasePool.swift +++ b/GRDB/Core/DatabasePool.swift @@ -330,22 +330,14 @@ extension DatabasePool: DatabaseReader { } public func asyncRead(_ value: @escaping (Result) -> Void) { - // First async jump in order to grab a reader connection. - // Honor configuration dispatching (qos/targetQueue). - let label = configuration.identifier( - defaultLabel: "GRDB.DatabasePool", - purpose: "asyncRead") - configuration - .makeReaderDispatchQueue(label: label) - .async { + do { + guard let readerPool = self.readerPool else { + throw DatabaseError(resultCode: .SQLITE_MISUSE, message: "Connection is closed") + } + readerPool.async { result in do { - guard let readerPool = self.readerPool else { - throw DatabaseError.connectionIsClosed() - } - let (reader, releaseReader) = try readerPool.get() - - // Second async jump because sync could deadlock if - // configuration has a serial targetQueue. + let (reader, releaseReader) = try result.get() + // Second async jump because that's how `Pool.async` has to be used. reader.async { db in defer { try? db.commit() // Ignore commit error @@ -364,6 +356,9 @@ extension DatabasePool: DatabaseReader { value(.failure(error)) } } + } catch { + value(.failure(error)) + } } @_disfavoredOverload // SR-15150 Async overloading in protocol implementation fails @@ -381,22 +376,14 @@ extension DatabasePool: DatabaseReader { } public func asyncUnsafeRead(_ value: @escaping (Result) -> Void) { - // First async jump in order to grab a reader connection. - // Honor configuration dispatching (qos/targetQueue). - let label = configuration.identifier( - defaultLabel: "GRDB.DatabasePool", - purpose: "asyncUnsafeRead") - configuration - .makeReaderDispatchQueue(label: label) - .async { + do { + guard let readerPool = self.readerPool else { + throw DatabaseError(resultCode: .SQLITE_MISUSE, message: "Connection is closed") + } + readerPool.async { result in do { - guard let readerPool = self.readerPool else { - throw DatabaseError(resultCode: .SQLITE_MISUSE, message: "Connection is closed") - } - let (reader, releaseReader) = try readerPool.get() - - // Second async jump because sync could deadlock if - // configuration has a serial targetQueue. + let (reader, releaseReader) = try result.get() + // Second async jump because that's how `Pool.async` has to be used. reader.async { db in defer { releaseReader() @@ -413,6 +400,9 @@ extension DatabasePool: DatabaseReader { value(.failure(error)) } } + } catch { + value(.failure(error)) + } } public func unsafeReentrantRead(_ value: (Database) throws -> T) throws -> T { diff --git a/GRDB/Utils/Pool.swift b/GRDB/Utils/Pool.swift index c611bb242..7f63a388d 100644 --- a/GRDB/Utils/Pool.swift +++ b/GRDB/Utils/Pool.swift @@ -51,6 +51,7 @@ final class Pool { private let itemsSemaphore: DispatchSemaphore // limits the number of elements private let itemsGroup: DispatchGroup // knows when no element is used private let barrierQueue: DispatchQueue + private let semaphoreWaitingQueue: DispatchQueue // Inspired by https://khanlou.com/2016/04/the-GCD-handbook/ init(maximumCount: Int, makeElement: @escaping () throws -> T) { GRDBPrecondition(maximumCount > 0, "Pool size must be at least 1") @@ -58,6 +59,7 @@ final class Pool { self.itemsSemaphore = DispatchSemaphore(value: maximumCount) self.itemsGroup = DispatchGroup() self.barrierQueue = DispatchQueue(label: "GRDB.Pool.barrier", attributes: [.concurrent]) + self.semaphoreWaitingQueue = DispatchQueue(label: "GRDB.Pool.wait") } /// Returns a tuple (element, release) @@ -87,6 +89,25 @@ final class Pool { } } + /// Eventually produces a tuple (element, release), where element is + /// intended to be used asynchronously. + /// + /// Client must call release(), only once, after the element has been used. + /// + /// - important: The `execute` argument is executed in a serial dispatch + /// queue, so make sure you use the element asynchronously. + func async(_ execute: @escaping (Result<(element: T, release: () -> Void), Error>) -> Void) { + // Inspired by https://khanlou.com/2016/04/the-GCD-handbook/ + // > We wait on the semaphore in the serial queue, which means that + // > we’ll have at most one blocked thread when we reach maximum + // > executing blocks on the concurrent queue. Any other tasks the user + // > enqueues will sit inertly on the serial queue waiting to be + // > executed, and won’t cause new threads to be started. + semaphoreWaitingQueue.async { + execute(Result { try self.get() }) + } + } + /// Performs a synchronous block with an element. The element turns /// available after the block has executed. func get(block: (T) throws -> U) throws -> U { From 42050e5e28486dd32e4bc7b39fadc19d54a4fc35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Tue, 24 May 2022 19:54:51 +0200 Subject: [PATCH 03/13] CHANGELOG --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4cf1ca54e..7e517c3a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -91,6 +91,10 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception: --- +## Next Release + +- **Fixed**: [#1225](https://github.com/groue/GRDB.swift/pull/1225) by [@groue](https://github.com/groue): Prevent heavy DatabasePool concurrent reads from creating too many threads + ## 5.24.0 Released May 1, 2022 • [diff](https://github.com/groue/GRDB.swift/compare/v5.23.0...v5.24.0) From 26fedd27029484570260b872e3e0a701cbe8f0d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Tue, 24 May 2022 20:47:34 +0200 Subject: [PATCH 04/13] CI: raise timeouts --- Tests/GRDBTests/DatabaseMigratorTests.swift | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Tests/GRDBTests/DatabaseMigratorTests.swift b/Tests/GRDBTests/DatabaseMigratorTests.swift index 60465e5de..c1384c914 100644 --- a/Tests/GRDBTests/DatabaseMigratorTests.swift +++ b/Tests/GRDBTests/DatabaseMigratorTests.swift @@ -26,7 +26,7 @@ class DatabaseMigratorTests : GRDBTestCase { try! db.execute(sql: "CREATE TABLE t(a)") expectation.fulfill() }) - waitForExpectations(timeout: 1, handler: nil) + waitForExpectations(timeout: 5, handler: nil) } try Test(test) @@ -141,7 +141,7 @@ class DatabaseMigratorTests : GRDBTestCase { expectation.fulfill() }) }) - waitForExpectations(timeout: 1, handler: nil) + waitForExpectations(timeout: 5, handler: nil) } try Test(test) @@ -224,7 +224,7 @@ class DatabaseMigratorTests : GRDBTestCase { }) semaphore.signal() - waitForExpectations(timeout: 1, handler: nil) + waitForExpectations(timeout: 5, handler: nil) cancellable.cancel() } @@ -252,7 +252,7 @@ class DatabaseMigratorTests : GRDBTestCase { }) semaphore.signal() - waitForExpectations(timeout: 1, handler: nil) + waitForExpectations(timeout: 5, handler: nil) cancellable.cancel() } @@ -282,7 +282,7 @@ class DatabaseMigratorTests : GRDBTestCase { expectation.fulfill() }) - waitForExpectations(timeout: 1, handler: nil) + waitForExpectations(timeout: 5, handler: nil) cancellable.cancel() } @@ -313,7 +313,7 @@ class DatabaseMigratorTests : GRDBTestCase { expectation.fulfill() }) - waitForExpectations(timeout: 1, handler: nil) + waitForExpectations(timeout: 5, handler: nil) cancellable.cancel() } @@ -427,7 +427,7 @@ class DatabaseMigratorTests : GRDBTestCase { expectation.fulfill() }) - waitForExpectations(timeout: 1, handler: nil) + waitForExpectations(timeout: 5, handler: nil) } } From 6c48e492d1caa93bd33cefc384811c7892f7cc43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Tue, 24 May 2022 21:58:46 +0200 Subject: [PATCH 05/13] CI: raise timeouts --- .../DatabaseWriterWritePublisherTests.swift | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/Tests/GRDBCombineTests/DatabaseWriterWritePublisherTests.swift b/Tests/GRDBCombineTests/DatabaseWriterWritePublisherTests.swift index 2a9ef1f85..a8fbb14c1 100644 --- a/Tests/GRDBCombineTests/DatabaseWriterWritePublisherTests.swift +++ b/Tests/GRDBCombineTests/DatabaseWriterWritePublisherTests.swift @@ -37,7 +37,7 @@ class DatabaseWriterWritePublisherTests : XCTestCase { try Player(id: 1, name: "Arthur", score: 1000).insert(db) }) let recorder = publisher.record() - try wait(for: recorder.single, timeout: 1) + try wait(for: recorder.single, timeout: 5) try XCTAssertEqual(writer.read(Player.fetchCount), 1) } @@ -65,7 +65,7 @@ class DatabaseWriterWritePublisherTests : XCTestCase { return try Player.fetchCount(db) }) let recorder = publisher.record() - let count = try wait(for: recorder.single, timeout: 1) + let count = try wait(for: recorder.single, timeout: 5) XCTAssertEqual(count, 1) } @@ -87,7 +87,7 @@ class DatabaseWriterWritePublisherTests : XCTestCase { try db.execute(sql: "THIS IS NOT SQL") }) let recorder = publisher.record() - let recording = try wait(for: recorder.recording, timeout: 1) + let recording = try wait(for: recorder.recording, timeout: 5) XCTAssertTrue(recording.output.isEmpty) assertFailure(recording.completion) { (error: DatabaseError) in XCTAssertEqual(error.resultCode, .SQLITE_ERROR) @@ -117,7 +117,7 @@ class DatabaseWriterWritePublisherTests : XCTestCase { try db.execute(sql: "THIS IS NOT SQL") }) let recorder = publisher.record() - let recording = try wait(for: recorder.recording, timeout: 1) + let recording = try wait(for: recorder.recording, timeout: 5) XCTAssertTrue(recording.output.isEmpty) assertFailure(recording.completion) { (error: DatabaseError) in XCTAssertEqual(error.resultCode, .SQLITE_ERROR) @@ -160,7 +160,7 @@ class DatabaseWriterWritePublisherTests : XCTestCase { }) semaphore.signal() - waitForExpectations(timeout: 1, handler: nil) + waitForExpectations(timeout: 5, handler: nil) cancellable.cancel() } @@ -199,7 +199,7 @@ class DatabaseWriterWritePublisherTests : XCTestCase { expectation.fulfill() }) - waitForExpectations(timeout: 1, handler: nil) + waitForExpectations(timeout: 5, handler: nil) cancellable.cancel() } @@ -239,7 +239,7 @@ class DatabaseWriterWritePublisherTests : XCTestCase { expectation.fulfill() }) - waitForExpectations(timeout: 1, handler: nil) + waitForExpectations(timeout: 5, handler: nil) cancellable.cancel() } @@ -269,7 +269,7 @@ class DatabaseWriterWritePublisherTests : XCTestCase { updates: { db in try Player(id: 1, name: "Arthur", score: 1000).insert(db) }, thenRead: { db, _ in try Player.fetchCount(db) }) let recorder = publisher.record() - let count = try wait(for: recorder.single, timeout: 1) + let count = try wait(for: recorder.single, timeout: 5) XCTAssertEqual(count, 1) } @@ -292,7 +292,7 @@ class DatabaseWriterWritePublisherTests : XCTestCase { updates: { _ in }, thenRead: { db, _ in try Player.createTable(db) }) let recorder = publisher.record() - let recording = try wait(for: recorder.recording, timeout: 1) + let recording = try wait(for: recorder.recording, timeout: 5) XCTAssertTrue(recording.output.isEmpty) assertFailure(recording.completion) { (error: DatabaseError) in XCTAssertEqual(error.resultCode, .SQLITE_READONLY) @@ -317,7 +317,7 @@ class DatabaseWriterWritePublisherTests : XCTestCase { updates: { db in try db.execute(sql: "THIS IS NOT SQL") }, thenRead: { _, _ in XCTFail("Should not read") }) let recorder = publisher.record() - let recording = try wait(for: recorder.recording, timeout: 1) + let recording = try wait(for: recorder.recording, timeout: 5) XCTAssertTrue(recording.output.isEmpty) assertFailure(recording.completion) { (error: DatabaseError) in XCTAssertEqual(error.resultCode, .SQLITE_ERROR) @@ -349,7 +349,7 @@ class DatabaseWriterWritePublisherTests : XCTestCase { }, thenRead: { _, _ in XCTFail("Should not read") }) let recorder = publisher.record() - let recording = try wait(for: recorder.recording, timeout: 1) + let recording = try wait(for: recorder.recording, timeout: 5) XCTAssertTrue(recording.output.isEmpty) assertFailure(recording.completion) { (error: DatabaseError) in XCTAssertEqual(error.resultCode, .SQLITE_ERROR) @@ -379,7 +379,7 @@ class DatabaseWriterWritePublisherTests : XCTestCase { updates: { _ in }, thenRead: { db, _ in try Row.fetchAll(db, sql: "THIS IS NOT SQL") }) let recorder = publisher.record() - let recording = try wait(for: recorder.recording, timeout: 1) + let recording = try wait(for: recorder.recording, timeout: 5) XCTAssertTrue(recording.output.isEmpty) assertFailure(recording.completion) { (error: DatabaseError) in XCTAssertEqual(error.resultCode, .SQLITE_ERROR) @@ -421,7 +421,7 @@ class DatabaseWriterWritePublisherTests : XCTestCase { .prefix(1) let recorder = publisher.record() scoreSubject.send(0) - let count = try wait(for: recorder.single, timeout: 1) + let count = try wait(for: recorder.single, timeout: 5) XCTAssertEqual(count, 1) } From 184d5aa4af2593db77ec8b7e8083d563707b862c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 25 May 2022 09:01:39 +0200 Subject: [PATCH 06/13] CI: raise timeouts --- .github/workflows/CI.yml | 10 +++---- ...bservationQueryInterfaceRequestTests.swift | 4 +-- .../GRDBTests/ValueObservationRecorder.swift | 28 +++++-------------- 3 files changed, 14 insertions(+), 28 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index a3a4fd76d..7d19d8101 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -24,7 +24,7 @@ jobs: macOS: name: macOS runs-on: ${{ matrix.runsOn }} - timeout-minutes: 40 + timeout-minutes: 60 strategy: fail-fast: false matrix: @@ -60,7 +60,7 @@ jobs: iOS: name: iOS runs-on: ${{ matrix.runsOn }} - timeout-minutes: 40 + timeout-minutes: 60 strategy: fail-fast: false matrix: @@ -80,7 +80,7 @@ jobs: SPM: name: SPM runs-on: ${{ matrix.runsOn }} - timeout-minutes: 40 + timeout-minutes: 60 strategy: fail-fast: false matrix: @@ -132,7 +132,7 @@ jobs: CustomSQLite: name: CustomSQLite runs-on: ${{ matrix.runsOn }} - timeout-minutes: 40 + timeout-minutes: 60 strategy: fail-fast: false matrix: @@ -152,7 +152,7 @@ jobs: XCFramework: name: XCFramework runs-on: ${{ matrix.runsOn }} - timeout-minutes: 40 + timeout-minutes: 60 strategy: fail-fast: false matrix: diff --git a/Tests/GRDBTests/ValueObservationQueryInterfaceRequestTests.swift b/Tests/GRDBTests/ValueObservationQueryInterfaceRequestTests.swift index 18eb61e27..bc9d03836 100644 --- a/Tests/GRDBTests/ValueObservationQueryInterfaceRequestTests.swift +++ b/Tests/GRDBTests/ValueObservationQueryInterfaceRequestTests.swift @@ -80,7 +80,7 @@ class ValueObservationQueryInterfaceRequestTests: GRDBTestCase { let recorder = observation.record(in: dbQueue) try dbQueue.writeWithoutTransaction(performDatabaseModifications) - let results = try wait(for: recorder.next(6), timeout: 1) + let results = try wait(for: recorder.next(6), timeout: 5) XCTAssertNil(results[0]) @@ -117,7 +117,7 @@ class ValueObservationQueryInterfaceRequestTests: GRDBTestCase { let recorder = observation.record(in: dbQueue) try dbQueue.writeWithoutTransaction(performDatabaseModifications) - let results = try wait(for: recorder.next(6), timeout: 1) + let results = try wait(for: recorder.next(6), timeout: 5) XCTAssertEqual(results[0].count, 0) diff --git a/Tests/GRDBTests/ValueObservationRecorder.swift b/Tests/GRDBTests/ValueObservationRecorder.swift index ca1ab71aa..4db189940 100644 --- a/Tests/GRDBTests/ValueObservationRecorder.swift +++ b/Tests/GRDBTests/ValueObservationRecorder.swift @@ -422,13 +422,6 @@ extension GRDBTestCase { throws where Reducer.Value: Equatable { - #if SQLITE_HAS_CODEC || GRDBCUSTOMSQLITE - // debug SQLite builds can be *very* slow - let timeout: TimeInterval = 4 - #else - let timeout: TimeInterval = 1 - #endif - func test( observation: ValueObservation, scheduling scheduler: ValueObservationScheduler, @@ -455,7 +448,7 @@ extension GRDBTestCase { try writer.writeWithoutTransaction(recordedUpdates) let expectation = recorder.next(expectedValues.count) - let values = try wait(for: expectation, timeout: timeout) + let values = try wait(for: expectation, timeout: 5) XCTAssertEqual( values, expectedValues, "\(#function), \(writer), \(scheduler)", file: file, line: line) @@ -485,7 +478,7 @@ extension GRDBTestCase { } let expectation = recorder.next(expectedValues.count) - let values = try wait(for: expectation, timeout: timeout) + let values = try wait(for: expectation, timeout: 5) XCTAssertEqual( values, expectedValues, "\(#function), \(writer), \(scheduler)", file: file, line: line) @@ -516,14 +509,14 @@ extension GRDBTestCase { if waitForLast { // Optimization! let expectation = recorder.prefix(until: { $0 == lastExpectedValue } ) - recordedValues = try wait(for: expectation, timeout: timeout) + recordedValues = try wait(for: expectation, timeout: 5) } else { // Slow! assertionFailure("Please rewrite your test, because it is too slow: make sure the last expected value is unique.") let expectation = recorder .prefix(expectedValues.count + 2 /* pool may perform double initial fetch */) .inverted - recordedValues = try wait(for: expectation, timeout: timeout) + recordedValues = try wait(for: expectation, timeout: 5) } if scheduler.immediateInitialValue() { @@ -565,14 +558,14 @@ extension GRDBTestCase { if waitForLast { // Optimization! let expectation = recorder.prefix(until: { $0 == lastExpectedValue } ) - recordedValues = try wait(for: expectation, timeout: timeout) + recordedValues = try wait(for: expectation, timeout: 5) } else { // Slow! assertionFailure("Please rewrite your test, because it is too slow: make sure the last expected value is unique.") let expectation = recorder .prefix(expectedValues.count + 2 /* pool may perform double initial fetch */) .inverted - recordedValues = try wait(for: expectation, timeout: timeout) + recordedValues = try wait(for: expectation, timeout: 5) } XCTAssertEqual(recordedValues.first, expectedValues.first) @@ -639,13 +632,6 @@ extension GRDBTestCase { line: UInt) throws { - #if SQLITE_HAS_CODEC || GRDBCUSTOMSQLITE - // debug SQLite builds can be *very* slow - let timeout: TimeInterval = 2 - #else - let timeout: TimeInterval = 1 - #endif - func test( observation: ValueObservation, scheduling scheduler: ValueObservationScheduler, @@ -659,7 +645,7 @@ extension GRDBTestCase { scheduling: scheduler, onError: { _ in testErrorDispatching() }) - let (_, error) = try wait(for: recorder.failure(), timeout: timeout) + let (_, error) = try wait(for: recorder.failure(), timeout: 5) if let error = error as? Failure { try testFailure(error, writer) } else { From 4b7a9bbe7be43c88ac7df36307fe43504e0cdb85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 25 May 2022 09:27:01 +0200 Subject: [PATCH 07/13] CI: raise timeouts --- .../DatabaseReaderReadPublisherTests.swift | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Tests/GRDBCombineTests/DatabaseReaderReadPublisherTests.swift b/Tests/GRDBCombineTests/DatabaseReaderReadPublisherTests.swift index 306ed2bce..9ffeffc51 100644 --- a/Tests/GRDBCombineTests/DatabaseReaderReadPublisherTests.swift +++ b/Tests/GRDBCombineTests/DatabaseReaderReadPublisherTests.swift @@ -36,7 +36,7 @@ class DatabaseReaderReadPublisherTests : XCTestCase { try Player.fetchCount(db) }) let recorder = publisher.record() - let value = try wait(for: recorder.single, timeout: 1) + let value = try wait(for: recorder.single, timeout: 5) XCTAssertEqual(value, 0) } @@ -135,7 +135,7 @@ class DatabaseReaderReadPublisherTests : XCTestCase { try Row.fetchAll(db, sql: "THIS IS NOT SQL") }) let recorder = publisher.record() - let recording = try wait(for: recorder.recording, timeout: 1) + let recording = try wait(for: recorder.recording, timeout: 5) XCTAssertTrue(recording.output.isEmpty) assertFailure(recording.completion) { (error: DatabaseError) in XCTAssertEqual(error.resultCode, .SQLITE_ERROR) @@ -177,7 +177,7 @@ class DatabaseReaderReadPublisherTests : XCTestCase { }) semaphore.signal() - waitForExpectations(timeout: 1, handler: nil) + waitForExpectations(timeout: 5, handler: nil) cancellable.cancel() } @@ -215,7 +215,7 @@ class DatabaseReaderReadPublisherTests : XCTestCase { dispatchPrecondition(condition: .onQueue(.main)) }) - waitForExpectations(timeout: 1, handler: nil) + waitForExpectations(timeout: 5, handler: nil) cancellable.cancel() } @@ -254,7 +254,7 @@ class DatabaseReaderReadPublisherTests : XCTestCase { dispatchPrecondition(condition: .onQueue(queue)) }) - waitForExpectations(timeout: 1, handler: nil) + waitForExpectations(timeout: 5, handler: nil) cancellable.cancel() } @@ -277,7 +277,7 @@ class DatabaseReaderReadPublisherTests : XCTestCase { try Player.createTable(db) }) let recorder = publisher.record() - let recording = try wait(for: recorder.recording, timeout: 1) + let recording = try wait(for: recorder.recording, timeout: 5) XCTAssertTrue(recording.output.isEmpty) assertFailure(recording.completion) { (error: DatabaseError) in XCTAssertEqual(error.resultCode, .SQLITE_READONLY) From de4266499cccce17ceb5bc50b225d6053fe220ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 25 May 2022 17:19:41 +0200 Subject: [PATCH 08/13] Configuration.readQoS --- GRDB/Core/Configuration.swift | 18 +++++------------- .../ValueConcurrentObserver.swift | 8 +++++--- .../ValueWriteOnlyObserver.swift | 8 +++++--- 3 files changed, 15 insertions(+), 19 deletions(-) diff --git a/GRDB/Core/Configuration.swift b/GRDB/Core/Configuration.swift index ed3d47455..bc996f453 100644 --- a/GRDB/Core/Configuration.swift +++ b/GRDB/Core/Configuration.swift @@ -248,6 +248,11 @@ public struct Configuration { /// Default: .default public var qos: DispatchQoS = .default + /// The quality of service of read accesses + var readQoS: DispatchQoS { + targetQueue?.qos ?? self.qos + } + /// A target queue for database accesses. /// /// Database connections which are not read-only will prefer @@ -316,17 +321,4 @@ public struct Configuration { return DispatchQueue(label: label, qos: qos) } } - - /// Creates a DispatchQueue which has the quality of service of - /// read accesses. - /// - /// The returned queue has no target queue, and won't create deadlocks when - /// used synchronously from a database access. - func makeDispatchQueue(label: String) -> DispatchQueue { - if let targetQueue = targetQueue { - return DispatchQueue(label: label, qos: targetQueue.qos) - } else { - return DispatchQueue(label: label, qos: qos) - } - } } diff --git a/GRDB/ValueObservation/ValueConcurrentObserver.swift b/GRDB/ValueObservation/ValueConcurrentObserver.swift index 232df1e4a..6e0f3c2aa 100644 --- a/GRDB/ValueObservation/ValueConcurrentObserver.swift +++ b/GRDB/ValueObservation/ValueConcurrentObserver.swift @@ -138,9 +138,11 @@ final class ValueConcurrentObserver { fetch: reducer._fetch) self.notificationCallbacks = NotificationCallbacks(events: events, onChange: onChange) self.reducer = reducer - self.reduceQueue = dbPool.configuration.makeDispatchQueue(label: dbPool.configuration.identifier( - defaultLabel: "GRDB", - purpose: "ValueObservation")) + self.reduceQueue = DispatchQueue( + label: dbPool.configuration.identifier( + defaultLabel: "GRDB", + purpose: "ValueObservation"), + qos: dbPool.configuration.readQoS) } } diff --git a/GRDB/ValueObservation/ValueWriteOnlyObserver.swift b/GRDB/ValueObservation/ValueWriteOnlyObserver.swift index d41a2e972..440a17ec4 100644 --- a/GRDB/ValueObservation/ValueWriteOnlyObserver.swift +++ b/GRDB/ValueObservation/ValueWriteOnlyObserver.swift @@ -139,9 +139,11 @@ final class ValueWriteOnlyObserver Date: Wed, 25 May 2022 17:21:50 +0200 Subject: [PATCH 09/13] Pool has a QoS --- GRDB/Core/DatabasePool.swift | 19 +++++++++++-------- GRDB/Utils/Pool.swift | 17 ++++++++++++++--- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/GRDB/Core/DatabasePool.swift b/GRDB/Core/DatabasePool.swift index 9af640d11..5e7a82332 100644 --- a/GRDB/Core/DatabasePool.swift +++ b/GRDB/Core/DatabasePool.swift @@ -57,14 +57,17 @@ public final class DatabasePool: DatabaseWriter { readerConfiguration.allowsUnsafeTransactions = false var readerCount = 0 - readerPool = Pool(maximumCount: configuration.maximumReaderCount, makeElement: { - readerCount += 1 // protected by Pool (TODO: document this protection behavior) - return try SerializedDatabase( - path: path, - configuration: readerConfiguration, - defaultLabel: "GRDB.DatabasePool", - purpose: "reader.\(readerCount)") - }) + readerPool = Pool( + maximumCount: configuration.maximumReaderCount, + qos: configuration.readQoS, + makeElement: { + readerCount += 1 // protected by Pool (TODO: document this protection behavior) + return try SerializedDatabase( + path: path, + configuration: readerConfiguration, + defaultLabel: "GRDB.DatabasePool", + purpose: "reader.\(readerCount)") + }) // Activate WAL Mode unless readonly if !configuration.readonly { diff --git a/GRDB/Utils/Pool.swift b/GRDB/Utils/Pool.swift index 7f63a388d..72c49df31 100644 --- a/GRDB/Utils/Pool.swift +++ b/GRDB/Utils/Pool.swift @@ -53,13 +53,24 @@ final class Pool { private let barrierQueue: DispatchQueue private let semaphoreWaitingQueue: DispatchQueue // Inspired by https://khanlou.com/2016/04/the-GCD-handbook/ - init(maximumCount: Int, makeElement: @escaping () throws -> T) { + /// Creates a Pool. + /// + /// - parameters: + /// - maximumCount: The maximum number of elements. + /// - qos: The quality of service of asynchronous accesses. + /// - makeElement: A function that creates an element. It is called + /// on demand. + init( + maximumCount: Int, + qos: DispatchQoS = .unspecified, + makeElement: @escaping () throws -> T) + { GRDBPrecondition(maximumCount > 0, "Pool size must be at least 1") self.makeElement = makeElement self.itemsSemaphore = DispatchSemaphore(value: maximumCount) self.itemsGroup = DispatchGroup() - self.barrierQueue = DispatchQueue(label: "GRDB.Pool.barrier", attributes: [.concurrent]) - self.semaphoreWaitingQueue = DispatchQueue(label: "GRDB.Pool.wait") + self.barrierQueue = DispatchQueue(label: "GRDB.Pool.barrier", qos: qos, attributes: [.concurrent]) + self.semaphoreWaitingQueue = DispatchQueue(label: "GRDB.Pool.wait", qos: qos) } /// Returns a tuple (element, release) From 952ee0a137833b131b9adbeee0910c50e26a062d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 25 May 2022 17:25:43 +0200 Subject: [PATCH 10/13] Rename Pool.async to asyncGet --- GRDB/Core/DatabasePool.swift | 92 +++++++++++++++++------------------- GRDB/Utils/Pool.swift | 2 +- 2 files changed, 45 insertions(+), 49 deletions(-) diff --git a/GRDB/Core/DatabasePool.swift b/GRDB/Core/DatabasePool.swift index 5e7a82332..24c4b6ea3 100644 --- a/GRDB/Core/DatabasePool.swift +++ b/GRDB/Core/DatabasePool.swift @@ -333,34 +333,32 @@ extension DatabasePool: DatabaseReader { } public func asyncRead(_ value: @escaping (Result) -> Void) { - do { - guard let readerPool = self.readerPool else { - throw DatabaseError(resultCode: .SQLITE_MISUSE, message: "Connection is closed") - } - readerPool.async { result in - do { - let (reader, releaseReader) = try result.get() - // Second async jump because that's how `Pool.async` has to be used. - reader.async { db in - defer { - try? db.commit() // Ignore commit error - releaseReader() - } - do { - // The block isolation comes from the DEFERRED transaction. - try db.beginTransaction(.deferred) - try db.clearSchemaCacheIfNeeded() - value(.success(db)) - } catch { - value(.failure(error)) - } + guard let readerPool = self.readerPool else { + value(.failure(DatabaseError(resultCode: .SQLITE_MISUSE, message: "Connection is closed"))) + return + } + + readerPool.asyncGet { result in + do { + let (reader, releaseReader) = try result.get() + // Second async jump because that's how `Pool.async` has to be used. + reader.async { db in + defer { + try? db.commit() // Ignore commit error + releaseReader() + } + do { + // The block isolation comes from the DEFERRED transaction. + try db.beginTransaction(.deferred) + try db.clearSchemaCacheIfNeeded() + value(.success(db)) + } catch { + value(.failure(error)) } - } catch { - value(.failure(error)) } + } catch { + value(.failure(error)) } - } catch { - value(.failure(error)) } } @@ -379,32 +377,30 @@ extension DatabasePool: DatabaseReader { } public func asyncUnsafeRead(_ value: @escaping (Result) -> Void) { - do { - guard let readerPool = self.readerPool else { - throw DatabaseError(resultCode: .SQLITE_MISUSE, message: "Connection is closed") - } - readerPool.async { result in - do { - let (reader, releaseReader) = try result.get() - // Second async jump because that's how `Pool.async` has to be used. - reader.async { db in - defer { - releaseReader() - } - do { - // The block isolation comes from the DEFERRED transaction. - try db.clearSchemaCacheIfNeeded() - value(.success(db)) - } catch { - value(.failure(error)) - } + guard let readerPool = self.readerPool else { + value(.failure(DatabaseError(resultCode: .SQLITE_MISUSE, message: "Connection is closed"))) + return + } + + readerPool.asyncGet { result in + do { + let (reader, releaseReader) = try result.get() + // Second async jump because that's how `Pool.async` has to be used. + reader.async { db in + defer { + releaseReader() + } + do { + // The block isolation comes from the DEFERRED transaction. + try db.clearSchemaCacheIfNeeded() + value(.success(db)) + } catch { + value(.failure(error)) } - } catch { - value(.failure(error)) } + } catch { + value(.failure(error)) } - } catch { - value(.failure(error)) } } diff --git a/GRDB/Utils/Pool.swift b/GRDB/Utils/Pool.swift index 72c49df31..8c7e84994 100644 --- a/GRDB/Utils/Pool.swift +++ b/GRDB/Utils/Pool.swift @@ -107,7 +107,7 @@ final class Pool { /// /// - important: The `execute` argument is executed in a serial dispatch /// queue, so make sure you use the element asynchronously. - func async(_ execute: @escaping (Result<(element: T, release: () -> Void), Error>) -> Void) { + func asyncGet(_ execute: @escaping (Result<(element: T, release: () -> Void), Error>) -> Void) { // Inspired by https://khanlou.com/2016/04/the-GCD-handbook/ // > We wait on the semaphore in the serial queue, which means that // > we’ll have at most one blocked thread when we reach maximum From 4a98a53dad08c12dcae0d9761b8866c472e4670f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Thu, 26 May 2022 12:09:42 +0200 Subject: [PATCH 11/13] CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e517c3a2..f2dc42e83 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -93,6 +93,7 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception: ## Next Release +- **Fixed**: [#1217](https://github.com/groue/GRDB.swift/pull/1217) by [@GetToSet](https://github.com/GetToSet): Fix doc issue on TableRequest.filter(keys:). - **Fixed**: [#1225](https://github.com/groue/GRDB.swift/pull/1225) by [@groue](https://github.com/groue): Prevent heavy DatabasePool concurrent reads from creating too many threads ## 5.24.0 From fd31632203a8eef4edaa4151a38cbd71475206b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Thu, 26 May 2022 13:26:19 +0200 Subject: [PATCH 12/13] Bump GRDBPerformance dependencies --- .../xcshareddata/swiftpm/Package.resolved | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Tests/Performance/GRDBPerformance/GRDBPerformance.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Tests/Performance/GRDBPerformance/GRDBPerformance.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index 5dd24b541..36969be9f 100644 --- a/Tests/Performance/GRDBPerformance/GRDBPerformance.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/Tests/Performance/GRDBPerformance/GRDBPerformance.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -14,8 +14,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/realm/realm-core", "state" : { - "revision" : "dcd3788f75bafb0cc397d145e1651aab3ad4bf0e", - "version" : "11.12.0" + "revision" : "d97eed3ae7dff2f2e2ffbda83fa8f3b8c445c6ba", + "version" : "11.17.0" } }, { @@ -23,8 +23,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/realm/realm-swift.git", "state" : { - "revision" : "628cf20632d65d3a3e90ae8aaf52bce596d7ad8f", - "version" : "10.24.2" + "revision" : "7f123e48ec12926bb5080e449df360127ee0352d", + "version" : "10.26.0" } }, { @@ -32,8 +32,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/stephencelis/SQLite.swift.git", "state" : { - "revision" : "5f5ad81ac0d0a0f3e56e39e646e8423c617df523", - "version" : "0.13.2" + "revision" : "4d543d811ee644fa4cc4bfa0be996b4dd6ba0f54", + "version" : "0.13.3" } } ], From 2a849d380c753854ad59987a1b7d691dba5e8bb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Thu, 26 May 2022 12:12:54 +0200 Subject: [PATCH 13/13] v5.24.1 --- CHANGELOG.md | 6 ++++-- GRDB.swift.podspec | 2 +- Makefile | 4 ++-- README.md | 4 ++-- Support/Info.plist | 2 +- 5 files changed, 10 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f2dc42e83..b53fd52b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception: #### 5.x Releases -- `5.24.x` Releases - [5.24.0](#5240) +- `5.24.x` Releases - [5.24.0](#5240) | [5.24.1](#5241) - `5.23.x` Releases - [5.23.0](#5230) - `5.22.x` Releases - [5.22.0](#5220) | [5.22.1](#5221) | [5.22.2](#5222) - `5.21.x` Releases - [5.21.0](#5210) @@ -91,7 +91,9 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception: --- -## Next Release +## 5.24.1 + +Released May 26, 2022 • [diff](https://github.com/groue/GRDB.swift/compare/v5.24.0...v5.24.1) - **Fixed**: [#1217](https://github.com/groue/GRDB.swift/pull/1217) by [@GetToSet](https://github.com/GetToSet): Fix doc issue on TableRequest.filter(keys:). - **Fixed**: [#1225](https://github.com/groue/GRDB.swift/pull/1225) by [@groue](https://github.com/groue): Prevent heavy DatabasePool concurrent reads from creating too many threads diff --git a/GRDB.swift.podspec b/GRDB.swift.podspec index a6f07e5f8..a8549b775 100644 --- a/GRDB.swift.podspec +++ b/GRDB.swift.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'GRDB.swift' - s.version = '5.24.0' + s.version = '5.24.1' s.license = { :type => 'MIT', :file => 'LICENSE' } s.summary = 'A toolkit for SQLite databases, with a focus on application development.' diff --git a/Makefile b/Makefile index 42bbc411a..1b3535555 100644 --- a/Makefile +++ b/Makefile @@ -577,8 +577,8 @@ ifdef JAZZY --author 'Gwendal Roué' \ --author_url https://github.com/groue \ --github_url https://github.com/groue/GRDB.swift \ - --github-file-prefix https://github.com/groue/GRDB.swift/tree/v5.24.0 \ - --module-version 5.24.0 \ + --github-file-prefix https://github.com/groue/GRDB.swift/tree/v5.24.1 \ + --module-version 5.24.1 \ --module GRDB \ --root-url http://groue.github.io/GRDB.swift/docs/5.24/ \ --output Documentation/Reference \ diff --git a/README.md b/README.md index 7f7829973..aba88f03d 100644 --- a/README.md +++ b/README.md @@ -11,13 +11,13 @@ --- -**Latest release**: May 1, 2022 • [version 5.24.0](https://github.com/groue/GRDB.swift/tree/v5.24.0) • [CHANGELOG](CHANGELOG.md) • [Migrating From GRDB 4 to GRDB 5](Documentation/GRDB5MigrationGuide.md) +**Latest release**: May 26, 2022 • [version 5.24.1](https://github.com/groue/GRDB.swift/tree/v5.24.1) • [CHANGELOG](CHANGELOG.md) • [Migrating From GRDB 4 to GRDB 5](Documentation/GRDB5MigrationGuide.md) **Requirements**: iOS 11.0+ / macOS 10.10+ / tvOS 9.0+ / watchOS 2.0+ • SQLite 3.8.5+ • Swift 5.3+ / Xcode 12+ | Swift version | GRDB version | | -------------- | ----------------------------------------------------------- | -| **Swift 5.3+** | **v5.24.0** | +| **Swift 5.3+** | **v5.24.1** | | Swift 5.2 | [v5.12.0](https://github.com/groue/GRDB.swift/tree/v5.12.0) | | Swift 5.1 | [v4.14.0](https://github.com/groue/GRDB.swift/tree/v4.14.0) | | Swift 5 | [v4.14.0](https://github.com/groue/GRDB.swift/tree/v4.14.0) | diff --git a/Support/Info.plist b/Support/Info.plist index 587e94083..e189c76a4 100644 --- a/Support/Info.plist +++ b/Support/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 5.24.0 + 5.24.1 CFBundleSignature ???? CFBundleVersion