node_test.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package raft
  2. import (
  3. "reflect"
  4. "testing"
  5. "github.com/coreos/etcd/raft/raftpb"
  6. "code.google.com/p/go.net/context"
  7. )
  8. func TestNode(t *testing.T) {
  9. ctx, cancel := context.WithCancel(context.Background())
  10. defer cancel()
  11. n := Start(1, []int64{1}, 0, 0)
  12. ch := make(chan Ready)
  13. go func() {
  14. for {
  15. ch <- <-n.Ready()
  16. }
  17. }()
  18. n.Campaign(ctx)
  19. n.Propose(ctx, []byte("foo"))
  20. want := []Ready{
  21. {
  22. State: raftpb.State{Term: 1, Vote: -1, Commit: 1, LastIndex: 1},
  23. Entries: []raftpb.Entry{{Term: 1, Index: 1}},
  24. CommittedEntries: []raftpb.Entry{{Term: 1, Index: 1}},
  25. },
  26. {
  27. State: raftpb.State{Term: 1, Vote: -1, Commit: 2, LastIndex: 2},
  28. Entries: []raftpb.Entry{{Term: 1, Index: 2, Data: []byte("foo")}},
  29. CommittedEntries: []raftpb.Entry{{Term: 1, Index: 2, Data: []byte("foo")}},
  30. },
  31. }
  32. for i, w := range want {
  33. if g := <-ch; !reflect.DeepEqual(g, w) {
  34. t.Errorf("#%d: g = %+v,\n w %+v", i, g, w)
  35. }
  36. }
  37. select {
  38. case rd := <-ch:
  39. t.Errorf("unexpected Ready: %+v", rd)
  40. default:
  41. }
  42. }