server_test.go 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  1. package etcdserver
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "math/rand"
  6. "reflect"
  7. "sync"
  8. "testing"
  9. "time"
  10. "github.com/coreos/etcd/Godeps/_workspace/src/code.google.com/p/go.net/context"
  11. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  12. "github.com/coreos/etcd/pkg"
  13. "github.com/coreos/etcd/raft"
  14. "github.com/coreos/etcd/raft/raftpb"
  15. "github.com/coreos/etcd/store"
  16. )
  17. func TestGetExpirationTime(t *testing.T) {
  18. tests := []struct {
  19. r pb.Request
  20. want time.Time
  21. }{
  22. {
  23. pb.Request{Expiration: 0},
  24. time.Time{},
  25. },
  26. {
  27. pb.Request{Expiration: 60000},
  28. time.Unix(0, 60000),
  29. },
  30. {
  31. pb.Request{Expiration: -60000},
  32. time.Unix(0, -60000),
  33. },
  34. }
  35. for i, tt := range tests {
  36. got := getExpirationTime(&tt.r)
  37. if !reflect.DeepEqual(tt.want, got) {
  38. t.Errorf("#%d: incorrect expiration time: want=%v got=%v", i, tt.want, got)
  39. }
  40. }
  41. }
  42. // TestDoLocalAction tests requests which do not need to go through raft to be applied,
  43. // and are served through local data.
  44. func TestDoLocalAction(t *testing.T) {
  45. tests := []struct {
  46. req pb.Request
  47. wresp Response
  48. werr error
  49. wactions []action
  50. }{
  51. {
  52. pb.Request{Method: "GET", ID: 1, Wait: true},
  53. Response{Watcher: &stubWatcher{}}, nil, []action{action{name: "Watch"}},
  54. },
  55. {
  56. pb.Request{Method: "GET", ID: 1},
  57. Response{Event: &store.Event{}}, nil,
  58. []action{
  59. action{
  60. name: "Get",
  61. params: []interface{}{"", false, false},
  62. },
  63. },
  64. },
  65. {
  66. pb.Request{Method: "BADMETHOD", ID: 1},
  67. Response{}, ErrUnknownMethod, []action{},
  68. },
  69. }
  70. for i, tt := range tests {
  71. st := &storeRecorder{}
  72. srv := &EtcdServer{store: st}
  73. resp, err := srv.Do(context.TODO(), tt.req)
  74. if err != tt.werr {
  75. t.Fatalf("#%d: err = %+v, want %+v", i, err, tt.werr)
  76. }
  77. if !reflect.DeepEqual(resp, tt.wresp) {
  78. t.Errorf("#%d: resp = %+v, want %+v", i, resp, tt.wresp)
  79. }
  80. gaction := st.Action()
  81. if !reflect.DeepEqual(gaction, tt.wactions) {
  82. t.Errorf("#%d: action = %+v, want %+v", i, gaction, tt.wactions)
  83. }
  84. }
  85. }
  86. // TestDoBadLocalAction tests server requests which do not need to go through consensus,
  87. // and return errors when they fetch from local data.
  88. func TestDoBadLocalAction(t *testing.T) {
  89. storeErr := fmt.Errorf("bah")
  90. tests := []struct {
  91. req pb.Request
  92. wactions []action
  93. }{
  94. {
  95. pb.Request{Method: "GET", ID: 1, Wait: true},
  96. []action{action{name: "Watch"}},
  97. },
  98. {
  99. pb.Request{Method: "GET", ID: 1},
  100. []action{action{name: "Get"}},
  101. },
  102. }
  103. for i, tt := range tests {
  104. st := &errStoreRecorder{err: storeErr}
  105. srv := &EtcdServer{store: st}
  106. resp, err := srv.Do(context.Background(), tt.req)
  107. if err != storeErr {
  108. t.Fatalf("#%d: err = %+v, want %+v", i, err, storeErr)
  109. }
  110. if !reflect.DeepEqual(resp, Response{}) {
  111. t.Errorf("#%d: resp = %+v, want %+v", i, resp, Response{})
  112. }
  113. gaction := st.Action()
  114. if !reflect.DeepEqual(gaction, tt.wactions) {
  115. t.Errorf("#%d: action = %+v, want %+v", i, gaction, tt.wactions)
  116. }
  117. }
  118. }
  119. func TestApplyRequest(t *testing.T) {
  120. tests := []struct {
  121. req pb.Request
  122. wresp Response
  123. wactions []action
  124. }{
  125. // POST ==> Create
  126. {
  127. pb.Request{Method: "POST", ID: 1},
  128. Response{Event: &store.Event{}},
  129. []action{
  130. action{
  131. name: "Create",
  132. params: []interface{}{"", false, "", true, time.Time{}},
  133. },
  134. },
  135. },
  136. // POST ==> Create, with expiration
  137. {
  138. pb.Request{Method: "POST", ID: 1, Expiration: 1337},
  139. Response{Event: &store.Event{}},
  140. []action{
  141. action{
  142. name: "Create",
  143. params: []interface{}{"", false, "", true, time.Unix(0, 1337)},
  144. },
  145. },
  146. },
  147. // POST ==> Create, with dir
  148. {
  149. pb.Request{Method: "POST", ID: 1, Dir: true},
  150. Response{Event: &store.Event{}},
  151. []action{
  152. action{
  153. name: "Create",
  154. params: []interface{}{"", true, "", true, time.Time{}},
  155. },
  156. },
  157. },
  158. // PUT ==> Set
  159. {
  160. pb.Request{Method: "PUT", ID: 1},
  161. Response{Event: &store.Event{}},
  162. []action{
  163. action{
  164. name: "Set",
  165. params: []interface{}{"", false, "", time.Time{}},
  166. },
  167. },
  168. },
  169. // PUT ==> Set, with dir
  170. {
  171. pb.Request{Method: "PUT", ID: 1, Dir: true},
  172. Response{Event: &store.Event{}},
  173. []action{
  174. action{
  175. name: "Set",
  176. params: []interface{}{"", true, "", time.Time{}},
  177. },
  178. },
  179. },
  180. // PUT with PrevExist=true ==> Update
  181. {
  182. pb.Request{Method: "PUT", ID: 1, PrevExist: boolp(true)},
  183. Response{Event: &store.Event{}},
  184. []action{
  185. action{
  186. name: "Update",
  187. params: []interface{}{"", "", time.Time{}},
  188. },
  189. },
  190. },
  191. // PUT with PrevExist=false ==> Create
  192. {
  193. pb.Request{Method: "PUT", ID: 1, PrevExist: boolp(false)},
  194. Response{Event: &store.Event{}},
  195. []action{
  196. action{
  197. name: "Create",
  198. params: []interface{}{"", false, "", false, time.Time{}},
  199. },
  200. },
  201. },
  202. // PUT with PrevExist=true *and* PrevIndex set ==> Update
  203. // TODO(jonboulle): is this expected?!
  204. {
  205. pb.Request{Method: "PUT", ID: 1, PrevExist: boolp(true), PrevIndex: 1},
  206. Response{Event: &store.Event{}},
  207. []action{
  208. action{
  209. name: "Update",
  210. params: []interface{}{"", "", time.Time{}},
  211. },
  212. },
  213. },
  214. // PUT with PrevExist=false *and* PrevIndex set ==> Create
  215. // TODO(jonboulle): is this expected?!
  216. {
  217. pb.Request{Method: "PUT", ID: 1, PrevExist: boolp(false), PrevIndex: 1},
  218. Response{Event: &store.Event{}},
  219. []action{
  220. action{
  221. name: "Create",
  222. params: []interface{}{"", false, "", false, time.Time{}},
  223. },
  224. },
  225. },
  226. // PUT with PrevIndex set ==> CompareAndSwap
  227. {
  228. pb.Request{Method: "PUT", ID: 1, PrevIndex: 1},
  229. Response{Event: &store.Event{}},
  230. []action{
  231. action{
  232. name: "CompareAndSwap",
  233. params: []interface{}{"", "", uint64(1), "", time.Time{}},
  234. },
  235. },
  236. },
  237. // PUT with PrevValue set ==> CompareAndSwap
  238. {
  239. pb.Request{Method: "PUT", ID: 1, PrevValue: "bar"},
  240. Response{Event: &store.Event{}},
  241. []action{
  242. action{
  243. name: "CompareAndSwap",
  244. params: []interface{}{"", "bar", uint64(0), "", time.Time{}},
  245. },
  246. },
  247. },
  248. // PUT with PrevIndex and PrevValue set ==> CompareAndSwap
  249. {
  250. pb.Request{Method: "PUT", ID: 1, PrevIndex: 1, PrevValue: "bar"},
  251. Response{Event: &store.Event{}},
  252. []action{
  253. action{
  254. name: "CompareAndSwap",
  255. params: []interface{}{"", "bar", uint64(1), "", time.Time{}},
  256. },
  257. },
  258. },
  259. // DELETE ==> Delete
  260. {
  261. pb.Request{Method: "DELETE", ID: 1},
  262. Response{Event: &store.Event{}},
  263. []action{
  264. action{
  265. name: "Delete",
  266. params: []interface{}{"", false, false},
  267. },
  268. },
  269. },
  270. // DELETE with PrevIndex set ==> CompareAndDelete
  271. {
  272. pb.Request{Method: "DELETE", ID: 1, PrevIndex: 1},
  273. Response{Event: &store.Event{}},
  274. []action{
  275. action{
  276. name: "CompareAndDelete",
  277. params: []interface{}{"", "", uint64(1)},
  278. },
  279. },
  280. },
  281. // DELETE with PrevValue set ==> CompareAndDelete
  282. {
  283. pb.Request{Method: "DELETE", ID: 1, PrevValue: "bar"},
  284. Response{Event: &store.Event{}},
  285. []action{
  286. action{
  287. name: "CompareAndDelete",
  288. params: []interface{}{"", "bar", uint64(0)},
  289. },
  290. },
  291. },
  292. // DELETE with PrevIndex *and* PrevValue set ==> CompareAndDelete
  293. {
  294. pb.Request{Method: "DELETE", ID: 1, PrevIndex: 5, PrevValue: "bar"},
  295. Response{Event: &store.Event{}},
  296. []action{
  297. action{
  298. name: "CompareAndDelete",
  299. params: []interface{}{"", "bar", uint64(5)},
  300. },
  301. },
  302. },
  303. // QGET ==> Get
  304. {
  305. pb.Request{Method: "QGET", ID: 1},
  306. Response{Event: &store.Event{}},
  307. []action{
  308. action{
  309. name: "Get",
  310. params: []interface{}{"", false, false},
  311. },
  312. },
  313. },
  314. // SYNC ==> DeleteExpiredKeys
  315. {
  316. pb.Request{Method: "SYNC", ID: 1},
  317. Response{},
  318. []action{
  319. action{
  320. name: "DeleteExpiredKeys",
  321. params: []interface{}{time.Unix(0, 0)},
  322. },
  323. },
  324. },
  325. {
  326. pb.Request{Method: "SYNC", ID: 1, Time: 12345},
  327. Response{},
  328. []action{
  329. action{
  330. name: "DeleteExpiredKeys",
  331. params: []interface{}{time.Unix(0, 12345)},
  332. },
  333. },
  334. },
  335. // Unknown method - error
  336. {
  337. pb.Request{Method: "BADMETHOD", ID: 1},
  338. Response{err: ErrUnknownMethod},
  339. []action{},
  340. },
  341. }
  342. for i, tt := range tests {
  343. st := &storeRecorder{}
  344. srv := &EtcdServer{store: st}
  345. resp := srv.applyRequest(tt.req)
  346. if !reflect.DeepEqual(resp, tt.wresp) {
  347. t.Errorf("#%d: resp = %+v, want %+v", i, resp, tt.wresp)
  348. }
  349. gaction := st.Action()
  350. if !reflect.DeepEqual(gaction, tt.wactions) {
  351. t.Errorf("#%d: action = %#v, want %#v", i, gaction, tt.wactions)
  352. }
  353. }
  354. }
  355. func TestApplyConfChangeError(t *testing.T) {
  356. nodes := []uint64{1, 2, 3}
  357. removedNodes := []uint64{4}
  358. tests := []struct {
  359. cc raftpb.ConfChange
  360. werr error
  361. }{
  362. {
  363. raftpb.ConfChange{
  364. Type: raftpb.ConfChangeAddNode,
  365. NodeID: 1,
  366. },
  367. ErrIDExists,
  368. },
  369. {
  370. raftpb.ConfChange{
  371. Type: raftpb.ConfChangeAddNode,
  372. NodeID: 4,
  373. },
  374. ErrIDRemoved,
  375. },
  376. {
  377. raftpb.ConfChange{
  378. Type: raftpb.ConfChangeRemoveNode,
  379. NodeID: 4,
  380. },
  381. ErrIDRemoved,
  382. },
  383. {
  384. raftpb.ConfChange{
  385. Type: raftpb.ConfChangeRemoveNode,
  386. NodeID: 5,
  387. },
  388. ErrIDNotFound,
  389. },
  390. }
  391. for i, tt := range tests {
  392. n := &nodeRecorder{}
  393. srv := &EtcdServer{
  394. node: n,
  395. }
  396. err := srv.applyConfChange(tt.cc, nodes, removedNodes)
  397. if err != tt.werr {
  398. t.Errorf("#%d: applyConfChange error = %v, want %v", i, err, tt.werr)
  399. }
  400. cc := raftpb.ConfChange{Type: tt.cc.Type, NodeID: raft.None}
  401. w := []action{
  402. {
  403. name: "ApplyConfChange",
  404. params: []interface{}{cc},
  405. },
  406. }
  407. if g := n.Action(); !reflect.DeepEqual(g, w) {
  408. t.Errorf("#%d: action = %+v, want %+v", i, g, w)
  409. }
  410. }
  411. }
  412. func TestClusterOf1(t *testing.T) { testServer(t, 1) }
  413. func TestClusterOf3(t *testing.T) { testServer(t, 3) }
  414. func testServer(t *testing.T, ns uint64) {
  415. ctx, cancel := context.WithCancel(context.Background())
  416. defer cancel()
  417. ss := make([]*EtcdServer, ns)
  418. send := func(msgs []raftpb.Message) {
  419. for _, m := range msgs {
  420. t.Logf("m = %+v\n", m)
  421. ss[m.To-1].node.Step(ctx, m)
  422. }
  423. }
  424. ids := make([]uint64, ns)
  425. for i := uint64(0); i < ns; i++ {
  426. ids[i] = i + 1
  427. }
  428. members := mustMakePeerSlice(t, ids...)
  429. for i := uint64(0); i < ns; i++ {
  430. id := i + 1
  431. n := raft.StartNode(id, members, 10, 1)
  432. tk := time.NewTicker(10 * time.Millisecond)
  433. defer tk.Stop()
  434. srv := &EtcdServer{
  435. node: n,
  436. store: store.New(),
  437. send: send,
  438. storage: &storageRecorder{},
  439. ticker: tk.C,
  440. ClusterStore: &clusterStoreRecorder{},
  441. }
  442. srv.start()
  443. ss[i] = srv
  444. }
  445. for i := 1; i <= 10; i++ {
  446. r := pb.Request{
  447. Method: "PUT",
  448. ID: uint64(i),
  449. Path: "/foo",
  450. Val: "bar",
  451. }
  452. j := rand.Intn(len(ss))
  453. t.Logf("ss = %d", j)
  454. resp, err := ss[j].Do(ctx, r)
  455. if err != nil {
  456. t.Fatal(err)
  457. }
  458. g, w := resp.Event.Node, &store.NodeExtern{
  459. Key: "/foo",
  460. ModifiedIndex: uint64(i),
  461. CreatedIndex: uint64(i),
  462. Value: stringp("bar"),
  463. }
  464. if !reflect.DeepEqual(g, w) {
  465. t.Error("value:", *g.Value)
  466. t.Errorf("g = %+v, w %+v", g, w)
  467. }
  468. }
  469. time.Sleep(10 * time.Millisecond)
  470. var last interface{}
  471. for i, sv := range ss {
  472. sv.Stop()
  473. g, _ := sv.store.Get("/", true, true)
  474. if last != nil && !reflect.DeepEqual(last, g) {
  475. t.Errorf("server %d: Root = %#v, want %#v", i, g, last)
  476. }
  477. last = g
  478. }
  479. }
  480. func TestDoProposal(t *testing.T) {
  481. tests := []pb.Request{
  482. pb.Request{Method: "POST", ID: 1},
  483. pb.Request{Method: "PUT", ID: 1},
  484. pb.Request{Method: "DELETE", ID: 1},
  485. pb.Request{Method: "GET", ID: 1, Quorum: true},
  486. }
  487. for i, tt := range tests {
  488. ctx, _ := context.WithCancel(context.Background())
  489. n := raft.StartNode(0xBAD0, mustMakePeerSlice(t, 0xBAD0), 10, 1)
  490. st := &storeRecorder{}
  491. tk := make(chan time.Time)
  492. // this makes <-tk always successful, which accelerates internal clock
  493. close(tk)
  494. srv := &EtcdServer{
  495. node: n,
  496. store: st,
  497. send: func(_ []raftpb.Message) {},
  498. storage: &storageRecorder{},
  499. ticker: tk,
  500. ClusterStore: &clusterStoreRecorder{},
  501. }
  502. srv.start()
  503. resp, err := srv.Do(ctx, tt)
  504. srv.Stop()
  505. action := st.Action()
  506. if len(action) != 1 {
  507. t.Errorf("#%d: len(action) = %d, want 1", i, len(action))
  508. }
  509. if err != nil {
  510. t.Fatalf("#%d: err = %v, want nil", i, err)
  511. }
  512. wresp := Response{Event: &store.Event{}}
  513. if !reflect.DeepEqual(resp, wresp) {
  514. t.Errorf("#%d: resp = %v, want %v", i, resp, wresp)
  515. }
  516. }
  517. }
  518. func TestDoProposalCancelled(t *testing.T) {
  519. ctx, cancel := context.WithCancel(context.Background())
  520. // node cannot make any progress because there are two nodes
  521. n := raft.StartNode(0xBAD0, mustMakePeerSlice(t, 0xBAD0, 0xBAD1), 10, 1)
  522. st := &storeRecorder{}
  523. wait := &waitRecorder{}
  524. srv := &EtcdServer{
  525. // TODO: use fake node for better testability
  526. node: n,
  527. store: st,
  528. w: wait,
  529. }
  530. done := make(chan struct{})
  531. var err error
  532. go func() {
  533. _, err = srv.Do(ctx, pb.Request{Method: "PUT", ID: 1})
  534. close(done)
  535. }()
  536. cancel()
  537. <-done
  538. gaction := st.Action()
  539. if len(gaction) != 0 {
  540. t.Errorf("len(action) = %v, want 0", len(gaction))
  541. }
  542. if err != context.Canceled {
  543. t.Fatalf("err = %v, want %v", err, context.Canceled)
  544. }
  545. w := []action{action{name: "Register1"}, action{name: "Trigger1"}}
  546. if !reflect.DeepEqual(wait.action, w) {
  547. t.Errorf("wait.action = %+v, want %+v", wait.action, w)
  548. }
  549. }
  550. func TestDoProposalStopped(t *testing.T) {
  551. ctx, cancel := context.WithCancel(context.Background())
  552. defer cancel()
  553. // node cannot make any progress because there are two nodes
  554. n := raft.StartNode(0xBAD0, mustMakePeerSlice(t, 0xBAD0, 0xBAD1), 10, 1)
  555. st := &storeRecorder{}
  556. tk := make(chan time.Time)
  557. // this makes <-tk always successful, which accelarates internal clock
  558. close(tk)
  559. srv := &EtcdServer{
  560. // TODO: use fake node for better testability
  561. node: n,
  562. store: st,
  563. send: func(_ []raftpb.Message) {},
  564. storage: &storageRecorder{},
  565. ticker: tk,
  566. }
  567. srv.start()
  568. done := make(chan struct{})
  569. var err error
  570. go func() {
  571. _, err = srv.Do(ctx, pb.Request{Method: "PUT", ID: 1})
  572. close(done)
  573. }()
  574. srv.Stop()
  575. <-done
  576. action := st.Action()
  577. if len(action) != 0 {
  578. t.Errorf("len(action) = %v, want 0", len(action))
  579. }
  580. if err != ErrStopped {
  581. t.Errorf("err = %v, want %v", err, ErrStopped)
  582. }
  583. }
  584. // TestSync tests sync 1. is nonblocking 2. sends out SYNC request.
  585. func TestSync(t *testing.T) {
  586. n := &nodeProposeDataRecorder{}
  587. srv := &EtcdServer{
  588. node: n,
  589. }
  590. start := time.Now()
  591. srv.sync(defaultSyncTimeout)
  592. // check that sync is non-blocking
  593. if d := time.Since(start); d > time.Millisecond {
  594. t.Errorf("CallSyncTime = %v, want < %v", d, time.Millisecond)
  595. }
  596. pkg.ForceGosched()
  597. data := n.data()
  598. if len(data) != 1 {
  599. t.Fatalf("len(proposeData) = %d, want 1", len(data))
  600. }
  601. var r pb.Request
  602. if err := r.Unmarshal(data[0]); err != nil {
  603. t.Fatalf("unmarshal request error: %v", err)
  604. }
  605. if r.Method != "SYNC" {
  606. t.Errorf("method = %s, want SYNC", r.Method)
  607. }
  608. }
  609. // TestSyncTimeout tests the case that sync 1. is non-blocking 2. cancel request
  610. // after timeout
  611. func TestSyncTimeout(t *testing.T) {
  612. n := &nodeProposalBlockerRecorder{}
  613. srv := &EtcdServer{
  614. node: n,
  615. }
  616. start := time.Now()
  617. srv.sync(0)
  618. // check that sync is non-blocking
  619. if d := time.Since(start); d > time.Millisecond {
  620. t.Errorf("CallSyncTime = %v, want < %v", d, time.Millisecond)
  621. }
  622. // give time for goroutine in sync to cancel
  623. // TODO: use fake clock
  624. pkg.ForceGosched()
  625. w := []action{action{name: "Propose blocked"}}
  626. if g := n.Action(); !reflect.DeepEqual(g, w) {
  627. t.Errorf("action = %v, want %v", g, w)
  628. }
  629. }
  630. // TODO: TestNoSyncWhenNoLeader
  631. // blockingNodeProposer implements the node interface to allow users to
  632. // block until Propose has been called and then verify the Proposed data
  633. type blockingNodeProposer struct {
  634. ch chan []byte
  635. readyNode
  636. }
  637. func (n *blockingNodeProposer) Propose(_ context.Context, data []byte) error {
  638. n.ch <- data
  639. return nil
  640. }
  641. // TestSyncTrigger tests that the server proposes a SYNC request when its sync timer ticks
  642. func TestSyncTrigger(t *testing.T) {
  643. n := &blockingNodeProposer{
  644. ch: make(chan []byte),
  645. readyNode: *newReadyNode(),
  646. }
  647. st := make(chan time.Time, 1)
  648. srv := &EtcdServer{
  649. node: n,
  650. store: &storeRecorder{},
  651. send: func(_ []raftpb.Message) {},
  652. storage: &storageRecorder{},
  653. syncTicker: st,
  654. }
  655. srv.start()
  656. // trigger the server to become a leader and accept sync requests
  657. n.readyc <- raft.Ready{
  658. SoftState: &raft.SoftState{
  659. RaftState: raft.StateLeader,
  660. },
  661. }
  662. // trigger a sync request
  663. st <- time.Time{}
  664. var data []byte
  665. select {
  666. case <-time.After(time.Second):
  667. t.Fatalf("did not receive proposed request as expected!")
  668. case data = <-n.ch:
  669. }
  670. srv.Stop()
  671. var req pb.Request
  672. if err := req.Unmarshal(data); err != nil {
  673. t.Fatalf("error unmarshalling data: %v", err)
  674. }
  675. if req.Method != "SYNC" {
  676. t.Fatalf("unexpected proposed request: %#v", req.Method)
  677. }
  678. }
  679. // snapshot should snapshot the store and cut the persistent
  680. // TODO: node.Compact is called... we need to make the node an interface
  681. func TestSnapshot(t *testing.T) {
  682. n := raft.StartNode(0xBAD0, mustMakePeerSlice(t, 0xBAD0), 10, 1)
  683. defer n.Stop()
  684. st := &storeRecorder{}
  685. p := &storageRecorder{}
  686. s := &EtcdServer{
  687. store: st,
  688. storage: p,
  689. node: n,
  690. }
  691. s.snapshot(0, []uint64{1})
  692. gaction := st.Action()
  693. if len(gaction) != 1 {
  694. t.Fatalf("len(action) = %d, want 1", len(gaction))
  695. }
  696. if !reflect.DeepEqual(gaction[0], action{name: "Save"}) {
  697. t.Errorf("action = %s, want Save", gaction[0])
  698. }
  699. gaction = p.Action()
  700. if len(gaction) != 1 {
  701. t.Fatalf("len(action) = %d, want 1", len(gaction))
  702. }
  703. if !reflect.DeepEqual(gaction[0], action{name: "Cut"}) {
  704. t.Errorf("action = %s, want Cut", gaction[0])
  705. }
  706. }
  707. // Applied > SnapCount should trigger a SaveSnap event
  708. func TestTriggerSnap(t *testing.T) {
  709. ctx := context.Background()
  710. n := raft.StartNode(0xBAD0, mustMakePeerSlice(t, 0xBAD0), 10, 1)
  711. <-n.Ready()
  712. n.ApplyConfChange(raftpb.ConfChange{Type: raftpb.ConfChangeAddNode, NodeID: 0xBAD0})
  713. n.Campaign(ctx)
  714. st := &storeRecorder{}
  715. p := &storageRecorder{}
  716. s := &EtcdServer{
  717. store: st,
  718. send: func(_ []raftpb.Message) {},
  719. storage: p,
  720. node: n,
  721. snapCount: 10,
  722. ClusterStore: &clusterStoreRecorder{},
  723. }
  724. s.start()
  725. for i := 0; uint64(i) < s.snapCount-1; i++ {
  726. s.Do(ctx, pb.Request{Method: "PUT", ID: 1})
  727. }
  728. time.Sleep(time.Millisecond)
  729. s.Stop()
  730. gaction := p.Action()
  731. // each operation is recorded as a Save
  732. // BootstrapConfig/Nop + (SnapCount - 1) * Puts + Cut + SaveSnap = Save + (SnapCount - 1) * Save + Cut + SaveSnap
  733. if len(gaction) != 2+int(s.snapCount) {
  734. t.Fatalf("len(action) = %d, want %d", len(gaction), 2+int(s.snapCount))
  735. }
  736. if !reflect.DeepEqual(gaction[11], action{name: "SaveSnap"}) {
  737. t.Errorf("action = %s, want SaveSnap", gaction[11])
  738. }
  739. }
  740. // TestRecvSnapshot tests when it receives a snapshot from raft leader,
  741. // it should trigger storage.SaveSnap and also store.Recover.
  742. func TestRecvSnapshot(t *testing.T) {
  743. n := newReadyNode()
  744. st := &storeRecorder{}
  745. p := &storageRecorder{}
  746. s := &EtcdServer{
  747. store: st,
  748. send: func(_ []raftpb.Message) {},
  749. storage: p,
  750. node: n,
  751. }
  752. s.start()
  753. n.readyc <- raft.Ready{Snapshot: raftpb.Snapshot{Index: 1}}
  754. // make goroutines move forward to receive snapshot
  755. pkg.ForceGosched()
  756. s.Stop()
  757. wactions := []action{action{name: "Recovery"}}
  758. if g := st.Action(); !reflect.DeepEqual(g, wactions) {
  759. t.Errorf("store action = %v, want %v", g, wactions)
  760. }
  761. wactions = []action{action{name: "Save"}, action{name: "SaveSnap"}}
  762. if g := p.Action(); !reflect.DeepEqual(g, wactions) {
  763. t.Errorf("storage action = %v, want %v", g, wactions)
  764. }
  765. }
  766. // TestRecvSlowSnapshot tests that slow snapshot will not be applied
  767. // to store.
  768. func TestRecvSlowSnapshot(t *testing.T) {
  769. n := newReadyNode()
  770. st := &storeRecorder{}
  771. s := &EtcdServer{
  772. store: st,
  773. send: func(_ []raftpb.Message) {},
  774. storage: &storageRecorder{},
  775. node: n,
  776. }
  777. s.start()
  778. n.readyc <- raft.Ready{Snapshot: raftpb.Snapshot{Index: 1}}
  779. // make goroutines move forward to receive snapshot
  780. pkg.ForceGosched()
  781. action := st.Action()
  782. n.readyc <- raft.Ready{Snapshot: raftpb.Snapshot{Index: 1}}
  783. // make goroutines move forward to receive snapshot
  784. pkg.ForceGosched()
  785. s.Stop()
  786. if g := st.Action(); !reflect.DeepEqual(g, action) {
  787. t.Errorf("store action = %v, want %v", g, action)
  788. }
  789. }
  790. // TestAddMember tests AddMember can propose and perform node addition.
  791. func TestAddMember(t *testing.T) {
  792. n := newNodeConfChangeCommitterRecorder()
  793. n.readyc <- raft.Ready{
  794. SoftState: &raft.SoftState{
  795. RaftState: raft.StateLeader,
  796. Nodes: []uint64{2, 3},
  797. },
  798. }
  799. cs := &clusterStoreRecorder{}
  800. s := &EtcdServer{
  801. node: n,
  802. store: &storeRecorder{},
  803. send: func(_ []raftpb.Message) {},
  804. storage: &storageRecorder{},
  805. ClusterStore: cs,
  806. }
  807. s.start()
  808. m := Member{ID: 1, RaftAttributes: RaftAttributes{PeerURLs: []string{"foo"}}}
  809. err := s.AddMember(context.TODO(), m)
  810. gaction := n.Action()
  811. s.Stop()
  812. if err != nil {
  813. t.Fatalf("AddMember error: %v", err)
  814. }
  815. wactions := []action{action{name: "ProposeConfChange:ConfChangeAddNode"}, action{name: "ApplyConfChange:ConfChangeAddNode"}}
  816. if !reflect.DeepEqual(gaction, wactions) {
  817. t.Errorf("action = %v, want %v", gaction, wactions)
  818. }
  819. wcsactions := []action{{name: "Add", params: []interface{}{m}}}
  820. if g := cs.Action(); !reflect.DeepEqual(g, wcsactions) {
  821. t.Errorf("csaction = %v, want %v", g, wcsactions)
  822. }
  823. }
  824. // TestRemoveMember tests RemoveMember can propose and perform node removal.
  825. func TestRemoveMember(t *testing.T) {
  826. n := newNodeConfChangeCommitterRecorder()
  827. n.readyc <- raft.Ready{
  828. SoftState: &raft.SoftState{
  829. RaftState: raft.StateLeader,
  830. Nodes: []uint64{1, 2, 3},
  831. },
  832. }
  833. cs := &clusterStoreRecorder{}
  834. s := &EtcdServer{
  835. node: n,
  836. store: &storeRecorder{},
  837. send: func(_ []raftpb.Message) {},
  838. storage: &storageRecorder{},
  839. ClusterStore: cs,
  840. }
  841. s.start()
  842. id := uint64(1)
  843. err := s.RemoveMember(context.TODO(), id)
  844. gaction := n.Action()
  845. s.Stop()
  846. if err != nil {
  847. t.Fatalf("RemoveMember error: %v", err)
  848. }
  849. wactions := []action{action{name: "ProposeConfChange:ConfChangeRemoveNode"}, action{name: "ApplyConfChange:ConfChangeRemoveNode"}}
  850. if !reflect.DeepEqual(gaction, wactions) {
  851. t.Errorf("action = %v, want %v", gaction, wactions)
  852. }
  853. wcsactions := []action{{name: "Remove", params: []interface{}{id}}}
  854. if g := cs.Action(); !reflect.DeepEqual(g, wcsactions) {
  855. t.Errorf("csaction = %v, want %v", g, wcsactions)
  856. }
  857. }
  858. // TestServerStopItself tests that if node sends out Ready with ShouldStop,
  859. // server will stop.
  860. func TestServerStopItself(t *testing.T) {
  861. n := newReadyNode()
  862. s := &EtcdServer{
  863. node: n,
  864. store: &storeRecorder{},
  865. send: func(_ []raftpb.Message) {},
  866. storage: &storageRecorder{},
  867. }
  868. s.start()
  869. n.readyc <- raft.Ready{SoftState: &raft.SoftState{ShouldStop: true}}
  870. select {
  871. case <-s.done:
  872. case <-time.After(time.Millisecond):
  873. t.Errorf("did not receive from closed done channel as expected")
  874. }
  875. }
  876. // TODO: test wait trigger correctness in multi-server case
  877. func TestPublish(t *testing.T) {
  878. n := &nodeProposeDataRecorder{}
  879. ch := make(chan interface{}, 1)
  880. // simulate that request has gone through consensus
  881. ch <- Response{}
  882. w := &waitWithResponse{ch: ch}
  883. srv := &EtcdServer{
  884. id: 1,
  885. attributes: Attributes{Name: "node1", ClientURLs: []string{"http://a", "http://b"}},
  886. node: n,
  887. w: w,
  888. }
  889. srv.publish(time.Hour)
  890. data := n.data()
  891. if len(data) != 1 {
  892. t.Fatalf("len(proposeData) = %d, want 1", len(data))
  893. }
  894. var r pb.Request
  895. if err := r.Unmarshal(data[0]); err != nil {
  896. t.Fatalf("unmarshal request error: %v", err)
  897. }
  898. if r.Method != "PUT" {
  899. t.Errorf("method = %s, want PUT", r.Method)
  900. }
  901. wm := Member{ID: 1, Attributes: Attributes{Name: "node1", ClientURLs: []string{"http://a", "http://b"}}}
  902. if r.Path != wm.storeKey()+attributesSuffix {
  903. t.Errorf("path = %s, want %s", r.Path, wm.storeKey()+attributesSuffix)
  904. }
  905. var gattr Attributes
  906. if err := json.Unmarshal([]byte(r.Val), &gattr); err != nil {
  907. t.Fatalf("unmarshal val error: %v", err)
  908. }
  909. if !reflect.DeepEqual(gattr, wm.Attributes) {
  910. t.Errorf("member = %v, want %v", gattr, wm.Attributes)
  911. }
  912. }
  913. // TestPublishStopped tests that publish will be stopped if server is stopped.
  914. func TestPublishStopped(t *testing.T) {
  915. srv := &EtcdServer{
  916. node: &nodeRecorder{},
  917. w: &waitRecorder{},
  918. done: make(chan struct{}),
  919. }
  920. srv.Stop()
  921. srv.publish(time.Hour)
  922. }
  923. // TestPublishRetry tests that publish will keep retry until success.
  924. func TestPublishRetry(t *testing.T) {
  925. n := &nodeRecorder{}
  926. srv := &EtcdServer{
  927. node: n,
  928. w: &waitRecorder{},
  929. done: make(chan struct{}),
  930. }
  931. time.AfterFunc(500*time.Microsecond, srv.Stop)
  932. srv.publish(10 * time.Nanosecond)
  933. action := n.Action()
  934. // multiple Proposes
  935. if len(action) < 2 {
  936. t.Errorf("len(action) = %d, want >= 2", action)
  937. }
  938. }
  939. func TestGetBool(t *testing.T) {
  940. tests := []struct {
  941. b *bool
  942. wb bool
  943. wset bool
  944. }{
  945. {nil, false, false},
  946. {boolp(true), true, true},
  947. {boolp(false), false, true},
  948. }
  949. for i, tt := range tests {
  950. b, set := getBool(tt.b)
  951. if b != tt.wb {
  952. t.Errorf("#%d: value = %v, want %v", i, b, tt.wb)
  953. }
  954. if set != tt.wset {
  955. t.Errorf("#%d: set = %v, want %v", i, set, tt.wset)
  956. }
  957. }
  958. }
  959. func TestGenID(t *testing.T) {
  960. // Sanity check that the GenID function has been seeded appropriately
  961. // (math/rand is seeded with 1 by default)
  962. r := rand.NewSource(int64(1))
  963. var n uint64
  964. for n == 0 {
  965. n = uint64(r.Int63())
  966. }
  967. if n == GenID() {
  968. t.Fatalf("GenID's rand seeded with 1!")
  969. }
  970. }
  971. type action struct {
  972. name string
  973. params []interface{}
  974. }
  975. type recorder struct {
  976. sync.Mutex
  977. actions []action
  978. }
  979. func (r *recorder) record(a action) {
  980. r.Lock()
  981. r.actions = append(r.actions, a)
  982. r.Unlock()
  983. }
  984. func (r *recorder) Action() []action {
  985. r.Lock()
  986. cpy := make([]action, len(r.actions))
  987. copy(cpy, r.actions)
  988. r.Unlock()
  989. return cpy
  990. }
  991. type storeRecorder struct {
  992. recorder
  993. }
  994. func (s *storeRecorder) Version() int { return 0 }
  995. func (s *storeRecorder) Index() uint64 { return 0 }
  996. func (s *storeRecorder) Get(path string, recursive, sorted bool) (*store.Event, error) {
  997. s.record(action{
  998. name: "Get",
  999. params: []interface{}{path, recursive, sorted},
  1000. })
  1001. return &store.Event{}, nil
  1002. }
  1003. func (s *storeRecorder) Set(path string, dir bool, val string, expr time.Time) (*store.Event, error) {
  1004. s.record(action{
  1005. name: "Set",
  1006. params: []interface{}{path, dir, val, expr},
  1007. })
  1008. return &store.Event{}, nil
  1009. }
  1010. func (s *storeRecorder) Update(path, val string, expr time.Time) (*store.Event, error) {
  1011. s.record(action{
  1012. name: "Update",
  1013. params: []interface{}{path, val, expr},
  1014. })
  1015. return &store.Event{}, nil
  1016. }
  1017. func (s *storeRecorder) Create(path string, dir bool, val string, uniq bool, exp time.Time) (*store.Event, error) {
  1018. s.record(action{
  1019. name: "Create",
  1020. params: []interface{}{path, dir, val, uniq, exp},
  1021. })
  1022. return &store.Event{}, nil
  1023. }
  1024. func (s *storeRecorder) CompareAndSwap(path, prevVal string, prevIdx uint64, val string, expr time.Time) (*store.Event, error) {
  1025. s.record(action{
  1026. name: "CompareAndSwap",
  1027. params: []interface{}{path, prevVal, prevIdx, val, expr},
  1028. })
  1029. return &store.Event{}, nil
  1030. }
  1031. func (s *storeRecorder) Delete(path string, dir, recursive bool) (*store.Event, error) {
  1032. s.record(action{
  1033. name: "Delete",
  1034. params: []interface{}{path, dir, recursive},
  1035. })
  1036. return &store.Event{}, nil
  1037. }
  1038. func (s *storeRecorder) CompareAndDelete(path, prevVal string, prevIdx uint64) (*store.Event, error) {
  1039. s.record(action{
  1040. name: "CompareAndDelete",
  1041. params: []interface{}{path, prevVal, prevIdx},
  1042. })
  1043. return &store.Event{}, nil
  1044. }
  1045. func (s *storeRecorder) Watch(_ string, _, _ bool, _ uint64) (store.Watcher, error) {
  1046. s.record(action{name: "Watch"})
  1047. return &stubWatcher{}, nil
  1048. }
  1049. func (s *storeRecorder) Save() ([]byte, error) {
  1050. s.record(action{name: "Save"})
  1051. return nil, nil
  1052. }
  1053. func (s *storeRecorder) Recovery(b []byte) error {
  1054. s.record(action{name: "Recovery"})
  1055. return nil
  1056. }
  1057. func (s *storeRecorder) JsonStats() []byte { return nil }
  1058. func (s *storeRecorder) DeleteExpiredKeys(cutoff time.Time) {
  1059. s.record(action{
  1060. name: "DeleteExpiredKeys",
  1061. params: []interface{}{cutoff},
  1062. })
  1063. }
  1064. type stubWatcher struct{}
  1065. func (w *stubWatcher) EventChan() chan *store.Event { return nil }
  1066. func (w *stubWatcher) StartIndex() uint64 { return 0 }
  1067. func (w *stubWatcher) Remove() {}
  1068. // errStoreRecorder returns an store error on Get, Watch request
  1069. type errStoreRecorder struct {
  1070. storeRecorder
  1071. err error
  1072. }
  1073. func (s *errStoreRecorder) Get(_ string, _, _ bool) (*store.Event, error) {
  1074. s.record(action{name: "Get"})
  1075. return nil, s.err
  1076. }
  1077. func (s *errStoreRecorder) Watch(_ string, _, _ bool, _ uint64) (store.Watcher, error) {
  1078. s.record(action{name: "Watch"})
  1079. return nil, s.err
  1080. }
  1081. type waitRecorder struct {
  1082. action []action
  1083. }
  1084. func (w *waitRecorder) Register(id uint64) <-chan interface{} {
  1085. w.action = append(w.action, action{name: fmt.Sprint("Register", id)})
  1086. return nil
  1087. }
  1088. func (w *waitRecorder) Trigger(id uint64, x interface{}) {
  1089. w.action = append(w.action, action{name: fmt.Sprint("Trigger", id)})
  1090. }
  1091. func boolp(b bool) *bool { return &b }
  1092. func stringp(s string) *string { return &s }
  1093. type storageRecorder struct {
  1094. recorder
  1095. }
  1096. func (p *storageRecorder) Save(st raftpb.HardState, ents []raftpb.Entry) {
  1097. p.record(action{name: "Save"})
  1098. }
  1099. func (p *storageRecorder) Cut() error {
  1100. p.record(action{name: "Cut"})
  1101. return nil
  1102. }
  1103. func (p *storageRecorder) SaveSnap(st raftpb.Snapshot) {
  1104. if raft.IsEmptySnap(st) {
  1105. return
  1106. }
  1107. p.record(action{name: "SaveSnap"})
  1108. }
  1109. type readyNode struct {
  1110. readyc chan raft.Ready
  1111. }
  1112. func newReadyNode() *readyNode {
  1113. readyc := make(chan raft.Ready, 1)
  1114. return &readyNode{readyc: readyc}
  1115. }
  1116. func (n *readyNode) Tick() {}
  1117. func (n *readyNode) Campaign(ctx context.Context) error { return nil }
  1118. func (n *readyNode) Propose(ctx context.Context, data []byte) error { return nil }
  1119. func (n *readyNode) ProposeConfChange(ctx context.Context, conf raftpb.ConfChange) error {
  1120. return nil
  1121. }
  1122. func (n *readyNode) Step(ctx context.Context, msg raftpb.Message) error { return nil }
  1123. func (n *readyNode) Ready() <-chan raft.Ready { return n.readyc }
  1124. func (n *readyNode) ApplyConfChange(conf raftpb.ConfChange) {}
  1125. func (n *readyNode) Stop() {}
  1126. func (n *readyNode) Compact(index uint64, nodes []uint64, d []byte) {}
  1127. type nodeRecorder struct {
  1128. recorder
  1129. }
  1130. func (n *nodeRecorder) Tick() {
  1131. n.record(action{name: "Tick"})
  1132. }
  1133. func (n *nodeRecorder) Campaign(ctx context.Context) error {
  1134. n.record(action{name: "Campaign"})
  1135. return nil
  1136. }
  1137. func (n *nodeRecorder) Propose(ctx context.Context, data []byte) error {
  1138. n.record(action{name: "Propose"})
  1139. return nil
  1140. }
  1141. func (n *nodeRecorder) ProposeConfChange(ctx context.Context, conf raftpb.ConfChange) error {
  1142. n.record(action{name: "ProposeConfChange"})
  1143. return nil
  1144. }
  1145. func (n *nodeRecorder) Step(ctx context.Context, msg raftpb.Message) error {
  1146. n.record(action{name: "Step"})
  1147. return nil
  1148. }
  1149. func (n *nodeRecorder) Ready() <-chan raft.Ready { return nil }
  1150. func (n *nodeRecorder) ApplyConfChange(conf raftpb.ConfChange) {
  1151. n.record(action{name: "ApplyConfChange", params: []interface{}{conf}})
  1152. }
  1153. func (n *nodeRecorder) Stop() {
  1154. n.record(action{name: "Stop"})
  1155. }
  1156. func (n *nodeRecorder) Compact(index uint64, nodes []uint64, d []byte) {
  1157. n.record(action{name: "Compact"})
  1158. }
  1159. type nodeProposeDataRecorder struct {
  1160. nodeRecorder
  1161. sync.Mutex
  1162. d [][]byte
  1163. }
  1164. func (n *nodeProposeDataRecorder) data() [][]byte {
  1165. n.Lock()
  1166. d := n.d
  1167. n.Unlock()
  1168. return d
  1169. }
  1170. func (n *nodeProposeDataRecorder) Propose(ctx context.Context, data []byte) error {
  1171. n.nodeRecorder.Propose(ctx, data)
  1172. n.Lock()
  1173. n.d = append(n.d, data)
  1174. n.Unlock()
  1175. return nil
  1176. }
  1177. type nodeProposalBlockerRecorder struct {
  1178. nodeRecorder
  1179. }
  1180. func (n *nodeProposalBlockerRecorder) Propose(ctx context.Context, data []byte) error {
  1181. <-ctx.Done()
  1182. n.record(action{name: "Propose blocked"})
  1183. return nil
  1184. }
  1185. type nodeConfChangeCommitterRecorder struct {
  1186. nodeRecorder
  1187. readyc chan raft.Ready
  1188. }
  1189. func newNodeConfChangeCommitterRecorder() *nodeConfChangeCommitterRecorder {
  1190. readyc := make(chan raft.Ready, 1)
  1191. return &nodeConfChangeCommitterRecorder{readyc: readyc}
  1192. }
  1193. func (n *nodeConfChangeCommitterRecorder) ProposeConfChange(ctx context.Context, conf raftpb.ConfChange) error {
  1194. data, err := conf.Marshal()
  1195. if err != nil {
  1196. return err
  1197. }
  1198. n.readyc <- raft.Ready{CommittedEntries: []raftpb.Entry{{Type: raftpb.EntryConfChange, Data: data}}}
  1199. n.record(action{name: "ProposeConfChange:" + conf.Type.String()})
  1200. return nil
  1201. }
  1202. func (n *nodeConfChangeCommitterRecorder) Ready() <-chan raft.Ready {
  1203. return n.readyc
  1204. }
  1205. func (n *nodeConfChangeCommitterRecorder) ApplyConfChange(conf raftpb.ConfChange) {
  1206. n.record(action{name: "ApplyConfChange:" + conf.Type.String()})
  1207. }
  1208. type waitWithResponse struct {
  1209. ch <-chan interface{}
  1210. }
  1211. func (w *waitWithResponse) Register(id uint64) <-chan interface{} {
  1212. return w.ch
  1213. }
  1214. func (w *waitWithResponse) Trigger(id uint64, x interface{}) {}
  1215. type clusterStoreRecorder struct {
  1216. recorder
  1217. }
  1218. func (cs *clusterStoreRecorder) Add(m Member) {
  1219. cs.record(action{name: "Add", params: []interface{}{m}})
  1220. }
  1221. func (cs *clusterStoreRecorder) Get() Cluster {
  1222. cs.record(action{name: "Get"})
  1223. return nil
  1224. }
  1225. func (cs *clusterStoreRecorder) Remove(id uint64) {
  1226. cs.record(action{name: "Remove", params: []interface{}{id}})
  1227. }
  1228. func mustMakePeerSlice(t *testing.T, ids ...uint64) []raft.Peer {
  1229. peers := make([]raft.Peer, len(ids))
  1230. for i, id := range ids {
  1231. m := Member{ID: id}
  1232. b, err := json.Marshal(m)
  1233. if err != nil {
  1234. t.Fatal(err)
  1235. }
  1236. peers[i] = raft.Peer{ID: id, Context: b}
  1237. }
  1238. return peers
  1239. }