|
| 1 | +// Copyright (c) Tailscale Inc & contributors |
| 2 | +// SPDX-License-Identifier: BSD-3-Clause |
| 3 | + |
| 4 | +package ctxkey |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "regexp" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + "go.astrophena.name/base/testutil" |
| 14 | +) |
| 15 | + |
| 16 | +func TestKey(t *testing.T) { |
| 17 | + ctx := t.Context() |
| 18 | + |
| 19 | + // Test keys with the same name as being distinct. |
| 20 | + k1 := New("same.Name", "") |
| 21 | + testutil.AssertEqual(t, k1.String(), "same.Name") |
| 22 | + k2 := New("same.Name", "") |
| 23 | + testutil.AssertEqual(t, k2.String(), "same.Name") |
| 24 | + testutil.AssertEqual(t, k1 == k2, false) |
| 25 | + ctx = k1.WithValue(ctx, "hello") |
| 26 | + testutil.AssertEqual(t, k1.Has(ctx), true) |
| 27 | + testutil.AssertEqual(t, k1.Value(ctx), "hello") |
| 28 | + testutil.AssertEqual(t, k2.Has(ctx), false) |
| 29 | + testutil.AssertEqual(t, k2.Value(ctx), "") |
| 30 | + ctx = k2.WithValue(ctx, "goodbye") |
| 31 | + testutil.AssertEqual(t, k1.Has(ctx), true) |
| 32 | + testutil.AssertEqual(t, k1.Value(ctx), "hello") |
| 33 | + testutil.AssertEqual(t, k2.Has(ctx), true) |
| 34 | + testutil.AssertEqual(t, k2.Value(ctx), "goodbye") |
| 35 | + |
| 36 | + // Test default value. |
| 37 | + k3 := New("mapreduce.Timeout", time.Hour) |
| 38 | + testutil.AssertEqual(t, k3.Has(ctx), false) |
| 39 | + testutil.AssertEqual(t, k3.Value(ctx), time.Hour) |
| 40 | + ctx = k3.WithValue(ctx, time.Minute) |
| 41 | + testutil.AssertEqual(t, k3.Has(ctx), true) |
| 42 | + testutil.AssertEqual(t, k3.Value(ctx), time.Minute) |
| 43 | + |
| 44 | + // Test incomparable value. |
| 45 | + k4 := New("slice", []int(nil)) |
| 46 | + testutil.AssertEqual(t, k4.Has(ctx), false) |
| 47 | + testutil.AssertEqual(t, k4.Value(ctx), []int(nil)) |
| 48 | + ctx = k4.WithValue(ctx, []int{1, 2, 3}) |
| 49 | + testutil.AssertEqual(t, k4.Has(ctx), true) |
| 50 | + testutil.AssertEqual(t, k4.Value(ctx), []int{1, 2, 3}) |
| 51 | + |
| 52 | + // Accessors should be allocation free. |
| 53 | + testutil.AssertEqual(t, testing.AllocsPerRun(100, func() { |
| 54 | + k1.Value(ctx) |
| 55 | + k1.Has(ctx) |
| 56 | + k1.ValueOk(ctx) |
| 57 | + }), 0.0) |
| 58 | + |
| 59 | + // Test keys that are created without New. |
| 60 | + var k5 Key[string] |
| 61 | + testutil.AssertEqual(t, k5.String(), "string") |
| 62 | + testutil.AssertEqual(t, k1 == k5, false) // should be different from key created by New |
| 63 | + testutil.AssertEqual(t, k5.Has(ctx), false) |
| 64 | + ctx = k5.WithValue(ctx, "fizz") |
| 65 | + testutil.AssertEqual(t, k5.Value(ctx), "fizz") |
| 66 | + var k6 Key[string] |
| 67 | + testutil.AssertEqual(t, k6.String(), "string") |
| 68 | + testutil.AssertEqual(t, k5 == k6, true) |
| 69 | + testutil.AssertEqual(t, k6.Has(ctx), true) |
| 70 | + ctx = k6.WithValue(ctx, "fizz") |
| 71 | + |
| 72 | + // Test interface value types. |
| 73 | + var k7 Key[any] |
| 74 | + testutil.AssertEqual(t, k7.Has(ctx), false) |
| 75 | + ctx = k7.WithValue(ctx, "whatever") |
| 76 | + testutil.AssertEqual(t, k7.Value(ctx), "whatever") |
| 77 | + ctx = k7.WithValue(ctx, []int{1, 2, 3}) |
| 78 | + testutil.AssertEqual(t, k7.Value(ctx), []int{1, 2, 3}) |
| 79 | + ctx = k7.WithValue(ctx, nil) |
| 80 | + testutil.AssertEqual(t, k7.Has(ctx), true) |
| 81 | + testutil.AssertEqual(t, k7.Value(ctx), nil) |
| 82 | + k8 := New[error]("error", io.EOF) |
| 83 | + testutil.AssertEqual(t, k8.Has(ctx), false) |
| 84 | + testutil.AssertEqual(t, k8.Value(ctx), io.EOF) |
| 85 | + ctx = k8.WithValue(ctx, nil) |
| 86 | + testutil.AssertEqual(t, k8.Value(ctx), nil) |
| 87 | + testutil.AssertEqual(t, k8.Has(ctx), true) |
| 88 | + err := fmt.Errorf("read error: %w", io.ErrUnexpectedEOF) |
| 89 | + ctx = k8.WithValue(ctx, err) |
| 90 | + testutil.AssertEqual(t, k8.Value(ctx), err) |
| 91 | + testutil.AssertEqual(t, k8.Has(ctx), true) |
| 92 | +} |
| 93 | + |
| 94 | +func TestStringer(t *testing.T) { |
| 95 | + ctx := t.Context() |
| 96 | + assertMatches(t, fmt.Sprint(New("foo.Bar", "").WithValue(ctx, "baz")), regexp.MustCompile("foo.Bar.*baz")) |
| 97 | + assertMatches(t, fmt.Sprint(New("", []int{}).WithValue(ctx, []int{1, 2, 3})), regexp.MustCompile(fmt.Sprintf("%[1]T.*%[1]v", []int{1, 2, 3}))) |
| 98 | + assertMatches(t, fmt.Sprint(New("", 0).WithValue(ctx, 5)), regexp.MustCompile("int.*5")) |
| 99 | + assertMatches(t, fmt.Sprint(Key[time.Duration]{}.WithValue(ctx, time.Hour)), regexp.MustCompile(fmt.Sprintf("%[1]T.*%[1]v", time.Hour))) |
| 100 | +} |
| 101 | + |
| 102 | +func assertMatches(t *testing.T, got string, re *regexp.Regexp) { |
| 103 | + t.Helper() |
| 104 | + if !re.MatchString(got) { |
| 105 | + t.Fatalf("value does not match regexp:\ngot: %q\nregexp: %q", got, re) |
| 106 | + } |
| 107 | +} |
0 commit comments