-
Notifications
You must be signed in to change notification settings - Fork 71
Fix data race #307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix data race #307
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -87,6 +87,48 @@ func TestFindField(t *testing.T) { | |
| } | ||
| } | ||
|
|
||
| func testMap() *Map { | ||
| return &Map{ | ||
| Fields: []StructField{ | ||
| { | ||
| Name: "a", | ||
| Type: TypeRef{NamedType: strptr("aaa")}, | ||
| }, { | ||
| Name: "b", | ||
| Type: TypeRef{NamedType: strptr("bbb")}, | ||
| }, { | ||
| Name: "c", | ||
| Type: TypeRef{NamedType: strptr("ccc")}, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func BenchmarkFindFieldCached(b *testing.B) { | ||
| m := testMap() | ||
| m.FindField("a") | ||
| for i := 0; i < b.N; i++ { | ||
| m.FindField("a") | ||
| } | ||
| } | ||
|
|
||
| func BenchmarkFindFieldNew(b *testing.B) { | ||
| for i := 0; i < b.N; i++ { | ||
| m := testMap() | ||
| m.FindField("a") | ||
| } | ||
| } | ||
|
|
||
| // As a baseline of BenchmarkFindFieldNew | ||
| func BenchmarkMakeMap(b *testing.B) { | ||
| var m *Map | ||
| for i := 0; i < b.N; i++ { | ||
| m = testMap() | ||
| } | ||
| b.StopTimer() | ||
| b.Log(m) // prevent dead code elimination | ||
| } | ||
|
|
||
| func TestResolve(t *testing.T) { | ||
| existing := "existing" | ||
| notExisting := "not-existing" | ||
|
|
@@ -177,3 +219,27 @@ func TestCopyInto(t *testing.T) { | |
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestMapCopyInto(t *testing.T) { | ||
| s := Map{ | ||
| Fields: []StructField{ | ||
| { | ||
| Name: "a", | ||
| Type: TypeRef{NamedType: strptr("aaa")}, | ||
| }, | ||
| }, | ||
| } | ||
| theCopy := Map{} | ||
|
|
||
| assert := func(sf StructField, ok bool) { | ||
| if !ok || *sf.Type.NamedType != "aaa" { | ||
| t.Error("expected NamedType aaa not found") | ||
| } | ||
| } | ||
|
|
||
| go func() { | ||
| s.CopyInto(&theCopy) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CopyInto documents the function is not thread safe ... do we really call it in multi-threaded contexts normally?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Didn't see that. Will also remove the documents in this PR
I think yes. From the original logs of integration test: APIServer will call |
||
| assert(theCopy.FindField("a")) | ||
| }() | ||
| assert(s.FindField("a")) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still don't think this is thread-safe with respect to
dstThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, if you mean it is not safe to read dst when we are copying, then yes. Updated doc to clarify this.