(t *testing.T)
| 8 | ) |
| 9 | |
| 10 | func TestMemDB_Isolation(t *testing.T) { |
| 11 | |
| 12 | id1 := "object-one" |
| 13 | id2 := "object-two" |
| 14 | id3 := "object-three" |
| 15 | |
| 16 | mustNoError := func(t *testing.T, err error) { |
| 17 | if err != nil { |
| 18 | t.Fatalf("unexpected test error: %v", err) |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | setup := func(t *testing.T) *MemDB { |
| 23 | t.Helper() |
| 24 | |
| 25 | db, err := NewMemDB(testValidSchema()) |
| 26 | if err != nil { |
| 27 | t.Fatalf("err: %v", err) |
| 28 | } |
| 29 | |
| 30 | // Add two objects (with a gap between their IDs) |
| 31 | obj1a := testObj() |
| 32 | obj1a.ID = id1 |
| 33 | txn := db.Txn(true) |
| 34 | mustNoError(t, txn.Insert("main", obj1a)) |
| 35 | |
| 36 | obj3 := testObj() |
| 37 | obj3.ID = id3 |
| 38 | mustNoError(t, txn.Insert("main", obj3)) |
| 39 | txn.Commit() |
| 40 | return db |
| 41 | } |
| 42 | |
| 43 | t.Run("snapshot dirty read", func(t *testing.T) { |
| 44 | db := setup(t) |
| 45 | db2 := db.Snapshot() |
| 46 | |
| 47 | // Update an object |
| 48 | obj1b := testObj() |
| 49 | obj1b.ID = id1 |
| 50 | txn1 := db.Txn(true) |
| 51 | obj1b.Baz = "nope" |
| 52 | mustNoError(t, txn1.Insert("main", obj1b)) |
| 53 | |
| 54 | // Insert an object |
| 55 | obj2 := testObj() |
| 56 | obj2.ID = id2 |
| 57 | mustNoError(t, txn1.Insert("main", obj2)) |
| 58 | |
| 59 | txn2 := db2.Txn(false) |
| 60 | out, err := txn2.First("main", "id", id1) |
| 61 | mustNoError(t, err) |
| 62 | if out == nil { |
| 63 | t.Fatalf("should exist") |
| 64 | } |
| 65 | if out.(*TestObject).Baz == "nope" { |
| 66 | t.Fatalf("read from snapshot should not observe uncommitted update (dirty read)") |
| 67 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…