Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Add TypingCache to maintain a list of users typing #559

Merged
merged 9 commits into from
Jul 31, 2018

Conversation

APwhitehat
Copy link
Contributor

@APwhitehat APwhitehat commented Jul 24, 2018

ready for review.

Signed-off-by: Anant Prakash <anantprakashjsr@gmail.com>

}
}

func testGetTypingUsersAfterTimeout(t *testing.T, tCache *TypingCache) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test fails since the removeUserAfterDuration function will remove user1 after the first timeout occurs regardless of the fact that user1 is again added with a longer expiry.

https://github.com/APwhitehat/dendrite/blob/3e697873a65ccb25ea984b3ac66db39a5e2513bb/src/github.com/matrix-org/dendrite/typingserver/cache/cache.go#L75-L81

}

func testGetTypingUsersAfterTimeout(t *testing.T, tCache *TypingCache) {
time.Sleep(defaultInterval)
Copy link
Member

Choose a reason for hiding this comment

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

Please don't use Sleep in tests, they should complete quickly. Instead have the cache accept a "clock", this can then be mocked in the unit tests

}

t.data[roomID][userID] = userExists
t.Unlock()
Copy link
Member

Choose a reason for hiding this comment

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

I would heavily recommend using defer where possible.

t.Lock()
defer t.Unlock()
...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I got swept away by the argument that defer introduce an overhead. :P
But good programming practice.

@APwhitehat APwhitehat changed the title [WIP] Add TypingCache to maintain a list of users typing Add TypingCache to maintain a list of users typing Jul 25, 2018

for _, tt := range tests {
tCache.removeUserIfExpired(tt.userID, tt.roomID)
if gotUsers := tCache.GetTypingUsers(tt.roomID); !reflect.DeepEqual(gotUsers, tt.wantUsers) {
Copy link
Contributor

Choose a reason for hiding this comment

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

DeepEqual fails when the order of the entries does not match.
This makes this test flaky.

Copy link
Contributor

Choose a reason for hiding this comment

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

BTW there is this. You might reuse that somehow:

func compareUnsortedStringSlices(a []string, b []string) bool {
if len(a) != len(b) {
return false
}
sort.Strings(a)
sort.Strings(b)
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion. :)

@APwhitehat
Copy link
Contributor Author

New test from @krombel. Yay!

@APwhitehat
Copy link
Contributor Author

For some reason travis build is not visible. One can it here https://travis-ci.org/matrix-org/dendrite/builds/409862091

Copy link
Member

@erikjohnston erikjohnston left a comment

Choose a reason for hiding this comment

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

LGTM other than removing the rand

}
i := rand.Intn(len(tt.userIDs))
tCache.removeUser(tt.userIDs[i], tt.roomID)
expLeftUsers := append(tt.userIDs[:i], tt.userIDs[i+1:]...)
Copy link
Member

Choose a reason for hiding this comment

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

Please don't use random in tests, it can make them flakey and annoying to debug. I'd just pick one to remove, e.g. the last, as there's nothing that should depend on the ordering here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure.

@APwhitehat APwhitehat merged commit 9cdd3a6 into matrix-org:master Jul 31, 2018
@APwhitehat APwhitehat deleted the typingCache branch July 31, 2018 10:53
@APwhitehat APwhitehat added this to the Typing Server milestone Aug 4, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants