Commit a95287ab authored by Kirill Smelkov's avatar Kirill Smelkov

X draft tests for Load via Client -- Storage

parent cb2ce72c
......@@ -61,7 +61,7 @@ func (c *Client) LastTid() (zodb.Tid, error) {
switch reply := reply.(type) {
case *Error:
return 0, reply // XXX err context
return 0, errDecode(reply) // XXX err context
default:
// XXX more error context ?
return 0, fmt.Errorf("protocol error: unexpected reply: %T", reply)
......@@ -95,7 +95,7 @@ func (c *Client) Load(xid zodb.Xid) (data []byte, tid zodb.Tid, err error) {
switch reply := reply.(type) {
case *Error:
return nil, 0, reply // XXX err context
return nil, 0, errDecode(reply) // XXX err context
default:
// XXX more error context ?
return nil, 0, fmt.Errorf("protocol error: unexpected reply: %T", reply)
......
......@@ -19,7 +19,10 @@ package neo
// test interaction between nodes
import (
"bytes"
"context"
"io"
"reflect"
"testing"
"../zodb"
......@@ -65,7 +68,7 @@ func TestClientStorage(t *testing.T) {
ziter := zstor.Iterate(zodb.Tid(0), zodb.TidMax)
for {
txni, dataIter, err := ziter.NextTxn()
_, dataIter, err := ziter.NextTxn()
if err == io.EOF {
break
}
......@@ -73,8 +76,6 @@ func TestClientStorage(t *testing.T) {
t.Fatalf("ziter.NextTxn: %v", err)
}
txni.Tid
for {
datai, err := dataIter.NextData()
if err == io.EOF {
......@@ -84,15 +85,30 @@ func TestClientStorage(t *testing.T) {
t.Fatalf("ziter.NextData: %v", err)
}
datai.Oid
datai.Tid
.Data
.DataTid
// XXX .Data = nil means deleted
for _, tidBefore := range []bool{false, true} {
xid := zodb.Xid{Oid: datai.Oid} // {=,<}tid:oid
xid.Tid = datai.Tid
xid.TidBefore = tidBefore
if tidBefore {
xid.Tid++
}
data, tid, err := C.Load(xid)
if datai.Data != nil {
if !(bytes.Equal(data, datai.Data) && tid == datai.Tid && err == nil) {
t.Fatalf("load: %v:\nhave: %v %v %q\nwant: %v nil %q",
xid, tid, err, data, datai.Tid, datai.Data)
}
} else {
// deleted
errWant := &zodb.ErrXidMissing{xid}
if !(data == nil && tid == 0 && reflect.DeepEqual(err, errWant)) {
t.Fatalf("load: %v:\nhave: %v, %#v, %#v\nwant: %v, %#v, %#v",
xid, tid, err, data, zodb.Tid(0), errWant, []byte(nil))
}
}
}
zodb.Xid{zodb.XTid{datai.Tid, false}, datai.Oid} // =tid:oid
// TODO check Load
}
}
......
......@@ -98,6 +98,37 @@ func (stor *Storage) ServeLink(ctx context.Context, link *NodeLink) {
// TODO wait all spawned serveConn
}
// XXX move err{Encode,Decode} out of here
// errEncode translates an error into Error packet
func errEncode(err error) *Error {
switch err := err.(type) {
case *Error:
return err
case *zodb.ErrXidMissing:
// XXX abusing message for xid
return &Error{Code: OID_NOT_FOUND, Message: err.Xid.String()}
default:
return &Error{Code: BROKEN_NODE, Message: err.Error()}
}
}
// errDecode decodes error from Error packet
func errDecode(e *Error) error {
switch e.Code {
case OID_NOT_FOUND:
xid, err := zodb.ParseXid(e.Message) // XXX abusing message for xid
if err == nil {
return &zodb.ErrXidMissing{xid}
}
}
return e
}
// ServeClient serves incoming connection on which peer identified itself as client
// XXX +error return?
func (stor *Storage) ServeClient(ctx context.Context, conn *Conn) {
......@@ -141,7 +172,7 @@ func (stor *Storage) ServeClient(ctx context.Context, conn *Conn) {
data, tid, err := stor.zstor.Load(xid)
if err != nil {
// TODO translate err to NEO protocol error codes
reply = &Error{Code: 0, Message: err.Error()} // XXX Code
reply = errEncode(err)
} else {
reply = &AnswerGetObject{
Oid: xid.Oid,
......@@ -163,7 +194,7 @@ func (stor *Storage) ServeClient(ctx context.Context, conn *Conn) {
lastTid, err := stor.zstor.LastTid()
if err != nil {
reply = &Error{Code:0, Message: err.Error()}
reply = errEncode(err)
} else {
reply = &AnswerLastTransaction{lastTid}
}
......
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