k8s.io/apiserver@v0.31.1/pkg/endpoints/request/received_time_test.go (about) 1 /* 2 Copyright 2020 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package request 18 19 import ( 20 "context" 21 "strconv" 22 "testing" 23 "time" 24 ) 25 26 func TestWithRequestReceiveTime(t *testing.T) { 27 tests := []struct { 28 name string 29 receivedTimestamp time.Time 30 expected bool 31 }{ 32 { 33 name: "request received time is set", 34 receivedTimestamp: time.Now(), 35 expected: true, 36 }, 37 { 38 name: "request received time is empty", 39 receivedTimestamp: time.Time{}, 40 expected: false, 41 }, 42 } 43 44 for _, test := range tests { 45 t.Run(test.name, func(t *testing.T) { 46 parent := context.TODO() 47 ctx := WithReceivedTimestamp(parent, test.receivedTimestamp) 48 if ctx == nil { 49 t.Fatal("WithReceivedTimestamp: expected a non nil context, got nil") 50 } 51 52 receivedTimestampGot, ok := ReceivedTimestampFrom(ctx) 53 if test.expected != ok { 54 t.Errorf("ReceivedTimestampFrom: expected=%s got=%s", strconv.FormatBool(test.expected), strconv.FormatBool(ok)) 55 } 56 57 if test.receivedTimestamp != receivedTimestampGot { 58 t.Errorf("ReceivedTimestampFrom: received timestamp expected=%s but got=%s", test.receivedTimestamp, receivedTimestampGot) 59 } 60 }) 61 } 62 }