public async Task RunTransactionAsync_RollbackNoRetryOnCommitFailure(StatusCode code)
        {
            var client = new TransactionTestingClient(1, CreateRpcException(code));
            var db     = FirestoreDb.Create("proj", "db", client);
            await Assert.ThrowsAsync <RpcException>(() => db.RunTransactionAsync(CreateCountingCallback()));

            Assert.Equal(new[] { CreateCommitRequest("transaction 1") }, client.CommitRequests);
            Assert.Equal(new[] { CreateRollbackRequest("transaction 1") }, client.RollbackRequests);
        }
        public async Task RunTransactionAsync_CommitSuccess_WithResult()
        {
            var client = new TransactionTestingClient();
            var db     = FirestoreDb.Create("proj", "db", client);
            var result = await db.RunTransactionAsync(CreateCountingCallback());

            Assert.Equal(1, result);
            Assert.Equal(new[] { CreateCommitRequest("transaction 1") }, client.CommitRequests);
            Assert.Empty(client.RollbackRequests);
        }
コード例 #3
0
        public async Task RunTransactionAsync_CommitSuccess_Sync_NoResult()
        {
            var client = new TransactionTestingClient();
            var db     = FirestoreDb.Create("proj", "db", client);
            await db.RunTransactionAsync(transaction =>
            {
                transaction.Create(db.Document("col/doc1"), new { Name = "Test" });
                transaction.Delete(db.Document("col/doc2"));
            });

            Assert.Equal(new[] { CreateCommitRequest("transaction 1") }, client.CommitRequests);
            Assert.Empty(client.RollbackRequests);
        }
        public async Task RunTransactionAsync_TooManyRetries(int?userSpecifiedAttempts)
        {
            int actualAttempts = userSpecifiedAttempts ?? TransactionOptions.Default.MaxAttempts;
            var options        = userSpecifiedAttempts == null ? null : TransactionOptions.ForMaxAttempts(userSpecifiedAttempts.Value);

            var client    = new TransactionTestingClient(actualAttempts, CreateRpcException(StatusCode.Aborted));
            var db        = FirestoreDb.Create("proj", "db", client);
            var exception = await Assert.ThrowsAsync <RpcException>(() => db.RunTransactionAsync(CreateCountingCallback(), options));

            Assert.Equal(StatusCode.Aborted, exception.Status.StatusCode);
            // We should have made as many attempts as we were allowed, and all should have been rolled back.
            Assert.Equal(actualAttempts, client.CommitRequests.Count);
            Assert.Equal(actualAttempts, client.RollbackRequests.Count);
        }
        public async Task RunTransactionAsync_RollbackOnlyOnCallbackFailure()
        {
            var client = new TransactionTestingClient(2, CreateRpcException(StatusCode.Aborted));
            var db     = FirestoreDb.Create("proj", "db", client);

            var exception = await Assert.ThrowsAsync <IOException>(() => db.RunTransactionAsync <int>(
                                                                       async transaction =>
            {
                await transaction.GetSnapshotAsync(db.Document("col/x"));
                throw new IOException("Bang!");
            }));

            Assert.Equal("Bang!", exception.Message);

            Assert.Empty(client.CommitRequests);
            Assert.Equal(new[] { CreateRollbackRequest("transaction 1") }, client.RollbackRequests);
        }
コード例 #6
0
        public async Task RollbackAsync()
        {
            var client      = new TransactionTestingClient();
            var db          = FirestoreDb.Create("proj", "db", client);
            var transaction = await Transaction.BeginAsync(db, null, default);

            await transaction.RollbackAsync();

            var expectedRequest = new RollbackRequest
            {
                Database    = "projects/proj/databases/db",
                Transaction = ByteString.CopyFromUtf8("transaction 1")
            };

            Assert.Empty(client.CommitRequests);
            Assert.Equal(new[] { expectedRequest }, client.RollbackRequests);
        }
        public async Task RunTransactionAsync_RollbackAndRetry()
        {
            var client = new TransactionTestingClient(2, CreateRpcException(StatusCode.Aborted));
            var db     = FirestoreDb.Create("proj", "db", client);
            var result = await db.RunTransactionAsync(CreateCountingCallback());

            // Two failures were retries, so our callback executed 3 times.
            Assert.Equal(3, result);

            var expectedCommitRequests = new[]
            {
                CreateCommitRequest("transaction 1"),
                CreateCommitRequest("transaction 2; retrying transaction 1"),
                CreateCommitRequest("transaction 3; retrying transaction 2")
            };
            var expectedRollbackRequests = new[]
            {
                CreateRollbackRequest("transaction 1"),
                CreateRollbackRequest("transaction 2; retrying transaction 1"),
            };

            Assert.Equal(expectedCommitRequests, client.CommitRequests);
            Assert.Equal(expectedRollbackRequests, client.RollbackRequests);
        }
コード例 #8
0
        public async Task CommitAsync()
        {
            var client      = new TransactionTestingClient();
            var db          = FirestoreDb.Create("proj", "db", client);
            var transaction = await Transaction.BeginAsync(db, null, default);

            var doc = db.Document("col/doc");

            // A simple write of each kind, just to check they're all passed along as expected.
            transaction.Create(doc, new { Name = "Test" });
            transaction.Update(doc, new Dictionary <FieldPath, object> {
                { new FieldPath("Name"), "Test2" }
            });
            transaction.Set(doc, new { Name = "Test3" });
            transaction.Delete(doc);
            await transaction.CommitAsync();

            var expectedRequest = new CommitRequest
            {
                Database    = "projects/proj/databases/db",
                Transaction = ByteString.CopyFromUtf8("transaction 1"),
                Writes      =
                {
                    // Create
                    new Write
                    {
                        CurrentDocument = new V1Beta1.Precondition{
                            Exists = false
                        },
                        Update = new Document
                        {
                            Name   = doc.Path,
                            Fields ={              { "Name", CreateValue("Test")  } }
                        }
                    },
                    // Update
                    new Write
                    {
                        CurrentDocument = new V1Beta1.Precondition{
                            Exists = true
                        },
                        Update = new Document
                        {
                            Name   = doc.Path,
                            Fields ={              { "Name", CreateValue("Test2") } }
                        },
                        UpdateMask = new DocumentMask{
                            FieldPaths ={ "Name" }
                        }
                    },
                    // Set
                    new Write
                    {
                        Update = new Document
                        {
                            Name   = doc.Path,
                            Fields ={              { "Name", CreateValue("Test3") } }
                        },
                    },
                    // Delete
                    new Write {
                        Delete = doc.Path
                    }
                }
            };

            Assert.Equal(new[] { expectedRequest }, client.CommitRequests);
            Assert.Empty(client.RollbackRequests);
        }