kv_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. // Copyright 2016 The etcd Authors
  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 integration
  15. import (
  16. "bytes"
  17. "reflect"
  18. "strings"
  19. "testing"
  20. "time"
  21. "github.com/coreos/etcd/clientv3"
  22. "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  23. "github.com/coreos/etcd/integration"
  24. "github.com/coreos/etcd/mvcc/mvccpb"
  25. "github.com/coreos/etcd/pkg/testutil"
  26. "golang.org/x/net/context"
  27. )
  28. func TestKVPutError(t *testing.T) {
  29. defer testutil.AfterTest(t)
  30. var (
  31. maxReqBytes = 1.5 * 1024 * 1024
  32. quota = int64(maxReqBytes * 1.2)
  33. )
  34. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1, QuotaBackendBytes: quota})
  35. defer clus.Terminate(t)
  36. kv := clientv3.NewKV(clus.RandClient())
  37. ctx := context.TODO()
  38. _, err := kv.Put(ctx, "", "bar")
  39. if err != rpctypes.ErrEmptyKey {
  40. t.Fatalf("expected %v, got %v", rpctypes.ErrEmptyKey, err)
  41. }
  42. _, err = kv.Put(ctx, "key", strings.Repeat("a", int(maxReqBytes+100))) // 1.5MB
  43. if err != rpctypes.ErrRequestTooLarge {
  44. t.Fatalf("expected %v, got %v", rpctypes.ErrRequestTooLarge, err)
  45. }
  46. _, err = kv.Put(ctx, "foo1", strings.Repeat("a", int(maxReqBytes-50)))
  47. if err != nil { // below quota
  48. t.Fatal(err)
  49. }
  50. time.Sleep(500 * time.Millisecond) // give enough time for commit
  51. _, err = kv.Put(ctx, "foo2", strings.Repeat("a", int(maxReqBytes-50)))
  52. if err != rpctypes.ErrNoSpace { // over quota
  53. t.Fatalf("expected %v, got %v", rpctypes.ErrNoSpace, err)
  54. }
  55. }
  56. func TestKVPut(t *testing.T) {
  57. defer testutil.AfterTest(t)
  58. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  59. defer clus.Terminate(t)
  60. lapi := clientv3.NewLease(clus.RandClient())
  61. defer lapi.Close()
  62. kv := clientv3.NewKV(clus.RandClient())
  63. ctx := context.TODO()
  64. resp, err := lapi.Grant(context.Background(), 10)
  65. if err != nil {
  66. t.Fatalf("failed to create lease %v", err)
  67. }
  68. tests := []struct {
  69. key, val string
  70. leaseID clientv3.LeaseID
  71. }{
  72. {"foo", "bar", clientv3.NoLease},
  73. {"hello", "world", resp.ID},
  74. }
  75. for i, tt := range tests {
  76. if _, err := kv.Put(ctx, tt.key, tt.val, clientv3.WithLease(tt.leaseID)); err != nil {
  77. t.Fatalf("#%d: couldn't put %q (%v)", i, tt.key, err)
  78. }
  79. resp, err := kv.Get(ctx, tt.key)
  80. if err != nil {
  81. t.Fatalf("#%d: couldn't get key (%v)", i, err)
  82. }
  83. if len(resp.Kvs) != 1 {
  84. t.Fatalf("#%d: expected 1 key, got %d", i, len(resp.Kvs))
  85. }
  86. if !bytes.Equal([]byte(tt.val), resp.Kvs[0].Value) {
  87. t.Errorf("#%d: val = %s, want %s", i, tt.val, resp.Kvs[0].Value)
  88. }
  89. if tt.leaseID != clientv3.LeaseID(resp.Kvs[0].Lease) {
  90. t.Errorf("#%d: val = %d, want %d", i, tt.leaseID, resp.Kvs[0].Lease)
  91. }
  92. }
  93. }
  94. func TestKVPutWithRequireLeader(t *testing.T) {
  95. defer testutil.AfterTest(t)
  96. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  97. defer clus.Terminate(t)
  98. clus.Members[1].Stop(t)
  99. clus.Members[2].Stop(t)
  100. // wait for election timeout, then member[0] will not have a leader.
  101. var (
  102. electionTicks = 10
  103. tickDuration = 10 * time.Millisecond
  104. )
  105. time.Sleep(time.Duration(3*electionTicks) * tickDuration)
  106. kv := clientv3.NewKV(clus.Client(0))
  107. _, err := kv.Put(clientv3.WithRequireLeader(context.Background()), "foo", "bar")
  108. if err != rpctypes.ErrNoLeader {
  109. t.Fatal(err)
  110. }
  111. // clients may give timeout errors since the members are stopped; take
  112. // the clients so that terminating the cluster won't complain
  113. clus.Client(1).Close()
  114. clus.Client(2).Close()
  115. clus.TakeClient(1)
  116. clus.TakeClient(2)
  117. }
  118. func TestKVRange(t *testing.T) {
  119. defer testutil.AfterTest(t)
  120. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  121. defer clus.Terminate(t)
  122. kv := clientv3.NewKV(clus.RandClient())
  123. ctx := context.TODO()
  124. keySet := []string{"a", "b", "c", "c", "c", "foo", "foo/abc", "fop"}
  125. for i, key := range keySet {
  126. if _, err := kv.Put(ctx, key, ""); err != nil {
  127. t.Fatalf("#%d: couldn't put %q (%v)", i, key, err)
  128. }
  129. }
  130. resp, err := kv.Get(ctx, keySet[0])
  131. if err != nil {
  132. t.Fatalf("couldn't get key (%v)", err)
  133. }
  134. wheader := resp.Header
  135. tests := []struct {
  136. begin, end string
  137. rev int64
  138. opts []clientv3.OpOption
  139. wantSet []*mvccpb.KeyValue
  140. }{
  141. // range first two
  142. {
  143. "a", "c",
  144. 0,
  145. nil,
  146. []*mvccpb.KeyValue{
  147. {Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
  148. {Key: []byte("b"), Value: nil, CreateRevision: 3, ModRevision: 3, Version: 1},
  149. },
  150. },
  151. // range first two with serializable
  152. {
  153. "a", "c",
  154. 0,
  155. []clientv3.OpOption{clientv3.WithSerializable()},
  156. []*mvccpb.KeyValue{
  157. {Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
  158. {Key: []byte("b"), Value: nil, CreateRevision: 3, ModRevision: 3, Version: 1},
  159. },
  160. },
  161. // range all with rev
  162. {
  163. "a", "x",
  164. 2,
  165. nil,
  166. []*mvccpb.KeyValue{
  167. {Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
  168. },
  169. },
  170. // range all with SortByKey, SortAscend
  171. {
  172. "a", "x",
  173. 0,
  174. []clientv3.OpOption{clientv3.WithSort(clientv3.SortByKey, clientv3.SortAscend)},
  175. []*mvccpb.KeyValue{
  176. {Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
  177. {Key: []byte("b"), Value: nil, CreateRevision: 3, ModRevision: 3, Version: 1},
  178. {Key: []byte("c"), Value: nil, CreateRevision: 4, ModRevision: 6, Version: 3},
  179. {Key: []byte("foo"), Value: nil, CreateRevision: 7, ModRevision: 7, Version: 1},
  180. {Key: []byte("foo/abc"), Value: nil, CreateRevision: 8, ModRevision: 8, Version: 1},
  181. {Key: []byte("fop"), Value: nil, CreateRevision: 9, ModRevision: 9, Version: 1},
  182. },
  183. },
  184. // range all with SortByCreateRevision, SortDescend
  185. {
  186. "a", "x",
  187. 0,
  188. []clientv3.OpOption{clientv3.WithSort(clientv3.SortByCreateRevision, clientv3.SortDescend)},
  189. []*mvccpb.KeyValue{
  190. {Key: []byte("fop"), Value: nil, CreateRevision: 9, ModRevision: 9, Version: 1},
  191. {Key: []byte("foo/abc"), Value: nil, CreateRevision: 8, ModRevision: 8, Version: 1},
  192. {Key: []byte("foo"), Value: nil, CreateRevision: 7, ModRevision: 7, Version: 1},
  193. {Key: []byte("c"), Value: nil, CreateRevision: 4, ModRevision: 6, Version: 3},
  194. {Key: []byte("b"), Value: nil, CreateRevision: 3, ModRevision: 3, Version: 1},
  195. {Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
  196. },
  197. },
  198. // range all with SortByModRevision, SortDescend
  199. {
  200. "a", "x",
  201. 0,
  202. []clientv3.OpOption{clientv3.WithSort(clientv3.SortByModRevision, clientv3.SortDescend)},
  203. []*mvccpb.KeyValue{
  204. {Key: []byte("fop"), Value: nil, CreateRevision: 9, ModRevision: 9, Version: 1},
  205. {Key: []byte("foo/abc"), Value: nil, CreateRevision: 8, ModRevision: 8, Version: 1},
  206. {Key: []byte("foo"), Value: nil, CreateRevision: 7, ModRevision: 7, Version: 1},
  207. {Key: []byte("c"), Value: nil, CreateRevision: 4, ModRevision: 6, Version: 3},
  208. {Key: []byte("b"), Value: nil, CreateRevision: 3, ModRevision: 3, Version: 1},
  209. {Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
  210. },
  211. },
  212. // WithPrefix
  213. {
  214. "foo", "",
  215. 0,
  216. []clientv3.OpOption{clientv3.WithPrefix()},
  217. []*mvccpb.KeyValue{
  218. {Key: []byte("foo"), Value: nil, CreateRevision: 7, ModRevision: 7, Version: 1},
  219. {Key: []byte("foo/abc"), Value: nil, CreateRevision: 8, ModRevision: 8, Version: 1},
  220. },
  221. },
  222. // WithFromKey
  223. {
  224. "fo", "",
  225. 0,
  226. []clientv3.OpOption{clientv3.WithFromKey()},
  227. []*mvccpb.KeyValue{
  228. {Key: []byte("foo"), Value: nil, CreateRevision: 7, ModRevision: 7, Version: 1},
  229. {Key: []byte("foo/abc"), Value: nil, CreateRevision: 8, ModRevision: 8, Version: 1},
  230. {Key: []byte("fop"), Value: nil, CreateRevision: 9, ModRevision: 9, Version: 1},
  231. },
  232. },
  233. }
  234. for i, tt := range tests {
  235. opts := []clientv3.OpOption{clientv3.WithRange(tt.end), clientv3.WithRev(tt.rev)}
  236. opts = append(opts, tt.opts...)
  237. resp, err := kv.Get(ctx, tt.begin, opts...)
  238. if err != nil {
  239. t.Fatalf("#%d: couldn't range (%v)", i, err)
  240. }
  241. if !reflect.DeepEqual(wheader, resp.Header) {
  242. t.Fatalf("#%d: wheader expected %+v, got %+v", i, wheader, resp.Header)
  243. }
  244. if !reflect.DeepEqual(tt.wantSet, resp.Kvs) {
  245. t.Fatalf("#%d: resp.Kvs expected %+v, got %+v", i, tt.wantSet, resp.Kvs)
  246. }
  247. }
  248. }
  249. func TestKVGetErrConnClosed(t *testing.T) {
  250. defer testutil.AfterTest(t)
  251. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
  252. defer clus.Terminate(t)
  253. cli := clus.Client(0)
  254. kv := clientv3.NewKV(cli)
  255. donec := make(chan struct{})
  256. go func() {
  257. defer close(donec)
  258. _, err := kv.Get(context.TODO(), "foo")
  259. if err != nil && err != rpctypes.ErrConnClosed {
  260. t.Fatalf("expected %v, got %v", rpctypes.ErrConnClosed, err)
  261. }
  262. }()
  263. if err := cli.Close(); err != nil {
  264. t.Fatal(err)
  265. }
  266. clus.TakeClient(0)
  267. select {
  268. case <-time.After(3 * time.Second):
  269. t.Fatal("kv.Get took too long")
  270. case <-donec:
  271. }
  272. }
  273. func TestKVNewAfterClose(t *testing.T) {
  274. defer testutil.AfterTest(t)
  275. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
  276. defer clus.Terminate(t)
  277. cli := clus.Client(0)
  278. clus.TakeClient(0)
  279. if err := cli.Close(); err != nil {
  280. t.Fatal(err)
  281. }
  282. donec := make(chan struct{})
  283. go func() {
  284. kv := clientv3.NewKV(cli)
  285. if _, err := kv.Get(context.TODO(), "foo"); err != rpctypes.ErrConnClosed {
  286. t.Fatalf("expected %v, got %v", rpctypes.ErrConnClosed, err)
  287. }
  288. close(donec)
  289. }()
  290. select {
  291. case <-time.After(3 * time.Second):
  292. t.Fatal("kv.Get took too long")
  293. case <-donec:
  294. }
  295. }
  296. func TestKVDeleteRange(t *testing.T) {
  297. defer testutil.AfterTest(t)
  298. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  299. defer clus.Terminate(t)
  300. kv := clientv3.NewKV(clus.RandClient())
  301. ctx := context.TODO()
  302. tests := []struct {
  303. key string
  304. opts []clientv3.OpOption
  305. wkeys []string
  306. }{
  307. // [a, c)
  308. {
  309. key: "a",
  310. opts: []clientv3.OpOption{clientv3.WithRange("c")},
  311. wkeys: []string{"c", "c/abc", "d"},
  312. },
  313. // >= c
  314. {
  315. key: "c",
  316. opts: []clientv3.OpOption{clientv3.WithFromKey()},
  317. wkeys: []string{"a", "b"},
  318. },
  319. // c*
  320. {
  321. key: "c",
  322. opts: []clientv3.OpOption{clientv3.WithPrefix()},
  323. wkeys: []string{"a", "b", "d"},
  324. },
  325. // *
  326. {
  327. key: "\x00",
  328. opts: []clientv3.OpOption{clientv3.WithFromKey()},
  329. wkeys: []string{},
  330. },
  331. }
  332. for i, tt := range tests {
  333. keySet := []string{"a", "b", "c", "c/abc", "d"}
  334. for j, key := range keySet {
  335. if _, err := kv.Put(ctx, key, ""); err != nil {
  336. t.Fatalf("#%d: couldn't put %q (%v)", j, key, err)
  337. }
  338. }
  339. _, err := kv.Delete(ctx, tt.key, tt.opts...)
  340. if err != nil {
  341. t.Fatalf("#%d: couldn't delete range (%v)", i, err)
  342. }
  343. resp, err := kv.Get(ctx, "a", clientv3.WithFromKey())
  344. if err != nil {
  345. t.Fatalf("#%d: couldn't get keys (%v)", i, err)
  346. }
  347. keys := []string{}
  348. for _, kv := range resp.Kvs {
  349. keys = append(keys, string(kv.Key))
  350. }
  351. if !reflect.DeepEqual(tt.wkeys, keys) {
  352. t.Errorf("#%d: resp.Kvs got %v, expected %v", i, keys, tt.wkeys)
  353. }
  354. }
  355. }
  356. func TestKVDelete(t *testing.T) {
  357. defer testutil.AfterTest(t)
  358. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  359. defer clus.Terminate(t)
  360. kv := clientv3.NewKV(clus.RandClient())
  361. ctx := context.TODO()
  362. presp, err := kv.Put(ctx, "foo", "")
  363. if err != nil {
  364. t.Fatalf("couldn't put 'foo' (%v)", err)
  365. }
  366. if presp.Header.Revision != 2 {
  367. t.Fatalf("presp.Header.Revision got %d, want %d", presp.Header.Revision, 2)
  368. }
  369. resp, err := kv.Delete(ctx, "foo")
  370. if err != nil {
  371. t.Fatalf("couldn't delete key (%v)", err)
  372. }
  373. if resp.Header.Revision != 3 {
  374. t.Fatalf("resp.Header.Revision got %d, want %d", resp.Header.Revision, 3)
  375. }
  376. gresp, err := kv.Get(ctx, "foo")
  377. if err != nil {
  378. t.Fatalf("couldn't get key (%v)", err)
  379. }
  380. if len(gresp.Kvs) > 0 {
  381. t.Fatalf("gresp.Kvs got %+v, want none", gresp.Kvs)
  382. }
  383. }
  384. func TestKVCompactError(t *testing.T) {
  385. defer testutil.AfterTest(t)
  386. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
  387. defer clus.Terminate(t)
  388. kv := clientv3.NewKV(clus.RandClient())
  389. ctx := context.TODO()
  390. for i := 0; i < 5; i++ {
  391. if _, err := kv.Put(ctx, "foo", "bar"); err != nil {
  392. t.Fatalf("couldn't put 'foo' (%v)", err)
  393. }
  394. }
  395. err := kv.Compact(ctx, 6)
  396. if err != nil {
  397. t.Fatalf("couldn't compact 6 (%v)", err)
  398. }
  399. err = kv.Compact(ctx, 6)
  400. if err != rpctypes.ErrCompacted {
  401. t.Fatalf("expected %v, got %v", rpctypes.ErrCompacted, err)
  402. }
  403. err = kv.Compact(ctx, 100)
  404. if err != rpctypes.ErrFutureRev {
  405. t.Fatalf("expected %v, got %v", rpctypes.ErrFutureRev, err)
  406. }
  407. }
  408. func TestKVCompact(t *testing.T) {
  409. defer testutil.AfterTest(t)
  410. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  411. defer clus.Terminate(t)
  412. kv := clientv3.NewKV(clus.RandClient())
  413. ctx := context.TODO()
  414. for i := 0; i < 10; i++ {
  415. if _, err := kv.Put(ctx, "foo", "bar"); err != nil {
  416. t.Fatalf("couldn't put 'foo' (%v)", err)
  417. }
  418. }
  419. err := kv.Compact(ctx, 7)
  420. if err != nil {
  421. t.Fatalf("couldn't compact kv space (%v)", err)
  422. }
  423. err = kv.Compact(ctx, 7)
  424. if err == nil || err != rpctypes.ErrCompacted {
  425. t.Fatalf("error got %v, want %v", err, rpctypes.ErrCompacted)
  426. }
  427. wcli := clus.RandClient()
  428. // new watcher could precede receiving the compaction without quorum first
  429. wcli.Get(ctx, "quorum-get")
  430. wc := clientv3.NewWatcher(wcli)
  431. defer wc.Close()
  432. wchan := wc.Watch(ctx, "foo", clientv3.WithRev(3))
  433. if wr := <-wchan; wr.CompactRevision != 7 {
  434. t.Fatalf("wchan CompactRevision got %v, want 7", wr.CompactRevision)
  435. }
  436. if wr, ok := <-wchan; ok {
  437. t.Fatalf("wchan got %v, expected closed", wr)
  438. }
  439. err = kv.Compact(ctx, 1000)
  440. if err == nil || err != rpctypes.ErrFutureRev {
  441. t.Fatalf("error got %v, want %v", err, rpctypes.ErrFutureRev)
  442. }
  443. }
  444. // TestKVGetRetry ensures get will retry on disconnect.
  445. func TestKVGetRetry(t *testing.T) {
  446. defer testutil.AfterTest(t)
  447. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  448. defer clus.Terminate(t)
  449. kv := clientv3.NewKV(clus.Client(0))
  450. ctx := context.TODO()
  451. if _, err := kv.Put(ctx, "foo", "bar"); err != nil {
  452. t.Fatal(err)
  453. }
  454. clus.Members[0].Stop(t)
  455. <-clus.Members[0].StopNotify()
  456. donec := make(chan struct{})
  457. go func() {
  458. // Get will fail, but reconnect will trigger
  459. gresp, gerr := kv.Get(ctx, "foo")
  460. if gerr != nil {
  461. t.Fatal(gerr)
  462. }
  463. wkvs := []*mvccpb.KeyValue{
  464. {
  465. Key: []byte("foo"),
  466. Value: []byte("bar"),
  467. CreateRevision: 2,
  468. ModRevision: 2,
  469. Version: 1,
  470. },
  471. }
  472. if !reflect.DeepEqual(gresp.Kvs, wkvs) {
  473. t.Fatalf("bad get: got %v, want %v", gresp.Kvs, wkvs)
  474. }
  475. donec <- struct{}{}
  476. }()
  477. time.Sleep(100 * time.Millisecond)
  478. clus.Members[0].Restart(t)
  479. select {
  480. case <-time.After(5 * time.Second):
  481. t.Fatalf("timed out waiting for get")
  482. case <-donec:
  483. }
  484. }
  485. // TestKVPutFailGetRetry ensures a get will retry following a failed put.
  486. func TestKVPutFailGetRetry(t *testing.T) {
  487. defer testutil.AfterTest(t)
  488. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  489. defer clus.Terminate(t)
  490. kv := clientv3.NewKV(clus.Client(0))
  491. ctx := context.TODO()
  492. clus.Members[0].Stop(t)
  493. <-clus.Members[0].StopNotify()
  494. _, err := kv.Put(ctx, "foo", "bar")
  495. if err == nil {
  496. t.Fatalf("got success on disconnected put, wanted error")
  497. }
  498. donec := make(chan struct{})
  499. go func() {
  500. // Get will fail, but reconnect will trigger
  501. gresp, gerr := kv.Get(ctx, "foo")
  502. if gerr != nil {
  503. t.Fatal(gerr)
  504. }
  505. if len(gresp.Kvs) != 0 {
  506. t.Fatalf("bad get kvs: got %+v, want empty", gresp.Kvs)
  507. }
  508. donec <- struct{}{}
  509. }()
  510. time.Sleep(100 * time.Millisecond)
  511. clus.Members[0].Restart(t)
  512. select {
  513. case <-time.After(5 * time.Second):
  514. t.Fatalf("timed out waiting for get")
  515. case <-donec:
  516. }
  517. }
  518. // TestKVGetCancel tests that a context cancel on a Get terminates as expected.
  519. func TestKVGetCancel(t *testing.T) {
  520. defer testutil.AfterTest(t)
  521. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
  522. defer clus.Terminate(t)
  523. oldconn := clus.Client(0).ActiveConnection()
  524. kv := clientv3.NewKV(clus.Client(0))
  525. ctx, cancel := context.WithCancel(context.TODO())
  526. cancel()
  527. resp, err := kv.Get(ctx, "abc")
  528. if err == nil {
  529. t.Fatalf("cancel on get response %v, expected context error", resp)
  530. }
  531. newconn := clus.Client(0).ActiveConnection()
  532. if oldconn != newconn {
  533. t.Fatalf("cancel on get broke client connection")
  534. }
  535. }
  536. // TestKVPutStoppedServerAndClose ensures closing after a failed Put works.
  537. func TestKVPutStoppedServerAndClose(t *testing.T) {
  538. defer testutil.AfterTest(t)
  539. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
  540. defer clus.Terminate(t)
  541. cli := clus.Client(0)
  542. clus.Members[0].Stop(t)
  543. // this Put fails and triggers an asynchronous connection retry
  544. _, err := cli.Put(context.TODO(), "abc", "123")
  545. if err == nil ||
  546. (!strings.Contains(err.Error(), "connection is closing") &&
  547. !strings.Contains(err.Error(), "transport is closing")) {
  548. t.Fatal(err)
  549. }
  550. // wait some so the client closes with the retry in-flight
  551. time.Sleep(time.Second)
  552. // get the timeout
  553. clus.TakeClient(0)
  554. if err := cli.Close(); err == nil || !strings.Contains(err.Error(), "timed out") {
  555. t.Fatal(err)
  556. }
  557. }