transport_test.go 4.9 KB

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