-
Notifications
You must be signed in to change notification settings - Fork 213
/
memdb_test.go
84 lines (70 loc) · 1.49 KB
/
memdb_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package memdb
import (
"testing"
"time"
)
func TestMemDB_SingleWriter_MultiReader(t *testing.T) {
db, err := NewMemDB(testValidSchema())
if err != nil {
t.Fatalf("err: %v", err)
}
tx1 := db.Txn(true)
tx2 := db.Txn(false) // Should not block!
tx3 := db.Txn(false) // Should not block!
tx4 := db.Txn(false) // Should not block!
doneCh := make(chan struct{})
go func() {
defer close(doneCh)
db.Txn(true)
}()
select {
case <-doneCh:
t.Fatalf("should not allow another writer")
case <-time.After(10 * time.Millisecond):
}
tx1.Abort()
tx2.Abort()
tx3.Abort()
tx4.Abort()
select {
case <-doneCh:
case <-time.After(10 * time.Millisecond):
t.Fatalf("should allow another writer")
}
}
func TestMemDB_Snapshot(t *testing.T) {
db, err := NewMemDB(testValidSchema())
if err != nil {
t.Fatalf("err: %v", err)
}
// Add an object
obj := testObj()
txn := db.Txn(true)
txn.Insert("main", obj)
txn.Commit()
// Clone the db
db2 := db.Snapshot()
// Remove the object
txn = db.Txn(true)
txn.Delete("main", obj)
txn.Commit()
// Object should exist in second snapshot but not first
txn = db.Txn(false)
out, err := txn.First("main", "id", obj.ID)
if err != nil {
t.Fatalf("err: %v", err)
}
if out != nil {
t.Fatalf("should not exist %#v", out)
}
txn = db2.Txn(true)
out, err = txn.First("main", "id", obj.ID)
if err != nil {
t.Fatalf("err: %v", err)
}
if out == nil {
t.Fatalf("should exist")
}
}