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