Add test for TypingCache.removeUser

Signed-Off-By: Matthias Kesler <krombel@krombel.de>
This commit is contained in:
Krombel 2018-07-27 20:10:28 +02:00
parent 8602cc617f
commit 738f014fcb
2 changed files with 31 additions and 3 deletions

View file

@ -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)
}
}

View file

@ -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)
}
}
}