[MM-21378] Add mutex to model.Post to guard against race conditions on Post.Props (#13884)

* Add mutex to model.Post to guard against race conditions on Post.Props

* Rename mutex

* Add GetProp() method to Post

* Fix more tests

* Fix flaky test

Benchmarks:

BenchmarkPostPropsGet_indirect
BenchmarkPostPropsGet_indirect-2     	85026746	        13.0 ns/op	       0 B/op	       0 allocs/op
BenchmarkPostPropsGet_indirect-4     	90273747	        13.0 ns/op	       0 B/op	       0 allocs/op
BenchmarkPostPropsGet_indirect-8     	88324293	        13.0 ns/op	       0 B/op	       0 allocs/op
BenchmarkPostPropsGet_indirect-16    	91427720	        13.1 ns/op	       0 B/op	       0 allocs/op
BenchmarkPostPropsGet_direct
BenchmarkPostPropsGet_direct-2       	1000000000	         0.242 ns/op	       0 B/op	       0 allocs/op
BenchmarkPostPropsGet_direct-4       	1000000000	         0.241 ns/op	       0 B/op	       0 allocs/op
BenchmarkPostPropsGet_direct-8       	1000000000	         0.240 ns/op	       0 B/op	       0 allocs/op
BenchmarkPostPropsGet_direct-16      	1000000000	         0.241 ns/op	       0 B/op	       0 allocs/op
BenchmarkPostPropsAdd_indirect
BenchmarkPostPropsAdd_indirect-2     	 5602224	       203 ns/op	     336 B/op	       2 allocs/op
BenchmarkPostPropsAdd_indirect-4     	 5959496	       206 ns/op	     336 B/op	       2 allocs/op
BenchmarkPostPropsAdd_indirect-8     	 5833999	       205 ns/op	     336 B/op	       2 allocs/op
BenchmarkPostPropsAdd_indirect-16    	 5802493	       225 ns/op	     336 B/op	       2 allocs/op
BenchmarkPostPropsAdd_direct
BenchmarkPostPropsAdd_direct-2       	100000000	        11.3 ns/op	       0 B/op	       0 allocs/op
BenchmarkPostPropsAdd_direct-4       	100000000	        11.3 ns/op	       0 B/op	       0 allocs/op
BenchmarkPostPropsAdd_direct-8       	100000000	        11.6 ns/op	       0 B/op	       0 allocs/op
BenchmarkPostPropsAdd_direct-16      	99840794	        11.4 ns/op	       0 B/op	       0 allocs/op
BenchmarkPostPropsDel_indirect
BenchmarkPostPropsDel_indirect-2     	18824002	        61.9 ns/op	      48 B/op	       1 allocs/op
BenchmarkPostPropsDel_indirect-4     	19470736	        63.8 ns/op	      48 B/op	       1 allocs/op
BenchmarkPostPropsDel_indirect-8     	17640460	        65.3 ns/op	      48 B/op	       1 allocs/op
BenchmarkPostPropsDel_indirect-16    	18692962	        65.4 ns/op	      48 B/op	       1 allocs/op
BenchmarkPostPropsDel_direct
BenchmarkPostPropsDel_direct-2       	516257440	         2.34 ns/op	       0 B/op	       0 allocs/op
BenchmarkPostPropsDel_direct-4       	514865216	         2.43 ns/op	       0 B/op	       0 allocs/op
BenchmarkPostPropsDel_direct-8       	511330477	         2.37 ns/op	       0 B/op	       0 allocs/op
BenchmarkPostPropsDel_direct-16      	499504010	         2.38 ns/op	       0 B/op	       0 allocs/op
Этот коммит содержится в:
Claudio Costa
2020-03-13 21:12:20 +01:00
коммит произвёл GitHub
родитель 9e580361c2
Коммит 1e53fe85ad
28 изменённых файлов: 430 добавлений и 195 удалений

Просмотреть файл

@@ -256,13 +256,13 @@ func TestUpdatePostEditAt(t *testing.T) {
defer th.TearDown()
post := &model.Post{}
*post = *th.BasicPost
post = th.BasicPost.Clone()
post.IsPinned = true
saved, err := th.App.UpdatePost(post, true)
require.Nil(t, err)
assert.Equal(t, saved.EditAt, post.EditAt, "shouldn't have updated post.EditAt when pinning post")
*post = *saved
post = saved.Clone()
time.Sleep(time.Millisecond * 100)
@@ -279,7 +279,7 @@ func TestUpdatePostTimeLimit(t *testing.T) {
defer th.TearDown()
post := &model.Post{}
*post = *th.BasicPost
post = th.BasicPost.Clone()
th.App.SetLicense(model.NewTestLicense())
@@ -433,7 +433,7 @@ func TestPostChannelMentions(t *testing.T) {
"mention-test": map[string]interface{}{
"display_name": "Mention Test",
},
}, result.Props["channel_mentions"])
}, result.GetProp("channel_mentions"))
post.Message = fmt.Sprintf("goodbye, ~%v!", channelToMention.Name)
result, err = th.App.UpdatePost(post, false)
@@ -442,7 +442,7 @@ func TestPostChannelMentions(t *testing.T) {
"mention-test": map[string]interface{}{
"display_name": "Mention Test",
},
}, result.Props["channel_mentions"])
}, result.GetProp("channel_mentions"))
}
func TestImageProxy(t *testing.T) {
@@ -694,7 +694,7 @@ func TestCreatePost(t *testing.T) {
}
rpost, err := th.App.CreatePost(postWithNoMention, th.BasicChannel, false)
require.Nil(t, err)
assert.Equal(t, rpost.Props, model.StringInterface{})
assert.Equal(t, rpost.GetProps(), model.StringInterface{})
postWithMention := &model.Post{
ChannelId: th.BasicChannel.Id,
@@ -703,7 +703,7 @@ func TestCreatePost(t *testing.T) {
}
rpost, err = th.App.CreatePost(postWithMention, th.BasicChannel, false)
require.Nil(t, err)
assert.Equal(t, rpost.Props, model.StringInterface{})
assert.Equal(t, rpost.GetProps(), model.StringInterface{})
})
t.Run("Sets prop when post has mentions and user does not have USE_CHANNEL_MENTIONS", func(t *testing.T) {
@@ -716,7 +716,7 @@ func TestCreatePost(t *testing.T) {
}
rpost, err := th.App.CreatePost(postWithNoMention, th.BasicChannel, false)
require.Nil(t, err)
assert.Equal(t, rpost.Props, model.StringInterface{})
assert.Equal(t, rpost.GetProps(), model.StringInterface{})
postWithMention := &model.Post{
ChannelId: th.BasicChannel.Id,
@@ -725,7 +725,7 @@ func TestCreatePost(t *testing.T) {
}
rpost, err = th.App.CreatePost(postWithMention, th.BasicChannel, false)
require.Nil(t, err)
assert.Equal(t, rpost.Props[model.POST_PROPS_MENTION_HIGHLIGHT_DISABLED], true)
assert.Equal(t, rpost.GetProp(model.POST_PROPS_MENTION_HIGHLIGHT_DISABLED), true)
th.AddPermissionToRole(model.PERMISSION_USE_CHANNEL_MENTIONS.Id, model.CHANNEL_USER_ROLE_ID)
})
@@ -787,13 +787,13 @@ func TestPatchPost(t *testing.T) {
rpost, err = th.App.PatchPost(rpost.Id, patchWithNoMention)
require.Nil(t, err)
assert.Equal(t, rpost.Props, model.StringInterface{})
assert.Equal(t, rpost.GetProps(), model.StringInterface{})
patchWithMention := &model.PostPatch{Message: model.NewString("This patch has a mention now @here")}
rpost, err = th.App.PatchPost(rpost.Id, patchWithMention)
require.Nil(t, err)
assert.Equal(t, rpost.Props, model.StringInterface{})
assert.Equal(t, rpost.GetProps(), model.StringInterface{})
})
t.Run("Sets prop when user does not have USE_CHANNEL_MENTIONS", func(t *testing.T) {
@@ -802,13 +802,13 @@ func TestPatchPost(t *testing.T) {
patchWithNoMention := &model.PostPatch{Message: model.NewString("This patch still does not have a mention")}
rpost, err = th.App.PatchPost(rpost.Id, patchWithNoMention)
require.Nil(t, err)
assert.Equal(t, rpost.Props, model.StringInterface{})
assert.Equal(t, rpost.GetProps(), model.StringInterface{})
patchWithMention := &model.PostPatch{Message: model.NewString("This patch has a mention now @here")}
rpost, err = th.App.PatchPost(rpost.Id, patchWithMention)
require.Nil(t, err)
assert.Equal(t, rpost.Props[model.POST_PROPS_MENTION_HIGHLIGHT_DISABLED], true)
assert.Equal(t, rpost.GetProp(model.POST_PROPS_MENTION_HIGHLIGHT_DISABLED), true)
th.AddPermissionToRole(model.PERMISSION_USE_CHANNEL_MENTIONS.Id, model.CHANNEL_USER_ROLE_ID)
})