transport_test.go 5.1 KB

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