Skip to content

Commit 74585c0

Browse files
committed
Fix interference from other tests
1 parent adfa2a4 commit 74585c0

File tree

2 files changed

+44
-38
lines changed

2 files changed

+44
-38
lines changed

core/src/commonMain/kotlin/com/powersync/db/ActiveInstanceStore.kt

+26-23
Original file line numberDiff line numberDiff line change
@@ -22,49 +22,43 @@ internal expect fun disposeWhenDeallocated(resource: ActiveDatabaseResource): An
2222
*/
2323
internal class ActiveDatabaseGroup(
2424
val identifier: String,
25+
private val collection: GroupsCollection,
2526
) {
2627
internal var refCount = 0 // Guarded by companion object
2728
internal val syncMutex = Mutex()
2829

2930
fun removeUsage() {
30-
ActiveDatabaseGroup.synchronize {
31+
collection.synchronize {
3132
if (--refCount == 0) {
32-
allGroups.remove(this)
33+
collection.allGroups.remove(this)
3334
}
3435
}
3536
}
3637

37-
companion object : Synchronizable() {
38-
internal val multipleInstancesMessage =
39-
"""
40-
Multiple PowerSync instances for the same database have been detected.
41-
This can cause unexpected results.
42-
Please check your PowerSync client instantiation logic if this is not intentional.
43-
""".trimIndent()
44-
38+
internal open class GroupsCollection: Synchronizable() {
4539
internal val allGroups = mutableListOf<ActiveDatabaseGroup>()
4640

4741
private fun findGroup(
4842
warnOnDuplicate: Logger,
4943
identifier: String,
5044
): ActiveDatabaseGroup = synchronize {
51-
val existing = allGroups.asSequence().firstOrNull { it.identifier == identifier }
52-
val resolvedGroup =
53-
if (existing == null) {
54-
val added = ActiveDatabaseGroup(identifier)
55-
allGroups.add(added)
56-
added
57-
} else {
58-
existing
59-
}
60-
61-
if (resolvedGroup.refCount++ != 0) {
62-
warnOnDuplicate.w { multipleInstancesMessage }
45+
val existing = allGroups.asSequence().firstOrNull { it.identifier == identifier }
46+
val resolvedGroup =
47+
if (existing == null) {
48+
val added = ActiveDatabaseGroup(identifier, this)
49+
allGroups.add(added)
50+
added
51+
} else {
52+
existing
6353
}
6454

65-
resolvedGroup
55+
if (resolvedGroup.refCount++ != 0) {
56+
warnOnDuplicate.w { multipleInstancesMessage }
6657
}
6758

59+
resolvedGroup
60+
}
61+
6862
internal fun referenceDatabase(
6963
warnOnDuplicate: Logger,
7064
identifier: String,
@@ -75,6 +69,15 @@ internal class ActiveDatabaseGroup(
7569
return resource to disposeWhenDeallocated(resource)
7670
}
7771
}
72+
73+
companion object : GroupsCollection() {
74+
internal val multipleInstancesMessage =
75+
"""
76+
Multiple PowerSync instances for the same database have been detected.
77+
This can cause unexpected results.
78+
Please check your PowerSync client instantiation logic if this is not intentional.
79+
""".trimIndent()
80+
}
7881
}
7982

8083
internal class ActiveDatabaseResource(

core/src/commonTest/kotlin/com/powersync/db/ActiveDatabaseGroupTest.kt

+18-15
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,31 @@ class ActiveDatabaseGroupTest {
2626
),
2727
)
2828

29+
private lateinit var collection: ActiveDatabaseGroup.GroupsCollection
30+
2931
@BeforeTest
30-
fun setupDatabase() {
32+
fun setup() {
33+
collection = ActiveDatabaseGroup.GroupsCollection()
3134
logWriter.reset()
3235
}
3336

3437
@Test
3538
fun testTrackDatabase() {
36-
val usage = ActiveDatabaseGroup.referenceDatabase(logger, "test")
37-
assertEquals(1, ActiveDatabaseGroup.allGroups.size)
39+
val usage = collection.referenceDatabase(logger, "test")
40+
assertEquals(1, collection.allGroups.size)
3841

3942
usage.first.dispose()
40-
assertEquals(0, ActiveDatabaseGroup.allGroups.size)
43+
assertEquals(0, collection.allGroups.size)
4144
}
4245

4346
@Test
4447
fun testWarnsOnDuplicate() {
45-
val usage = ActiveDatabaseGroup.referenceDatabase(logger, "test")
46-
assertEquals(1, ActiveDatabaseGroup.allGroups.size)
48+
val usage = collection.referenceDatabase(logger, "test")
49+
assertEquals(1, collection.allGroups.size)
4750

4851
assertEquals(0, logWriter.logs.size)
4952

50-
val another = ActiveDatabaseGroup.referenceDatabase(logger, "test")
53+
val another = collection.referenceDatabase(logger, "test")
5154
assertNotNull(
5255
logWriter.logs.find {
5356
it.message == ActiveDatabaseGroup.multipleInstancesMessage
@@ -57,23 +60,23 @@ class ActiveDatabaseGroupTest {
5760
assertEquals(usage.first.group, another.first.group)
5861

5962
usage.first.dispose()
60-
assertEquals(1, ActiveDatabaseGroup.allGroups.size)
63+
assertEquals(1, collection.allGroups.size)
6164
another.first.dispose()
62-
assertEquals(0, ActiveDatabaseGroup.allGroups.size)
65+
assertEquals(0, collection.allGroups.size)
6366
}
6467

6568
@Test
6669
fun testDoesNotWarnForDifferentIdentifiers() {
67-
val usage = ActiveDatabaseGroup.referenceDatabase(logger, "test")
68-
assertEquals(1, ActiveDatabaseGroup.allGroups.size)
69-
val another = ActiveDatabaseGroup.referenceDatabase(logger, "test2")
70-
assertEquals(2, ActiveDatabaseGroup.allGroups.size)
70+
val usage = collection.referenceDatabase(logger, "test")
71+
assertEquals(1, collection.allGroups.size)
72+
val another = collection.referenceDatabase(logger, "test2")
73+
assertEquals(2, collection.allGroups.size)
7174

7275
assertEquals(0, logWriter.logs.size)
7376

7477
usage.first.dispose()
75-
assertEquals(1, ActiveDatabaseGroup.allGroups.size)
78+
assertEquals(1, collection.allGroups.size)
7679
another.first.dispose()
77-
assertEquals(0, ActiveDatabaseGroup.allGroups.size)
80+
assertEquals(0, collection.allGroups.size)
7881
}
7982
}

0 commit comments

Comments
 (0)