functional_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. "net/http/httptest"
  17. "reflect"
  18. "testing"
  19. "time"
  20. "github.com/coreos/etcd/etcdserver/stats"
  21. "github.com/coreos/etcd/pkg/types"
  22. "github.com/coreos/etcd/raft"
  23. "github.com/coreos/etcd/raft/raftpb"
  24. "golang.org/x/net/context"
  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. ss := &stats.ServerStats{}
  132. ss.Initialize()
  133. return ss
  134. }
  135. func waitStreamWorking(p *peer) bool {
  136. for i := 0; i < 1000; i++ {
  137. time.Sleep(time.Millisecond)
  138. if _, ok := p.msgAppV2Writer.writec(); !ok {
  139. continue
  140. }
  141. if _, ok := p.writer.writec(); !ok {
  142. continue
  143. }
  144. return true
  145. }
  146. return false
  147. }
  148. type fakeRaft struct {
  149. recvc chan<- raftpb.Message
  150. err error
  151. removedID uint64
  152. }
  153. func (p *fakeRaft) Process(ctx context.Context, m raftpb.Message) error {
  154. select {
  155. case p.recvc <- m:
  156. default:
  157. }
  158. return p.err
  159. }
  160. func (p *fakeRaft) IsIDRemoved(id uint64) bool { return id == p.removedID }
  161. func (p *fakeRaft) ReportUnreachable(id uint64) {}
  162. func (p *fakeRaft) ReportSnapshot(id uint64, status raft.SnapshotStatus) {}