-
Notifications
You must be signed in to change notification settings - Fork 159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Database transactions #199
Open
michaelprichardson
wants to merge
37
commits into
master
Choose a base branch
from
database-transactions
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 24 commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
63d290a
Add database runTransaction expect and js externals
michaelprichardson b5a0b30
Add runTransaction methods for jvm, ios and js
michaelprichardson 0b84470
Add serialization plugin to database build
michaelprichardson 1536e42
Fix KSerialiser
michaelprichardson aaca522
Merge branch 'master' into database-transactions
michaelprichardson 8b63096
Fix error thrown
michaelprichardson ab8d5d2
Merge branch 'database-transactions' of github.com:GitLiveApp/firebas…
michaelprichardson 900426b
Fix ios error thrown
michaelprichardson c6fb31b
Remove Result type
michaelprichardson 50e33d6
Add delete app method
michaelprichardson fcc97ce
Add database emulator and rules
michaelprichardson 515b7da
Remove suspend
michaelprichardson 872c1a1
Add database tests
michaelprichardson 98fd19a
Fix js transaction name
michaelprichardson 1d8b989
Add env vars for emulators to actions
michaelprichardson b4fbdb1
Merge branch 'master' into database-transactions
michaelprichardson 59bbacf
Use GITHUB_ENV for env vars
michaelprichardson b9688c9
Move env to run level
michaelprichardson 7350bb3
Move Firebase cleanup into function instead of AfterTest
michaelprichardson 2f7a650
Remove await from js.transaction
michaelprichardson 6ae78af
Merge branch 'database-transactions' of github.com:GitLiveApp/firebas…
michaelprichardson eccce95
Remove FIREBASE_DATABASE_EMULATOR_HOST env var
michaelprichardson 06634d2
Reset mocha timeouts to 5s
michaelprichardson b97957d
Remove comment
michaelprichardson 7425a6b
Merge branch 'master' into database-transactions
michaelprichardson a2332de
Merge branch 'master' into database-transactions
michaelprichardson 6e41e70
updates to database-transactions
michaelprichardson 87bb543
update serialization to 1.5.32
michaelprichardson 9ae03ae
use .value
michaelprichardson 6aef9dc
Merge branch 'master' into database-transactions
michaelprichardson 1ed1028
Merge remote-tracking branch 'origin/database-transactions' into data…
michaelprichardson 1546e58
Merge branch 'master' into database-transactions
michaelprichardson 320846c
Merge branch 'master' into database-transactions
michaelprichardson b928409
enable the new memory model and disable freezing
michaelprichardson 3c95915
move database tests into single file
michaelprichardson cbcdd48
import CompletableDeferred
michaelprichardson 2dff075
return correct type to FIRTransactionResult
michaelprichardson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
15 changes: 15 additions & 0 deletions
15
firebase-database/src/androidAndroidTest/kotlin/dev/gitlive/firebase/database/database.kt
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,15 @@ | ||
/* | ||
* Copyright (c) 2020 GitLive Ltd. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
@file:JvmName("tests") | ||
package dev.gitlive.firebase.database | ||
|
||
import androidx.test.platform.app.InstrumentationRegistry | ||
import kotlinx.coroutines.runBlocking | ||
|
||
actual val emulatorHost: String = "10.0.2.2" | ||
|
||
actual val context: Any = InstrumentationRegistry.getInstrumentation().targetContext | ||
|
||
actual fun runTest(test: suspend () -> Unit) = runBlocking { test() } |
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
97 changes: 97 additions & 0 deletions
97
firebase-database/src/commonTest/kotlin/dev/gitlive/firebase/database/database.kt
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,97 @@ | ||
package dev.gitlive.firebase.database | ||
|
||
import dev.gitlive.firebase.* | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.serialization.* | ||
import kotlin.test.* | ||
|
||
expect val emulatorHost: String | ||
expect val context: Any | ||
expect fun runTest(test: suspend () -> Unit) | ||
|
||
class FirebaseDatabaseTest { | ||
|
||
@Serializable | ||
data class DatabaseTest(val title: String, val likes: Int = 0) | ||
|
||
@BeforeTest | ||
fun initializeFirebase() { | ||
Firebase | ||
.takeIf { Firebase.apps(context).isEmpty() } | ||
?.apply { | ||
initialize( | ||
context, | ||
FirebaseOptions( | ||
applicationId = "1:846484016111:ios:dd1f6688bad7af768c841a", | ||
apiKey = "AIzaSyCK87dcMFhzCz_kJVs2cT2AVlqOTLuyWV0", | ||
databaseUrl = "http://fir-kotlin-sdk.firebaseio.com", | ||
storageBucket = "fir-kotlin-sdk.appspot.com", | ||
projectId = "fir-kotlin-sdk", | ||
gcmSenderId = "846484016111" | ||
) | ||
) | ||
Firebase.database.useEmulator(emulatorHost, 9000) | ||
} | ||
} | ||
|
||
@Test | ||
fun testBasicIncrementTransaction() = runTest { | ||
val data = DatabaseTest("PostOne", 2) | ||
val userRef = Firebase.database.reference("users/user_1/post_id_1") | ||
setupDatabase(userRef, data, DatabaseTest.serializer()) | ||
|
||
// Check database before transaction | ||
val userDocBefore = userRef.valueEvents.first().value(DatabaseTest.serializer()) | ||
assertEquals(data.title, userDocBefore.title) | ||
assertEquals(data.likes, userDocBefore.likes) | ||
|
||
// Run transaction | ||
val transactionSnapshot = userRef.runTransaction(DatabaseTest.serializer()) { DatabaseTest(data.title, it.likes + 1) } | ||
val userDocAfter = transactionSnapshot.value(DatabaseTest.serializer()) | ||
|
||
// Check the database after transaction | ||
assertEquals(data.title, userDocAfter.title) | ||
assertEquals(data.likes + 1, userDocAfter.likes) | ||
|
||
// cleanUp Firebase | ||
cleanUp() | ||
} | ||
|
||
@Test | ||
fun testBasicDecrementTransaction() = runTest { | ||
val data = DatabaseTest("PostTwo", 2) | ||
val userRef = Firebase.database.reference("users/user_1/post_id_2") | ||
setupDatabase(userRef, data, DatabaseTest.serializer()) | ||
|
||
// Check database before transaction | ||
val userDocBefore = userRef.valueEvents.first().value(DatabaseTest.serializer()) | ||
assertEquals(data.title, userDocBefore.title) | ||
assertEquals(data.likes, userDocBefore.likes) | ||
|
||
// Run transaction | ||
val transactionSnapshot = userRef.runTransaction(DatabaseTest.serializer()) { DatabaseTest(data.title, it.likes - 1) } | ||
val userDocAfter = transactionSnapshot.value(DatabaseTest.serializer()) | ||
|
||
// Check the database after transaction | ||
assertEquals(data.title, userDocAfter.title) | ||
assertEquals(data.likes - 1, userDocAfter.likes) | ||
|
||
// cleanUp Firebase | ||
cleanUp() | ||
} | ||
|
||
private fun cleanUp() { | ||
Firebase | ||
.takeIf { Firebase.apps(context).isNotEmpty() } | ||
?.apply { app.delete() } | ||
} | ||
|
||
private suspend fun <T> setupDatabase(ref: DatabaseReference, data: T, strategy: SerializationStrategy<T>) { | ||
try { | ||
ref.setValue(strategy, data) | ||
} catch (err: DatabaseException) { | ||
println(err) | ||
} | ||
} | ||
|
||
} |
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
20 changes: 20 additions & 0 deletions
20
firebase-database/src/iosTest/kotlin/dev/gitlive/firebase/database/database.kt
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,20 @@ | ||
package dev.gitlive.firebase.database | ||
|
||
import kotlinx.coroutines.* | ||
import platform.Foundation.* | ||
|
||
actual val emulatorHost: String = "localhost" | ||
|
||
actual val context: Any = Unit | ||
|
||
actual fun runTest(test: suspend () -> Unit) = runBlocking { | ||
val testRun = MainScope().async { test() } | ||
while (testRun.isActive) { | ||
NSRunLoop.mainRunLoop.runMode( | ||
NSDefaultRunLoopMode, | ||
beforeDate = NSDate.create(timeInterval = 1.0, sinceDate = NSDate()) | ||
) | ||
yield() | ||
} | ||
testRun.await() | ||
} |
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
26 changes: 26 additions & 0 deletions
26
firebase-database/src/jsTest/kotlin/dev/gitlive/firebase/database/database.kt
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,26 @@ | ||
package dev.gitlive.firebase.database | ||
|
||
import kotlinx.coroutines.GlobalScope | ||
import kotlinx.coroutines.promise | ||
|
||
actual val emulatorHost: String = "localhost" | ||
|
||
actual val context: Any = Unit | ||
|
||
actual fun runTest(test: suspend () -> Unit) = GlobalScope | ||
.promise { | ||
try { | ||
test() | ||
} catch (e: Throwable) { | ||
e.log() | ||
throw e | ||
} | ||
}.asDynamic() | ||
|
||
internal fun Throwable.log() { | ||
console.error(this) | ||
cause?.let { | ||
console.error("Caused by:") | ||
it.log() | ||
} | ||
} |
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,6 @@ | ||
{ | ||
"rules": { | ||
".read": true, | ||
".write": true | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm suggesting this change because I think transactions are not working.
We need to work with
currentData.value
, not withcurrentData
itself.Also we need to tolerate
null
value as described in the Firebase documentation.I'm happy to help to update and merge this PR but I'm not sure I'll be able to do the same in js and iOS.