Commit e938faba authored by Kirill Smelkov's avatar Kirill Smelkov

X fix current tests

parent 7ee9a615
...@@ -137,19 +137,19 @@ type NodeUUID int32 ...@@ -137,19 +137,19 @@ type NodeUUID int32
var ErrDecodeOverflow = errors.New("decode: bufer overflow") 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 { type NEOEncoder interface {
// NEOEncodedInfo returns message code needed to be used for the packet // NEOEncodedInfo returns message code needed to be used for the packet
// on the wire and how much space is needed to encode payload // on the wire and how much space is needed to encode payload
// XXX naming? // XXX naming?
NEOEncodedInfo() (msgCode uint16, payloadLen int) NEOEncodedInfo() (msgCode uint16, payloadLen int)
// perform the encoding. // NEOEncode performs the encoding.
// len(buf) must be >= payloadLen returned by NEOEncodedInfo // len(buf) must be >= payloadLen returned by NEOEncodedInfo
NEOEncode(buf []byte) NEOEncode(buf []byte)
} }
// NEODecoder is interface for unmarshalling objects from wire format // NEODecoder is interface for unmarshalling packets from wire format
type NEODecoder interface { type NEODecoder interface {
NEODecode(data []byte) (nread int, err error) NEODecode(data []byte) (nread int, err error)
} }
......
...@@ -215,7 +215,7 @@ func TestPktMarshal(t *testing.T) { ...@@ -215,7 +215,7 @@ func TestPktMarshal(t *testing.T) {
// map[uint32]UUID + trailing ... // map[uint32]UUID + trailing ...
{&CheckReplicas{ {&CheckReplicas{
PartitionDict: map[uint32]NodeID{ PartitionDict: map[uint32]NodeUUID{
1: 7, 1: 7,
2: 9, 2: 9,
7: 3, 7: 3,
...@@ -234,7 +234,7 @@ func TestPktMarshal(t *testing.T) { ...@@ -234,7 +234,7 @@ func TestPktMarshal(t *testing.T) {
}, },
// uint32, []uint32 // 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), 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 { ...@@ -235,40 +235,59 @@ func Ask(conn *Conn, req NEOEncoder, resp NEODecoder) error {
return err 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 // Expect receives 1 packet and expects it to be exactly of msg type
// XXX naming (-> Recv1 ?) // XXX naming (-> Recv1 ?)
func Expect(conn *Conn, msg NEODecoder) error { func Expect(conn *Conn, msg NEODecoder) (err error) {
pkt, err := conn.Recv() pkt, err := conn.Recv()
if err != nil { if err != nil {
return err return err
} }
// received ok. Now it is all decoding
// XXX dup wrt RecvAndDecode // XXX dup wrt RecvAndDecode
pkth := pkt.Header() pkth := pkt.Header()
msgCode := ntoh16(pkth.MsgCode) msgCode := ntoh16(pkth.MsgCode)
msgType := pktTypeRegistry[msgCode] msgType := pktTypeRegistry[msgCode]
if msgType == nil { 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) { // FIXME -> better compare on just msgCode
// Error response if msgType != reflect.TypeOf(msg).Elem() {
// unexpected Error response
if msgType == reflect.TypeOf(Error{}) { if msgType == reflect.TypeOf(Error{}) {
errResp := Error{} errResp := Error{}
_, err = errResp.NEODecode(pkt.Payload()) _, err = errResp.NEODecode(pkt.Payload())
if err != nil { 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()) _, err = msg.NEODecode(pkt.Payload())
if err != nil { if err != nil {
return err // XXX err ctx return &ProtoError{conn, err}
} }
return nil 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