Skip to content

Instantly share code, notes, and snippets.

@sideshow
Created August 24, 2018 10:12
Show Gist options
  • Save sideshow/79687342c5f7021bb7d019c1baeee43d to your computer and use it in GitHub Desktop.
Save sideshow/79687342c5f7021bb7d019c1baeee43d to your computer and use it in GitHub Desktop.
package main
import (
"flag"
"log"
"sync"
"github.com/apple/foundationdb/bindings/go/src/fdb"
)
func main() {
tests := flag.Int("tests", 0, "The amount of times we will run this test")
concurrency := flag.Int("concurrency", 0, "The amount of concurrent transactions that we will run")
flag.Parse()
if *tests == 0 || *concurrency == 0 {
flag.Usage()
}
var wg sync.WaitGroup
fdb.MustAPIVersion(510)
db := fdb.MustOpenDefault()
for i := 0; i < *tests; i++ {
var mu sync.Mutex
var succeedCount = 0
key := fdb.Key(string(i))
for i := 0; i < *concurrency; i++ {
wg.Add(1)
go func() {
tr, e := db.CreateTransaction()
if e != nil {
log.Fatal(e)
}
tr.AddReadConflictKey(key)
tr.Set(key, []byte(""))
err := tr.Commit().Get()
if err == nil {
mu.Lock()
succeedCount++
mu.Unlock()
}
wg.Done()
}()
}
wg.Wait()
if succeedCount == 1 {
log.Printf("Transaction committed %v times", succeedCount)
} else {
log.Fatalf("Error: Should have committed 1 time. Committed %v times", succeedCount)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment