msg_codec_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2015 The etcd Authors
  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/raft/raftpb"
  20. )
  21. func TestMessage(t *testing.T) {
  22. tests := []raftpb.Message{
  23. {
  24. Type: raftpb.MsgApp,
  25. From: 1,
  26. To: 2,
  27. Term: 1,
  28. LogTerm: 1,
  29. Index: 3,
  30. Entries: []raftpb.Entry{{Term: 1, Index: 4}},
  31. },
  32. {
  33. Type: raftpb.MsgProp,
  34. From: 1,
  35. To: 2,
  36. Entries: []raftpb.Entry{
  37. {Data: []byte("some data")},
  38. {Data: []byte("some data")},
  39. {Data: []byte("some data")},
  40. },
  41. },
  42. linkHeartbeatMessage,
  43. }
  44. for i, tt := range tests {
  45. b := &bytes.Buffer{}
  46. enc := &messageEncoder{w: b}
  47. if err := enc.encode(tt); err != nil {
  48. t.Errorf("#%d: unexpected encode message error: %v", i, err)
  49. continue
  50. }
  51. dec := &messageDecoder{r: b}
  52. m, err := dec.decode()
  53. if err != nil {
  54. t.Errorf("#%d: unexpected decode message error: %v", i, err)
  55. continue
  56. }
  57. if !reflect.DeepEqual(m, tt) {
  58. t.Errorf("#%d: message = %+v, want %+v", i, m, tt)
  59. }
  60. }
  61. }