mirror of
https://github.com/sosedoff/pgweb.git
synced 2024-12-14 19:21:46 +03:00
Add session manager tests
This commit is contained in:
parent
9074d4bfb8
commit
29a6a0df8c
79
pkg/api/session_manager_test.go
Normal file
79
pkg/api/session_manager_test.go
Normal file
@ -0,0 +1,79 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/sosedoff/pgweb/pkg/client"
|
||||
"github.com/sosedoff/pgweb/pkg/command"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestSessionManager(t *testing.T) {
|
||||
t.Run("returns ids", func(t *testing.T) {
|
||||
manager := NewSessionManager(nil)
|
||||
assert.Equal(t, []string{}, manager.IDs())
|
||||
|
||||
manager.sessions["foo"] = &client.Client{}
|
||||
manager.sessions["bar"] = &client.Client{}
|
||||
|
||||
ids := manager.IDs()
|
||||
sort.Strings(ids)
|
||||
assert.Equal(t, []string{"bar", "foo"}, ids)
|
||||
})
|
||||
|
||||
t.Run("get session", func(t *testing.T) {
|
||||
manager := NewSessionManager(nil)
|
||||
assert.Nil(t, manager.Get("foo"))
|
||||
|
||||
manager.sessions["foo"] = &client.Client{}
|
||||
assert.NotNil(t, manager.Get("foo"))
|
||||
})
|
||||
|
||||
t.Run("set session", func(t *testing.T) {
|
||||
manager := NewSessionManager(nil)
|
||||
assert.Nil(t, manager.Get("foo"))
|
||||
|
||||
manager.Add("foo", &client.Client{})
|
||||
assert.NotNil(t, manager.Get("foo"))
|
||||
})
|
||||
|
||||
t.Run("remove session", func(t *testing.T) {
|
||||
manager := NewSessionManager(nil)
|
||||
assert.Nil(t, manager.Get("foo"))
|
||||
|
||||
manager.Add("foo", &client.Client{})
|
||||
assert.NotNil(t, manager.Get("foo"))
|
||||
assert.True(t, manager.Remove("foo"))
|
||||
assert.False(t, manager.Remove("foo"))
|
||||
assert.Nil(t, manager.Get("foo"))
|
||||
})
|
||||
|
||||
t.Run("returns len", func(t *testing.T) {
|
||||
manager := NewSessionManager(nil)
|
||||
manager.sessions["foo"] = &client.Client{}
|
||||
manager.sessions["bar"] = &client.Client{}
|
||||
|
||||
assert.Equal(t, 2, manager.Len())
|
||||
})
|
||||
|
||||
t.Run("cleans up stale sessions", func(t *testing.T) {
|
||||
defer func() {
|
||||
command.Opts.ConnectionIdleTimeout = 0
|
||||
}()
|
||||
|
||||
manager := NewSessionManager(logrus.New())
|
||||
conn := &client.Client{}
|
||||
manager.Add("foo", conn)
|
||||
|
||||
command.Opts.ConnectionIdleTimeout = 0
|
||||
assert.Equal(t, 1, manager.Len())
|
||||
assert.Equal(t, 0, manager.Cleanup())
|
||||
assert.Equal(t, 1, manager.Len())
|
||||
|
||||
command.Opts.ConnectionIdleTimeout = 1
|
||||
assert.Equal(t, 1, manager.Cleanup())
|
||||
assert.Equal(t, 0, manager.Len())
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue
Block a user