Skip to content

Commit

Permalink
fix: session driver lock problem (#558)
Browse files Browse the repository at this point in the history
* fix: session driver lock problem

* fix: start_session test

* fix: MakeSession test
  • Loading branch information
devhaozi authored Jul 12, 2024
1 parent 18c3e99 commit fda5657
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 20 deletions.
2 changes: 2 additions & 0 deletions foundation/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,8 @@ func (s *ApplicationTestSuite) TestMakeSchedule() {

func (s *ApplicationTestSuite) TestMakeSession() {
mockConfig := &configmocks.Config{}
mockConfig.On("GetInt", "session.lifetime").Return(120).Once()
mockConfig.On("GetString", "session.files").Return("storage/framework/sessions").Once()

s.app.Singleton(frameworkconfig.Binding, func(app foundation.Application) (any, error) {
return mockConfig, nil
Expand Down
14 changes: 7 additions & 7 deletions session/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ import (

type Manager struct {
config config.Config
customDrivers map[string]func() sessioncontract.Driver
drivers map[string]func() sessioncontract.Driver
customDrivers map[string]sessioncontract.Driver
drivers map[string]sessioncontract.Driver
json foundation.Json
}

func NewManager(config config.Config, json foundation.Json) *Manager {
manager := &Manager{
config: config,
customDrivers: make(map[string]func() sessioncontract.Driver),
drivers: make(map[string]func() sessioncontract.Driver),
customDrivers: make(map[string]sessioncontract.Driver),
drivers: make(map[string]sessioncontract.Driver),
json: json,
}
manager.registerDrivers()
Expand Down Expand Up @@ -51,11 +51,11 @@ func (m *Manager) Driver(name ...string) (sessioncontract.Driver, error) {
m.drivers[driverName] = m.customDrivers[driverName]
}

return m.drivers[driverName](), nil
return m.drivers[driverName], nil
}

func (m *Manager) Extend(driver string, handler func() sessioncontract.Driver) sessioncontract.Manager {
m.customDrivers[driver] = handler
m.customDrivers[driver] = handler()
return m
}

Expand All @@ -69,5 +69,5 @@ func (m *Manager) createFileDriver() sessioncontract.Driver {
}

func (m *Manager) registerDrivers() {
m.drivers["file"] = m.createFileDriver
m.drivers["file"] = m.createFileDriver()
}
7 changes: 2 additions & 5 deletions session/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@ func TestManagerTestSuite(t *testing.T) {

func (m *ManagerTestSuite) SetupTest() {
m.mockConfig = mockconfig.NewConfig(m.T())
m.mockConfig.On("GetInt", "session.lifetime").Return(120).Once()
m.mockConfig.On("GetString", "session.files").Return("storage/framework/sessions").Once()
m.manager = m.getManager()
m.json = json.NewJson()
}

func (m *ManagerTestSuite) TestDriver() {
m.mockConfig.On("GetInt", "session.lifetime").Return(120).Once()
m.mockConfig.On("GetString", "session.files").Return("storage/framework/sessions").Once()

// provide driver name
driver, err := m.manager.Driver("file")
m.Nil(err)
Expand All @@ -41,8 +40,6 @@ func (m *ManagerTestSuite) TestDriver() {

// provide no driver name
m.mockConfig.On("GetString", "session.driver").Return("file").Once()
m.mockConfig.On("GetInt", "session.lifetime").Return(120).Once()
m.mockConfig.On("GetString", "session.files").Return("storage/framework/sessions").Once()

driver, err = m.manager.Driver()
m.Nil(err)
Expand Down
15 changes: 7 additions & 8 deletions session/middleware/start_session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/stretchr/testify/require"

"github.com/goravel/framework/contracts/filesystem"
"github.com/goravel/framework/contracts/foundation"
contractshttp "github.com/goravel/framework/contracts/http"
contractsession "github.com/goravel/framework/contracts/session"
"github.com/goravel/framework/contracts/validation"
Expand All @@ -22,19 +21,16 @@ import (
"github.com/goravel/framework/support/file"
)

func testHttpSessionMiddleware(next nethttp.Handler, mockConfig *configmocks.Config, json foundation.Json) nethttp.Handler {
func testHttpSessionMiddleware(next nethttp.Handler, mockConfig *configmocks.Config) nethttp.Handler {
return nethttp.HandlerFunc(func(w nethttp.ResponseWriter, r *nethttp.Request) {
session.ConfigFacade = mockConfig
session.SessionFacade = session.NewManager(mockConfig, json)
mockConfigFacade(mockConfig)
StartSession()(NewTestContext(r.Context(), next, w, r))
})
}

func mockConfigFacade(mockConfig *configmocks.Config) {
mockConfig.On("GetString", "session.driver").Return("file").Twice()
mockConfig.On("GetInt", "session.lifetime").Return(60).Times(3)
mockConfig.On("GetString", "session.files").Return("sessions").Once()
mockConfig.On("GetInt", "session.lifetime").Return(60).Twice()
mockConfig.On("GetString", "session.cookie").Return("goravel_session").Once()
mockConfig.On("GetString", "session.path").Return("/").Once()
mockConfig.On("GetString", "session.domain").Return("").Once()
Expand All @@ -46,7 +42,10 @@ func mockConfigFacade(mockConfig *configmocks.Config) {

func TestStartSession(t *testing.T) {
mockConfig := &configmocks.Config{}
j := json.NewJson()
session.ConfigFacade = mockConfig
mockConfig.On("GetInt", "session.lifetime").Return(120).Once()
mockConfig.On("GetString", "session.files").Return("storage/framework/sessions").Once()
session.SessionFacade = session.NewManager(mockConfig, json.NewJson())
server := httptest.NewServer(testHttpSessionMiddleware(nethttp.HandlerFunc(func(w nethttp.ResponseWriter, r *nethttp.Request) {
switch r.URL.Path {
case "/add":
Expand All @@ -59,7 +58,7 @@ func TestStartSession(t *testing.T) {
assert.Equal(t, "bar", s.Get("foo"))
assert.Equal(t, "qux", s.Get("baz"))
}
}), mockConfig, j))
}), mockConfig))
defer server.Close()

client := &nethttp.Client{}
Expand Down

0 comments on commit fda5657

Please sign in to comment.