-
-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #294 from orlandos-nl/transaction-crash
Adds test for transaction.
- Loading branch information
Showing
9 changed files
with
201 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import NIO | ||
import MongoKitten | ||
import XCTest | ||
import MongoCore | ||
|
||
class TransactionTests: XCTestCase { | ||
struct ModelA: Codable { | ||
static let collection = "model_a" | ||
let _id: ObjectId | ||
let value: String | ||
} | ||
|
||
struct ModelB: Codable { | ||
static let collection = "model_b" | ||
let _id: ObjectId | ||
let value: String | ||
} | ||
|
||
var mongo: MongoDatabase! | ||
|
||
override func setUp() async throws { | ||
try await super.setUp() | ||
let mongoSettings = try ConnectionSettings("mongodb://\(ProcessInfo.processInfo.environment["MONGO_HOSTNAME_A"] ?? "localhost")/mongokitten-test") | ||
mongo = try await MongoDatabase.connect(to: mongoSettings) | ||
} | ||
|
||
override func tearDown() async throws { | ||
try await super.tearDown() | ||
try await mongo.drop() | ||
} | ||
|
||
func test_transaction() async throws { | ||
try await mongo.transaction { db in | ||
try await db[ModelA.collection].insertEncoded(ModelA(_id: .init(), value: UUID().uuidString)) | ||
try await db[ModelB.collection].insertEncoded(ModelB(_id: .init(), value: UUID().uuidString)) | ||
} | ||
} | ||
|
||
func test_backToBackTransaction() async throws { | ||
for _ in 0..<100 { | ||
try await mongo.transaction { db in | ||
|
||
try await db[ModelA.collection].insertEncoded(ModelA(_id: .init(), value: UUID().uuidString)) | ||
try await db[ModelB.collection].insertEncoded(ModelB(_id: .init(), value: UUID().uuidString)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
extension MongoDatabase { | ||
func transaction<T>(_ closure: @escaping (MongoDatabase) async throws -> T) async throws -> T { | ||
guard !self.isInTransaction else { | ||
return try await closure(self) | ||
} | ||
let transactionDatabase = try await self.startTransaction(autoCommitChanges: false) | ||
|
||
do { | ||
let value = try await closure(transactionDatabase) | ||
|
||
try await transactionDatabase.commit() | ||
return value | ||
} catch { | ||
try await transactionDatabase.abort() | ||
throw error | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
version: "3" | ||
|
||
services: | ||
mongo-1: | ||
image: mongo:5.0 | ||
ports: | ||
- "27017:27017" | ||
container_name: mongo-1 | ||
hostname: mongo-1 | ||
networks: | ||
- mongo_cluster | ||
command: mongod --replSet rs0 | ||
|
||
mongo-2: | ||
image: mongo:5.0 | ||
ports: | ||
- "27018:27017" | ||
container_name: mongo-2 | ||
hostname: mongo-2 | ||
networks: | ||
- mongo_cluster | ||
command: mongod --replSet rs0 | ||
depends_on: | ||
- mongo-1 | ||
|
||
mongo-3: | ||
image: mongo:5.0 | ||
ports: | ||
- "27019:27017" | ||
container_name: mongo-3 | ||
hostname: mongo-3 | ||
networks: | ||
- mongo_cluster | ||
command: mongod --replSet rs0 | ||
depends_on: | ||
- mongo-2 | ||
|
||
mongosetup: | ||
image: mongo:5.0 | ||
networks: | ||
- mongo_cluster | ||
volumes: | ||
- ./scripts:/scripts | ||
command: bash -c "chmod +x /scripts/setup.sh && /scripts/setup.sh" | ||
|
||
networks: | ||
mongo_cluster: | ||
driver: bridge |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#!/bin/sh | ||
sleep 5 | ||
mongo --host mongo-1:27017 <<EOF | ||
var cfg = { | ||
"_id": "rs0", | ||
"version": 1, | ||
"members": [ | ||
{ | ||
"_id": 0, | ||
"host": "mongo-1:27017", | ||
"priority": 2 | ||
}, | ||
{ | ||
"_id": 1, | ||
"host": "mongo-2:27017", | ||
"priority": 1 | ||
}, | ||
{ | ||
"_id": 2, | ||
"host": "mongo-3:27017", | ||
"priority": 0, | ||
"arbiterOnly": true | ||
} | ||
] | ||
}; | ||
rs.initiate(cfg, { force: true }); | ||
rs.reconfig(cfg, { force: true }); | ||
rs.status(); | ||
EOF |