From 738f014fcbd35fce7f6fb255390109934ffafd41 Mon Sep 17 00:00:00 2001 From: Krombel Date: Fri, 27 Jul 2018 20:10:28 +0200 Subject: [PATCH] Add test for TypingCache.removeUser Signed-Off-By: Matthias Kesler --- .../dendrite/typingserver/cache/cache.go | 6 ++-- .../dendrite/typingserver/cache/cache_test.go | 28 +++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/github.com/matrix-org/dendrite/typingserver/cache/cache.go b/src/github.com/matrix-org/dendrite/typingserver/cache/cache.go index bc50612f3..b1b9452a8 100644 --- a/src/github.com/matrix-org/dendrite/typingserver/cache/cache.go +++ b/src/github.com/matrix-org/dendrite/typingserver/cache/cache.go @@ -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) } } diff --git a/src/github.com/matrix-org/dendrite/typingserver/cache/cache_test.go b/src/github.com/matrix-org/dendrite/typingserver/cache/cache_test.go index 86521cbf9..85a447707 100644 --- a/src/github.com/matrix-org/dendrite/typingserver/cache/cache_test.go +++ b/src/github.com/matrix-org/dendrite/typingserver/cache/cache_test.go @@ -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) + } + } + +}