msgappv2_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. "bytes"
  17. "reflect"
  18. "testing"
  19. "github.com/coreos/etcd/etcdserver/stats"
  20. "github.com/coreos/etcd/pkg/types"
  21. "github.com/coreos/etcd/raft/raftpb"
  22. )
  23. func TestMsgAppV2(t *testing.T) {
  24. tests := []raftpb.Message{
  25. linkHeartbeatMessage,
  26. {
  27. Type: raftpb.MsgApp,
  28. From: 1,
  29. To: 2,
  30. Term: 1,
  31. LogTerm: 1,
  32. Index: 0,
  33. Entries: []raftpb.Entry{
  34. {Term: 1, Index: 1, Data: []byte("some data")},
  35. {Term: 1, Index: 2, Data: []byte("some data")},
  36. {Term: 1, Index: 3, Data: []byte("some data")},
  37. },
  38. },
  39. // consecutive MsgApp
  40. {
  41. Type: raftpb.MsgApp,
  42. From: 1,
  43. To: 2,
  44. Term: 1,
  45. LogTerm: 1,
  46. Index: 3,
  47. Entries: []raftpb.Entry{
  48. {Term: 1, Index: 4, Data: []byte("some data")},
  49. },
  50. },
  51. linkHeartbeatMessage,
  52. // consecutive MsgApp after linkHeartbeatMessage
  53. {
  54. Type: raftpb.MsgApp,
  55. From: 1,
  56. To: 2,
  57. Term: 1,
  58. LogTerm: 1,
  59. Index: 4,
  60. Entries: []raftpb.Entry{
  61. {Term: 1, Index: 5, Data: []byte("some data")},
  62. },
  63. },
  64. // MsgApp with higher term
  65. {
  66. Type: raftpb.MsgApp,
  67. From: 1,
  68. To: 2,
  69. Term: 3,
  70. LogTerm: 1,
  71. Index: 5,
  72. Entries: []raftpb.Entry{
  73. {Term: 3, Index: 6, Data: []byte("some data")},
  74. },
  75. },
  76. linkHeartbeatMessage,
  77. // consecutive MsgApp
  78. {
  79. Type: raftpb.MsgApp,
  80. From: 1,
  81. To: 2,
  82. Term: 3,
  83. LogTerm: 2,
  84. Index: 6,
  85. Entries: []raftpb.Entry{
  86. {Term: 3, Index: 7, Data: []byte("some data")},
  87. },
  88. },
  89. // consecutive empty MsgApp
  90. {
  91. Type: raftpb.MsgApp,
  92. From: 1,
  93. To: 2,
  94. Term: 3,
  95. LogTerm: 2,
  96. Index: 7,
  97. Entries: nil,
  98. },
  99. linkHeartbeatMessage,
  100. }
  101. b := &bytes.Buffer{}
  102. enc := newMsgAppV2Encoder(b, &stats.FollowerStats{})
  103. dec := newMsgAppV2Decoder(b, types.ID(2), types.ID(1))
  104. for i, tt := range tests {
  105. if err := enc.encode(tt); err != nil {
  106. t.Errorf("#%d: unexpected encode message error: %v", i, err)
  107. continue
  108. }
  109. m, err := dec.decode()
  110. if err != nil {
  111. t.Errorf("#%d: unexpected decode message error: %v", i, err)
  112. continue
  113. }
  114. if !reflect.DeepEqual(m, tt) {
  115. t.Errorf("#%d: message = %+v, want %+v", i, m, tt)
  116. }
  117. }
  118. }