Skip to content
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 resusing process of scanning context #322

Merged
merged 1 commit into from Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 49 additions & 0 deletions decode_test.go
Expand Up @@ -2732,3 +2732,52 @@ bar:
}
})
}

func TestUnmarshalMapSliceParallel(t *testing.T) {
content := `
steps:
req0:
desc: Get /users/1
req:
/users/1:
get: nil
test: |
current.res.status == 200
req1:
desc: Get /private
req:
/private:
get: nil
test: |
current.res.status == 403
req2:
desc: Get /users
req:
/users:
get: nil
test: |
current.res.status == 200
`
type mappedSteps struct {
Steps yaml.MapSlice `yaml:"steps,omitempty"`
}
for i := 0; i < 100; i++ {
t.Run(fmt.Sprintf("i=%d", i), func(t *testing.T) {
t.Parallel()
for i := 0; i < 10; i++ {
m := mappedSteps{
Steps: yaml.MapSlice{},
}
if err := yaml.Unmarshal([]byte(content), &m); err != nil {
t.Fatal(err)
}
for _, s := range m.Steps {
_, ok := s.Value.(map[string]interface{})
if !ok {
t.Fatal("unexpected error")
}
}
}
})
}
}
4 changes: 4 additions & 0 deletions scanner/context.go
Expand Up @@ -55,7 +55,11 @@ func (c *Context) reset(src []rune) {
c.src = src
c.tokens = c.tokens[:0]
c.resetBuffer()
c.isRawFolded = false
c.isSingleLine = true
c.isLiteral = false
c.isFolded = false
c.literalOpt = ""
Comment on lines +58 to +62

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Individually zeroing fields keeps open the potential for this class of bug in the future if new fields are added to *Context. This is why the original proposal was to use a single assignment to *c only retaining the non-zero valued fields.

}

func (c *Context) resetBuffer() {
Expand Down