v3lock_grpc_test.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2017 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. "context"
  17. "testing"
  18. "time"
  19. lockpb "go.etcd.io/etcd/etcdserver/api/v3lock/v3lockpb"
  20. pb "go.etcd.io/etcd/etcdserver/etcdserverpb"
  21. "go.etcd.io/etcd/pkg/testutil"
  22. )
  23. // TestV3LockLockWaiter tests that a client will wait for a lock, then acquire it
  24. // once it is unlocked.
  25. func TestV3LockLockWaiter(t *testing.T) {
  26. defer testutil.AfterTest(t)
  27. clus := NewClusterV3(t, &ClusterConfig{Size: 1})
  28. defer clus.Terminate(t)
  29. lease1, err1 := toGRPC(clus.RandClient()).Lease.LeaseGrant(context.TODO(), &pb.LeaseGrantRequest{TTL: 30})
  30. if err1 != nil {
  31. t.Fatal(err1)
  32. }
  33. lease2, err2 := toGRPC(clus.RandClient()).Lease.LeaseGrant(context.TODO(), &pb.LeaseGrantRequest{TTL: 30})
  34. if err2 != nil {
  35. t.Fatal(err2)
  36. }
  37. lc := toGRPC(clus.Client(0)).Lock
  38. l1, lerr1 := lc.Lock(context.TODO(), &lockpb.LockRequest{Name: []byte("foo"), Lease: lease1.ID})
  39. if lerr1 != nil {
  40. t.Fatal(lerr1)
  41. }
  42. lockc := make(chan struct{})
  43. go func() {
  44. l2, lerr2 := lc.Lock(context.TODO(), &lockpb.LockRequest{Name: []byte("foo"), Lease: lease2.ID})
  45. if lerr2 != nil {
  46. t.Error(lerr2)
  47. }
  48. if l1.Header.Revision >= l2.Header.Revision {
  49. t.Errorf("expected l1 revision < l2 revision, got %d >= %d", l1.Header.Revision, l2.Header.Revision)
  50. }
  51. close(lockc)
  52. }()
  53. select {
  54. case <-time.After(200 * time.Millisecond):
  55. case <-lockc:
  56. t.Fatalf("locked before unlock")
  57. }
  58. if _, uerr := lc.Unlock(context.TODO(), &lockpb.UnlockRequest{Key: l1.Key}); uerr != nil {
  59. t.Fatal(uerr)
  60. }
  61. select {
  62. case <-time.After(200 * time.Millisecond):
  63. t.Fatalf("waiter did not lock after unlock")
  64. case <-lockc:
  65. }
  66. }