etcdserver/etcdhttp: add simple test for peers send

release-2.0
Jonathan Boulle 2014-09-15 20:50:51 -07:00
parent 6f17fa6c90
commit 30b70e18c5
2 changed files with 78 additions and 3 deletions

View File

@ -103,11 +103,11 @@ func send(p Peers, m raftpb.Message) {
// TODO: unknown peer id.. what do we do? I
// don't think his should ever happen, need to
// look into this further.
log.Println("etcdhttp: no addr for %d", m.To)
log.Printf("etcdhttp: no addr for %d", m.To)
return
}
url += "/raft"
url += raftPrefix
// TODO: don't block. we should be able to have 1000s
// of messages out at a time.

View File

@ -5,7 +5,10 @@ import (
"net/http/httptest"
"reflect"
"sort"
"strings"
"testing"
"github.com/coreos/etcd/raft/raftpb"
)
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")
}
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
ts.Close()
@ -162,3 +165,75 @@ func TestHttpPost(t *testing.T) {
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()
}
}