discovery_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package discovery
  15. import (
  16. "errors"
  17. "math"
  18. "math/rand"
  19. "net/http"
  20. "reflect"
  21. "sort"
  22. "strconv"
  23. "testing"
  24. "time"
  25. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/jonboulle/clockwork"
  26. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  27. "github.com/coreos/etcd/client"
  28. )
  29. const (
  30. maxRetryInTest = 3
  31. )
  32. func TestNewProxyFuncUnset(t *testing.T) {
  33. pf, err := newProxyFunc("")
  34. if pf != nil {
  35. t.Fatal("unexpected non-nil proxyFunc")
  36. }
  37. if err != nil {
  38. t.Fatalf("unexpected non-nil err: %v", err)
  39. }
  40. }
  41. func TestNewProxyFuncBad(t *testing.T) {
  42. tests := []string{
  43. "%%",
  44. "http://foo.com/%1",
  45. }
  46. for i, in := range tests {
  47. pf, err := newProxyFunc(in)
  48. if pf != nil {
  49. t.Errorf("#%d: unexpected non-nil proxyFunc", i)
  50. }
  51. if err == nil {
  52. t.Errorf("#%d: unexpected nil err", i)
  53. }
  54. }
  55. }
  56. func TestNewProxyFunc(t *testing.T) {
  57. tests := map[string]string{
  58. "bar.com": "http://bar.com",
  59. "http://disco.foo.bar": "http://disco.foo.bar",
  60. }
  61. for in, w := range tests {
  62. pf, err := newProxyFunc(in)
  63. if pf == nil {
  64. t.Errorf("%s: unexpected nil proxyFunc", in)
  65. continue
  66. }
  67. if err != nil {
  68. t.Errorf("%s: unexpected non-nil err: %v", in, err)
  69. continue
  70. }
  71. g, err := pf(&http.Request{})
  72. if err != nil {
  73. t.Errorf("%s: unexpected non-nil err: %v", in, err)
  74. }
  75. if g.String() != w {
  76. t.Errorf("%s: proxyURL=%q, want %q", in, g, w)
  77. }
  78. }
  79. }
  80. func TestCheckCluster(t *testing.T) {
  81. cluster := "/prefix/1000"
  82. self := "/1000/1"
  83. tests := []struct {
  84. nodes []*client.Node
  85. index uint64
  86. werr error
  87. wsize int
  88. }{
  89. {
  90. // self is in the size range
  91. []*client.Node{
  92. {Key: "/1000/_config/size", Value: "3", CreatedIndex: 1},
  93. {Key: "/1000/_config/"},
  94. {Key: self, CreatedIndex: 2},
  95. {Key: "/1000/2", CreatedIndex: 3},
  96. {Key: "/1000/3", CreatedIndex: 4},
  97. {Key: "/1000/4", CreatedIndex: 5},
  98. },
  99. 5,
  100. nil,
  101. 3,
  102. },
  103. {
  104. // self is in the size range
  105. []*client.Node{
  106. {Key: "/1000/_config/size", Value: "3", CreatedIndex: 1},
  107. {Key: "/1000/_config/"},
  108. {Key: "/1000/2", CreatedIndex: 2},
  109. {Key: "/1000/3", CreatedIndex: 3},
  110. {Key: self, CreatedIndex: 4},
  111. {Key: "/1000/4", CreatedIndex: 5},
  112. },
  113. 5,
  114. nil,
  115. 3,
  116. },
  117. {
  118. // self is out of the size range
  119. []*client.Node{
  120. {Key: "/1000/_config/size", Value: "3", CreatedIndex: 1},
  121. {Key: "/1000/_config/"},
  122. {Key: "/1000/2", CreatedIndex: 2},
  123. {Key: "/1000/3", CreatedIndex: 3},
  124. {Key: "/1000/4", CreatedIndex: 4},
  125. {Key: self, CreatedIndex: 5},
  126. },
  127. 5,
  128. ErrFullCluster,
  129. 3,
  130. },
  131. {
  132. // self is not in the cluster
  133. []*client.Node{
  134. {Key: "/1000/_config/size", Value: "3", CreatedIndex: 1},
  135. {Key: "/1000/_config/"},
  136. {Key: "/1000/2", CreatedIndex: 2},
  137. {Key: "/1000/3", CreatedIndex: 3},
  138. },
  139. 3,
  140. nil,
  141. 3,
  142. },
  143. {
  144. []*client.Node{
  145. {Key: "/1000/_config/size", Value: "3", CreatedIndex: 1},
  146. {Key: "/1000/_config/"},
  147. {Key: "/1000/2", CreatedIndex: 2},
  148. {Key: "/1000/3", CreatedIndex: 3},
  149. {Key: "/1000/4", CreatedIndex: 4},
  150. },
  151. 3,
  152. ErrFullCluster,
  153. 3,
  154. },
  155. {
  156. // bad size key
  157. []*client.Node{
  158. {Key: "/1000/_config/size", Value: "bad", CreatedIndex: 1},
  159. },
  160. 0,
  161. ErrBadSizeKey,
  162. 0,
  163. },
  164. {
  165. // no size key
  166. []*client.Node{},
  167. 0,
  168. ErrSizeNotFound,
  169. 0,
  170. },
  171. }
  172. for i, tt := range tests {
  173. rs := make([]*client.Response, 0)
  174. if len(tt.nodes) > 0 {
  175. rs = append(rs, &client.Response{Node: tt.nodes[0], Index: tt.index})
  176. rs = append(rs, &client.Response{
  177. Node: &client.Node{
  178. Key: cluster,
  179. Nodes: tt.nodes[1:],
  180. },
  181. Index: tt.index,
  182. })
  183. }
  184. c := &clientWithResp{rs: rs}
  185. dBase := discovery{cluster: cluster, id: 1, c: c}
  186. cRetry := &clientWithRetry{failTimes: 3}
  187. cRetry.rs = rs
  188. fc := clockwork.NewFakeClock()
  189. dRetry := discovery{cluster: cluster, id: 1, c: cRetry, clock: fc}
  190. for _, d := range []discovery{dBase, dRetry} {
  191. go func() {
  192. for i := uint(1); i <= maxRetryInTest; i++ {
  193. fc.BlockUntil(1)
  194. fc.Advance(time.Second * (0x1 << i))
  195. }
  196. }()
  197. ns, size, index, err := d.checkCluster()
  198. if err != tt.werr {
  199. t.Errorf("#%d: err = %v, want %v", i, err, tt.werr)
  200. }
  201. if reflect.DeepEqual(ns, tt.nodes) {
  202. t.Errorf("#%d: nodes = %v, want %v", i, ns, tt.nodes)
  203. }
  204. if size != tt.wsize {
  205. t.Errorf("#%d: size = %v, want %d", i, size, tt.wsize)
  206. }
  207. if index != tt.index {
  208. t.Errorf("#%d: index = %v, want %d", i, index, tt.index)
  209. }
  210. }
  211. }
  212. }
  213. func TestWaitNodes(t *testing.T) {
  214. all := client.Nodes{
  215. 0: {Key: "/1000/1", CreatedIndex: 2},
  216. 1: {Key: "/1000/2", CreatedIndex: 3},
  217. 2: {Key: "/1000/3", CreatedIndex: 4},
  218. }
  219. tests := []struct {
  220. nodes client.Nodes
  221. rs []*client.Response
  222. }{
  223. {
  224. all,
  225. []*client.Response{},
  226. },
  227. {
  228. all[:1],
  229. []*client.Response{
  230. {Node: &client.Node{Key: "/1000/2", CreatedIndex: 3}},
  231. {Node: &client.Node{Key: "/1000/3", CreatedIndex: 4}},
  232. },
  233. },
  234. {
  235. all[:2],
  236. []*client.Response{
  237. {Node: &client.Node{Key: "/1000/3", CreatedIndex: 4}},
  238. },
  239. },
  240. {
  241. append(all, &client.Node{Key: "/1000/4", CreatedIndex: 5}),
  242. []*client.Response{
  243. {Node: &client.Node{Key: "/1000/3", CreatedIndex: 4}},
  244. },
  245. },
  246. }
  247. for i, tt := range tests {
  248. // Basic case
  249. c := &clientWithResp{nil, &watcherWithResp{tt.rs}}
  250. dBase := &discovery{cluster: "1000", c: c}
  251. // Retry case
  252. retryScanResp := make([]*client.Response, 0)
  253. if len(tt.nodes) > 0 {
  254. retryScanResp = append(retryScanResp, &client.Response{
  255. Node: &client.Node{
  256. Key: "1000",
  257. Value: strconv.Itoa(3),
  258. },
  259. })
  260. retryScanResp = append(retryScanResp, &client.Response{
  261. Node: &client.Node{
  262. Nodes: tt.nodes,
  263. },
  264. })
  265. }
  266. cRetry := &clientWithResp{
  267. rs: retryScanResp,
  268. w: &watcherWithRetry{rs: tt.rs, failTimes: 2},
  269. }
  270. fc := clockwork.NewFakeClock()
  271. dRetry := &discovery{
  272. cluster: "1000",
  273. c: cRetry,
  274. clock: fc,
  275. }
  276. for _, d := range []*discovery{dBase, dRetry} {
  277. go func() {
  278. for i := uint(1); i <= maxRetryInTest; i++ {
  279. fc.BlockUntil(1)
  280. fc.Advance(time.Second * (0x1 << i))
  281. }
  282. }()
  283. g, err := d.waitNodes(tt.nodes, 3, 0) // we do not care about index in this test
  284. if err != nil {
  285. t.Errorf("#%d: err = %v, want %v", i, err, nil)
  286. }
  287. if !reflect.DeepEqual(g, all) {
  288. t.Errorf("#%d: all = %v, want %v", i, g, all)
  289. }
  290. }
  291. }
  292. }
  293. func TestCreateSelf(t *testing.T) {
  294. rs := []*client.Response{{Node: &client.Node{Key: "1000/1", CreatedIndex: 2}}}
  295. w := &watcherWithResp{rs}
  296. errw := &watcherWithErr{errors.New("watch err")}
  297. c := &clientWithResp{rs, w}
  298. errc := &clientWithErr{errors.New("create err"), w}
  299. errwc := &clientWithResp{rs, errw}
  300. tests := []struct {
  301. c client.KeysAPI
  302. werr error
  303. }{
  304. // no error
  305. {c, nil},
  306. // client.create returns an error
  307. {errc, errc.err},
  308. // watcher.next retuens an error
  309. {errwc, errw.err},
  310. }
  311. for i, tt := range tests {
  312. d := discovery{cluster: "1000", c: tt.c}
  313. if err := d.createSelf(""); err != tt.werr {
  314. t.Errorf("#%d: err = %v, want %v", i, err, nil)
  315. }
  316. }
  317. }
  318. func TestNodesToCluster(t *testing.T) {
  319. nodes := client.Nodes{
  320. 0: {Key: "/1000/1", Value: "1=1.1.1.1", CreatedIndex: 1},
  321. 1: {Key: "/1000/2", Value: "2=2.2.2.2", CreatedIndex: 2},
  322. 2: {Key: "/1000/3", Value: "3=3.3.3.3", CreatedIndex: 3},
  323. }
  324. w := "1=1.1.1.1,2=2.2.2.2,3=3.3.3.3"
  325. cluster := nodesToCluster(nodes)
  326. if !reflect.DeepEqual(cluster, w) {
  327. t.Errorf("cluster = %v, want %v", cluster, w)
  328. }
  329. }
  330. func TestSortableNodes(t *testing.T) {
  331. ns := client.Nodes{
  332. 0: {CreatedIndex: 5},
  333. 1: {CreatedIndex: 1},
  334. 2: {CreatedIndex: 3},
  335. 3: {CreatedIndex: 4},
  336. }
  337. // add some randomness
  338. for i := 0; i < 10000; i++ {
  339. ns = append(ns, &client.Node{CreatedIndex: uint64(rand.Int31())})
  340. }
  341. sns := sortableNodes{ns}
  342. sort.Sort(sns)
  343. cis := make([]int, 0)
  344. for _, n := range sns.Nodes {
  345. cis = append(cis, int(n.CreatedIndex))
  346. }
  347. if sort.IntsAreSorted(cis) != true {
  348. t.Errorf("isSorted = %v, want %v", sort.IntsAreSorted(cis), true)
  349. }
  350. cis = make([]int, 0)
  351. for _, n := range ns {
  352. cis = append(cis, int(n.CreatedIndex))
  353. }
  354. if sort.IntsAreSorted(cis) != true {
  355. t.Errorf("isSorted = %v, want %v", sort.IntsAreSorted(cis), true)
  356. }
  357. }
  358. func TestRetryFailure(t *testing.T) {
  359. nRetries = maxRetryInTest
  360. defer func() { nRetries = math.MaxUint32 }()
  361. cluster := "1000"
  362. c := &clientWithRetry{failTimes: 4}
  363. fc := clockwork.NewFakeClock()
  364. d := discovery{
  365. cluster: cluster,
  366. id: 1,
  367. c: c,
  368. clock: fc,
  369. }
  370. go func() {
  371. for i := uint(1); i <= maxRetryInTest; i++ {
  372. fc.BlockUntil(1)
  373. fc.Advance(time.Second * (0x1 << i))
  374. }
  375. }()
  376. if _, _, _, err := d.checkCluster(); err != ErrTooManyRetries {
  377. t.Errorf("err = %v, want %v", err, ErrTooManyRetries)
  378. }
  379. }
  380. type clientWithResp struct {
  381. rs []*client.Response
  382. w client.Watcher
  383. }
  384. func (c *clientWithResp) Create(ctx context.Context, key string, value string, ttl time.Duration) (*client.Response, error) {
  385. if len(c.rs) == 0 {
  386. return &client.Response{}, nil
  387. }
  388. r := c.rs[0]
  389. c.rs = c.rs[1:]
  390. return r, nil
  391. }
  392. func (c *clientWithResp) Get(ctx context.Context, key string) (*client.Response, error) {
  393. if len(c.rs) == 0 {
  394. return &client.Response{}, client.ErrKeyNoExist
  395. }
  396. r := c.rs[0]
  397. c.rs = append(c.rs[1:], r)
  398. return r, nil
  399. }
  400. func (c *clientWithResp) Watch(key string, waitIndex uint64) client.Watcher {
  401. return c.w
  402. }
  403. func (c *clientWithResp) RecursiveWatch(key string, waitIndex uint64) client.Watcher {
  404. return c.w
  405. }
  406. type clientWithErr struct {
  407. err error
  408. w client.Watcher
  409. }
  410. func (c *clientWithErr) Create(ctx context.Context, key string, value string, ttl time.Duration) (*client.Response, error) {
  411. return &client.Response{}, c.err
  412. }
  413. func (c *clientWithErr) Get(ctx context.Context, key string) (*client.Response, error) {
  414. return &client.Response{}, c.err
  415. }
  416. func (c *clientWithErr) Watch(key string, waitIndex uint64) client.Watcher {
  417. return c.w
  418. }
  419. func (c *clientWithErr) RecursiveWatch(key string, waitIndex uint64) client.Watcher {
  420. return c.w
  421. }
  422. type watcherWithResp struct {
  423. rs []*client.Response
  424. }
  425. func (w *watcherWithResp) Next(context.Context) (*client.Response, error) {
  426. if len(w.rs) == 0 {
  427. return &client.Response{}, nil
  428. }
  429. r := w.rs[0]
  430. w.rs = w.rs[1:]
  431. return r, nil
  432. }
  433. type watcherWithErr struct {
  434. err error
  435. }
  436. func (w *watcherWithErr) Next(context.Context) (*client.Response, error) {
  437. return &client.Response{}, w.err
  438. }
  439. // clientWithRetry will timeout all requests up to failTimes
  440. type clientWithRetry struct {
  441. clientWithResp
  442. failCount int
  443. failTimes int
  444. }
  445. func (c *clientWithRetry) Create(ctx context.Context, key string, value string, ttl time.Duration) (*client.Response, error) {
  446. if c.failCount < c.failTimes {
  447. c.failCount++
  448. return nil, client.ErrTimeout
  449. }
  450. return c.clientWithResp.Create(ctx, key, value, ttl)
  451. }
  452. func (c *clientWithRetry) Get(ctx context.Context, key string) (*client.Response, error) {
  453. if c.failCount < c.failTimes {
  454. c.failCount++
  455. return nil, client.ErrTimeout
  456. }
  457. return c.clientWithResp.Get(ctx, key)
  458. }
  459. // watcherWithRetry will timeout all requests up to failTimes
  460. type watcherWithRetry struct {
  461. rs []*client.Response
  462. failCount int
  463. failTimes int
  464. }
  465. func (w *watcherWithRetry) Next(context.Context) (*client.Response, error) {
  466. if w.failCount < w.failTimes {
  467. w.failCount++
  468. return nil, client.ErrTimeout
  469. }
  470. if len(w.rs) == 0 {
  471. return &client.Response{}, nil
  472. }
  473. r := w.rs[0]
  474. w.rs = w.rs[1:]
  475. return r, nil
  476. }