transport_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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/etcdserver/stats"
  21. "github.com/coreos/etcd/pkg/testutil"
  22. "github.com/coreos/etcd/pkg/types"
  23. "github.com/coreos/etcd/raft/raftpb"
  24. )
  25. // TestTransportSend tests that transport can send messages using correct
  26. // underlying peer, and drop local or unknown-target messages.
  27. func TestTransportSend(t *testing.T) {
  28. ss := &stats.ServerStats{}
  29. ss.Initialize()
  30. peer1 := newFakePeer()
  31. peer2 := newFakePeer()
  32. tr := &transport{
  33. serverStats: ss,
  34. peers: map[types.ID]Peer{types.ID(1): peer1, types.ID(2): peer2},
  35. }
  36. wmsgsIgnored := []raftpb.Message{
  37. // bad local message
  38. {Type: raftpb.MsgBeat},
  39. // bad remote message
  40. {Type: raftpb.MsgProp, To: 3},
  41. }
  42. wmsgsTo1 := []raftpb.Message{
  43. // good message
  44. {Type: raftpb.MsgProp, To: 1},
  45. {Type: raftpb.MsgApp, To: 1},
  46. }
  47. wmsgsTo2 := []raftpb.Message{
  48. // good message
  49. {Type: raftpb.MsgProp, To: 2},
  50. {Type: raftpb.MsgApp, To: 2},
  51. }
  52. tr.Send(wmsgsIgnored)
  53. tr.Send(wmsgsTo1)
  54. tr.Send(wmsgsTo2)
  55. if !reflect.DeepEqual(peer1.msgs, wmsgsTo1) {
  56. t.Errorf("msgs to peer 1 = %+v, want %+v", peer1.msgs, wmsgsTo1)
  57. }
  58. if !reflect.DeepEqual(peer2.msgs, wmsgsTo2) {
  59. t.Errorf("msgs to peer 2 = %+v, want %+v", peer2.msgs, wmsgsTo2)
  60. }
  61. }
  62. func TestTransportAdd(t *testing.T) {
  63. ls := stats.NewLeaderStats("")
  64. term := uint64(10)
  65. tr := &transport{
  66. roundTripper: &roundTripperRecorder{},
  67. leaderStats: ls,
  68. term: term,
  69. peers: make(map[types.ID]Peer),
  70. }
  71. tr.AddPeer(1, []string{"http://localhost:2380"})
  72. if _, ok := ls.Followers["1"]; !ok {
  73. t.Errorf("FollowerStats[1] is nil, want exists")
  74. }
  75. s, ok := tr.peers[types.ID(1)]
  76. if !ok {
  77. tr.Stop()
  78. t.Fatalf("senders[1] is nil, want exists")
  79. }
  80. // duplicate AddPeer is ignored
  81. tr.AddPeer(1, []string{"http://localhost:2380"})
  82. ns := tr.peers[types.ID(1)]
  83. if s != ns {
  84. t.Errorf("sender = %v, want %v", ns, s)
  85. }
  86. tr.Stop()
  87. if g := s.(*peer).msgAppReader.msgAppTerm; g != term {
  88. t.Errorf("peer.term = %d, want %d", g, term)
  89. }
  90. }
  91. func TestTransportRemove(t *testing.T) {
  92. tr := &transport{
  93. roundTripper: &roundTripperRecorder{},
  94. leaderStats: stats.NewLeaderStats(""),
  95. peers: make(map[types.ID]Peer),
  96. }
  97. tr.AddPeer(1, []string{"http://localhost:2380"})
  98. tr.RemovePeer(types.ID(1))
  99. defer tr.Stop()
  100. if _, ok := tr.peers[types.ID(1)]; ok {
  101. t.Fatalf("senders[1] exists, want removed")
  102. }
  103. }
  104. func TestTransportUpdate(t *testing.T) {
  105. peer := newFakePeer()
  106. tr := &transport{
  107. peers: map[types.ID]Peer{types.ID(1): peer},
  108. }
  109. u := "http://localhost:2380"
  110. tr.UpdatePeer(types.ID(1), []string{u})
  111. wurls := types.URLs(testutil.MustNewURLs(t, []string{"http://localhost:2380"}))
  112. if !reflect.DeepEqual(peer.urls, wurls) {
  113. t.Errorf("urls = %+v, want %+v", peer.urls, wurls)
  114. }
  115. }
  116. func TestTransportErrorc(t *testing.T) {
  117. errorc := make(chan error, 1)
  118. tr := &transport{
  119. roundTripper: newRespRoundTripper(http.StatusForbidden, nil),
  120. leaderStats: stats.NewLeaderStats(""),
  121. peers: make(map[types.ID]Peer),
  122. errorc: errorc,
  123. }
  124. tr.AddPeer(1, []string{"http://localhost:2380"})
  125. defer tr.Stop()
  126. select {
  127. case <-errorc:
  128. t.Fatalf("received unexpected from errorc")
  129. case <-time.After(10 * time.Millisecond):
  130. }
  131. tr.peers[1].Send(raftpb.Message{})
  132. testutil.WaitSchedule()
  133. select {
  134. case <-errorc:
  135. default:
  136. t.Fatalf("cannot receive error from errorc")
  137. }
  138. }