http_test.go 3.3 KB

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