Commit 3041fee6 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent afe3cd61
...@@ -21,6 +21,7 @@ package zeo ...@@ -21,6 +21,7 @@ package zeo
import ( import (
"context" "context"
"fmt"
"io/ioutil" "io/ioutil"
"net/url" "net/url"
"os" "os"
...@@ -34,6 +35,7 @@ import ( ...@@ -34,6 +35,7 @@ import (
"lab.nexedi.com/kirr/go123/exc" "lab.nexedi.com/kirr/go123/exc"
"lab.nexedi.com/kirr/go123/xerr" "lab.nexedi.com/kirr/go123/xerr"
"lab.nexedi.com/kirr/go123/xnet"
) )
// ZEOPySrv represents running ZEO/py server. // ZEOPySrv represents running ZEO/py server.
...@@ -47,14 +49,23 @@ type ZEOPySrv struct { ...@@ -47,14 +49,23 @@ type ZEOPySrv struct {
errExit error // error from Wait errExit error // error from Wait
} }
type ZEOPyOptions struct {
msgpack bool // whether to advertise msgpack
}
func StartZEOPySrv(fs1path string) (_ *ZEOPySrv, err error) { // StartZEOPySrv starts ZEO/py server for FileStorage database located at fs1path.
func StartZEOPySrv(fs1path string, opt ZEOPyOptions) (_ *ZEOPySrv, err error) {
defer xerr.Contextf(&err, "startzeo %s", fs1path) defer xerr.Contextf(&err, "startzeo %s", fs1path)
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
z := &ZEOPySrv{fs1path: fs1path, cancel: cancel, done: make(chan struct{})} z := &ZEOPySrv{fs1path: fs1path, cancel: cancel, done: make(chan struct{})}
z.pysrv = exec.CommandContext(ctx, "python", "-m", "ZEO.runzeo", "-f", fs1path, "-a", z.zaddr()) z.pysrv = exec.CommandContext(ctx, "python", "-m", "ZEO.runzeo", "-f", fs1path, "-a", z.zaddr())
msgpack := ""
if opt.msgpack {
msgpack = "y"
}
z.pysrv.Env = append(os.Environ(), "ZEO_MSGPACK="+msgpack)
z.pysrv.Stdin = nil z.pysrv.Stdin = nil
z.pysrv.Stdout = os.Stdout z.pysrv.Stdout = os.Stdout
z.pysrv.Stderr = os.Stderr z.pysrv.Stderr = os.Stderr
...@@ -118,6 +129,46 @@ func (z *ZEOPySrv) Close() (err error) { ...@@ -118,6 +129,46 @@ func (z *ZEOPySrv) Close() (err error) {
// -------- // --------
// withZEOPySrv spawns new ZEO/py server and runs f in that environment.
func withZEOPySrv(t *testing.T, opt ZEOPyOptions, f func(zpy *ZEOPySrv)) {
X := mkFatalIf(t)
t.Helper()
needZEOpy(t)
work := xtempdir(t)
defer os.RemoveAll(work)
fs1path := work + "/1.fs"
zpy, err := StartZEOPySrv(fs1path, opt); X(err)
defer func() {
err := zpy.Close(); X(err)
}()
f(zpy)
}
func TestHandshake(t *testing.T) {
X := mkFatalIf(t)
for _, msgpack := range []bool{false, true} {
t.Run(fmt.Sprintf("msgpack=%v", msgpack), func(t *testing.T) {
withZEOPySrv(t, ZEOPyOptions{msgpack: msgpack}, func(zpy *ZEOPySrv) {
ctx := context.Background()
net := xnet.NetPlain("unix")
zlink, err := dialZLink(ctx, net, zpy.zaddr()); X(err)
defer func() {
err := zlink.Close(); X(err)
}()
ewant := byte('Z')
if msgpack { ewant = byte('M') }
if zlink.encoding != ewant {
t.Fatalf("handshake: encoding=%c ; want %c", zlink.encoding, ewant)
}
})
})
}
}
func TestLoad(t *testing.T) { func TestLoad(t *testing.T) {
X := exc.Raiseif X := exc.Raiseif
needZEOpy(t) needZEOpy(t)
...@@ -132,7 +183,7 @@ func TestLoad(t *testing.T) { ...@@ -132,7 +183,7 @@ func TestLoad(t *testing.T) {
txnvOk, err := xtesting.LoadDB(fs1path); X(err) txnvOk, err := xtesting.LoadDB(fs1path); X(err)
zpy, err := StartZEOPySrv(fs1path); X(err) zpy, err := StartZEOPySrv(fs1path, ZEOPyOptions{}); X(err)
defer func() { defer func() {
err := zpy.Close(); X(err) err := zpy.Close(); X(err)
}() }()
...@@ -153,7 +204,7 @@ func TestWatch(t *testing.T) { ...@@ -153,7 +204,7 @@ func TestWatch(t *testing.T) {
defer os.RemoveAll(work) defer os.RemoveAll(work)
fs1path := work + "/1.fs" fs1path := work + "/1.fs"
zpy, err := StartZEOPySrv(fs1path); X(err) zpy, err := StartZEOPySrv(fs1path, ZEOPyOptions{}); X(err)
defer func() { defer func() {
err := zpy.Close(); X(err) err := zpy.Close(); X(err)
}() }()
...@@ -186,6 +237,13 @@ func xtempdir(t *testing.T) string { ...@@ -186,6 +237,13 @@ func xtempdir(t *testing.T) string {
return tmpd return tmpd
} }
func mkFatalIf(t *testing.T) func(error) {
return func(err error) {
if err != nil {
t.Fatal(err)
}
}
}
func needZEOpy(t *testing.T) { func needZEOpy(t *testing.T) {
xtesting.NeedPy(t, "ZEO") // XXX +msgpack? xtesting.NeedPy(t, "ZEO") // XXX +msgpack?
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment