-
-
Notifications
You must be signed in to change notification settings - Fork 678
Add TypingCache to maintain a list of users typing #559
Conversation
} | ||
} | ||
|
||
func testGetTypingUsersAfterTimeout(t *testing.T, tCache *TypingCache) { |
There was a problem hiding this comment.
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.
} | ||
|
||
func testGetTypingUsersAfterTimeout(t *testing.T, tCache *TypingCache) { | ||
time.Sleep(defaultInterval) |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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()
...
There was a problem hiding this comment.
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.
|
||
for _, tt := range tests { | ||
tCache.removeUserIfExpired(tt.userID, tt.roomID) | ||
if gotUsers := tCache.GetTypingUsers(tt.roomID); !reflect.DeepEqual(gotUsers, tt.wantUsers) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
dendrite/src/github.com/matrix-org/dendrite/roomserver/query/query_test.go
Lines 94 to 109 in 9e352e7
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 | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion. :)
Signed-Off-By: Matthias Kesler <krombel@krombel.de>
New test from @krombel. Yay! |
For some reason travis build is not visible. One can it here https://travis-ci.org/matrix-org/dendrite/builds/409862091 |
There was a problem hiding this 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:]...) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure.
ready for review.
Signed-off-by: Anant Prakash <anantprakashjsr@gmail.com>