transport_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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"
  17. "reflect"
  18. "testing"
  19. "time"
  20. "github.com/coreos/etcd/etcdserver/stats"
  21. "github.com/coreos/etcd/pkg/testutil"
  22. "github.com/coreos/etcd/pkg/types"
  23. "github.com/coreos/etcd/raft/raftpb"
  24. "github.com/xiang90/probing"
  25. )
  26. // TestTransportSend tests that transport can send messages using correct
  27. // underlying peer, and drop local or unknown-target messages.
  28. func TestTransportSend(t *testing.T) {
  29. ss := &stats.ServerStats{}
  30. ss.Initialize()
  31. peer1 := newFakePeer()
  32. peer2 := newFakePeer()
  33. tr := &Transport{
  34. ServerStats: ss,
  35. peers: map[types.ID]Peer{types.ID(1): peer1, types.ID(2): peer2},
  36. pipelineProber: probing.NewProber(nil),
  37. streamProber: probing.NewProber(nil),
  38. }
  39. wmsgsIgnored := []raftpb.Message{
  40. // bad local message
  41. {Type: raftpb.MsgBeat},
  42. // bad remote message
  43. {Type: raftpb.MsgProp, To: 3},
  44. }
  45. wmsgsTo1 := []raftpb.Message{
  46. // good message
  47. {Type: raftpb.MsgProp, To: 1},
  48. {Type: raftpb.MsgApp, To: 1},
  49. }
  50. wmsgsTo2 := []raftpb.Message{
  51. // good message
  52. {Type: raftpb.MsgProp, To: 2},
  53. {Type: raftpb.MsgApp, To: 2},
  54. }
  55. tr.Send(wmsgsIgnored)
  56. tr.Send(wmsgsTo1)
  57. tr.Send(wmsgsTo2)
  58. if !reflect.DeepEqual(peer1.msgs, wmsgsTo1) {
  59. t.Errorf("msgs to peer 1 = %+v, want %+v", peer1.msgs, wmsgsTo1)
  60. }
  61. if !reflect.DeepEqual(peer2.msgs, wmsgsTo2) {
  62. t.Errorf("msgs to peer 2 = %+v, want %+v", peer2.msgs, wmsgsTo2)
  63. }
  64. }
  65. func TestTransportCutMend(t *testing.T) {
  66. ss := &stats.ServerStats{}
  67. ss.Initialize()
  68. peer1 := newFakePeer()
  69. peer2 := newFakePeer()
  70. tr := &Transport{
  71. ServerStats: ss,
  72. peers: map[types.ID]Peer{types.ID(1): peer1, types.ID(2): peer2},
  73. pipelineProber: probing.NewProber(nil),
  74. streamProber: probing.NewProber(nil),
  75. }
  76. tr.CutPeer(types.ID(1))
  77. wmsgsTo := []raftpb.Message{
  78. // good message
  79. {Type: raftpb.MsgProp, To: 1},
  80. {Type: raftpb.MsgApp, To: 1},
  81. }
  82. tr.Send(wmsgsTo)
  83. if len(peer1.msgs) > 0 {
  84. t.Fatalf("msgs expected to be ignored, got %+v", peer1.msgs)
  85. }
  86. tr.MendPeer(types.ID(1))
  87. tr.Send(wmsgsTo)
  88. if !reflect.DeepEqual(peer1.msgs, wmsgsTo) {
  89. t.Errorf("msgs to peer 1 = %+v, want %+v", peer1.msgs, wmsgsTo)
  90. }
  91. }
  92. func TestTransportAdd(t *testing.T) {
  93. ls := stats.NewLeaderStats("")
  94. tr := &Transport{
  95. LeaderStats: ls,
  96. streamRt: &roundTripperRecorder{},
  97. peers: make(map[types.ID]Peer),
  98. pipelineProber: probing.NewProber(nil),
  99. streamProber: probing.NewProber(nil),
  100. }
  101. tr.AddPeer(1, []string{"http://localhost:2380"})
  102. if _, ok := ls.Followers["1"]; !ok {
  103. t.Errorf("FollowerStats[1] is nil, want exists")
  104. }
  105. s, ok := tr.peers[types.ID(1)]
  106. if !ok {
  107. tr.Stop()
  108. t.Fatalf("senders[1] is nil, want exists")
  109. }
  110. // duplicate AddPeer is ignored
  111. tr.AddPeer(1, []string{"http://localhost:2380"})
  112. ns := tr.peers[types.ID(1)]
  113. if s != ns {
  114. t.Errorf("sender = %v, want %v", ns, s)
  115. }
  116. tr.Stop()
  117. }
  118. func TestTransportRemove(t *testing.T) {
  119. tr := &Transport{
  120. LeaderStats: stats.NewLeaderStats(""),
  121. streamRt: &roundTripperRecorder{},
  122. peers: make(map[types.ID]Peer),
  123. pipelineProber: probing.NewProber(nil),
  124. streamProber: probing.NewProber(nil),
  125. }
  126. tr.AddPeer(1, []string{"http://localhost:2380"})
  127. tr.RemovePeer(types.ID(1))
  128. defer tr.Stop()
  129. if _, ok := tr.peers[types.ID(1)]; ok {
  130. t.Fatalf("senders[1] exists, want removed")
  131. }
  132. }
  133. func TestTransportUpdate(t *testing.T) {
  134. peer := newFakePeer()
  135. tr := &Transport{
  136. peers: map[types.ID]Peer{types.ID(1): peer},
  137. pipelineProber: probing.NewProber(nil),
  138. streamProber: probing.NewProber(nil),
  139. }
  140. u := "http://localhost:2380"
  141. tr.UpdatePeer(types.ID(1), []string{u})
  142. wurls := types.URLs(testutil.MustNewURLs(t, []string{"http://localhost:2380"}))
  143. if !reflect.DeepEqual(peer.peerURLs, wurls) {
  144. t.Errorf("urls = %+v, want %+v", peer.peerURLs, wurls)
  145. }
  146. }
  147. func TestTransportErrorc(t *testing.T) {
  148. errorc := make(chan error, 1)
  149. tr := &Transport{
  150. Raft: &fakeRaft{},
  151. LeaderStats: stats.NewLeaderStats(""),
  152. ErrorC: errorc,
  153. streamRt: newRespRoundTripper(http.StatusForbidden, nil),
  154. pipelineRt: newRespRoundTripper(http.StatusForbidden, nil),
  155. peers: make(map[types.ID]Peer),
  156. pipelineProber: probing.NewProber(nil),
  157. streamProber: probing.NewProber(nil),
  158. }
  159. tr.AddPeer(1, []string{"http://localhost:2380"})
  160. defer tr.Stop()
  161. select {
  162. case <-errorc:
  163. t.Fatalf("received unexpected from errorc")
  164. case <-time.After(10 * time.Millisecond):
  165. }
  166. tr.peers[1].send(raftpb.Message{})
  167. select {
  168. case <-errorc:
  169. case <-time.After(1 * time.Second):
  170. t.Fatalf("cannot receive error from errorc")
  171. }
  172. }