server_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package etcdserver
  2. import (
  3. "math/rand"
  4. "reflect"
  5. "testing"
  6. "time"
  7. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  8. "github.com/coreos/etcd/raft"
  9. "github.com/coreos/etcd/raft/raftpb"
  10. "github.com/coreos/etcd/store"
  11. "github.com/coreos/etcd/third_party/code.google.com/p/go.net/context"
  12. )
  13. func TestClusterOf1(t *testing.T) { testServer(t, 1) }
  14. func TestClusterOf3(t *testing.T) { testServer(t, 3) }
  15. // firstId is the id of the first raft machine in the array.
  16. // It implies the way to set id for raft machines:
  17. // The id of n-th machine is firstId+n, and machine with machineId is at machineId-firstId place in the array.
  18. const firstId int64 = 0x1000
  19. func testServer(t *testing.T, ns int64) {
  20. ctx, cancel := context.WithCancel(context.Background())
  21. defer cancel()
  22. ss := make([]*Server, ns)
  23. send := func(msgs []raftpb.Message) {
  24. for _, m := range msgs {
  25. t.Logf("m = %+v\n", m)
  26. ss[m.To-firstId].Node.Step(ctx, m)
  27. }
  28. }
  29. peers := make([]int64, ns)
  30. for i := int64(0); i < ns; i++ {
  31. peers[i] = firstId + i
  32. }
  33. for i := int64(0); i < ns; i++ {
  34. id := firstId + i
  35. n := raft.Start(id, peers, 10, 1)
  36. tk := time.NewTicker(10 * time.Millisecond)
  37. defer tk.Stop()
  38. srv := &Server{
  39. Node: n,
  40. Store: store.New(),
  41. Send: send,
  42. Save: func(_ raftpb.State, _ []raftpb.Entry) {},
  43. Ticker: tk.C,
  44. }
  45. Start(srv)
  46. // TODO(xiangli): randomize election timeout
  47. // then remove this sleep.
  48. time.Sleep(1 * time.Millisecond)
  49. ss[i] = srv
  50. }
  51. // TODO: find fast way to trigger leader election
  52. // TODO: introduce the way to know that the leader has been elected
  53. // then remove this sleep.
  54. time.Sleep(110 * time.Millisecond)
  55. for i := 1; i <= 10; i++ {
  56. r := pb.Request{
  57. Method: "PUT",
  58. Id: int64(i),
  59. Path: "/foo",
  60. Val: "bar",
  61. }
  62. j := rand.Intn(len(ss))
  63. t.Logf("ss = %d", j)
  64. resp, err := ss[j].Do(ctx, r)
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. g, w := resp.Event.Node, &store.NodeExtern{
  69. Key: "/foo",
  70. ModifiedIndex: uint64(i),
  71. CreatedIndex: uint64(i),
  72. Value: stringp("bar"),
  73. }
  74. if !reflect.DeepEqual(g, w) {
  75. t.Error("value:", *g.Value)
  76. t.Errorf("g = %+v, w %+v", g, w)
  77. }
  78. }
  79. time.Sleep(10 * time.Millisecond)
  80. var last interface{}
  81. for i, sv := range ss {
  82. sv.Stop()
  83. g, _ := sv.Store.Get("/", true, true)
  84. if last != nil && !reflect.DeepEqual(last, g) {
  85. t.Errorf("server %d: Root = %#v, want %#v", i, g, last)
  86. }
  87. last = g
  88. }
  89. }
  90. func stringp(s string) *string { return &s }