http_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. "testing"
  20. "time"
  21. "github.com/coreos/etcd/lease"
  22. "github.com/coreos/etcd/mvcc/backend"
  23. "golang.org/x/net/context"
  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, 3)
  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))
  36. defer ts.Close()
  37. ttl, err := RenewHTTP(context.TODO(), l.ID, ts.URL+"/leases", 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. }