Commit e938faba authored by Kirill Smelkov's avatar Kirill Smelkov

X fix current tests

parent 7ee9a615
......@@ -137,19 +137,19 @@ type NodeUUID int32
var ErrDecodeOverflow = errors.New("decode: bufer overflow")
// NEOEncoder is interface for marshaling objects to wire format
// NEOEncoder is interface for marshaling packets to wire format
type NEOEncoder interface {
// NEOEncodedInfo returns message code needed to be used for the packet
// on the wire and how much space is needed to encode payload
// XXX naming?
NEOEncodedInfo() (msgCode uint16, payloadLen int)
// perform the encoding.
// NEOEncode performs the encoding.
// len(buf) must be >= payloadLen returned by NEOEncodedInfo
NEOEncode(buf []byte)
}
// NEODecoder is interface for unmarshalling objects from wire format
// NEODecoder is interface for unmarshalling packets from wire format
type NEODecoder interface {
NEODecode(data []byte) (nread int, err error)
}
......
......@@ -215,7 +215,7 @@ func TestPktMarshal(t *testing.T) {
// map[uint32]UUID + trailing ...
{&CheckReplicas{
PartitionDict: map[uint32]NodeID{
PartitionDict: map[uint32]NodeUUID{
1: 7,
2: 9,
7: 3,
......@@ -234,7 +234,7 @@ func TestPktMarshal(t *testing.T) {
},
// uint32, []uint32
{&PartitionCorrupted{7, []NodeID{1,3,9,4}},
{&PartitionCorrupted{7, []NodeUUID{1,3,9,4}},
u32(7) + u32(4) + u32(1) + u32(3) + u32(9) + u32(4),
},
......
......@@ -235,40 +235,59 @@ func Ask(conn *Conn, req NEOEncoder, resp NEODecoder) error {
return err
}
// ProtoError is returned when there waa a protocol error, like receiving
// unexpected packet or packet with wrong header
type ProtoError struct {
Conn *Conn
Err error
}
func (e *ProtoError) Error() string {
return fmt.Sprintf("%v: %v", e.Conn, e.Err)
}
// Expect receives 1 packet and expects it to be exactly of msg type
// XXX naming (-> Recv1 ?)
func Expect(conn *Conn, msg NEODecoder) error {
func Expect(conn *Conn, msg NEODecoder) (err error) {
pkt, err := conn.Recv()
if err != nil {
return err
}
// received ok. Now it is all decoding
// XXX dup wrt RecvAndDecode
pkth := pkt.Header()
msgCode := ntoh16(pkth.MsgCode)
msgType := pktTypeRegistry[msgCode]
if msgType == nil {
return fmt.Errorf("invalid msgCode (%d)", msgCode) // XXX err ctx
return &ProtoError{conn, fmt.Errorf("invalid msgCode (%d)", msgCode)}
}
if msgType != reflect.TypeOf(msg) {
// Error response
// FIXME -> better compare on just msgCode
if msgType != reflect.TypeOf(msg).Elem() {
// unexpected Error response
if msgType == reflect.TypeOf(Error{}) {
errResp := Error{}
_, err = errResp.NEODecode(pkt.Payload())
if err != nil {
return err // XXX err ctx
return &ProtoError{conn, err}
}
return errDecode(&errResp) // XXX err ctx
// FIXME clarify error decoding logic:
// - in some cases Error is one of "expected" answers (e.g. Ask(GetObject))
// - in other cases Error is completely not expected
// (e.g. getting 1st packet on connection)
return errDecode(&errResp) // XXX err ctx vs ^^^ errcontextf ?
}
return fmt.Errorf("unexpected packet: %T", msgType) // XXX err ctx -> + conn ?
return &ProtoError{conn, fmt.Errorf("unexpected packet: %v", msgType)}
}
_, err = msg.NEODecode(pkt.Payload())
if err != nil {
return err // XXX err ctx
return &ProtoError{conn, err}
}
return nil
......
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