Commit 4eb451a4 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 5c8cfac1
......@@ -21,9 +21,15 @@
package xtesting
import (
"bytes"
"fmt"
"os"
"os/exec"
"sync"
"testing"
"lab.nexedi.com/kirr/go123/xerr"
"lab.nexedi.com/kirr/neo/go/zodb"
)
var (
......@@ -80,3 +86,45 @@ func NeedPy(t testing.TB, modules ...string) {
// we verified everything - now it is ok not to skip.
return
}
// ZObject represents object state to be committed.
type ZObject struct {
Oid zodb.Oid
Data string
}
// ZPyCommit commits new transaction into database @ zurl with data specified by objv.
//
// The commit is performed via zodbtools/py.
func ZPyCommit(zurl string, at zodb.Tid, objv ...ZObject) (_ zodb.Tid, err error) {
defer xerr.Contextf(&err, "%s: zcommit @%s", zurl, at)
// prepare text input for `zodb commit`
zin := &bytes.Buffer{}
fmt.Fprintf(zin, "user %q\n", "author")
fmt.Fprintf(zin, "description %q\n", fmt.Sprintf("test commit; at=%s", at))
fmt.Fprintf(zin, "extension %q\n", "")
for _, obj := range objv {
fmt.Fprintf(zin, "obj %s %d null:00\n", obj.Oid, len(obj.Data))
zin.WriteString(obj.Data)
zin.WriteString("\n")
}
zin.WriteString("\n")
// run py `zodb commit`
cmd:= exec.Command("python2", "-m", "zodbtools.zodb", "commit", zurl, at.String())
cmd.Stdin = zin
cmd.Stderr = os.Stderr
out, err := cmd.Output()
if err != nil {
return zodb.InvalidTid, err
}
out = bytes.TrimSuffix(out, []byte("\n"))
tid, err := zodb.ParseTid(string(out))
if err != nil {
return zodb.InvalidTid, fmt.Errorf("committed, but invalid output: %s", err)
}
return tid, nil
}
// Copyright (C) 2018 Nexedi SA and Contributors.
// Copyright (C) 2019 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com>
//
// This program is free software: you can Use, Study, Modify and Redistribute
......@@ -19,4 +19,5 @@
package zodb
// TODO
func TestDB() {
}
......@@ -20,13 +20,10 @@
package fs1
import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"reflect"
"testing"
......@@ -34,7 +31,6 @@ import (
"lab.nexedi.com/kirr/neo/go/zodb"
"lab.nexedi.com/kirr/go123/exc"
"lab.nexedi.com/kirr/go123/xerr"
)
// one database transaction record
......@@ -362,54 +358,15 @@ func TestWatch(t *testing.T) {
workdir := xworkdir(t)
tfs := workdir + "/t.fs"
// Object represents object state to be committed.
type Object struct {
oid zodb.Oid
data string
}
// zcommit commits new transaction into tfs with data specified by objv.
zcommit := func(at zodb.Tid, objv ...Object) (_ zodb.Tid, err error) {
defer xerr.Contextf(&err, "zcommit @%s", at)
// prepare text input for `zodb commit`
zin := &bytes.Buffer{}
fmt.Fprintf(zin, "user %q\n", "author")
fmt.Fprintf(zin, "description %q\n", fmt.Sprintf("test commit; at=%s", at))
fmt.Fprintf(zin, "extension %q\n", "")
for _, obj := range objv {
fmt.Fprintf(zin, "obj %s %d null:00\n", obj.oid, len(obj.data))
zin.WriteString(obj.data)
zin.WriteString("\n")
}
zin.WriteString("\n")
// run py `zodb commit`
cmd:= exec.Command("python2", "-m", "zodbtools.zodb", "commit", tfs, at.String())
cmd.Stdin = zin
cmd.Stderr = os.Stderr
out, err := cmd.Output()
if err != nil {
return zodb.InvalidTid, err
}
out = bytes.TrimSuffix(out, []byte("\n"))
tid, err := zodb.ParseTid(string(out))
if err != nil {
return zodb.InvalidTid, fmt.Errorf("committed, but invalid output: %s", err)
}
return tid, nil
}
xcommit := func(at zodb.Tid, objv ...Object) zodb.Tid {
// xcommit commits new transaction into tfs with data specified by objv.
xcommit := func(at zodb.Tid, objv ...xtesting.ZObject) zodb.Tid {
t.Helper()
tid, err := zcommit(at, objv...); X(err)
tid, err := xtesting.ZPyCommit(tfs, at, objv...); X(err)
return tid
}
// force tfs creation & open tfs at go side
at := xcommit(0, Object{0, "data0"})
at := xcommit(0, xtesting.ZObject{0, "data0"})
watchq := make(chan zodb.CommitEvent)
fs := xfsopenopt(t, tfs, &zodb.DriverOptions{ReadOnly: true, Watchq: watchq})
......@@ -450,8 +407,8 @@ func TestWatch(t *testing.T) {
data0 := fmt.Sprintf("data0.%d", i)
datai := fmt.Sprintf("data%d", i)
at = xcommit(at,
Object{0, data0},
Object{i, datai})
xtesting.ZObject{0, data0},
xtesting.ZObject{i, datai})
// TODO also test for watcher errors
e := <-watchq
......
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