Skip to content

Commit

Permalink
fix: server_test.go
Browse files Browse the repository at this point in the history
  • Loading branch information
clement2026 committed Sep 12, 2024
1 parent 981061a commit 6970220
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions server/etcdserver/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,12 @@ func TestApplyRepeat(t *testing.T) {
SyncTicker: &time.Ticker{},
consistIndex: cindex.NewFakeConsistentIndex(0),
uberApply: uberApplierMock{},
kv: mvcc.New(zaptest.NewLogger(t), be, &lease.FakeLessor{}, mvcc.StoreConfig{}),
}
s.start()

n.readyc <- newDummyPutReqReady()

req := &pb.InternalRaftRequest{
Header: &pb.RequestHeader{ID: 1},
Put: &pb.PutRequest{Key: []byte("foo"), Value: []byte("bar")},
Expand Down Expand Up @@ -885,8 +889,12 @@ func TestAddMember(t *testing.T) {
SyncTicker: &time.Ticker{},
consistIndex: cindex.NewFakeConsistentIndex(0),
beHooks: serverstorage.NewBackendHooks(lg, nil),
kv: mvcc.New(zaptest.NewLogger(t), be, &lease.FakeLessor{}, mvcc.StoreConfig{}),
}
s.start()

n.readyc <- newDummyPutReqReady()

m := membership.Member{ID: 1234, RaftAttributes: membership.RaftAttributes{PeerURLs: []string{"foo"}}}
_, err := s.AddMember(context.Background(), m)
gaction := n.Action()
Expand Down Expand Up @@ -992,8 +1000,12 @@ func TestRemoveMember(t *testing.T) {
SyncTicker: &time.Ticker{},
consistIndex: cindex.NewFakeConsistentIndex(0),
beHooks: serverstorage.NewBackendHooks(lg, nil),
kv: mvcc.New(zaptest.NewLogger(t), be, &lease.FakeLessor{}, mvcc.StoreConfig{}),
}
s.start()

n.readyc <- newDummyPutReqReady()

_, err := s.RemoveMember(context.Background(), 1234)
gaction := n.Action()
s.Stop()
Expand Down Expand Up @@ -1041,8 +1053,12 @@ func TestUpdateMember(t *testing.T) {
SyncTicker: &time.Ticker{},
consistIndex: cindex.NewFakeConsistentIndex(0),
beHooks: serverstorage.NewBackendHooks(lg, nil),
kv: mvcc.New(zaptest.NewLogger(t), be, &lease.FakeLessor{}, mvcc.StoreConfig{}),
}
s.start()

n.readyc <- newDummyPutReqReady()

wm := membership.Member{ID: 1234, RaftAttributes: membership.RaftAttributes{PeerURLs: []string{"http://127.0.0.1:1"}}}
_, err := s.UpdateMember(context.Background(), wm)
gaction := n.Action()
Expand Down Expand Up @@ -1581,3 +1597,18 @@ func TestIsActive(t *testing.T) {
require.Equal(t, tc.expectActive, s.isActive())
}
}

// newDummyPutReqReady creates a dummy raft.Ready.
//
// In unit tests, EtcdServer doesn't append raft log entries upon start like the full-featured etcd does.
// This can crash the program when creating a raft log snapshot due to no available entries.
// To append raft log entries, we can send a newDummyPutReqReady() to raft.Node's readyc after the server starts.
func newDummyPutReqReady() raft.Ready {
req := &pb.InternalRaftRequest{
Header: &pb.RequestHeader{ID: 1},
Put: &pb.PutRequest{Key: []byte("newDummyPutReqReady"), Value: []byte("bar")},
}
ents := []raftpb.Entry{{Index: 1, Data: pbutil.MustMarshal(req)}}

return raft.Ready{Entries: ents}
}

0 comments on commit 6970220

Please sign in to comment.