server_test.go 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  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. removed := map[uint64]bool{4: true}
  371. tests := []struct {
  372. cc raftpb.ConfChange
  373. werr error
  374. }{
  375. {
  376. raftpb.ConfChange{
  377. Type: raftpb.ConfChangeAddNode,
  378. NodeID: 4,
  379. },
  380. ErrIDRemoved,
  381. },
  382. {
  383. raftpb.ConfChange{
  384. Type: raftpb.ConfChangeRemoveNode,
  385. NodeID: 4,
  386. },
  387. ErrIDRemoved,
  388. },
  389. {
  390. raftpb.ConfChange{
  391. Type: raftpb.ConfChangeAddNode,
  392. NodeID: 1,
  393. },
  394. ErrIDExists,
  395. },
  396. {
  397. raftpb.ConfChange{
  398. Type: raftpb.ConfChangeRemoveNode,
  399. NodeID: 5,
  400. },
  401. ErrIDNotFound,
  402. },
  403. }
  404. for i, tt := range tests {
  405. n := &nodeRecorder{}
  406. cs := &removedClusterStore{removed: removed}
  407. srv := &EtcdServer{
  408. node: n,
  409. ClusterStore: cs,
  410. }
  411. err := srv.applyConfChange(tt.cc, nodes)
  412. if err != tt.werr {
  413. t.Errorf("#%d: applyConfChange error = %v, want %v", i, err, tt.werr)
  414. }
  415. cc := raftpb.ConfChange{Type: tt.cc.Type, NodeID: raft.None}
  416. w := []action{
  417. {
  418. name: "ApplyConfChange",
  419. params: []interface{}{cc},
  420. },
  421. }
  422. if g := n.Action(); !reflect.DeepEqual(g, w) {
  423. t.Errorf("#%d: action = %+v, want %+v", i, g, w)
  424. }
  425. }
  426. }
  427. func TestClusterOf1(t *testing.T) { testServer(t, 1) }
  428. func TestClusterOf3(t *testing.T) { testServer(t, 3) }
  429. func testServer(t *testing.T, ns uint64) {
  430. ctx, cancel := context.WithCancel(context.Background())
  431. defer cancel()
  432. ss := make([]*EtcdServer, ns)
  433. send := func(msgs []raftpb.Message) {
  434. for _, m := range msgs {
  435. t.Logf("m = %+v\n", m)
  436. ss[m.To-1].node.Step(ctx, m)
  437. }
  438. }
  439. ids := make([]uint64, ns)
  440. for i := uint64(0); i < ns; i++ {
  441. ids[i] = i + 1
  442. }
  443. members := mustMakePeerSlice(t, ids...)
  444. for i := uint64(0); i < ns; i++ {
  445. id := i + 1
  446. n := raft.StartNode(id, members, 10, 1)
  447. tk := time.NewTicker(10 * time.Millisecond)
  448. defer tk.Stop()
  449. srv := &EtcdServer{
  450. node: n,
  451. store: store.New(),
  452. send: send,
  453. storage: &storageRecorder{},
  454. Ticker: tk.C,
  455. ClusterStore: &clusterStoreRecorder{},
  456. }
  457. srv.start()
  458. ss[i] = srv
  459. }
  460. for i := 1; i <= 10; i++ {
  461. r := pb.Request{
  462. Method: "PUT",
  463. ID: uint64(i),
  464. Path: "/foo",
  465. Val: "bar",
  466. }
  467. j := rand.Intn(len(ss))
  468. t.Logf("ss = %d", j)
  469. resp, err := ss[j].Do(ctx, r)
  470. if err != nil {
  471. t.Fatal(err)
  472. }
  473. g, w := resp.Event.Node, &store.NodeExtern{
  474. Key: "/foo",
  475. ModifiedIndex: uint64(i),
  476. CreatedIndex: uint64(i),
  477. Value: stringp("bar"),
  478. }
  479. if !reflect.DeepEqual(g, w) {
  480. t.Error("value:", *g.Value)
  481. t.Errorf("g = %+v, w %+v", g, w)
  482. }
  483. }
  484. time.Sleep(10 * time.Millisecond)
  485. var last interface{}
  486. for i, sv := range ss {
  487. sv.Stop()
  488. g, _ := sv.store.Get("/", true, true)
  489. if last != nil && !reflect.DeepEqual(last, g) {
  490. t.Errorf("server %d: Root = %#v, want %#v", i, g, last)
  491. }
  492. last = g
  493. }
  494. }
  495. func TestDoProposal(t *testing.T) {
  496. tests := []pb.Request{
  497. pb.Request{Method: "POST", ID: 1},
  498. pb.Request{Method: "PUT", ID: 1},
  499. pb.Request{Method: "DELETE", ID: 1},
  500. pb.Request{Method: "GET", ID: 1, Quorum: true},
  501. }
  502. for i, tt := range tests {
  503. ctx, _ := context.WithCancel(context.Background())
  504. n := raft.StartNode(0xBAD0, mustMakePeerSlice(t, 0xBAD0), 10, 1)
  505. st := &storeRecorder{}
  506. tk := make(chan time.Time)
  507. // this makes <-tk always successful, which accelerates internal clock
  508. close(tk)
  509. srv := &EtcdServer{
  510. node: n,
  511. store: st,
  512. send: func(_ []raftpb.Message) {},
  513. storage: &storageRecorder{},
  514. Ticker: tk,
  515. ClusterStore: &clusterStoreRecorder{},
  516. }
  517. srv.start()
  518. resp, err := srv.Do(ctx, tt)
  519. srv.Stop()
  520. action := st.Action()
  521. if len(action) != 1 {
  522. t.Errorf("#%d: len(action) = %d, want 1", i, len(action))
  523. }
  524. if err != nil {
  525. t.Fatalf("#%d: err = %v, want nil", i, err)
  526. }
  527. wresp := Response{Event: &store.Event{}}
  528. if !reflect.DeepEqual(resp, wresp) {
  529. t.Errorf("#%d: resp = %v, want %v", i, resp, wresp)
  530. }
  531. }
  532. }
  533. func TestDoProposalCancelled(t *testing.T) {
  534. ctx, cancel := context.WithCancel(context.Background())
  535. // node cannot make any progress because there are two nodes
  536. n := raft.StartNode(0xBAD0, mustMakePeerSlice(t, 0xBAD0, 0xBAD1), 10, 1)
  537. st := &storeRecorder{}
  538. wait := &waitRecorder{}
  539. srv := &EtcdServer{
  540. // TODO: use fake node for better testability
  541. node: n,
  542. store: st,
  543. w: wait,
  544. }
  545. done := make(chan struct{})
  546. var err error
  547. go func() {
  548. _, err = srv.Do(ctx, pb.Request{Method: "PUT", ID: 1})
  549. close(done)
  550. }()
  551. cancel()
  552. <-done
  553. gaction := st.Action()
  554. if len(gaction) != 0 {
  555. t.Errorf("len(action) = %v, want 0", len(gaction))
  556. }
  557. if err != context.Canceled {
  558. t.Fatalf("err = %v, want %v", err, context.Canceled)
  559. }
  560. w := []action{action{name: "Register1"}, action{name: "Trigger1"}}
  561. if !reflect.DeepEqual(wait.action, w) {
  562. t.Errorf("wait.action = %+v, want %+v", wait.action, w)
  563. }
  564. }
  565. func TestDoProposalStopped(t *testing.T) {
  566. ctx, cancel := context.WithCancel(context.Background())
  567. defer cancel()
  568. // node cannot make any progress because there are two nodes
  569. n := raft.StartNode(0xBAD0, mustMakePeerSlice(t, 0xBAD0, 0xBAD1), 10, 1)
  570. st := &storeRecorder{}
  571. tk := make(chan time.Time)
  572. // this makes <-tk always successful, which accelarates internal clock
  573. close(tk)
  574. srv := &EtcdServer{
  575. // TODO: use fake node for better testability
  576. node: n,
  577. store: st,
  578. send: func(_ []raftpb.Message) {},
  579. storage: &storageRecorder{},
  580. Ticker: tk,
  581. }
  582. srv.start()
  583. done := make(chan struct{})
  584. var err error
  585. go func() {
  586. _, err = srv.Do(ctx, pb.Request{Method: "PUT", ID: 1})
  587. close(done)
  588. }()
  589. srv.Stop()
  590. <-done
  591. action := st.Action()
  592. if len(action) != 0 {
  593. t.Errorf("len(action) = %v, want 0", len(action))
  594. }
  595. if err != ErrStopped {
  596. t.Errorf("err = %v, want %v", err, ErrStopped)
  597. }
  598. }
  599. // TestSync tests sync 1. is nonblocking 2. sends out SYNC request.
  600. func TestSync(t *testing.T) {
  601. n := &nodeProposeDataRecorder{}
  602. srv := &EtcdServer{
  603. node: n,
  604. }
  605. start := time.Now()
  606. srv.sync(defaultSyncTimeout)
  607. // check that sync is non-blocking
  608. if d := time.Since(start); d > time.Millisecond {
  609. t.Errorf("CallSyncTime = %v, want < %v", d, time.Millisecond)
  610. }
  611. pkg.ForceGosched()
  612. data := n.data()
  613. if len(data) != 1 {
  614. t.Fatalf("len(proposeData) = %d, want 1", len(data))
  615. }
  616. var r pb.Request
  617. if err := r.Unmarshal(data[0]); err != nil {
  618. t.Fatalf("unmarshal request error: %v", err)
  619. }
  620. if r.Method != "SYNC" {
  621. t.Errorf("method = %s, want SYNC", r.Method)
  622. }
  623. }
  624. // TestSyncTimeout tests the case that sync 1. is non-blocking 2. cancel request
  625. // after timeout
  626. func TestSyncTimeout(t *testing.T) {
  627. n := &nodeProposalBlockerRecorder{}
  628. srv := &EtcdServer{
  629. node: n,
  630. }
  631. start := time.Now()
  632. srv.sync(0)
  633. // check that sync is non-blocking
  634. if d := time.Since(start); d > time.Millisecond {
  635. t.Errorf("CallSyncTime = %v, want < %v", d, time.Millisecond)
  636. }
  637. // give time for goroutine in sync to cancel
  638. // TODO: use fake clock
  639. pkg.ForceGosched()
  640. w := []action{action{name: "Propose blocked"}}
  641. if g := n.Action(); !reflect.DeepEqual(g, w) {
  642. t.Errorf("action = %v, want %v", g, w)
  643. }
  644. }
  645. // TODO: TestNoSyncWhenNoLeader
  646. // blockingNodeProposer implements the node interface to allow users to
  647. // block until Propose has been called and then verify the Proposed data
  648. type blockingNodeProposer struct {
  649. ch chan []byte
  650. readyNode
  651. }
  652. func (n *blockingNodeProposer) Propose(_ context.Context, data []byte) error {
  653. n.ch <- data
  654. return nil
  655. }
  656. // TestSyncTrigger tests that the server proposes a SYNC request when its sync timer ticks
  657. func TestSyncTrigger(t *testing.T) {
  658. n := &blockingNodeProposer{
  659. ch: make(chan []byte),
  660. readyNode: *newReadyNode(),
  661. }
  662. st := make(chan time.Time, 1)
  663. srv := &EtcdServer{
  664. node: n,
  665. store: &storeRecorder{},
  666. send: func(_ []raftpb.Message) {},
  667. storage: &storageRecorder{},
  668. SyncTicker: st,
  669. }
  670. srv.start()
  671. // trigger the server to become a leader and accept sync requests
  672. n.readyc <- raft.Ready{
  673. SoftState: &raft.SoftState{
  674. RaftState: raft.StateLeader,
  675. },
  676. }
  677. // trigger a sync request
  678. st <- time.Time{}
  679. var data []byte
  680. select {
  681. case <-time.After(time.Second):
  682. t.Fatalf("did not receive proposed request as expected!")
  683. case data = <-n.ch:
  684. }
  685. srv.Stop()
  686. var req pb.Request
  687. if err := req.Unmarshal(data); err != nil {
  688. t.Fatalf("error unmarshalling data: %v", err)
  689. }
  690. if req.Method != "SYNC" {
  691. t.Fatalf("unexpected proposed request: %#v", req.Method)
  692. }
  693. }
  694. // snapshot should snapshot the store and cut the persistent
  695. // TODO: node.Compact is called... we need to make the node an interface
  696. func TestSnapshot(t *testing.T) {
  697. n := raft.StartNode(0xBAD0, mustMakePeerSlice(t, 0xBAD0), 10, 1)
  698. defer n.Stop()
  699. st := &storeRecorder{}
  700. p := &storageRecorder{}
  701. s := &EtcdServer{
  702. store: st,
  703. storage: p,
  704. node: n,
  705. }
  706. s.snapshot(0, []uint64{1})
  707. gaction := st.Action()
  708. if len(gaction) != 1 {
  709. t.Fatalf("len(action) = %d, want 1", len(gaction))
  710. }
  711. if !reflect.DeepEqual(gaction[0], action{name: "Save"}) {
  712. t.Errorf("action = %s, want Save", gaction[0])
  713. }
  714. gaction = p.Action()
  715. if len(gaction) != 1 {
  716. t.Fatalf("len(action) = %d, want 1", len(gaction))
  717. }
  718. if !reflect.DeepEqual(gaction[0], action{name: "Cut"}) {
  719. t.Errorf("action = %s, want Cut", gaction[0])
  720. }
  721. }
  722. // Applied > SnapCount should trigger a SaveSnap event
  723. func TestTriggerSnap(t *testing.T) {
  724. ctx := context.Background()
  725. n := raft.StartNode(0xBAD0, mustMakePeerSlice(t, 0xBAD0), 10, 1)
  726. <-n.Ready()
  727. n.ApplyConfChange(raftpb.ConfChange{Type: raftpb.ConfChangeAddNode, NodeID: 0xBAD0})
  728. n.Campaign(ctx)
  729. st := &storeRecorder{}
  730. p := &storageRecorder{}
  731. s := &EtcdServer{
  732. store: st,
  733. send: func(_ []raftpb.Message) {},
  734. storage: p,
  735. node: n,
  736. snapCount: 10,
  737. ClusterStore: &clusterStoreRecorder{},
  738. }
  739. s.start()
  740. for i := 0; uint64(i) < s.snapCount-1; i++ {
  741. s.Do(ctx, pb.Request{Method: "PUT", ID: 1})
  742. }
  743. time.Sleep(time.Millisecond)
  744. s.Stop()
  745. gaction := p.Action()
  746. // each operation is recorded as a Save
  747. // BootstrapConfig/Nop + (SnapCount - 1) * Puts + Cut + SaveSnap = Save + (SnapCount - 1) * Save + Cut + SaveSnap
  748. wcnt := 2 + int(s.snapCount)
  749. if len(gaction) != wcnt {
  750. t.Fatalf("len(action) = %d, want %d", len(gaction), wcnt)
  751. }
  752. if !reflect.DeepEqual(gaction[wcnt-1], action{name: "SaveSnap"}) {
  753. t.Errorf("action = %s, want SaveSnap", gaction[wcnt-1])
  754. }
  755. }
  756. // TestRecvSnapshot tests when it receives a snapshot from raft leader,
  757. // it should trigger storage.SaveSnap and also store.Recover.
  758. func TestRecvSnapshot(t *testing.T) {
  759. n := newReadyNode()
  760. st := &storeRecorder{}
  761. p := &storageRecorder{}
  762. s := &EtcdServer{
  763. store: st,
  764. send: func(_ []raftpb.Message) {},
  765. storage: p,
  766. node: n,
  767. }
  768. s.start()
  769. n.readyc <- raft.Ready{Snapshot: raftpb.Snapshot{Index: 1}}
  770. // make goroutines move forward to receive snapshot
  771. pkg.ForceGosched()
  772. s.Stop()
  773. wactions := []action{action{name: "Recovery"}}
  774. if g := st.Action(); !reflect.DeepEqual(g, wactions) {
  775. t.Errorf("store action = %v, want %v", g, wactions)
  776. }
  777. wactions = []action{action{name: "Save"}, action{name: "SaveSnap"}}
  778. if g := p.Action(); !reflect.DeepEqual(g, wactions) {
  779. t.Errorf("storage action = %v, want %v", g, wactions)
  780. }
  781. }
  782. // TestRecvSlowSnapshot tests that slow snapshot will not be applied
  783. // to store.
  784. func TestRecvSlowSnapshot(t *testing.T) {
  785. n := newReadyNode()
  786. st := &storeRecorder{}
  787. s := &EtcdServer{
  788. store: st,
  789. send: func(_ []raftpb.Message) {},
  790. storage: &storageRecorder{},
  791. node: n,
  792. }
  793. s.start()
  794. n.readyc <- raft.Ready{Snapshot: raftpb.Snapshot{Index: 1}}
  795. // make goroutines move forward to receive snapshot
  796. pkg.ForceGosched()
  797. action := st.Action()
  798. n.readyc <- raft.Ready{Snapshot: raftpb.Snapshot{Index: 1}}
  799. // make goroutines move forward to receive snapshot
  800. pkg.ForceGosched()
  801. s.Stop()
  802. if g := st.Action(); !reflect.DeepEqual(g, action) {
  803. t.Errorf("store action = %v, want %v", g, action)
  804. }
  805. }
  806. // TestAddMember tests AddMember can propose and perform node addition.
  807. func TestAddMember(t *testing.T) {
  808. n := newNodeConfChangeCommitterRecorder()
  809. n.readyc <- raft.Ready{
  810. SoftState: &raft.SoftState{
  811. RaftState: raft.StateLeader,
  812. Nodes: []uint64{2, 3},
  813. },
  814. }
  815. cs := &clusterStoreRecorder{}
  816. s := &EtcdServer{
  817. node: n,
  818. store: &storeRecorder{},
  819. send: func(_ []raftpb.Message) {},
  820. storage: &storageRecorder{},
  821. ClusterStore: cs,
  822. }
  823. s.start()
  824. m := Member{ID: 1, RaftAttributes: RaftAttributes{PeerURLs: []string{"foo"}}}
  825. err := s.AddMember(context.TODO(), m)
  826. gaction := n.Action()
  827. s.Stop()
  828. if err != nil {
  829. t.Fatalf("AddMember error: %v", err)
  830. }
  831. wactions := []action{action{name: "ProposeConfChange:ConfChangeAddNode"}, action{name: "ApplyConfChange:ConfChangeAddNode"}}
  832. if !reflect.DeepEqual(gaction, wactions) {
  833. t.Errorf("action = %v, want %v", gaction, wactions)
  834. }
  835. wcsactions := []action{{name: "Add", params: []interface{}{m}}}
  836. if g := cs.Action(); !reflect.DeepEqual(g, wcsactions) {
  837. t.Errorf("csaction = %v, want %v", g, wcsactions)
  838. }
  839. }
  840. // TestRemoveMember tests RemoveMember can propose and perform node removal.
  841. func TestRemoveMember(t *testing.T) {
  842. n := newNodeConfChangeCommitterRecorder()
  843. n.readyc <- raft.Ready{
  844. SoftState: &raft.SoftState{
  845. RaftState: raft.StateLeader,
  846. Nodes: []uint64{1, 2, 3},
  847. },
  848. }
  849. cs := &clusterStoreRecorder{}
  850. s := &EtcdServer{
  851. node: n,
  852. store: &storeRecorder{},
  853. send: func(_ []raftpb.Message) {},
  854. storage: &storageRecorder{},
  855. ClusterStore: cs,
  856. }
  857. s.start()
  858. id := uint64(1)
  859. err := s.RemoveMember(context.TODO(), id)
  860. gaction := n.Action()
  861. s.Stop()
  862. if err != nil {
  863. t.Fatalf("RemoveMember error: %v", err)
  864. }
  865. wactions := []action{action{name: "ProposeConfChange:ConfChangeRemoveNode"}, action{name: "ApplyConfChange:ConfChangeRemoveNode"}}
  866. if !reflect.DeepEqual(gaction, wactions) {
  867. t.Errorf("action = %v, want %v", gaction, wactions)
  868. }
  869. wcsactions := []action{{name: "Remove", params: []interface{}{id}}}
  870. if g := cs.Action(); !reflect.DeepEqual(g, wcsactions) {
  871. t.Errorf("csaction = %v, want %v", g, wcsactions)
  872. }
  873. }
  874. // TODO: test server could stop itself when being removed
  875. // TODO: test wait trigger correctness in multi-server case
  876. func TestPublish(t *testing.T) {
  877. n := &nodeProposeDataRecorder{}
  878. ch := make(chan interface{}, 1)
  879. // simulate that request has gone through consensus
  880. ch <- Response{}
  881. w := &waitWithResponse{ch: ch}
  882. srv := &EtcdServer{
  883. id: 1,
  884. attributes: Attributes{Name: "node1", ClientURLs: []string{"http://a", "http://b"}},
  885. node: n,
  886. w: w,
  887. }
  888. srv.publish(time.Hour)
  889. data := n.data()
  890. if len(data) != 1 {
  891. t.Fatalf("len(proposeData) = %d, want 1", len(data))
  892. }
  893. var r pb.Request
  894. if err := r.Unmarshal(data[0]); err != nil {
  895. t.Fatalf("unmarshal request error: %v", err)
  896. }
  897. if r.Method != "PUT" {
  898. t.Errorf("method = %s, want PUT", r.Method)
  899. }
  900. wm := Member{ID: 1, Attributes: Attributes{Name: "node1", ClientURLs: []string{"http://a", "http://b"}}}
  901. if w := memberStoreKey(wm.ID) + attributesSuffix; r.Path != w {
  902. t.Errorf("path = %s, want %s", r.Path, w)
  903. }
  904. var gattr Attributes
  905. if err := json.Unmarshal([]byte(r.Val), &gattr); err != nil {
  906. t.Fatalf("unmarshal val error: %v", err)
  907. }
  908. if !reflect.DeepEqual(gattr, wm.Attributes) {
  909. t.Errorf("member = %v, want %v", gattr, wm.Attributes)
  910. }
  911. }
  912. // TestPublishStopped tests that publish will be stopped if server is stopped.
  913. func TestPublishStopped(t *testing.T) {
  914. srv := &EtcdServer{
  915. node: &nodeRecorder{},
  916. w: &waitRecorder{},
  917. done: make(chan struct{}),
  918. stopped: make(chan struct{}),
  919. }
  920. close(srv.stopped)
  921. srv.Stop()
  922. srv.publish(time.Hour)
  923. }
  924. // TestPublishRetry tests that publish will keep retry until success.
  925. func TestPublishRetry(t *testing.T) {
  926. n := &nodeRecorder{}
  927. srv := &EtcdServer{
  928. node: n,
  929. w: &waitRecorder{},
  930. done: make(chan struct{}),
  931. }
  932. time.AfterFunc(500*time.Microsecond, srv.Stop)
  933. srv.publish(10 * time.Nanosecond)
  934. action := n.Action()
  935. // multiple Proposes
  936. if n := len(action); n < 2 {
  937. t.Errorf("len(action) = %d, want >= 2", n)
  938. }
  939. }
  940. func TestGetBool(t *testing.T) {
  941. tests := []struct {
  942. b *bool
  943. wb bool
  944. wset bool
  945. }{
  946. {nil, false, false},
  947. {boolp(true), true, true},
  948. {boolp(false), false, true},
  949. }
  950. for i, tt := range tests {
  951. b, set := getBool(tt.b)
  952. if b != tt.wb {
  953. t.Errorf("#%d: value = %v, want %v", i, b, tt.wb)
  954. }
  955. if set != tt.wset {
  956. t.Errorf("#%d: set = %v, want %v", i, set, tt.wset)
  957. }
  958. }
  959. }
  960. func TestGenID(t *testing.T) {
  961. // Sanity check that the GenID function has been seeded appropriately
  962. // (math/rand is seeded with 1 by default)
  963. r := rand.NewSource(int64(1))
  964. var n uint64
  965. for n == 0 {
  966. n = uint64(r.Int63())
  967. }
  968. if n == GenID() {
  969. t.Fatalf("GenID's rand seeded with 1!")
  970. }
  971. }
  972. type action struct {
  973. name string
  974. params []interface{}
  975. }
  976. type recorder struct {
  977. sync.Mutex
  978. actions []action
  979. }
  980. func (r *recorder) record(a action) {
  981. r.Lock()
  982. r.actions = append(r.actions, a)
  983. r.Unlock()
  984. }
  985. func (r *recorder) Action() []action {
  986. r.Lock()
  987. cpy := make([]action, len(r.actions))
  988. copy(cpy, r.actions)
  989. r.Unlock()
  990. return cpy
  991. }
  992. type storeRecorder struct {
  993. recorder
  994. }
  995. func (s *storeRecorder) Version() int { return 0 }
  996. func (s *storeRecorder) Index() uint64 { return 0 }
  997. func (s *storeRecorder) Get(path string, recursive, sorted bool) (*store.Event, error) {
  998. s.record(action{
  999. name: "Get",
  1000. params: []interface{}{path, recursive, sorted},
  1001. })
  1002. return &store.Event{}, nil
  1003. }
  1004. func (s *storeRecorder) Set(path string, dir bool, val string, expr time.Time) (*store.Event, error) {
  1005. s.record(action{
  1006. name: "Set",
  1007. params: []interface{}{path, dir, val, expr},
  1008. })
  1009. return &store.Event{}, nil
  1010. }
  1011. func (s *storeRecorder) Update(path, val string, expr time.Time) (*store.Event, error) {
  1012. s.record(action{
  1013. name: "Update",
  1014. params: []interface{}{path, val, expr},
  1015. })
  1016. return &store.Event{}, nil
  1017. }
  1018. func (s *storeRecorder) Create(path string, dir bool, val string, uniq bool, exp time.Time) (*store.Event, error) {
  1019. s.record(action{
  1020. name: "Create",
  1021. params: []interface{}{path, dir, val, uniq, exp},
  1022. })
  1023. return &store.Event{}, nil
  1024. }
  1025. func (s *storeRecorder) CompareAndSwap(path, prevVal string, prevIdx uint64, val string, expr time.Time) (*store.Event, error) {
  1026. s.record(action{
  1027. name: "CompareAndSwap",
  1028. params: []interface{}{path, prevVal, prevIdx, val, expr},
  1029. })
  1030. return &store.Event{}, nil
  1031. }
  1032. func (s *storeRecorder) Delete(path string, dir, recursive bool) (*store.Event, error) {
  1033. s.record(action{
  1034. name: "Delete",
  1035. params: []interface{}{path, dir, recursive},
  1036. })
  1037. return &store.Event{}, nil
  1038. }
  1039. func (s *storeRecorder) CompareAndDelete(path, prevVal string, prevIdx uint64) (*store.Event, error) {
  1040. s.record(action{
  1041. name: "CompareAndDelete",
  1042. params: []interface{}{path, prevVal, prevIdx},
  1043. })
  1044. return &store.Event{}, nil
  1045. }
  1046. func (s *storeRecorder) Watch(_ string, _, _ bool, _ uint64) (store.Watcher, error) {
  1047. s.record(action{name: "Watch"})
  1048. return &stubWatcher{}, nil
  1049. }
  1050. func (s *storeRecorder) Save() ([]byte, error) {
  1051. s.record(action{name: "Save"})
  1052. return nil, nil
  1053. }
  1054. func (s *storeRecorder) Recovery(b []byte) error {
  1055. s.record(action{name: "Recovery"})
  1056. return nil
  1057. }
  1058. func (s *storeRecorder) JsonStats() []byte { return nil }
  1059. func (s *storeRecorder) DeleteExpiredKeys(cutoff time.Time) {
  1060. s.record(action{
  1061. name: "DeleteExpiredKeys",
  1062. params: []interface{}{cutoff},
  1063. })
  1064. }
  1065. type stubWatcher struct{}
  1066. func (w *stubWatcher) EventChan() chan *store.Event { return nil }
  1067. func (w *stubWatcher) StartIndex() uint64 { return 0 }
  1068. func (w *stubWatcher) Remove() {}
  1069. // errStoreRecorder returns an store error on Get, Watch request
  1070. type errStoreRecorder struct {
  1071. storeRecorder
  1072. err error
  1073. }
  1074. func (s *errStoreRecorder) Get(_ string, _, _ bool) (*store.Event, error) {
  1075. s.record(action{name: "Get"})
  1076. return nil, s.err
  1077. }
  1078. func (s *errStoreRecorder) Watch(_ string, _, _ bool, _ uint64) (store.Watcher, error) {
  1079. s.record(action{name: "Watch"})
  1080. return nil, s.err
  1081. }
  1082. type waitRecorder struct {
  1083. action []action
  1084. }
  1085. func (w *waitRecorder) Register(id uint64) <-chan interface{} {
  1086. w.action = append(w.action, action{name: fmt.Sprint("Register", id)})
  1087. return nil
  1088. }
  1089. func (w *waitRecorder) Trigger(id uint64, x interface{}) {
  1090. w.action = append(w.action, action{name: fmt.Sprint("Trigger", id)})
  1091. }
  1092. func boolp(b bool) *bool { return &b }
  1093. func stringp(s string) *string { return &s }
  1094. type storageRecorder struct {
  1095. recorder
  1096. }
  1097. func (p *storageRecorder) Save(st raftpb.HardState, ents []raftpb.Entry) {
  1098. p.record(action{name: "Save"})
  1099. }
  1100. func (p *storageRecorder) Cut() error {
  1101. p.record(action{name: "Cut"})
  1102. return nil
  1103. }
  1104. func (p *storageRecorder) SaveSnap(st raftpb.Snapshot) {
  1105. if raft.IsEmptySnap(st) {
  1106. return
  1107. }
  1108. p.record(action{name: "SaveSnap"})
  1109. }
  1110. type readyNode struct {
  1111. readyc chan raft.Ready
  1112. }
  1113. func newReadyNode() *readyNode {
  1114. readyc := make(chan raft.Ready, 1)
  1115. return &readyNode{readyc: readyc}
  1116. }
  1117. func (n *readyNode) Tick() {}
  1118. func (n *readyNode) Campaign(ctx context.Context) error { return nil }
  1119. func (n *readyNode) Propose(ctx context.Context, data []byte) error { return nil }
  1120. func (n *readyNode) ProposeConfChange(ctx context.Context, conf raftpb.ConfChange) error {
  1121. return nil
  1122. }
  1123. func (n *readyNode) Step(ctx context.Context, msg raftpb.Message) error { return nil }
  1124. func (n *readyNode) Ready() <-chan raft.Ready { return n.readyc }
  1125. func (n *readyNode) ApplyConfChange(conf raftpb.ConfChange) {}
  1126. func (n *readyNode) Stop() {}
  1127. func (n *readyNode) Compact(index uint64, nodes []uint64, d []byte) {}
  1128. type nodeRecorder struct {
  1129. recorder
  1130. }
  1131. func (n *nodeRecorder) Tick() {
  1132. n.record(action{name: "Tick"})
  1133. }
  1134. func (n *nodeRecorder) Campaign(ctx context.Context) error {
  1135. n.record(action{name: "Campaign"})
  1136. return nil
  1137. }
  1138. func (n *nodeRecorder) Propose(ctx context.Context, data []byte) error {
  1139. n.record(action{name: "Propose"})
  1140. return nil
  1141. }
  1142. func (n *nodeRecorder) ProposeConfChange(ctx context.Context, conf raftpb.ConfChange) error {
  1143. n.record(action{name: "ProposeConfChange"})
  1144. return nil
  1145. }
  1146. func (n *nodeRecorder) Step(ctx context.Context, msg raftpb.Message) error {
  1147. n.record(action{name: "Step"})
  1148. return nil
  1149. }
  1150. func (n *nodeRecorder) Ready() <-chan raft.Ready { return nil }
  1151. func (n *nodeRecorder) ApplyConfChange(conf raftpb.ConfChange) {
  1152. n.record(action{name: "ApplyConfChange", params: []interface{}{conf}})
  1153. }
  1154. func (n *nodeRecorder) Stop() {
  1155. n.record(action{name: "Stop"})
  1156. }
  1157. func (n *nodeRecorder) Compact(index uint64, nodes []uint64, d []byte) {
  1158. n.record(action{name: "Compact"})
  1159. }
  1160. type nodeProposeDataRecorder struct {
  1161. nodeRecorder
  1162. sync.Mutex
  1163. d [][]byte
  1164. }
  1165. func (n *nodeProposeDataRecorder) data() [][]byte {
  1166. n.Lock()
  1167. d := n.d
  1168. n.Unlock()
  1169. return d
  1170. }
  1171. func (n *nodeProposeDataRecorder) Propose(ctx context.Context, data []byte) error {
  1172. n.nodeRecorder.Propose(ctx, data)
  1173. n.Lock()
  1174. n.d = append(n.d, data)
  1175. n.Unlock()
  1176. return nil
  1177. }
  1178. type nodeProposalBlockerRecorder struct {
  1179. nodeRecorder
  1180. }
  1181. func (n *nodeProposalBlockerRecorder) Propose(ctx context.Context, data []byte) error {
  1182. <-ctx.Done()
  1183. n.record(action{name: "Propose blocked"})
  1184. return nil
  1185. }
  1186. type nodeConfChangeCommitterRecorder struct {
  1187. nodeRecorder
  1188. readyc chan raft.Ready
  1189. }
  1190. func newNodeConfChangeCommitterRecorder() *nodeConfChangeCommitterRecorder {
  1191. readyc := make(chan raft.Ready, 1)
  1192. return &nodeConfChangeCommitterRecorder{readyc: readyc}
  1193. }
  1194. func (n *nodeConfChangeCommitterRecorder) ProposeConfChange(ctx context.Context, conf raftpb.ConfChange) error {
  1195. data, err := conf.Marshal()
  1196. if err != nil {
  1197. return err
  1198. }
  1199. n.readyc <- raft.Ready{CommittedEntries: []raftpb.Entry{{Type: raftpb.EntryConfChange, Data: data}}}
  1200. n.record(action{name: "ProposeConfChange:" + conf.Type.String()})
  1201. return nil
  1202. }
  1203. func (n *nodeConfChangeCommitterRecorder) Ready() <-chan raft.Ready {
  1204. return n.readyc
  1205. }
  1206. func (n *nodeConfChangeCommitterRecorder) ApplyConfChange(conf raftpb.ConfChange) {
  1207. n.record(action{name: "ApplyConfChange:" + conf.Type.String()})
  1208. }
  1209. type waitWithResponse struct {
  1210. ch <-chan interface{}
  1211. }
  1212. func (w *waitWithResponse) Register(id uint64) <-chan interface{} {
  1213. return w.ch
  1214. }
  1215. func (w *waitWithResponse) Trigger(id uint64, x interface{}) {}
  1216. type clusterStoreRecorder struct {
  1217. recorder
  1218. }
  1219. func (cs *clusterStoreRecorder) Add(m Member) {
  1220. cs.record(action{name: "Add", params: []interface{}{m}})
  1221. }
  1222. func (cs *clusterStoreRecorder) Get() Cluster {
  1223. cs.record(action{name: "Get"})
  1224. return Cluster{}
  1225. }
  1226. func (cs *clusterStoreRecorder) Remove(id uint64) {
  1227. cs.record(action{name: "Remove", params: []interface{}{id}})
  1228. }
  1229. func (cs *clusterStoreRecorder) IsRemoved(id uint64) bool { return false }
  1230. type removedClusterStore struct {
  1231. removed map[uint64]bool
  1232. }
  1233. func (cs *removedClusterStore) Add(m Member) {}
  1234. func (cs *removedClusterStore) Get() Cluster { return Cluster{} }
  1235. func (cs *removedClusterStore) Remove(id uint64) {}
  1236. func (cs *removedClusterStore) IsRemoved(id uint64) bool { return cs.removed[id] }
  1237. func mustMakePeerSlice(t *testing.T, ids ...uint64) []raft.Peer {
  1238. peers := make([]raft.Peer, len(ids))
  1239. for i, id := range ids {
  1240. m := Member{ID: id}
  1241. b, err := json.Marshal(m)
  1242. if err != nil {
  1243. t.Fatal(err)
  1244. }
  1245. peers[i] = raft.Peer{ID: id, Context: b}
  1246. }
  1247. return peers
  1248. }