functional_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // Copyright 2015 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package rafthttp
  15. import (
  16. "context"
  17. "net/http/httptest"
  18. "reflect"
  19. "testing"
  20. "time"
  21. "github.com/coreos/etcd/etcdserver/stats"
  22. "github.com/coreos/etcd/pkg/types"
  23. "github.com/coreos/etcd/raft"
  24. "github.com/coreos/etcd/raft/raftpb"
  25. )
  26. func TestSendMessage(t *testing.T) {
  27. // member 1
  28. tr := &Transport{
  29. ID: types.ID(1),
  30. ClusterID: types.ID(1),
  31. Raft: &fakeRaft{},
  32. ServerStats: newServerStats(),
  33. LeaderStats: stats.NewLeaderStats("1"),
  34. }
  35. tr.Start()
  36. srv := httptest.NewServer(tr.Handler())
  37. defer srv.Close()
  38. // member 2
  39. recvc := make(chan raftpb.Message, 1)
  40. p := &fakeRaft{recvc: recvc}
  41. tr2 := &Transport{
  42. ID: types.ID(2),
  43. ClusterID: types.ID(1),
  44. Raft: p,
  45. ServerStats: newServerStats(),
  46. LeaderStats: stats.NewLeaderStats("2"),
  47. }
  48. tr2.Start()
  49. srv2 := httptest.NewServer(tr2.Handler())
  50. defer srv2.Close()
  51. tr.AddPeer(types.ID(2), []string{srv2.URL})
  52. defer tr.Stop()
  53. tr2.AddPeer(types.ID(1), []string{srv.URL})
  54. defer tr2.Stop()
  55. if !waitStreamWorking(tr.Get(types.ID(2)).(*peer)) {
  56. t.Fatalf("stream from 1 to 2 is not in work as expected")
  57. }
  58. data := []byte("some data")
  59. tests := []raftpb.Message{
  60. // these messages are set to send to itself, which facilitates testing.
  61. {Type: raftpb.MsgProp, From: 1, To: 2, Entries: []raftpb.Entry{{Data: data}}},
  62. {Type: raftpb.MsgApp, From: 1, To: 2, Term: 1, Index: 3, LogTerm: 0, Entries: []raftpb.Entry{{Index: 4, Term: 1, Data: data}}, Commit: 3},
  63. {Type: raftpb.MsgAppResp, From: 1, To: 2, Term: 1, Index: 3},
  64. {Type: raftpb.MsgVote, From: 1, To: 2, Term: 1, Index: 3, LogTerm: 0},
  65. {Type: raftpb.MsgVoteResp, From: 1, To: 2, Term: 1},
  66. {Type: raftpb.MsgSnap, From: 1, To: 2, Term: 1, Snapshot: raftpb.Snapshot{Metadata: raftpb.SnapshotMetadata{Index: 1000, Term: 1}, Data: data}},
  67. {Type: raftpb.MsgHeartbeat, From: 1, To: 2, Term: 1, Commit: 3},
  68. {Type: raftpb.MsgHeartbeatResp, From: 1, To: 2, Term: 1},
  69. }
  70. for i, tt := range tests {
  71. tr.Send([]raftpb.Message{tt})
  72. msg := <-recvc
  73. if !reflect.DeepEqual(msg, tt) {
  74. t.Errorf("#%d: msg = %+v, want %+v", i, msg, tt)
  75. }
  76. }
  77. }
  78. // TestSendMessageWhenStreamIsBroken tests that message can be sent to the
  79. // remote in a limited time when all underlying connections are broken.
  80. func TestSendMessageWhenStreamIsBroken(t *testing.T) {
  81. // member 1
  82. tr := &Transport{
  83. ID: types.ID(1),
  84. ClusterID: types.ID(1),
  85. Raft: &fakeRaft{},
  86. ServerStats: newServerStats(),
  87. LeaderStats: stats.NewLeaderStats("1"),
  88. }
  89. tr.Start()
  90. srv := httptest.NewServer(tr.Handler())
  91. defer srv.Close()
  92. // member 2
  93. recvc := make(chan raftpb.Message, 1)
  94. p := &fakeRaft{recvc: recvc}
  95. tr2 := &Transport{
  96. ID: types.ID(2),
  97. ClusterID: types.ID(1),
  98. Raft: p,
  99. ServerStats: newServerStats(),
  100. LeaderStats: stats.NewLeaderStats("2"),
  101. }
  102. tr2.Start()
  103. srv2 := httptest.NewServer(tr2.Handler())
  104. defer srv2.Close()
  105. tr.AddPeer(types.ID(2), []string{srv2.URL})
  106. defer tr.Stop()
  107. tr2.AddPeer(types.ID(1), []string{srv.URL})
  108. defer tr2.Stop()
  109. if !waitStreamWorking(tr.Get(types.ID(2)).(*peer)) {
  110. t.Fatalf("stream from 1 to 2 is not in work as expected")
  111. }
  112. // break the stream
  113. srv.CloseClientConnections()
  114. srv2.CloseClientConnections()
  115. var n int
  116. for {
  117. select {
  118. // TODO: remove this resend logic when we add retry logic into the code
  119. case <-time.After(time.Millisecond):
  120. n++
  121. tr.Send([]raftpb.Message{{Type: raftpb.MsgHeartbeat, From: 1, To: 2, Term: 1, Commit: 3}})
  122. case <-recvc:
  123. if n > 50 {
  124. t.Errorf("disconnection time = %dms, want < 50ms", n)
  125. }
  126. return
  127. }
  128. }
  129. }
  130. func newServerStats() *stats.ServerStats {
  131. return stats.NewServerStats("", "")
  132. }
  133. func waitStreamWorking(p *peer) bool {
  134. for i := 0; i < 1000; i++ {
  135. time.Sleep(time.Millisecond)
  136. if _, ok := p.msgAppV2Writer.writec(); !ok {
  137. continue
  138. }
  139. if _, ok := p.writer.writec(); !ok {
  140. continue
  141. }
  142. return true
  143. }
  144. return false
  145. }
  146. type fakeRaft struct {
  147. recvc chan<- raftpb.Message
  148. err error
  149. removedID uint64
  150. }
  151. func (p *fakeRaft) Process(ctx context.Context, m raftpb.Message) error {
  152. select {
  153. case p.recvc <- m:
  154. default:
  155. }
  156. return p.err
  157. }
  158. func (p *fakeRaft) IsIDRemoved(id uint64) bool { return id == p.removedID }
  159. func (p *fakeRaft) ReportUnreachable(id uint64) {}
  160. func (p *fakeRaft) ReportSnapshot(id uint64, status raft.SnapshotStatus) {}