etcdserver/etcdhttp: add simple test for peers send
parent
6f17fa6c90
commit
30b70e18c5
|
@ -103,11 +103,11 @@ func send(p Peers, m raftpb.Message) {
|
||||||
// TODO: unknown peer id.. what do we do? I
|
// TODO: unknown peer id.. what do we do? I
|
||||||
// don't think his should ever happen, need to
|
// don't think his should ever happen, need to
|
||||||
// look into this further.
|
// look into this further.
|
||||||
log.Println("etcdhttp: no addr for %d", m.To)
|
log.Printf("etcdhttp: no addr for %d", m.To)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
url += "/raft"
|
url += raftPrefix
|
||||||
|
|
||||||
// TODO: don't block. we should be able to have 1000s
|
// TODO: don't block. we should be able to have 1000s
|
||||||
// of messages out at a time.
|
// of messages out at a time.
|
||||||
|
|
|
@ -5,7 +5,10 @@ import (
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/coreos/etcd/raft/raftpb"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestPeers(t *testing.T) {
|
func TestPeers(t *testing.T) {
|
||||||
|
@ -152,7 +155,7 @@ func TestHttpPost(t *testing.T) {
|
||||||
t.Errorf("#%d: Method=%q, want %q", i, tr.Method, "POST")
|
t.Errorf("#%d: Method=%q, want %q", i, tr.Method, "POST")
|
||||||
}
|
}
|
||||||
if ct := tr.Header.Get("Content-Type"); ct != "application/protobuf" {
|
if ct := tr.Header.Get("Content-Type"); ct != "application/protobuf" {
|
||||||
t.Errorf("%#d: Content-Type=%q, want %q", ct, "application/protobuf")
|
t.Errorf("#%d: Content-Type=%q, want %q", i, ct, "application/protobuf")
|
||||||
}
|
}
|
||||||
tr = nil
|
tr = nil
|
||||||
ts.Close()
|
ts.Close()
|
||||||
|
@ -162,3 +165,75 @@ func TestHttpPost(t *testing.T) {
|
||||||
t.Errorf("httpPost with bad URL returned true unexpectedly!")
|
t.Errorf("httpPost with bad URL returned true unexpectedly!")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSend(t *testing.T) {
|
||||||
|
var tr *http.Request
|
||||||
|
var rc int
|
||||||
|
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
tr = r
|
||||||
|
w.WriteHeader(rc)
|
||||||
|
})
|
||||||
|
tests := []struct {
|
||||||
|
m raftpb.Message
|
||||||
|
code int
|
||||||
|
|
||||||
|
ok bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
// all good
|
||||||
|
raftpb.Message{
|
||||||
|
To: 42,
|
||||||
|
Type: 4,
|
||||||
|
},
|
||||||
|
http.StatusNoContent,
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// bad response from server should be silently ignored
|
||||||
|
raftpb.Message{
|
||||||
|
To: 42,
|
||||||
|
Type: 2,
|
||||||
|
},
|
||||||
|
http.StatusInternalServerError,
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// unknown destination!
|
||||||
|
raftpb.Message{
|
||||||
|
To: 3,
|
||||||
|
Type: 2,
|
||||||
|
},
|
||||||
|
0,
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, tt := range tests {
|
||||||
|
tr = nil
|
||||||
|
rc = tt.code
|
||||||
|
ts := httptest.NewServer(h)
|
||||||
|
ps := Peers{
|
||||||
|
42: []string{strings.TrimPrefix(ts.URL, "http://")},
|
||||||
|
}
|
||||||
|
send(ps, tt.m)
|
||||||
|
|
||||||
|
if !tt.ok {
|
||||||
|
if tr != nil {
|
||||||
|
t.Errorf("#%d: got request=%#v, want nil", i, tr)
|
||||||
|
}
|
||||||
|
ts.Close()
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if tr.Method != "POST" {
|
||||||
|
t.Errorf("#%d: Method=%q, want %q", i, tr.Method, "POST")
|
||||||
|
}
|
||||||
|
if ct := tr.Header.Get("Content-Type"); ct != "application/protobuf" {
|
||||||
|
t.Errorf("#%d: Content-Type=%q, want %q", i, ct, "application/protobuf")
|
||||||
|
}
|
||||||
|
if tr.URL.String() != "/raft" {
|
||||||
|
t.Errorf("#%d: URL=%q, want %q", i, tr.URL.String(), "/raft")
|
||||||
|
}
|
||||||
|
ts.Close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue