peer_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. "testing"
  17. "github.com/coreos/etcd/raft/raftpb"
  18. )
  19. func TestPeerPick(t *testing.T) {
  20. tests := []struct {
  21. msgappWorking bool
  22. messageWorking bool
  23. m raftpb.Message
  24. wpicked string
  25. }{
  26. {
  27. true, true,
  28. raftpb.Message{Type: raftpb.MsgSnap},
  29. pipelineMsg,
  30. },
  31. {
  32. true, true,
  33. raftpb.Message{Type: raftpb.MsgApp, Term: 1, LogTerm: 1},
  34. streamApp,
  35. },
  36. {
  37. true, true,
  38. raftpb.Message{Type: raftpb.MsgProp},
  39. streamMsg,
  40. },
  41. {
  42. true, true,
  43. raftpb.Message{Type: raftpb.MsgHeartbeat},
  44. streamMsg,
  45. },
  46. {
  47. false, true,
  48. raftpb.Message{Type: raftpb.MsgApp, Term: 1, LogTerm: 1},
  49. streamMsg,
  50. },
  51. {
  52. false, false,
  53. raftpb.Message{Type: raftpb.MsgApp, Term: 1, LogTerm: 1},
  54. pipelineMsg,
  55. },
  56. {
  57. false, false,
  58. raftpb.Message{Type: raftpb.MsgProp},
  59. pipelineMsg,
  60. },
  61. {
  62. false, false,
  63. raftpb.Message{Type: raftpb.MsgSnap},
  64. pipelineMsg,
  65. },
  66. {
  67. false, false,
  68. raftpb.Message{Type: raftpb.MsgHeartbeat},
  69. pipelineMsg,
  70. },
  71. }
  72. for i, tt := range tests {
  73. peer := &peer{
  74. msgAppWriter: &streamWriter{working: tt.msgappWorking},
  75. writer: &streamWriter{working: tt.messageWorking},
  76. pipeline: &pipeline{},
  77. }
  78. _, picked := peer.pick(tt.m)
  79. if picked != tt.wpicked {
  80. t.Errorf("#%d: picked = %v, want %v", i, picked, tt.wpicked)
  81. }
  82. }
  83. }