transport_test.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. "testing"
  18. "time"
  19. "github.com/coreos/etcd/etcdserver/stats"
  20. "github.com/coreos/etcd/pkg/testutil"
  21. "github.com/coreos/etcd/pkg/types"
  22. "github.com/coreos/etcd/raft/raftpb"
  23. )
  24. func TestTransportAdd(t *testing.T) {
  25. ls := stats.NewLeaderStats("")
  26. tr := &transport{
  27. leaderStats: ls,
  28. peers: make(map[types.ID]*peer),
  29. }
  30. tr.AddPeer(1, []string{"http://a"})
  31. if _, ok := ls.Followers["1"]; !ok {
  32. t.Errorf("FollowerStats[1] is nil, want exists")
  33. }
  34. s, ok := tr.peers[types.ID(1)]
  35. if !ok {
  36. t.Fatalf("senders[1] is nil, want exists")
  37. }
  38. // duplicate AddPeer is ignored
  39. tr.AddPeer(1, []string{"http://a"})
  40. ns := tr.peers[types.ID(1)]
  41. if s != ns {
  42. t.Errorf("sender = %v, want %v", ns, s)
  43. }
  44. }
  45. func TestTransportRemove(t *testing.T) {
  46. tr := &transport{
  47. leaderStats: stats.NewLeaderStats(""),
  48. peers: make(map[types.ID]*peer),
  49. }
  50. tr.AddPeer(1, []string{"http://a"})
  51. tr.RemovePeer(types.ID(1))
  52. if _, ok := tr.peers[types.ID(1)]; ok {
  53. t.Fatalf("senders[1] exists, want removed")
  54. }
  55. }
  56. func TestTransportErrorc(t *testing.T) {
  57. errorc := make(chan error, 1)
  58. tr := &transport{
  59. roundTripper: newRespRoundTripper(http.StatusForbidden, nil),
  60. leaderStats: stats.NewLeaderStats(""),
  61. peers: make(map[types.ID]*peer),
  62. errorc: errorc,
  63. }
  64. tr.AddPeer(1, []string{"http://a"})
  65. select {
  66. case <-errorc:
  67. t.Fatalf("received unexpected from errorc")
  68. case <-time.After(10 * time.Millisecond):
  69. }
  70. tr.peers[1].Send(raftpb.Message{})
  71. testutil.ForceGosched()
  72. select {
  73. case <-errorc:
  74. default:
  75. t.Fatalf("cannot receive error from errorc")
  76. }
  77. }