mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-12 17:33:09 -06:00
Add test for TypingCache.removeUser
Signed-Off-By: Matthias Kesler <krombel@krombel.de>
This commit is contained in:
parent
8602cc617f
commit
738f014fcb
|
|
@ -17,7 +17,7 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
var defaultTypingTimeout = 10 * time.Second
|
||||
const defaultTypingTimeout = 10 * time.Second
|
||||
|
||||
// userSet is a map of user IDs to a timer, timer fires at expiry.
|
||||
type userSet map[string]*time.Timer
|
||||
|
|
@ -40,8 +40,8 @@ func (t *TypingCache) GetTypingUsers(roomID string) (users []string) {
|
|||
t.RUnlock()
|
||||
if ok {
|
||||
users = make([]string, 0, len(usersMap))
|
||||
for key := range usersMap {
|
||||
users = append(users, key)
|
||||
for userID := range usersMap {
|
||||
users = append(users, userID)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@
|
|||
package cache
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
|
@ -32,6 +33,10 @@ func TestTypingCache(t *testing.T) {
|
|||
t.Run("GetTypingUsers", func(t *testing.T) {
|
||||
testGetTypingUsers(t, tCache)
|
||||
})
|
||||
|
||||
t.Run("removeUser", func(t *testing.T) {
|
||||
testRemoveUser(t, tCache)
|
||||
})
|
||||
}
|
||||
|
||||
func testAddTypingUser(t *testing.T, tCache *TypingCache) {
|
||||
|
|
@ -70,3 +75,26 @@ func testGetTypingUsers(t *testing.T, tCache *TypingCache) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testRemoveUser(t *testing.T, tCache *TypingCache) {
|
||||
tests := []struct {
|
||||
roomID string
|
||||
userIDs []string
|
||||
}{
|
||||
{"room3", []string{"user1"}},
|
||||
{"room4", []string{"user1", "user2", "user3"}},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
for _, userID := range tt.userIDs {
|
||||
tCache.AddTypingUser(userID, tt.roomID, nil)
|
||||
}
|
||||
i := rand.Intn(len(tt.userIDs))
|
||||
tCache.removeUser(tt.userIDs[i], tt.roomID)
|
||||
expLeftUsers := append(tt.userIDs[:i], tt.userIDs[i+1:]...)
|
||||
|
||||
if leftUsers := tCache.GetTypingUsers(tt.roomID); !test.UnsortedStringSliceEqual(leftUsers, expLeftUsers) {
|
||||
t.Errorf("Response after removal is unexpected %s/%s", leftUsers, expLeftUsers)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue