Created
August 29, 2024 19:23
-
-
Save gm42/d845c689a4c85a5cd59897497b6ff4eb to your computer and use it in GitHub Desktop.
Testcase for FoundationDB Go binding issue 11621
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"time" | |
"github.com/apple/foundationdb/bindings/go/src/fdb" | |
) | |
func main() { | |
const useMultiVersion = true | |
err := setup(useMultiVersion) | |
if err != nil { | |
panic(err) | |
} | |
for _, d := range []time.Duration{0, time.Millisecond * 50} { | |
err := runTestcase("/fdb/cluster-file", useMultiVersion, d) | |
if err != nil { | |
panic(err) | |
} | |
} | |
} | |
func setup(useMultiVersion bool) error { | |
err := fdb.APIVersion(730) | |
if err != nil { | |
return err | |
} | |
v, err := fdb.GetAPIVersion() | |
if err != nil { | |
return err | |
} | |
if v != 730 { | |
return fmt.Errorf("unexpected API version: %v", v) | |
} | |
if useMultiVersion { | |
if err := fdb.Options().SetExternalClientDirectory("/usr/lib/multiversion-fdb"); err != nil { | |
return fmt.Errorf("failed to enable multiversion client: %w", err) | |
} | |
} | |
return nil | |
} | |
func runTestcase(clusterFile string, useMultiVersion bool, initialDelay time.Duration) error { | |
db, err := fdb.OpenDatabase(clusterFile) | |
if err != nil { | |
b, _ := os.ReadFile(clusterFile) | |
return fmt.Errorf("failed to initialize FDB with cluster file %v (content: %q): %w", clusterFile, string(b), err) | |
} | |
var emptyD time.Duration | |
if initialDelay != emptyD { | |
fmt.Printf("initial delay of %.2f seconds\n", initialDelay.Seconds()) | |
time.Sleep(initialDelay) | |
} else { | |
fmt.Printf("no initial delay\n") | |
} | |
var f fdb.FutureInt64 | |
_, err = db.ReadTransact(func(rt fdb.ReadTransaction) (interface{}, error) { | |
f = rt.GetReadVersion() | |
return nil, nil | |
}) | |
if err != nil { | |
fmt.Printf("initial empty R/O tx: %v (using multiversion client = %v)\n", err, useMultiVersion) | |
} else { | |
rv, err := f.Get() | |
if err != nil { | |
fmt.Printf("initial empty R/O tx, failed to get future: %v (using multiversion client = %v)\n", err, useMultiVersion) | |
} else { | |
fmt.Printf("initial empty R/O tx: got read version %v (using multiversion client = %v)\n", rv, useMultiVersion) | |
} | |
} | |
// second tx always works | |
_, err = db.ReadTransact(func(rt fdb.ReadTransaction) (interface{}, error) { | |
f = rt.GetReadVersion() | |
return nil, nil | |
}) | |
if err != nil { | |
return err | |
} | |
rv, err := f.Get() | |
if err != nil { | |
return err | |
} | |
fmt.Printf("second empty R/O tx: got read version %v (using multiversion client = %v)\n", rv, useMultiVersion) | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment