| 1 |
/* |
| 2 |
Copyright 2022 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 factory |
| 18 |
|
| 19 |
import ( |
| 20 |
"errors" |
| 21 |
"fmt" |
| 22 |
"testing" |
| 23 |
"time" |
| 24 |
) |
| 25 |
|
| 26 |
func Test_atomicLastError(t *testing.T) { |
| 27 |
aError := &atomicLastError{err: fmt.Errorf("initial error")} |
| 28 |
// no timestamp is always updated |
| 29 |
aError.Store(errors.New("updated error"), time.Time{}) |
| 30 |
err := aError.Load() |
| 31 |
if err.Error() != "updated error" { |
| 32 |
t.Fatalf("Expected: \"updated error\" got: %s", err.Error()) |
| 33 |
} |
| 34 |
// update to current time |
| 35 |
now := time.Now() |
| 36 |
aError.Store(errors.New("now error"), now) |
| 37 |
err = aError.Load() |
| 38 |
if err.Error() != "now error" { |
| 39 |
t.Fatalf("Expected: \"now error\" got: %s", err.Error()) |
| 40 |
} |
| 41 |
// no update to past time |
| 42 |
past := now.Add(-5 * time.Second) |
| 43 |
aError.Store(errors.New("past error"), past) |
| 44 |
err = aError.Load() |
| 45 |
if err.Error() != "now error" { |
| 46 |
t.Fatalf("Expected: \"now error\" got: %s", err.Error()) |
| 47 |
} |
| 48 |
} |