Skip to content

Commit ea381f0

Browse files
committed
chore: update tests
1 parent e92a599 commit ea381f0

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

pkg/golinters/fatcontext/testdata/fatcontext.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,44 @@ func example() {
3131
func wrapContext(ctx context.Context) context.Context {
3232
return context.WithoutCancel(ctx)
3333
}
34+
35+
// storing contexts in a struct isn't recommended, but local copies of a non-pointer struct should act like local copies of a context.
36+
func inStructs(ctx context.Context) {
37+
for i := 0; i < 10; i++ {
38+
c := struct{ Ctx context.Context }{ctx}
39+
c.Ctx = context.WithValue(c.Ctx, "key", i)
40+
c.Ctx = context.WithValue(c.Ctx, "other", "val")
41+
}
42+
43+
for i := 0; i < 10; i++ {
44+
c := []struct{ Ctx context.Context }{{ctx}}
45+
c[0].Ctx = context.WithValue(c[0].Ctx, "key", i)
46+
c[0].Ctx = context.WithValue(c[0].Ctx, "other", "val")
47+
}
48+
49+
c := struct{ Ctx context.Context }{ctx}
50+
for i := 0; i < 10; i++ {
51+
c := c
52+
c.Ctx = context.WithValue(c.Ctx, "key", i)
53+
c.Ctx = context.WithValue(c.Ctx, "other", "val")
54+
}
55+
56+
pc := &struct{ Ctx context.Context }{ctx}
57+
for i := 0; i < 10; i++ {
58+
c := pc
59+
c.Ctx = context.WithValue(c.Ctx, "key", i) // want "nested context in loop"
60+
c.Ctx = context.WithValue(c.Ctx, "other", "val")
61+
}
62+
63+
r := []struct{ Ctx context.Context }{{ctx}}
64+
for i := 0; i < 10; i++ {
65+
r[0].Ctx = context.WithValue(r[0].Ctx, "key", i) // want "nested context in loop"
66+
r[0].Ctx = context.WithValue(r[0].Ctx, "other", "val")
67+
}
68+
69+
rp := []*struct{ Ctx context.Context }{{ctx}}
70+
for i := 0; i < 10; i++ {
71+
rp[0].Ctx = context.WithValue(rp[0].Ctx, "key", i) // want "nested context in loop"
72+
rp[0].Ctx = context.WithValue(rp[0].Ctx, "other", "val")
73+
}
74+
}

0 commit comments

Comments
 (0)