From dce061630530c467966378ae3c5adbcf4a09e34f Mon Sep 17 00:00:00 2001 From: Joram Wilander Date: Wed, 17 Jan 2018 08:50:49 -0500 Subject: ABC-73 Move session clean-up to daily task (#8095) * Move session clean-up to daily task * Split delete query into batches --- store/storetest/mocks/SessionStore.go | 5 ++++ store/storetest/session_store.go | 48 ++++++++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) (limited to 'store/storetest') diff --git a/store/storetest/mocks/SessionStore.go b/store/storetest/mocks/SessionStore.go index 1bec89e11..70b2bd945 100644 --- a/store/storetest/mocks/SessionStore.go +++ b/store/storetest/mocks/SessionStore.go @@ -29,6 +29,11 @@ func (_m *SessionStore) AnalyticsSessionCount() store.StoreChannel { return r0 } +// Cleanup provides a mock function with given fields: expiryTime, batchSize +func (_m *SessionStore) Cleanup(expiryTime int64, batchSize int64) { + _m.Called(expiryTime, batchSize) +} + // Get provides a mock function with given fields: sessionIdOrToken func (_m *SessionStore) Get(sessionIdOrToken string) store.StoreChannel { ret := _m.Called(sessionIdOrToken) diff --git a/store/storetest/session_store.go b/store/storetest/session_store.go index 3d35b91ad..cf1db177d 100644 --- a/store/storetest/session_store.go +++ b/store/storetest/session_store.go @@ -8,9 +8,14 @@ import ( "github.com/mattermost/mattermost-server/model" "github.com/mattermost/mattermost-server/store" + + "github.com/stretchr/testify/assert" ) func TestSessionStore(t *testing.T, ss store.Store) { + // Run serially to prevent interfering with other tests + testSessionCleanup(t, ss) + t.Run("Save", func(t *testing.T) { testSessionStoreSave(t, ss) }) t.Run("SessionGet", func(t *testing.T) { testSessionGet(t, ss) }) t.Run("SessionGetWithDeviceId", func(t *testing.T) { testSessionGetWithDeviceId(t, ss) }) @@ -58,7 +63,7 @@ func testSessionGet(t *testing.T, ss store.Store) { if rs2 := (<-ss.Session().GetSessions(s1.UserId)); rs2.Err != nil { t.Fatal(rs2.Err) } else { - if len(rs2.Data.([]*model.Session)) != 2 { + if len(rs2.Data.([]*model.Session)) != 3 { t.Fatal("should match len") } } @@ -248,3 +253,44 @@ func testSessionCount(t *testing.T, ss store.Store) { } } } + +func testSessionCleanup(t *testing.T, ss store.Store) { + now := model.GetMillis() + + s1 := model.Session{} + s1.UserId = model.NewId() + s1.ExpiresAt = 0 // never expires + store.Must(ss.Session().Save(&s1)) + + s2 := model.Session{} + s2.UserId = s1.UserId + s2.ExpiresAt = now + 1000000 // expires in the future + store.Must(ss.Session().Save(&s2)) + + s3 := model.Session{} + s3.UserId = model.NewId() + s3.ExpiresAt = 1 // expired + store.Must(ss.Session().Save(&s3)) + + s4 := model.Session{} + s4.UserId = model.NewId() + s4.ExpiresAt = 2 // expired + store.Must(ss.Session().Save(&s4)) + + ss.Session().Cleanup(now, 1) + + err := (<-ss.Session().Get(s1.Id)).Err + assert.Nil(t, err) + + err = (<-ss.Session().Get(s2.Id)).Err + assert.Nil(t, err) + + err = (<-ss.Session().Get(s3.Id)).Err + assert.NotNil(t, err) + + err = (<-ss.Session().Get(s4.Id)).Err + assert.NotNil(t, err) + + store.Must(ss.Session().Remove(s1.Id)) + store.Must(ss.Session().Remove(s2.Id)) +} -- cgit v1.2.3-1-g7c22