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