Exemple #1
0
func TestRequestVote(t *testing.T) {
	rvr := raft.RequestVoteResponse{
		Term:        5,
		VoteGranted: true,
	}
	s := rafthttp.NewServer(&echoServer{
		id:  1,
		aer: raft.AppendEntriesResponse{},
		rvr: rvr,
	})
	m := newMockMux()
	s.Install(m)

	var body bytes.Buffer
	json.NewEncoder(&body).Encode(raft.RequestVote{})
	req, _ := http.NewRequest("POST", "", &body)
	resp, err := m.Call(rafthttp.RequestVotePath, req)
	if err != nil {
		t.Fatal(err)
	}

	var expected bytes.Buffer
	json.NewEncoder(&expected).Encode(rvr)
	if bytes.Compare(resp, expected.Bytes()) != 0 {
		t.Fatalf("expected '%s', got '%s'", expected.String(), string(resp))
	}
}
Exemple #2
0
func TestCommand(t *testing.T) {
	s := rafthttp.NewServer(&echoServer{
		id:  1,
		aer: raft.AppendEntriesResponse{},
		rvr: raft.RequestVoteResponse{}},
	)
	m := newMockMux()
	s.Install(m)

	cmd := `{"foo":123}`
	req, _ := http.NewRequest("POST", "", bytes.NewBufferString(cmd))
	resp, err := m.Call(rafthttp.CommandPath, req)
	if err != nil {
		t.Fatal(err)
	}

	if bytes.Compare([]byte(cmd), resp) != 0 {
		t.Fatalf("expected '%s', got '%s'", cmd, string(resp))
	}
}
Exemple #3
0
func TestId(t *testing.T) {
	id := uint64(33)
	s := rafthttp.NewServer(&echoServer{
		id:  id,
		aer: raft.AppendEntriesResponse{},
		rvr: raft.RequestVoteResponse{}},
	)
	m := newMockMux()
	s.Install(m)

	req, _ := http.NewRequest("GET", "", &bytes.Buffer{})
	resp, err := m.Call(rafthttp.IdPath, req)
	if err != nil {
		t.Fatal(err)
	}

	gotId, err := strconv.ParseUint(string(resp), 10, 64)
	if err != nil {
		t.Fatal(err)
	}
	if gotId != id {
		t.Fatalf("expected %d, got %d", id, gotId)
	}
}