kv_test.go 17 KB

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