aboutsummaryrefslogtreecommitdiff
path: root/stm_test.go
diff options
context:
space:
mode:
authorlukechampine <luke.champine@gmail.com>2016-04-02 16:41:58 -0400
committerlukechampine <luke.champine@gmail.com>2016-04-02 16:41:58 -0400
commit81f500bba70ce81c95630685eaa4a7d01e21a4b0 (patch)
tree83278ae89587ad36fdd4b5839358f0233c7e1974 /stm_test.go
parentdocument weak equality (diff)
downloadstm-81f500bba70ce81c95630685eaa4a7d01e21a4b0.tar.gz
stm-81f500bba70ce81c95630685eaa4a7d01e21a4b0.tar.xz
add TestVerify
Diffstat (limited to 'stm_test.go')
-rw-r--r--stm_test.go35
1 files changed, 35 insertions, 0 deletions
diff --git a/stm_test.go b/stm_test.go
index 5849fcf..06a3238 100644
--- a/stm_test.go
+++ b/stm_test.go
@@ -88,6 +88,41 @@ func TestRetry(t *testing.T) {
}
}
+func TestVerify(t *testing.T) {
+ // tx.verify should check more than pointer equality
+ type foo struct {
+ i int
+ }
+ x := NewVar(&foo{3})
+ read := make(chan struct{})
+
+ // spawn a transaction that modifies x
+ go func() {
+ Atomically(func(tx *Tx) {
+ <-read
+ rx := tx.Get(x).(*foo)
+ rx.i = 7
+ tx.Set(x, rx)
+ })
+ read <- struct{}{}
+ // other tx should retry, so we need to read/send again
+ read <- <-read
+ }()
+
+ // spawn a transaction that reads x, then y. The other tx will modify x in
+ // between the reads, causing this tx to retry.
+ var i int
+ Atomically(func(tx *Tx) {
+ f := tx.Get(x).(*foo)
+ i = f.i
+ read <- struct{}{}
+ <-read // wait for other tx to complete
+ })
+ if i == 3 {
+ t.Fatal("verify did not retry despite modified Var", i)
+ }
+}
+
func BenchmarkAtomicGet(b *testing.B) {
x := NewVar(0)
for i := 0; i < b.N; i++ {