From aa2ab425d1c13ba53bb3d69083f17a739f70ed34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noe=CC=81=20Malzieu?= Date: Wed, 21 Aug 2024 18:07:35 +0200 Subject: [PATCH] Added 2 tests showing a database lock when syncing many groups or one groups many times --- Tests/XMTPTests/GroupTests.swift | 72 ++++++++++++++++++++++++++++---- 1 file changed, 63 insertions(+), 9 deletions(-) diff --git a/Tests/XMTPTests/GroupTests.swift b/Tests/XMTPTests/GroupTests.swift index 1eb3bd97..82042ad1 100644 --- a/Tests/XMTPTests/GroupTests.swift +++ b/Tests/XMTPTests/GroupTests.swift @@ -858,13 +858,67 @@ class GroupTests: XCTestCase { } } - func listMembersInParallel(groups: [Group]) async throws { - await withThrowingTaskGroup(of: [Member].self) { taskGroup in - for group in groups { - taskGroup.addTask { - return try group.members - } - } - } - } + func testCanSyncManyGroupsInParallelInUnderASecond() async throws { + let fixtures = try await localFixtures() + var groups: [Group] = [] + + for _ in 0..<40 { + var group = try await fixtures.aliceClient.conversations.newGroup(with: [fixtures.bob.address]) + groups.append(group) + } + do { + let start = Date() + let _ = try await syncGroupsInParallel(groups: groups) + let end = Date() + print(end.timeIntervalSince(start)) + XCTAssert(end.timeIntervalSince(start) < 1) + } catch { + print("Failed to list groups members: \(error)") + throw error // Rethrow the error to fail the test if group creation fails + } + } + + func testCanSyncSameGroupManyTimesInParallelInUnderASecond() async throws { + let fixtures = try await localFixtures() + var groups: [Group] = [] + var group = try await fixtures.aliceClient.conversations.newGroup(with: [fixtures.bob.address]) + + for _ in 0..<40 { + groups.append(group) + } + do { + let start = Date() + let _ = try await syncGroupsInParallel(groups: groups) + let end = Date() + print(end.timeIntervalSince(start)) + XCTAssert(end.timeIntervalSince(start) < 1) + } catch { + print("Failed to list groups members: \(error)") + throw error // Rethrow the error to fail the test if group creation fails + } + } + + func listMembersInParallel(groups: [Group]) async throws { + await withThrowingTaskGroup(of: [Member].self) { taskGroup in + for group in groups { + taskGroup.addTask { + return try group.members + } + } + } + } + + func syncGroupsInParallel(groups: [Group]) async throws { + try await withThrowingTaskGroup(of: Void.self) { taskGroup in + // Loop over each group and add a task to the group + for group in groups { + taskGroup.addTask { + try await group.sync() + } + } + + // Wait for all tasks to complete + try await taskGroup.waitForAll() + } + } }