server_test.go 33 KB

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