http_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 leasehttp
  15. import (
  16. "context"
  17. "net/http"
  18. "net/http/httptest"
  19. "os"
  20. "testing"
  21. "time"
  22. "go.etcd.io/etcd/lease"
  23. "go.etcd.io/etcd/mvcc/backend"
  24. "go.uber.org/zap"
  25. )
  26. func TestRenewHTTP(t *testing.T) {
  27. lg := zap.NewNop()
  28. be, tmpPath := backend.NewTmpBackend(time.Hour, 10000)
  29. defer os.Remove(tmpPath)
  30. defer be.Close()
  31. le := lease.NewLessor(lg, be, lease.LessorConfig{MinLeaseTTL: int64(5)})
  32. le.Promote(time.Second)
  33. l, err := le.Grant(1, int64(5))
  34. if err != nil {
  35. t.Fatalf("failed to create lease: %v", err)
  36. }
  37. ts := httptest.NewServer(NewHandler(le, waitReady))
  38. defer ts.Close()
  39. ttl, err := RenewHTTP(context.TODO(), l.ID, ts.URL+LeasePrefix, http.DefaultTransport)
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. if ttl != 5 {
  44. t.Fatalf("ttl expected 5, got %d", ttl)
  45. }
  46. }
  47. func TestTimeToLiveHTTP(t *testing.T) {
  48. lg := zap.NewNop()
  49. be, tmpPath := backend.NewTmpBackend(time.Hour, 10000)
  50. defer os.Remove(tmpPath)
  51. defer be.Close()
  52. le := lease.NewLessor(lg, be, lease.LessorConfig{MinLeaseTTL: int64(5)})
  53. le.Promote(time.Second)
  54. l, err := le.Grant(1, int64(5))
  55. if err != nil {
  56. t.Fatalf("failed to create lease: %v", err)
  57. }
  58. ts := httptest.NewServer(NewHandler(le, waitReady))
  59. defer ts.Close()
  60. resp, err := TimeToLiveHTTP(context.TODO(), l.ID, true, ts.URL+LeaseInternalPrefix, http.DefaultTransport)
  61. if err != nil {
  62. t.Fatal(err)
  63. }
  64. if resp.LeaseTimeToLiveResponse.ID != 1 {
  65. t.Fatalf("lease id expected 1, got %d", resp.LeaseTimeToLiveResponse.ID)
  66. }
  67. if resp.LeaseTimeToLiveResponse.GrantedTTL != 5 {
  68. t.Fatalf("granted TTL expected 5, got %d", resp.LeaseTimeToLiveResponse.GrantedTTL)
  69. }
  70. }
  71. func TestRenewHTTPTimeout(t *testing.T) {
  72. testApplyTimeout(t, func(l *lease.Lease, serverURL string) error {
  73. _, err := RenewHTTP(context.TODO(), l.ID, serverURL+LeasePrefix, http.DefaultTransport)
  74. return err
  75. })
  76. }
  77. func TestTimeToLiveHTTPTimeout(t *testing.T) {
  78. testApplyTimeout(t, func(l *lease.Lease, serverURL string) error {
  79. _, err := TimeToLiveHTTP(context.TODO(), l.ID, true, serverURL+LeaseInternalPrefix, http.DefaultTransport)
  80. return err
  81. })
  82. }
  83. func testApplyTimeout(t *testing.T, f func(*lease.Lease, string) error) {
  84. lg := zap.NewNop()
  85. be, tmpPath := backend.NewTmpBackend(time.Hour, 10000)
  86. defer os.Remove(tmpPath)
  87. defer be.Close()
  88. le := lease.NewLessor(lg, be, lease.LessorConfig{MinLeaseTTL: int64(5)})
  89. le.Promote(time.Second)
  90. l, err := le.Grant(1, int64(5))
  91. if err != nil {
  92. t.Fatalf("failed to create lease: %v", err)
  93. }
  94. ts := httptest.NewServer(NewHandler(le, waitNotReady))
  95. defer ts.Close()
  96. err = f(l, ts.URL)
  97. if err == nil {
  98. t.Fatalf("expected timeout error, got nil")
  99. }
  100. if err.Error() != ErrLeaseHTTPTimeout.Error() {
  101. t.Fatalf("expected (%v), got (%v)", ErrLeaseHTTPTimeout.Error(), err.Error())
  102. }
  103. }
  104. func waitReady() <-chan struct{} {
  105. ch := make(chan struct{})
  106. close(ch)
  107. return ch
  108. }
  109. func waitNotReady() <-chan struct{} {
  110. return nil
  111. }