MCPcopy
hub / github.com/gofiber/fiber / Test_Session_Concurrency

Function Test_Session_Concurrency

middleware/session/session_test.go:1571–1673  ·  view source on GitHub ↗

go test -v -race -run Test_Session_Concurrency ./...

(t *testing.T)

Source from the content-addressed store, hash-verified

1569
1570// go test -v -race -run Test_Session_Concurrency ./...
1571func Test_Session_Concurrency(t *testing.T) {
1572 app := fiber.New()
1573 store := NewStore()
1574
1575 var wg sync.WaitGroup
1576 errChan := make(chan error, 10) // Buffered channel to collect errors
1577 const numGoroutines = 10 // Number of concurrent goroutines to test
1578
1579 // Start numGoroutines goroutines
1580 for range numGoroutines {
1581 wg.Go(func() {
1582 localCtx := app.AcquireCtx(&fasthttp.RequestCtx{})
1583
1584 sess, err := store.getSession(localCtx)
1585 if err != nil {
1586 errChan <- err
1587 return
1588 }
1589
1590 // Set a value
1591 sess.Set("name", "john")
1592
1593 // get the session id
1594 id := sess.ID()
1595
1596 // Check if the session is fresh
1597 if !sess.Fresh() {
1598 errChan <- errors.New("session should be fresh")
1599 return
1600 }
1601
1602 // Save the session
1603 if saveErr := sess.Save(); saveErr != nil {
1604 errChan <- saveErr
1605 return
1606 }
1607
1608 // release the session
1609 sess.Release()
1610
1611 // Release the context
1612 app.ReleaseCtx(localCtx)
1613
1614 // Acquire a new context
1615 localCtx = app.AcquireCtx(&fasthttp.RequestCtx{})
1616 defer app.ReleaseCtx(localCtx)
1617
1618 // Set the session id in the header
1619 localCtx.Request().Header.SetCookie("session_id", id)
1620
1621 // Get the session
1622 sess, err = store.Get(localCtx)
1623 if err != nil {
1624 errChan <- err
1625 return
1626 }
1627 defer sess.Release()
1628

Callers

nothing calls this directly

Calls 15

getSessionMethod · 0.95
GetMethod · 0.95
NewStoreFunction · 0.85
AcquireCtxMethod · 0.80
ReleaseCtxMethod · 0.80
NewMethod · 0.65
SetMethod · 0.65
FreshMethod · 0.65
RequestMethod · 0.65
GetMethod · 0.65
DeleteMethod · 0.65
IDMethod · 0.45

Tested by

no test coverage detected