transport_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright 2015 CoreOS, Inc.
  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/Godeps/_workspace/src/github.com/xiang90/probing"
  21. "github.com/coreos/etcd/etcdserver/stats"
  22. "github.com/coreos/etcd/pkg/testutil"
  23. "github.com/coreos/etcd/pkg/types"
  24. "github.com/coreos/etcd/raft/raftpb"
  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. }
  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 TestTransportAdd(t *testing.T) {
  64. ls := stats.NewLeaderStats("")
  65. term := uint64(10)
  66. tr := &transport{
  67. roundTripper: &roundTripperRecorder{},
  68. leaderStats: ls,
  69. term: term,
  70. peers: make(map[types.ID]Peer),
  71. prober: probing.NewProber(nil),
  72. }
  73. tr.AddPeer(1, []string{"http://localhost:2380"})
  74. if _, ok := ls.Followers["1"]; !ok {
  75. t.Errorf("FollowerStats[1] is nil, want exists")
  76. }
  77. s, ok := tr.peers[types.ID(1)]
  78. if !ok {
  79. tr.Stop()
  80. t.Fatalf("senders[1] is nil, want exists")
  81. }
  82. // duplicate AddPeer is ignored
  83. tr.AddPeer(1, []string{"http://localhost:2380"})
  84. ns := tr.peers[types.ID(1)]
  85. if s != ns {
  86. t.Errorf("sender = %v, want %v", ns, s)
  87. }
  88. tr.Stop()
  89. if g := s.(*peer).msgAppReader.msgAppTerm; g != term {
  90. t.Errorf("peer.term = %d, want %d", g, term)
  91. }
  92. }
  93. func TestTransportRemove(t *testing.T) {
  94. tr := &transport{
  95. roundTripper: &roundTripperRecorder{},
  96. leaderStats: stats.NewLeaderStats(""),
  97. peers: make(map[types.ID]Peer),
  98. prober: probing.NewProber(nil),
  99. }
  100. tr.AddPeer(1, []string{"http://localhost:2380"})
  101. tr.RemovePeer(types.ID(1))
  102. defer tr.Stop()
  103. if _, ok := tr.peers[types.ID(1)]; ok {
  104. t.Fatalf("senders[1] exists, want removed")
  105. }
  106. }
  107. func TestTransportUpdate(t *testing.T) {
  108. peer := newFakePeer()
  109. tr := &transport{
  110. peers: map[types.ID]Peer{types.ID(1): peer},
  111. prober: probing.NewProber(nil),
  112. }
  113. u := "http://localhost:2380"
  114. tr.UpdatePeer(types.ID(1), []string{u})
  115. wurls := types.URLs(testutil.MustNewURLs(t, []string{"http://localhost:2380"}))
  116. if !reflect.DeepEqual(peer.urls, wurls) {
  117. t.Errorf("urls = %+v, want %+v", peer.urls, wurls)
  118. }
  119. }
  120. func TestTransportErrorc(t *testing.T) {
  121. errorc := make(chan error, 1)
  122. tr := &transport{
  123. roundTripper: newRespRoundTripper(http.StatusForbidden, nil),
  124. leaderStats: stats.NewLeaderStats(""),
  125. peers: make(map[types.ID]Peer),
  126. prober: probing.NewProber(nil),
  127. errorc: errorc,
  128. }
  129. tr.AddPeer(1, []string{"http://localhost:2380"})
  130. defer tr.Stop()
  131. select {
  132. case <-errorc:
  133. t.Fatalf("received unexpected from errorc")
  134. case <-time.After(10 * time.Millisecond):
  135. }
  136. tr.peers[1].Send(raftpb.Message{})
  137. testutil.WaitSchedule()
  138. select {
  139. case <-errorc:
  140. default:
  141. t.Fatalf("cannot receive error from errorc")
  142. }
  143. }