-
Notifications
You must be signed in to change notification settings - Fork 27
/
transaction_test.go
101 lines (82 loc) · 2.07 KB
/
transaction_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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package nds_test
import (
"errors"
"testing"
"github.com/qedus/nds"
"golang.org/x/net/context"
"google.golang.org/appengine"
"google.golang.org/appengine/datastore"
)
func TestTransactionOptions(t *testing.T) {
c, closeFunc := NewContext(t)
defer closeFunc()
type testEntity struct {
Val int
}
opts := &datastore.TransactionOptions{XG: true}
err := nds.RunInTransaction(c, func(tc context.Context) error {
for i := 0; i < 4; i++ {
key := datastore.NewIncompleteKey(tc, "Entity", nil)
if _, err := nds.Put(tc, key, &testEntity{i}); err != nil {
return err
}
}
return nil
}, opts)
if err != nil {
t.Fatal(err)
}
opts = &datastore.TransactionOptions{XG: false}
err = nds.RunInTransaction(c, func(tc context.Context) error {
for i := 0; i < 4; i++ {
key := datastore.NewIncompleteKey(tc, "Entity", nil)
if _, err := nds.Put(tc, key, &testEntity{i}); err != nil {
return err
}
}
return nil
}, opts)
if err == nil {
t.Fatal("expected cross-group error")
}
}
// TestClearNamespacedLocks tests to make sure that locks are cleared when
// RunInTransaction is using a namespace.
func TestClearNamespacedLocks(t *testing.T) {
c, closeFunc := NewContext(t)
defer closeFunc()
c, err := appengine.Namespace(c, "testnamespace")
if err != nil {
t.Fatal(err)
}
type testEntity struct {
Val int
}
key := datastore.NewKey(c, "TestEntity", "", 1, nil)
// Prime cache.
if err := nds.Get(c, key, &testEntity{}); err == nil {
t.Fatal("expected no such entity")
} else if err != datastore.ErrNoSuchEntity {
t.Fatal(err)
}
if err := nds.RunInTransaction(c, func(tc context.Context) error {
if err := nds.Get(tc, key, &testEntity{}); err == nil {
return errors.New("expected no such entity")
} else if err != datastore.ErrNoSuchEntity {
return err
}
if _, err := nds.Put(tc, key, &testEntity{3}); err != nil {
return err
}
return nil
}, nil); err != nil {
t.Fatal(err)
}
entity := &testEntity{}
if err := nds.Get(c, key, entity); err != nil {
t.Fatal(err)
}
if entity.Val != 3 {
t.Fatal("incorrect val")
}
}