|
| 1 | +// Copyright 2026 Democratized Data Foundation |
| 2 | +// |
| 3 | +// Use of this software is governed by the Business Source License |
| 4 | +// included in the file licenses/BSL.txt. |
| 5 | +// |
| 6 | +// As of the Change Date specified in that file, in accordance with |
| 7 | +// the Business Source License, use of this software will be governed |
| 8 | +// by the Apache License, Version 2.0, included in the file |
| 9 | +// licenses/APL.txt. |
| 10 | + |
| 11 | +package db |
| 12 | + |
| 13 | +import ( |
| 14 | + "context" |
| 15 | + "testing" |
| 16 | + |
| 17 | + "github.com/stretchr/testify/require" |
| 18 | + |
| 19 | + "github.com/sourcenetwork/defradb/client" |
| 20 | +) |
| 21 | + |
| 22 | +// Reading one document must not put another document's keys in the transaction's read set. |
| 23 | +// Documents sit consecutively in the store, so a scan of one that runs past its end lands on |
| 24 | +// the next document, and a concurrent write there then rejects a transaction that never |
| 25 | +// touched it. |
| 26 | +func TestDocumentFetcher_ReadingOneDocDoesNotConflictWithItsNeighbour(t *testing.T) { |
| 27 | + ctx := context.Background() |
| 28 | + |
| 29 | + db, err := newBadgerDB(ctx) |
| 30 | + require.NoError(t, err) |
| 31 | + t.Cleanup(func() { db.Close() }) |
| 32 | + |
| 33 | + _, err = db.AddCollection(ctx, userSchema) |
| 34 | + require.NoError(t, err) |
| 35 | + col, err := db.GetCollectionByName(ctx, "User") |
| 36 | + require.NoError(t, err) |
| 37 | + |
| 38 | + first, err := client.NewDocFromJSON(ctx, []byte(`{"name":"alice","age":1}`), col.Version()) |
| 39 | + require.NoError(t, err) |
| 40 | + require.NoError(t, col.AddDocument(ctx, first)) |
| 41 | + |
| 42 | + second, err := client.NewDocFromJSON(ctx, []byte(`{"name":"bob","age":2}`), col.Version()) |
| 43 | + require.NoError(t, err) |
| 44 | + require.NoError(t, col.AddDocument(ctx, second)) |
| 45 | + |
| 46 | + // Read the first document and write it back, all in one transaction. |
| 47 | + txn, err := db.NewTxn(false) |
| 48 | + require.NoError(t, err) |
| 49 | + defer txn.Discard() |
| 50 | + txnCtx := InitContext(ctx, txn) |
| 51 | + |
| 52 | + read, err := col.GetDocument(txnCtx, first.ID()) |
| 53 | + require.NoError(t, err) |
| 54 | + require.NoError(t, read.Set(ctx, "age", 10)) |
| 55 | + require.NoError(t, col.UpdateDocument(txnCtx, read)) |
| 56 | + |
| 57 | + // Someone else rewrites the neighbour and commits, in between. Every field is written so |
| 58 | + // the test does not depend on which one holds the lowest field ID. |
| 59 | + other, err := col.GetDocument(ctx, second.ID()) |
| 60 | + require.NoError(t, err) |
| 61 | + require.NoError(t, other.Set(ctx, "name", "robert")) |
| 62 | + require.NoError(t, other.Set(ctx, "age", 99)) |
| 63 | + require.NoError(t, col.UpdateDocument(ctx, other)) |
| 64 | + |
| 65 | + require.NoError(t, txn.Commit(), |
| 66 | + "reading one document must not conflict with a write to the next one") |
| 67 | +} |
0 commit comments