Esempio n. 1
0
func (r *Request) Respond(code RetCode, val interface{}) {
	var body []byte
	if o, ok := val.(Body); ok {
		body = o
	} else {
		body = marshal.Write(val)
	}
	r.RespondBytes(code, body)
}
Esempio n. 2
0
func CallMsgBody(serv Service, m RequestType, r interface{}) *Response {
	var body []byte
	var ok bool
	if body, ok = r.(Body); !ok {
		body = marshal.Write(r)
	}
	res := make(Chan, 1)
	serv.Send(&Request{Msg: m, Body: body, Responder: res})
	return <-res
}
Esempio n. 3
0
func SendMsgBody(serv Service, m RequestType, r interface{}) (*Request, Chan) {
	var body []byte
	var ok bool
	if body, ok = r.(Body); !ok {
		body = marshal.Write(r)
	}
	res := make(Chan, 1)
	req := &Request{Msg: m, Body: body, Responder: res}
	serv.Send(req)
	return req, res
}
Esempio n. 4
0
func TestSelect(t *testing.T) {
	var s SelectReq
	var b, n []byte
	s = SelectReq{
		Space: 3, Index: 2, Offset: 4, Limit: 100,
		Keys: SKey{Id: 1, Domain: "mail.ru"},
	}
	b = []byte{
		3, 0, 0, 0, 2, 0, 0, 0, 4, 0, 0, 0, 100, 0, 0, 0,
		1, 0, 0, 0,
		2, 0, 0, 0,
		4, 1, 0, 0, 0, 7, 'm', 'a', 'i', 'l', '.', 'r', 'u',
	}
	n = marshal.Write(s)
	if !bytes.Equal(b, n) {
		t.Errorf("Select not match %+v\ngot:\t[% x]\nneed:\t[% x]", s, n, b)
	}
	s = SelectReq{
		Space: 3, Index: 2, Offset: 4, Limit: 100,
		Keys: &SKey{Id: 1, Domain: "mail.ru"},
	}
	n = marshal.Write(s)
	if !bytes.Equal(b, n) {
		t.Errorf("Select not match %+v\ngot:\t[% x]\nneed:\t[% x]", s, n, b)
	}
	s = SelectReq{
		Space: 3, Index: 2, Offset: 4, Limit: 100,
		Keys: []SKey{
			{Id: 1, Domain: "mail.ru"},
			{Id: -1, Domain: "google.com"},
		},
	}
	b = []byte{
		3, 0, 0, 0, 2, 0, 0, 0, 4, 0, 0, 0, 100, 0, 0, 0,
		2, 0, 0, 0,
		2, 0, 0, 0,
		4, 1, 0, 0, 0, 7, 'm', 'a', 'i', 'l', '.', 'r', 'u',
		2, 0, 0, 0,
		4, 255, 255, 255, 255, 10, 'g', 'o', 'o', 'g', 'l', 'e', '.', 'c', 'o', 'm',
	}
	n = marshal.Write(s)
	if !bytes.Equal(b, n) {
		t.Errorf("Select not match %+v\ngot:\t[% x]\nneed:\t[% x]", s, n, b)
	}
	s = SelectReq{
		Space: 3, Index: 2, Offset: 4, Limit: 100,
		Keys: &[]SKey{
			{Id: 1, Domain: "mail.ru"},
			{Id: -1, Domain: "google.com"},
		},
	}
	n = marshal.Write(s)
	if !bytes.Equal(b, n) {
		t.Errorf("Select not match %+v\ngot:\t[% x]\nneed:\t[% x]", s, n, b)
	}
	s = SelectReq{
		Space: 3, Index: 2, Offset: 4, Limit: 100,
		Keys: []*SKey{
			&SKey{Id: 1, Domain: "mail.ru"},
			&SKey{Id: -1, Domain: "google.com"},
		},
	}
	n = marshal.Write(s)
	if !bytes.Equal(b, n) {
		t.Errorf("Select not match %+v\ngot:\t[% x]\nneed:\t[% x]", s, n, b)
	}
	s = SelectReq{
		Space: 3, Index: 2, Offset: 4, Limit: 100,
		Keys: []interface{}{
			int32(1),
			[...]int32{1},
			[]int32{-1},
			&SKey{Id: -1, Domain: "google.com"},
		},
	}
	b = []byte{
		3, 0, 0, 0, 2, 0, 0, 0, 4, 0, 0, 0, 100, 0, 0, 0,
		4, 0, 0, 0,
		1, 0, 0, 0, 4, 1, 0, 0, 0,
		1, 0, 0, 0, 4, 1, 0, 0, 0,
		1, 0, 0, 0, 4, 255, 255, 255, 255,
		2, 0, 0, 0,
		4, 255, 255, 255, 255, 10, 'g', 'o', 'o', 'g', 'l', 'e', '.', 'c', 'o', 'm',
	}
	n = marshal.Write(s)
	if !bytes.Equal(b, n) {
		t.Errorf("Select not match %+v\ngot:\t[% x]\nneed:\t[% x]", s, n, b)
	}
}