entry_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package rafthttp
  14. import (
  15. "bytes"
  16. "reflect"
  17. "testing"
  18. "github.com/coreos/etcd/raft/raftpb"
  19. )
  20. func TestEntsWriteAndRead(t *testing.T) {
  21. tests := [][]raftpb.Entry{
  22. {
  23. {},
  24. },
  25. {
  26. {Term: 1, Index: 1},
  27. },
  28. {
  29. {Term: 1, Index: 1},
  30. {Term: 1, Index: 2},
  31. {Term: 1, Index: 3},
  32. },
  33. {
  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. for i, tt := range tests {
  40. b := &bytes.Buffer{}
  41. ew := &entryWriter{w: b}
  42. if err := ew.writeEntries(tt); err != nil {
  43. t.Errorf("#%d: unexpected write ents error: %v", i, err)
  44. continue
  45. }
  46. er := &entryReader{r: b}
  47. ents, err := er.readEntries()
  48. if err != nil {
  49. t.Errorf("#%d: unexpected read ents error: %v", i, err)
  50. continue
  51. }
  52. if !reflect.DeepEqual(ents, tt) {
  53. t.Errorf("#%d: ents = %+v, want %+v", i, ents, tt)
  54. }
  55. }
  56. }