From 30b70e18c53ba2eb20c51d58e3e6b6e3e997664c Mon Sep 17 00:00:00 2001 From: Jonathan Boulle Date: Mon, 15 Sep 2014 20:50:51 -0700 Subject: [PATCH] etcdserver/etcdhttp: add simple test for peers send --- etcdserver/etcdhttp/peers.go | 4 +- etcdserver/etcdhttp/peers_test.go | 77 ++++++++++++++++++++++++++++++- 2 files changed, 78 insertions(+), 3 deletions(-) diff --git a/etcdserver/etcdhttp/peers.go b/etcdserver/etcdhttp/peers.go index cd5fe0e43..4af765fe2 100644 --- a/etcdserver/etcdhttp/peers.go +++ b/etcdserver/etcdhttp/peers.go @@ -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. diff --git a/etcdserver/etcdhttp/peers_test.go b/etcdserver/etcdhttp/peers_test.go index f1d4617de..f733635b1 100644 --- a/etcdserver/etcdhttp/peers_test.go +++ b/etcdserver/etcdhttp/peers_test.go @@ -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() + } +}