Skip to content

Commit

Permalink
fix: handle commit-rollback in unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Elias Van Ootegem <elias@vega.xyz>
  • Loading branch information
EVODelavega committed Jun 14, 2024
1 parent 5bbdbfb commit 6413e15
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
9 changes: 1 addition & 8 deletions datanode/sqlstore/assets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,11 @@ func TestAssetCache(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, asset2, fetched)

// Commit the sub-transaction and fetch the asset, we should not yet get the asset with the new symbol
// after commit, the new asset should be there already
err = connectionSource.Commit(txCtx)
require.NoError(t, err)
fetched, err = as.GetByID(ctx, string(asset.ID))
require.NoError(t, err)
assert.Equal(t, asset2, fetched)

// now commit the main transaction, then we should get the new symbol
err = connectionSource.Commit(ctx)
require.NoError(t, err)
fetched, err = as.GetByID(context.Background(), string(asset.ID))
require.NoError(t, err)
assert.Equal(t, asset3, fetched)
}

Expand Down
8 changes: 7 additions & 1 deletion datanode/sqlstore/connection_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ func (c *ConnectionSource) Commit(ctx context.Context) error {
return fmt.Errorf("failed to commit transaction for context: %s, error: %w", ctx, err)
}
// invoke all post-commit hooks once the transaction (and its sub transactions) have been committed
if tx.parent != nil {
// make an exception for unit tests, so we don't need to commit DB transactions for hooks on the nested transaction.
if !c.isTest && tx.parent != nil {
// this is a nested transaction, don't invoke hooks until the parent is committed
// instead prepend the hooks and return.
tx.parent.mu.Lock()
Expand All @@ -156,6 +157,11 @@ func (c *ConnectionSource) Commit(ctx context.Context) error {
for _, f := range post {
f()
}
if tx.parent != nil {
tx.parent.mu.Lock()
delete(tx.parent.subTx, tx.id)
tx.parent.mu.Unlock()
}
return nil
}

Expand Down
2 changes: 2 additions & 0 deletions datanode/sqlstore/sqlstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ func TestMain(m *testing.M) {
databasetest.TestMain(m, ctx, func(cfg sqlstore.Config, source *sqlstore.ConnectionSource,
postgresLog *bytes.Buffer,
) {
// ensures nested transactions execute the post-commit hooks while the parent transaction still rolls back the overall changes.
source.ToggleTest()
testDBPort = cfg.ConnectionConfig.Port
connectionSource = source
testConfig = cfg
Expand Down

0 comments on commit 6413e15

Please sign in to comment.