http_test.go 3.3 KB

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