transport_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. }
  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. prober: probing.NewProber(nil),
  91. }
  92. tr.AddPeer(1, []string{"http://localhost:2380"})
  93. if _, ok := ls.Followers["1"]; !ok {
  94. t.Errorf("FollowerStats[1] is nil, want exists")
  95. }
  96. s, ok := tr.peers[types.ID(1)]
  97. if !ok {
  98. tr.Stop()
  99. t.Fatalf("senders[1] is nil, want exists")
  100. }
  101. // duplicate AddPeer is ignored
  102. tr.AddPeer(1, []string{"http://localhost:2380"})
  103. ns := tr.peers[types.ID(1)]
  104. if s != ns {
  105. t.Errorf("sender = %v, want %v", ns, s)
  106. }
  107. tr.Stop()
  108. }
  109. func TestTransportRemove(t *testing.T) {
  110. tr := &Transport{
  111. LeaderStats: stats.NewLeaderStats(""),
  112. streamRt: &roundTripperRecorder{},
  113. peers: make(map[types.ID]Peer),
  114. prober: probing.NewProber(nil),
  115. }
  116. tr.AddPeer(1, []string{"http://localhost:2380"})
  117. tr.RemovePeer(types.ID(1))
  118. defer tr.Stop()
  119. if _, ok := tr.peers[types.ID(1)]; ok {
  120. t.Fatalf("senders[1] exists, want removed")
  121. }
  122. }
  123. func TestTransportUpdate(t *testing.T) {
  124. peer := newFakePeer()
  125. tr := &Transport{
  126. peers: map[types.ID]Peer{types.ID(1): peer},
  127. prober: probing.NewProber(nil),
  128. }
  129. u := "http://localhost:2380"
  130. tr.UpdatePeer(types.ID(1), []string{u})
  131. wurls := types.URLs(testutil.MustNewURLs(t, []string{"http://localhost:2380"}))
  132. if !reflect.DeepEqual(peer.peerURLs, wurls) {
  133. t.Errorf("urls = %+v, want %+v", peer.peerURLs, wurls)
  134. }
  135. }
  136. func TestTransportErrorc(t *testing.T) {
  137. errorc := make(chan error, 1)
  138. tr := &Transport{
  139. Raft: &fakeRaft{},
  140. LeaderStats: stats.NewLeaderStats(""),
  141. ErrorC: errorc,
  142. streamRt: newRespRoundTripper(http.StatusForbidden, nil),
  143. pipelineRt: newRespRoundTripper(http.StatusForbidden, nil),
  144. peers: make(map[types.ID]Peer),
  145. prober: probing.NewProber(nil),
  146. }
  147. tr.AddPeer(1, []string{"http://localhost:2380"})
  148. defer tr.Stop()
  149. select {
  150. case <-errorc:
  151. t.Fatalf("received unexpected from errorc")
  152. case <-time.After(10 * time.Millisecond):
  153. }
  154. tr.peers[1].send(raftpb.Message{})
  155. select {
  156. case <-errorc:
  157. case <-time.After(1 * time.Second):
  158. t.Fatalf("cannot receive error from errorc")
  159. }
  160. }