/
reaction_store.go
489 lines (412 loc) · 12.4 KB
/
reaction_store.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package storetest
import (
"sync"
"testing"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/store"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestReactionStore(t *testing.T, ss store.Store) {
t.Run("ReactionSave", func(t *testing.T) { testReactionSave(t, ss) })
t.Run("ReactionDelete", func(t *testing.T) { testReactionDelete(t, ss) })
t.Run("ReactionGetForPost", func(t *testing.T) { testReactionGetForPost(t, ss) })
t.Run("ReactionDeleteAllWithEmojiName", func(t *testing.T) { testReactionDeleteAllWithEmojiName(t, ss) })
t.Run("PermanentDeleteBatch", func(t *testing.T) { testReactionStorePermanentDeleteBatch(t, ss) })
t.Run("ReactionBulkGetForPosts", func(t *testing.T) { testReactionBulkGetForPosts(t, ss) })
t.Run("ReactionDeadlock", func(t *testing.T) { testReactionDeadlock(t, ss) })
}
func testReactionSave(t *testing.T, ss store.Store) {
post, err := ss.Post().Save(&model.Post{
ChannelId: model.NewId(),
UserId: model.NewId(),
})
require.Nil(t, err)
firstUpdateAt := post.UpdateAt
reaction1 := &model.Reaction{
UserId: model.NewId(),
PostId: post.Id,
EmojiName: model.NewId(),
}
reaction, nErr := ss.Reaction().Save(reaction1)
require.Nil(t, nErr)
saved := reaction
assert.Equal(t, saved.UserId, reaction1.UserId, "should've saved reaction user_id and returned it")
assert.Equal(t, saved.PostId, reaction1.PostId, "should've saved reaction post_id and returned it")
assert.Equal(t, saved.EmojiName, reaction1.EmojiName, "should've saved reaction emoji_name and returned it")
var secondUpdateAt int64
postList, err := ss.Post().Get(reaction1.PostId, false)
require.Nil(t, err)
assert.True(t, postList.Posts[post.Id].HasReactions, "should've set HasReactions = true on post")
assert.NotEqual(t, postList.Posts[post.Id].UpdateAt, firstUpdateAt, "should've marked post as updated when HasReactions changed")
if postList.Posts[post.Id].HasReactions && postList.Posts[post.Id].UpdateAt != firstUpdateAt {
secondUpdateAt = postList.Posts[post.Id].UpdateAt
}
_, nErr = ss.Reaction().Save(reaction1)
assert.Nil(t, nErr, "should've allowed saving a duplicate reaction")
// different user
reaction2 := &model.Reaction{
UserId: model.NewId(),
PostId: reaction1.PostId,
EmojiName: reaction1.EmojiName,
}
_, nErr = ss.Reaction().Save(reaction2)
require.Nil(t, nErr)
postList, err = ss.Post().Get(reaction2.PostId, false)
require.Nil(t, err)
assert.NotEqual(t, postList.Posts[post.Id].UpdateAt, secondUpdateAt, "should've marked post as updated even if HasReactions doesn't change")
// different post
reaction3 := &model.Reaction{
UserId: reaction1.UserId,
PostId: model.NewId(),
EmojiName: reaction1.EmojiName,
}
_, nErr = ss.Reaction().Save(reaction3)
require.Nil(t, nErr)
// different emoji
reaction4 := &model.Reaction{
UserId: reaction1.UserId,
PostId: reaction1.PostId,
EmojiName: model.NewId(),
}
_, nErr = ss.Reaction().Save(reaction4)
require.Nil(t, nErr)
// invalid reaction
reaction5 := &model.Reaction{
UserId: reaction1.UserId,
PostId: reaction1.PostId,
}
_, nErr = ss.Reaction().Save(reaction5)
require.NotNil(t, nErr, "should've failed for invalid reaction")
}
func testReactionDelete(t *testing.T, ss store.Store) {
post, err := ss.Post().Save(&model.Post{
ChannelId: model.NewId(),
UserId: model.NewId(),
})
require.Nil(t, err)
reaction := &model.Reaction{
UserId: model.NewId(),
PostId: post.Id,
EmojiName: model.NewId(),
}
_, nErr := ss.Reaction().Save(reaction)
require.Nil(t, nErr)
result, err := ss.Post().Get(reaction.PostId, false)
require.Nil(t, err)
firstUpdateAt := result.Posts[post.Id].UpdateAt
_, nErr = ss.Reaction().Delete(reaction)
require.Nil(t, nErr)
reactions, rErr := ss.Reaction().GetForPost(post.Id, false)
require.Nil(t, rErr)
assert.Empty(t, reactions, "should've deleted reaction")
postList, err := ss.Post().Get(post.Id, false)
require.Nil(t, err)
assert.False(t, postList.Posts[post.Id].HasReactions, "should've set HasReactions = false on post")
assert.NotEqual(t, postList.Posts[post.Id].UpdateAt, firstUpdateAt, "should mark post as updated after deleting reactions")
}
func testReactionGetForPost(t *testing.T, ss store.Store) {
postId := model.NewId()
userId := model.NewId()
reactions := []*model.Reaction{
{
UserId: userId,
PostId: postId,
EmojiName: "smile",
},
{
UserId: model.NewId(),
PostId: postId,
EmojiName: "smile",
},
{
UserId: userId,
PostId: postId,
EmojiName: "sad",
},
{
UserId: userId,
PostId: model.NewId(),
EmojiName: "angry",
},
}
for _, reaction := range reactions {
_, err := ss.Reaction().Save(reaction)
require.Nil(t, err)
}
returned, err := ss.Reaction().GetForPost(postId, false)
require.Nil(t, err)
require.Len(t, returned, 3, "should've returned 3 reactions")
for _, reaction := range reactions {
found := false
for _, returnedReaction := range returned {
if returnedReaction.UserId == reaction.UserId && returnedReaction.PostId == reaction.PostId &&
returnedReaction.EmojiName == reaction.EmojiName {
found = true
break
}
}
if !found {
assert.NotEqual(t, reaction.PostId, postId, "should've returned reaction for post %v", reaction)
} else if found {
assert.Equal(t, reaction.PostId, postId, "shouldn't have returned reaction for another post")
}
}
// Should return cached item
returned, err = ss.Reaction().GetForPost(postId, true)
require.Nil(t, err)
require.Len(t, returned, 3, "should've returned 3 reactions")
for _, reaction := range reactions {
found := false
for _, returnedReaction := range returned {
if returnedReaction.UserId == reaction.UserId && returnedReaction.PostId == reaction.PostId &&
returnedReaction.EmojiName == reaction.EmojiName {
found = true
break
}
}
if !found {
assert.NotEqual(t, reaction.PostId, postId, "should've returned reaction for post %v", reaction)
} else if found {
assert.Equal(t, reaction.PostId, postId, "shouldn't have returned reaction for another post")
}
}
}
func testReactionDeleteAllWithEmojiName(t *testing.T, ss store.Store) {
emojiToDelete := model.NewId()
post, err1 := ss.Post().Save(&model.Post{
ChannelId: model.NewId(),
UserId: model.NewId(),
})
require.Nil(t, err1)
post2, err2 := ss.Post().Save(&model.Post{
ChannelId: model.NewId(),
UserId: model.NewId(),
})
require.Nil(t, err2)
post3, err3 := ss.Post().Save(&model.Post{
ChannelId: model.NewId(),
UserId: model.NewId(),
})
require.Nil(t, err3)
userId := model.NewId()
reactions := []*model.Reaction{
{
UserId: userId,
PostId: post.Id,
EmojiName: emojiToDelete,
},
{
UserId: model.NewId(),
PostId: post.Id,
EmojiName: emojiToDelete,
},
{
UserId: userId,
PostId: post.Id,
EmojiName: "sad",
},
{
UserId: userId,
PostId: post2.Id,
EmojiName: "angry",
},
{
UserId: userId,
PostId: post3.Id,
EmojiName: emojiToDelete,
},
}
for _, reaction := range reactions {
_, err := ss.Reaction().Save(reaction)
require.Nil(t, err)
}
err := ss.Reaction().DeleteAllWithEmojiName(emojiToDelete)
require.Nil(t, err)
// check that the reactions were deleted
returned, err := ss.Reaction().GetForPost(post.Id, false)
require.Nil(t, err)
require.Len(t, returned, 1, "should've only removed reactions with emoji name")
for _, reaction := range returned {
assert.NotEqual(t, reaction.EmojiName, "smile", "should've removed reaction with emoji name")
}
returned, err = ss.Reaction().GetForPost(post2.Id, false)
require.Nil(t, err)
assert.Len(t, returned, 1, "should've only removed reactions with emoji name")
returned, err = ss.Reaction().GetForPost(post3.Id, false)
require.Nil(t, err)
assert.Empty(t, returned, "should've only removed reactions with emoji name")
// check that the posts are updated
postList, err := ss.Post().Get(post.Id, false)
require.Nil(t, err)
assert.True(t, postList.Posts[post.Id].HasReactions, "post should still have reactions")
postList, err = ss.Post().Get(post2.Id, false)
require.Nil(t, err)
assert.True(t, postList.Posts[post2.Id].HasReactions, "post should still have reactions")
postList, err = ss.Post().Get(post3.Id, false)
require.Nil(t, err)
assert.False(t, postList.Posts[post3.Id].HasReactions, "post shouldn't have reactions any more")
}
func testReactionStorePermanentDeleteBatch(t *testing.T, ss store.Store) {
post, err1 := ss.Post().Save(&model.Post{
ChannelId: model.NewId(),
UserId: model.NewId(),
})
require.Nil(t, err1)
reactions := []*model.Reaction{
{
UserId: model.NewId(),
PostId: post.Id,
EmojiName: "sad",
CreateAt: 1000,
},
{
UserId: model.NewId(),
PostId: post.Id,
EmojiName: "sad",
CreateAt: 1500,
},
{
UserId: model.NewId(),
PostId: post.Id,
EmojiName: "sad",
CreateAt: 2000,
},
{
UserId: model.NewId(),
PostId: post.Id,
EmojiName: "sad",
CreateAt: 2000,
},
}
// Need to hang on to a reaction to delete later in order to clear the cache, as "allowFromCache" isn't honoured any more.
var lastReaction *model.Reaction
for _, reaction := range reactions {
var nErr error
lastReaction, nErr = ss.Reaction().Save(reaction)
require.Nil(t, nErr)
}
returned, err := ss.Reaction().GetForPost(post.Id, false)
require.Nil(t, err)
require.Len(t, returned, 4, "expected 4 reactions")
_, err = ss.Reaction().PermanentDeleteBatch(1800, 1000)
require.Nil(t, err)
// This is to force a clear of the cache.
_, err = ss.Reaction().Delete(lastReaction)
require.Nil(t, err)
returned, err = ss.Reaction().GetForPost(post.Id, false)
require.Nil(t, err)
require.Len(t, returned, 1, "expected 1 reaction. Got: %v", len(returned))
}
func testReactionBulkGetForPosts(t *testing.T, ss store.Store) {
postId := model.NewId()
post2Id := model.NewId()
post3Id := model.NewId()
post4Id := model.NewId()
userId := model.NewId()
reactions := []*model.Reaction{
{
UserId: userId,
PostId: postId,
EmojiName: "smile",
},
{
UserId: model.NewId(),
PostId: post2Id,
EmojiName: "smile",
},
{
UserId: userId,
PostId: post3Id,
EmojiName: "sad",
},
{
UserId: userId,
PostId: postId,
EmojiName: "angry",
},
{
UserId: userId,
PostId: post2Id,
EmojiName: "angry",
},
{
UserId: userId,
PostId: post4Id,
EmojiName: "angry",
},
}
for _, reaction := range reactions {
_, err := ss.Reaction().Save(reaction)
require.Nil(t, err)
}
postIds := []string{postId, post2Id, post3Id}
returned, err := ss.Reaction().BulkGetForPosts(postIds)
require.Nil(t, err)
require.Len(t, returned, 5, "should've returned 5 reactions")
post4IdFound := false
for _, reaction := range returned {
if reaction.PostId == post4Id {
post4IdFound = true
break
}
}
require.False(t, post4IdFound, "Wrong reaction returned")
}
// testReactionDeadlock is a best-case attempt to recreate the deadlock scenario.
// It at least deadlocks 2 times out of 5.
func testReactionDeadlock(t *testing.T, ss store.Store) {
post, err := ss.Post().Save(&model.Post{
ChannelId: model.NewId(),
UserId: model.NewId(),
})
require.Nil(t, err)
reaction1 := &model.Reaction{
UserId: model.NewId(),
PostId: post.Id,
EmojiName: model.NewId(),
}
_, nErr := ss.Reaction().Save(reaction1)
require.Nil(t, nErr)
// different user
reaction2 := &model.Reaction{
UserId: model.NewId(),
PostId: reaction1.PostId,
EmojiName: reaction1.EmojiName,
}
_, nErr = ss.Reaction().Save(reaction2)
require.Nil(t, nErr)
// different post
reaction3 := &model.Reaction{
UserId: reaction1.UserId,
PostId: model.NewId(),
EmojiName: reaction1.EmojiName,
}
_, nErr = ss.Reaction().Save(reaction3)
require.Nil(t, nErr)
// different emoji
reaction4 := &model.Reaction{
UserId: reaction1.UserId,
PostId: reaction1.PostId,
EmojiName: model.NewId(),
}
_, nErr = ss.Reaction().Save(reaction4)
require.Nil(t, nErr)
var wg sync.WaitGroup
wg.Add(2)
// 1st tx
go func() {
defer wg.Done()
err := ss.Reaction().DeleteAllWithEmojiName(reaction1.EmojiName)
require.Nil(t, err)
}()
// 2nd tx
go func() {
defer wg.Done()
_, err := ss.Reaction().Delete(reaction2)
require.Nil(t, err)
}()
wg.Wait()
}