discovery_test.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. package discovery
  2. import (
  3. "errors"
  4. "math/rand"
  5. "sort"
  6. "strconv"
  7. "reflect"
  8. "testing"
  9. "time"
  10. "github.com/coreos/etcd/client"
  11. )
  12. func TestCheckCluster(t *testing.T) {
  13. cluster := "1000"
  14. self := "/1000/1"
  15. tests := []struct {
  16. nodes []*client.Node
  17. werr error
  18. wsize int
  19. }{
  20. {
  21. // self is in the size range
  22. client.Nodes{
  23. {Key: "/1000/_config/size", Value: "3", CreatedIndex: 1},
  24. {Key: self, CreatedIndex: 2},
  25. {Key: "/1000/2", CreatedIndex: 3},
  26. {Key: "/1000/3", CreatedIndex: 4},
  27. {Key: "/1000/4", CreatedIndex: 5},
  28. },
  29. nil,
  30. 3,
  31. },
  32. {
  33. // self is in the size range
  34. client.Nodes{
  35. {Key: "/1000/_config/size", Value: "3", CreatedIndex: 1},
  36. {Key: "/1000/2", CreatedIndex: 2},
  37. {Key: "/1000/3", CreatedIndex: 3},
  38. {Key: self, CreatedIndex: 4},
  39. {Key: "/1000/4", CreatedIndex: 5},
  40. },
  41. nil,
  42. 3,
  43. },
  44. {
  45. // self is out of the size range
  46. client.Nodes{
  47. {Key: "/1000/_config/size", Value: "3", CreatedIndex: 1},
  48. {Key: "/1000/2", CreatedIndex: 2},
  49. {Key: "/1000/3", CreatedIndex: 3},
  50. {Key: "/1000/4", CreatedIndex: 4},
  51. {Key: self, CreatedIndex: 5},
  52. },
  53. ErrFullCluster,
  54. 3,
  55. },
  56. {
  57. // self is not in the cluster
  58. client.Nodes{
  59. {Key: "/1000/_config/size", Value: "3", CreatedIndex: 1},
  60. {Key: "/1000/2", CreatedIndex: 2},
  61. {Key: "/1000/3", CreatedIndex: 3},
  62. },
  63. nil,
  64. 3,
  65. },
  66. {
  67. client.Nodes{
  68. {Key: "/1000/_config/size", Value: "3", CreatedIndex: 1},
  69. {Key: "/1000/2", CreatedIndex: 2},
  70. {Key: "/1000/3", CreatedIndex: 3},
  71. {Key: "/1000/4", CreatedIndex: 4},
  72. },
  73. ErrFullCluster,
  74. 3,
  75. },
  76. {
  77. // bad size key
  78. client.Nodes{
  79. {Key: "/1000/_config/size", Value: "bad", CreatedIndex: 1},
  80. },
  81. ErrBadSizeKey,
  82. 0,
  83. },
  84. {
  85. // no size key
  86. client.Nodes{},
  87. ErrSizeNotFound,
  88. 0,
  89. },
  90. }
  91. for i, tt := range tests {
  92. rs := make([]*client.Response, 0)
  93. if len(tt.nodes) > 0 {
  94. rs = append(rs, &client.Response{Node: tt.nodes[0]})
  95. rs = append(rs, &client.Response{
  96. Node: &client.Node{
  97. Key: cluster,
  98. Nodes: tt.nodes,
  99. },
  100. })
  101. }
  102. c := &clientWithResp{rs: rs}
  103. d := discovery{cluster: cluster, id: 1, c: c}
  104. cRetry := &clientWithRetry{failTimes: 3}
  105. cRetry.rs = rs
  106. dRetry := discovery{cluster: cluster, id: 1, c: cRetry, timeoutTimescale: time.Millisecond * 2}
  107. for _, d := range []discovery{d, dRetry} {
  108. ns, size, err := d.checkCluster()
  109. if err != tt.werr {
  110. t.Errorf("#%d: err = %v, want %v", i, err, tt.werr)
  111. }
  112. if reflect.DeepEqual(ns, tt.nodes) {
  113. t.Errorf("#%d: nodes = %v, want %v", i, ns, tt.nodes)
  114. }
  115. if size != tt.wsize {
  116. t.Errorf("#%d: size = %v, want %d", i, size, tt.wsize)
  117. }
  118. }
  119. }
  120. }
  121. func TestWaitNodes(t *testing.T) {
  122. all := client.Nodes{
  123. {Key: "/1000/1", CreatedIndex: 2},
  124. {Key: "/1000/2", CreatedIndex: 3},
  125. {Key: "/1000/3", CreatedIndex: 4},
  126. }
  127. tests := []struct {
  128. nodes client.Nodes
  129. size int
  130. rs []*client.Response
  131. werr error
  132. wall client.Nodes
  133. }{
  134. {
  135. all,
  136. 3,
  137. []*client.Response{},
  138. nil,
  139. all,
  140. },
  141. {
  142. all[:1],
  143. 3,
  144. []*client.Response{
  145. {Node: &client.Node{Key: "/1000/2", CreatedIndex: 3}},
  146. {Node: &client.Node{Key: "/1000/3", CreatedIndex: 4}},
  147. },
  148. nil,
  149. all,
  150. },
  151. {
  152. all[:2],
  153. 3,
  154. []*client.Response{
  155. {Node: &client.Node{Key: "/1000/3", CreatedIndex: 4}},
  156. },
  157. nil,
  158. all,
  159. },
  160. {
  161. append(all, &client.Node{Key: "/1000/4", CreatedIndex: 5}),
  162. 3,
  163. []*client.Response{
  164. {Node: &client.Node{Key: "/1000/3", CreatedIndex: 4}},
  165. },
  166. nil,
  167. all,
  168. },
  169. }
  170. for i, tt := range tests {
  171. // Basic case
  172. c := &clientWithResp{nil, &watcherWithResp{tt.rs}}
  173. d := &discovery{cluster: "1000", c: c}
  174. // Retry case
  175. retryScanResp := make([]*client.Response, 0)
  176. if len(tt.nodes) > 0 {
  177. retryScanResp = append(retryScanResp, &client.Response{
  178. Node: &client.Node{
  179. Key: "1000",
  180. Value: strconv.Itoa(tt.size),
  181. },
  182. })
  183. retryScanResp = append(retryScanResp, &client.Response{
  184. Node: &client.Node{
  185. Nodes: tt.nodes,
  186. },
  187. })
  188. }
  189. cRetry := &clientWithResp{
  190. rs: retryScanResp,
  191. w: &watcherWithRetry{rs: tt.rs, failTimes: 2},
  192. }
  193. dRetry := &discovery{
  194. cluster: "1000",
  195. c: cRetry,
  196. timeoutTimescale: time.Millisecond * 2,
  197. }
  198. for _, d := range []*discovery{d, dRetry} {
  199. g, err := d.waitNodes(tt.nodes, tt.size)
  200. if err != tt.werr {
  201. t.Errorf("#%d: err = %v, want %v", i, err, tt.werr)
  202. }
  203. if !reflect.DeepEqual(g, tt.wall) {
  204. t.Errorf("#%d: all = %v, want %v", i, g, tt.wall)
  205. }
  206. }
  207. }
  208. }
  209. func TestCreateSelf(t *testing.T) {
  210. rs := []*client.Response{{Node: &client.Node{Key: "1000/1", CreatedIndex: 2}}}
  211. w := &watcherWithResp{rs}
  212. errw := &watcherWithErr{errors.New("watch err")}
  213. c := &clientWithResp{rs, w}
  214. errc := &clientWithErr{errors.New("create err"), w}
  215. errwc := &clientWithResp{rs, errw}
  216. tests := []struct {
  217. c client.Client
  218. werr error
  219. }{
  220. // no error
  221. {c, nil},
  222. // client.create returns an error
  223. {errc, errc.err},
  224. // watcher.next retuens an error
  225. {errwc, errw.err},
  226. }
  227. for i, tt := range tests {
  228. d := discovery{cluster: "1000", c: tt.c}
  229. if err := d.createSelf(); err != tt.werr {
  230. t.Errorf("#%d: err = %v, want %v", i, err, nil)
  231. }
  232. }
  233. }
  234. func TestNodesToCluster(t *testing.T) {
  235. nodes := client.Nodes{
  236. {Key: "/1000/1", Value: "1=1.1.1.1", CreatedIndex: 1},
  237. {Key: "/1000/2", Value: "2=2.2.2.2", CreatedIndex: 2},
  238. {Key: "/1000/3", Value: "3=3.3.3.3", CreatedIndex: 3},
  239. }
  240. w := "1=1.1.1.1,2=2.2.2.2,3=3.3.3.3"
  241. cluster := nodesToCluster(nodes)
  242. if !reflect.DeepEqual(cluster, w) {
  243. t.Errorf("cluster = %v, want %v", cluster, w)
  244. }
  245. }
  246. func TestSortableNodes(t *testing.T) {
  247. ns := client.Nodes{
  248. {CreatedIndex: 5},
  249. {CreatedIndex: 1},
  250. {CreatedIndex: 3},
  251. {CreatedIndex: 4},
  252. }
  253. // add some randomness
  254. for i := 0; i < 10000; i++ {
  255. ns = append(ns, &client.Node{CreatedIndex: uint64(rand.Int31())})
  256. }
  257. sns := sortableNodes{ns}
  258. sort.Sort(sns)
  259. cis := make([]int, 0)
  260. for _, n := range sns.Nodes {
  261. cis = append(cis, int(n.CreatedIndex))
  262. }
  263. if sort.IntsAreSorted(cis) != true {
  264. t.Errorf("isSorted = %v, want %v", sort.IntsAreSorted(cis), true)
  265. }
  266. cis = make([]int, 0)
  267. for _, n := range ns {
  268. cis = append(cis, int(n.CreatedIndex))
  269. }
  270. if sort.IntsAreSorted(cis) != true {
  271. t.Errorf("isSorted = %v, want %v", sort.IntsAreSorted(cis), true)
  272. }
  273. }
  274. func TestRetryFailure(t *testing.T) {
  275. cluster := "1000"
  276. c := &clientWithRetry{failTimes: 4}
  277. d := discovery{cluster: cluster, id: 1, c: c, timeoutTimescale: time.Millisecond * 2}
  278. _, _, err := d.checkCluster()
  279. if err != ErrTooManyRetries {
  280. t.Errorf("err = %v, want %v", err, ErrTooManyRetries)
  281. }
  282. }
  283. type clientWithResp struct {
  284. rs []*client.Response
  285. w client.Watcher
  286. }
  287. func (c *clientWithResp) Create(key string, value string, ttl time.Duration) (*client.Response, error) {
  288. if len(c.rs) == 0 {
  289. return &client.Response{}, nil
  290. }
  291. r := c.rs[0]
  292. c.rs = c.rs[1:]
  293. return r, nil
  294. }
  295. func (c *clientWithResp) Get(key string) (*client.Response, error) {
  296. if len(c.rs) == 0 {
  297. return &client.Response{}, client.ErrKeyNoExist
  298. }
  299. r := c.rs[0]
  300. c.rs = append(c.rs[1:], r)
  301. return r, nil
  302. }
  303. func (c *clientWithResp) Watch(key string, waitIndex uint64) client.Watcher {
  304. return c.w
  305. }
  306. func (c *clientWithResp) RecursiveWatch(key string, waitIndex uint64) client.Watcher {
  307. return c.w
  308. }
  309. type clientWithErr struct {
  310. err error
  311. w client.Watcher
  312. }
  313. func (c *clientWithErr) Create(key string, value string, ttl time.Duration) (*client.Response, error) {
  314. return &client.Response{}, c.err
  315. }
  316. func (c *clientWithErr) Get(key string) (*client.Response, error) {
  317. return &client.Response{}, c.err
  318. }
  319. func (c *clientWithErr) Watch(key string, waitIndex uint64) client.Watcher {
  320. return c.w
  321. }
  322. func (c *clientWithErr) RecursiveWatch(key string, waitIndex uint64) client.Watcher {
  323. return c.w
  324. }
  325. type watcherWithResp struct {
  326. rs []*client.Response
  327. }
  328. func (w *watcherWithResp) Next() (*client.Response, error) {
  329. if len(w.rs) == 0 {
  330. return &client.Response{}, nil
  331. }
  332. r := w.rs[0]
  333. w.rs = w.rs[1:]
  334. return r, nil
  335. }
  336. type watcherWithErr struct {
  337. err error
  338. }
  339. func (w *watcherWithErr) Next() (*client.Response, error) {
  340. return &client.Response{}, w.err
  341. }
  342. // Fails every other time
  343. type clientWithRetry struct {
  344. clientWithResp
  345. failCount int
  346. failTimes int
  347. }
  348. func (c *clientWithRetry) Create(key string, value string, ttl time.Duration) (*client.Response, error) {
  349. if c.failCount < c.failTimes {
  350. c.failCount++
  351. return nil, client.ErrTimeout
  352. }
  353. return c.clientWithResp.Create(key, value, ttl)
  354. }
  355. func (c *clientWithRetry) Get(key string) (*client.Response, error) {
  356. if c.failCount < c.failTimes {
  357. c.failCount++
  358. return nil, client.ErrTimeout
  359. }
  360. return c.clientWithResp.Get(key)
  361. }
  362. type watcherWithRetry struct {
  363. rs []*client.Response
  364. failCount int
  365. failTimes int
  366. }
  367. func (w *watcherWithRetry) Next() (*client.Response, error) {
  368. if w.failCount < w.failTimes {
  369. w.failCount++
  370. return nil, client.ErrTimeout
  371. }
  372. if len(w.rs) == 0 {
  373. return &client.Response{}, nil
  374. }
  375. r := w.rs[0]
  376. w.rs = w.rs[1:]
  377. return r, nil
  378. }