msgapp_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 TestMsgApp(t *testing.T) {
  24. tests := []raftpb.Message{
  25. {
  26. Type: raftpb.MsgApp,
  27. From: 1,
  28. To: 2,
  29. Term: 1,
  30. LogTerm: 1,
  31. Index: 3,
  32. Entries: []raftpb.Entry{{Term: 1, Index: 4}},
  33. },
  34. {
  35. Type: raftpb.MsgApp,
  36. From: 1,
  37. To: 2,
  38. Term: 1,
  39. LogTerm: 1,
  40. Index: 0,
  41. Entries: []raftpb.Entry{
  42. {Term: 1, Index: 1, Data: []byte("some data")},
  43. {Term: 1, Index: 2, Data: []byte("some data")},
  44. {Term: 1, Index: 3, Data: []byte("some data")},
  45. },
  46. },
  47. linkHeartbeatMessage,
  48. }
  49. for i, tt := range tests {
  50. b := &bytes.Buffer{}
  51. enc := &msgAppEncoder{w: b, fs: &stats.FollowerStats{}}
  52. if err := enc.encode(tt); err != nil {
  53. t.Errorf("#%d: unexpected encode message error: %v", i, err)
  54. continue
  55. }
  56. dec := &msgAppDecoder{r: b, local: types.ID(tt.To), remote: types.ID(tt.From), term: tt.Term}
  57. m, err := dec.decode()
  58. if err != nil {
  59. t.Errorf("#%d: unexpected decode message error: %v", i, err)
  60. continue
  61. }
  62. if !reflect.DeepEqual(m, tt) {
  63. t.Errorf("#%d: message = %+v, want %+v", i, m, tt)
  64. }
  65. }
  66. }