aboutsummaryrefslogtreecommitdiff
path: root/stm_test.go
diff options
context:
space:
mode:
authorlukechampine <luke.champine@gmail.com>2016-03-30 21:19:17 -0400
committerlukechampine <luke.champine@gmail.com>2016-03-30 21:19:17 -0400
commit47a426e7267368e78c2de57a97b8d2cfb72a4486 (patch)
tree2403062d0c48c12c33a30361af32a09522acabfc /stm_test.go
parentefficient retries via sync.Cond (diff)
downloadstm-47a426e7267368e78c2de57a97b8d2cfb72a4486.tar.gz
stm-47a426e7267368e78c2de57a97b8d2cfb72a4486.tar.xz
add TestRetry
Diffstat (limited to 'stm_test.go')
-rw-r--r--stm_test.go28
1 files changed, 28 insertions, 0 deletions
diff --git a/stm_test.go b/stm_test.go
index 795a8d8..c48d704 100644
--- a/stm_test.go
+++ b/stm_test.go
@@ -60,6 +60,34 @@ func TestReadVerify(t *testing.T) {
}
}
+func TestRetry(t *testing.T) {
+ x := NewVar(10)
+ // spawn 10 transactions, one every 10 milliseconds. This will decrement x
+ // to 0 over the course of 100 milliseconds.
+ go func() {
+ for i := 0; i < 10; i++ {
+ time.Sleep(10 * time.Millisecond)
+ Atomically(func(tx *Tx) {
+ cur := tx.Get(x).(int)
+ tx.Set(x, cur-1)
+ })
+ }
+ }()
+ // Each time we read x before the above loop has finished, we need to
+ // retry. This should result in no more than 1 retry per transaction.
+ retry := 0
+ Atomically(func(tx *Tx) {
+ cur := tx.Get(x).(int)
+ if cur != 0 {
+ retry++
+ tx.Retry()
+ }
+ })
+ if retry > 10 {
+ t.Fatal("should have retried at most 10 times, got", retry)
+ }
+}
+
func BenchmarkAtomicGet(b *testing.B) {
x := NewVar(0)
for i := 0; i < b.N; i++ {