Skip to content
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

run_transaction does not properly rollback transaction #148

Closed
lukemauldinks opened this issue Dec 21, 2023 · 2 comments · Fixed by #149
Closed

run_transaction does not properly rollback transaction #148

lukemauldinks opened this issue Dec 21, 2023 · 2 comments · Fixed by #149
Labels
bug Something isn't working

Comments

@lukemauldinks
Copy link

See example test case below. The test case results in this warning:

running 1 test
2023-12-21T20:01:29.739471Z  INFO firestore::db: Creating a new database client. database_path="projects/kidstrong-at-home/databases/(default)" api_url="https://firestore.googleapis.com" token_scopes="https://www.googleapis.com/auth/cloud-platform"
2023-12-21T20:01:30.444972Z DEBUG Firestore Delete Document{/firestore/collection_name="integration-test-transactions" /firestore/document_name="projects/kidstrong-at-home/databases/(default)/documents/integration-test-transactions/test-1" /firestore/response_time=499}: firestore::db::delete: Deleted a document. collection_id="integration-test-transactions" document_id="test-1"
2023-12-21T20:01:30.622907Z DEBUG Firestore Update Document{/firestore/collection_name="integration-test-transactions" /firestore/document_name="projects/kidstrong-at-home/databases/(default)/documents/integration-test-transactions/test-1" /firestore/response_time=177}: firestore::db::update: Updated the document. collection_id="integration-test-transactions" document_id="projects/kidstrong-at-home/databases/(default)/documents/integration-test-transactions/test-1"
2023-12-21T20:01:30.728739Z DEBUG Firestore Transaction{/firestore/transaction_id="11a6a1fe3465dcbd"}: firestore::db::transaction: Created a new transaction. mode=ReadWrite
2023-12-21T20:01:30.728948Z  WARN Firestore Transaction{/firestore/transaction_id="11a6a1fe3465dcbd"}: firestore::db::transaction: Transaction was neither committed nor rolled back.
Error: ErrorInTransaction(FirestoreErrorInTransaction { transaction_id: [17, 166, 161, 254, 52, 101, 220, 189, 0, 34, 89, 0, 203, 220, 143, 239, 246, 114, 138, 1, 84, 133, 207, 78, 36, 234, 142, 18, 93, 183, 207, 190, 126, 86, 92, 47, 161, 7, 101, 108, 228, 165, 235, 0, 154, 124, 176, 24, 192, 63, 46, 49, 156, 40, 228, 185, 191, 53, 103, 178, 46, 96, 22, 165, 40, 170, 24, 167, 9, 190, 70, 215, 217, 2, 177, 219, 204, 215, 233, 87, 162, 199, 162, 1, 79, 110, 72, 122, 87, 52, 9, 150, 146, 205, 112, 224, 43, 140, 73, 133], source: MyError { details: "test error" } })
test transaction_error_tests ... FAILED

Test code:

#[tokio::test]
async fn transaction_error_tests() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    let db = setup().await?;

    const TEST_COLLECTION_NAME: &str = "integration-test-transactions";

    let my_struct = MyTestStructure {
        some_id: "test-1".to_string(),
        some_string: "Test".to_string(),
    };

    db.fluent()
        .delete()
        .from(TEST_COLLECTION_NAME)
        .document_id(&my_struct.some_id)
        .execute()
        .await?;

    let object_created: MyTestStructure = db
        .fluent()
        .update()
        .in_col(TEST_COLLECTION_NAME)
        .precondition(FirestoreWritePrecondition::Exists(false))
        .document_id(&my_struct.some_id)
        .object(&my_struct.clone())
        .execute()
        .await?;

    assert_eq!(object_created, my_struct);
    db.run_transaction(|_db, _tx| {
        Box::pin(async move {
            //Test returning an error
            Err(backoff::Error::Permanent(MyError::new("test error")))
        })
    })
    .await?;

    Ok(())
}

#[derive(Debug)]
pub struct MyError {
    details: String,
}

impl MyError {
    fn new(msg: &str) -> MyError {
        MyError {
            details: msg.to_string(),
        }
    }
}

impl std::fmt::Display for MyError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", self.details)
    }
}

impl std::error::Error for MyError {
    fn description(&self) -> &str {
        &self.details
    }
}

@abdolence abdolence added the bug Something isn't working label Dec 21, 2023
@abdolence abdolence linked a pull request Dec 21, 2023 that will close this issue
@abdolence
Copy link
Owner

Hey, thanks for the report and a reproducible test(!), really appreciate it!

Fix is already prepared and I'll release it soon

@abdolence
Copy link
Owner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants