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

Scheduled post #848

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions loadtest/control/simulcontroller/scheduled_posts.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (c *SimulController) createScheduledPost(u user.User) control.UserActionRes
return control.UserActionResponse{Err: control.NewUserError(err)}
}

return control.UserActionResponse{Info: fmt.Sprintf("scheduled post created in channel id %v", channel.Id)}
return control.UserActionResponse{Info: fmt.Sprintf("scheduled post created in channel with id %s", channel.Id)}
}

func (c *SimulController) updateScheduledPost(u user.User) control.UserActionResponse {
Expand Down Expand Up @@ -98,7 +98,7 @@ func (c *SimulController) updateScheduledPost(u user.User) control.UserActionRes
return control.UserActionResponse{Err: control.NewUserError(err)}
}

return control.UserActionResponse{Info: fmt.Sprintf("scheduled post updated in channel id %v", channel.Id)}
return control.UserActionResponse{Info: fmt.Sprintf("scheduled post updated in channel with id %s", channel.Id)}
}

func (c *SimulController) deleteScheduledPost(u user.User) control.UserActionResponse {
Expand All @@ -116,11 +116,11 @@ func (c *SimulController) deleteScheduledPost(u user.User) control.UserActionRes
return control.UserActionResponse{Info: "no scheduled posts found"}
}

if err := u.DeleteScheduledPost(scheduledPost.Id); err != nil {
if err := u.DeleteScheduledPost(scheduledPost); err != nil {
return control.UserActionResponse{Err: control.NewUserError(err)}
}

return control.UserActionResponse{Info: fmt.Sprintf("scheduled post deleted with id %v", scheduledPost.Id)}
return control.UserActionResponse{Info: fmt.Sprintf("scheduled post with id %s deleted", scheduledPost.Id)}
}

func (c *SimulController) sendScheduledPostNow(u user.User) control.UserActionResponse {
Expand All @@ -147,9 +147,9 @@ func (c *SimulController) sendScheduledPostNow(u user.User) control.UserActionRe
return control.UserActionResponse{Err: control.NewUserError(err)}
}

if err := u.DeleteScheduledPost(scheduledPost.Id); err != nil {
if err := u.DeleteScheduledPost(scheduledPost); err != nil {
return control.UserActionResponse{Err: control.NewUserError(err)}
}

return control.UserActionResponse{Info: fmt.Sprintf("scheduled post with id %v manually sent now", scheduledPost.Id)}
return control.UserActionResponse{Info: fmt.Sprintf("scheduled post with id %s manually sent now", scheduledPost.Id)}
}
35 changes: 28 additions & 7 deletions loadtest/store/memstore/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type MemStore struct {
featureFlags map[string]bool
report *model.PerformanceReport
channelBookmarks map[string]*model.ChannelBookmarkWithFileInfo
scheduledPosts map[string]map[string][]*model.ScheduledPost
scheduledPosts map[string]map[string][]*model.ScheduledPost // map of team ID -> channel/thread ID -> list of scheduled posts
}

// New returns a new instance of MemStore with the given config.
Expand Down Expand Up @@ -1387,14 +1387,22 @@ func (s *MemStore) SetScheduledPost(teamId string, scheduledPost *model.Schedule
return nil
}

func (s *MemStore) DeleteScheduledPost(scheduledPostID string) {
func (s *MemStore) DeleteScheduledPost(scheduledPost *model.ScheduledPost) {
s.lock.Lock()
defer s.lock.Unlock()

for teamId := range s.scheduledPosts {
if _, ok := s.scheduledPosts[teamId][scheduledPostID]; ok {
delete(s.scheduledPosts[teamId], scheduledPostID)
break
channelOrThreadId := scheduledPost.ChannelId
if scheduledPost.RootId != "" {
channelOrThreadId = scheduledPost.RootId
}

// find index of scheduledPost in s.scheduledPosts[teamId][channelOrThreadId] and if found, delete it
for i, sp := range s.scheduledPosts[teamId][channelOrThreadId] {
if sp.Id == scheduledPost.Id {
s.scheduledPosts[teamId][channelOrThreadId] = append(s.scheduledPosts[teamId][channelOrThreadId][:i], s.scheduledPosts[teamId][channelOrThreadId][i+1:]...)
break
}
}
}
}
Expand All @@ -1403,9 +1411,22 @@ func (s *MemStore) UpdateScheduledPost(teamId string, scheduledPost *model.Sched
s.lock.Lock()
defer s.lock.Unlock()

channelOrThreadId := scheduledPost.ChannelId
if scheduledPost.RootId != "" {
channelOrThreadId = scheduledPost.RootId
}

if _, ok := s.scheduledPosts[teamId]; !ok {
s.scheduledPosts[teamId] = make(map[string][]*model.ScheduledPost)
s.scheduledPosts[teamId] = map[string][]*model.ScheduledPost{
channelOrThreadId: {scheduledPost},
}
return
}

s.scheduledPosts[teamId][scheduledPost.Id] = append(s.scheduledPosts[teamId][scheduledPost.Id], scheduledPost)
for i := range s.scheduledPosts[teamId][channelOrThreadId] {
if s.scheduledPosts[teamId][channelOrThreadId][i].Id == scheduledPost.Id {
s.scheduledPosts[teamId][channelOrThreadId][i] = scheduledPost
break
}
}
}
2 changes: 1 addition & 1 deletion loadtest/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ type UserStore interface {
DeleteChannelBookmark(bookmarkId string) error

GetRandomScheduledPost() (*model.ScheduledPost, error)
DeleteScheduledPost(scheduledPostID string)
DeleteScheduledPost(scheduledPost *model.ScheduledPost)
UpdateScheduledPost(teamId string, scheduledPost *model.ScheduledPost)
}

Expand Down
2 changes: 1 addition & 1 deletion loadtest/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,6 @@ type User interface {
// Scheduled Posts
CreateScheduledPost(teamId string, scheduledPost *model.ScheduledPost) error
UpdateScheduledPost(teamId string, scheduledPost *model.ScheduledPost) error
DeleteScheduledPost(scheduledPostId string) error
DeleteScheduledPost(scheduledPost *model.ScheduledPost) error
GetTeamScheduledPosts(teamID string) error
}
15 changes: 5 additions & 10 deletions loadtest/user/userentity/scheduled_posts.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,18 @@ func (ue *UserEntity) UpdateScheduledPost(teamId string, scheduledPost *model.Sc
return err
}

err = ue.store.SetScheduledPost(teamId, updatedScheduledPost)
if err != nil {
return err
}

ue.Store().UpdateScheduledPost(teamId, updatedScheduledPost)

return nil
}

func (ue *UserEntity) DeleteScheduledPost(scheduledPostId string) error {
_, _, err := ue.client.DeleteScheduledPost(context.Background(), scheduledPostId)
func (ue *UserEntity) DeleteScheduledPost(scheduledPost *model.ScheduledPost) error {
_, _, err := ue.client.DeleteScheduledPost(context.Background(), scheduledPost.Id)
if err != nil {
return err
}

ue.Store().DeleteScheduledPost(scheduledPostId)
ue.Store().DeleteScheduledPost(scheduledPost)
return nil
}

Expand All @@ -63,8 +58,8 @@ func (ue *UserEntity) GetTeamScheduledPosts(teamID string) error {
return err
}

for teamIdInResponse := range scheduledPostsByTeam {
for _, scheduledPost := range scheduledPostsByTeam[teamIdInResponse] {
for _, scheduledPostByTeamId := range scheduledPostsByTeam {
for _, scheduledPost := range scheduledPostByTeamId {
err := ue.store.SetScheduledPost(teamID, scheduledPost)
if err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions loadtest/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ func nextPowerOf2(val int) int {
return val
}

// RandomFutureTime returns a random Unix timestamp, in milliseconds, in the interval
// [now+deltaStart, now+maxUntil]
Copy link
Member

Choose a reason for hiding this comment

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

The comment is actually correct, thanks! But it surfaced a bug in this function: if maxUntil is smaller than deltaStart (let's say, you call RandomFutureTime(10*time.Second, 9*time.Second), then rand.Int63n panics because it gets a negative number). We should fix the function so that the interval is actually [now+deltaStart, now+deltaStart+maxUntil], and add unit testing for this function.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

func RandomFutureTime(deltaStart, maxUntil time.Duration) int64 {
now := time.Now()
diff := maxUntil - deltaStart
Expand Down
Loading