MCPcopy Index your code
hub / github.com/daptin/daptin / TestWebSocketMultiUserMessaging

Function TestWebSocketMultiUserMessaging

websocket_test.go:1575–1831  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1573}
1574
1575func TestWebSocketMultiUserMessaging(t *testing.T) {
1576 ensureServer()
1577
1578 const numUsers = 20
1579 const msgsPerUser = 100
1580
1581 // Phase 1: Create users and get tokens concurrently
1582 t.Log("Phase 1: Creating users...")
1583 tokens := make([]string, numUsers)
1584 var signupWg sync.WaitGroup
1585 var signupErrors int64
1586 signupStart := time.Now()
1587
1588 const signupBatch = 20
1589 for batchStart := 0; batchStart < numUsers; batchStart += signupBatch {
1590 batchEnd := batchStart + signupBatch
1591 if batchEnd > numUsers {
1592 batchEnd = numUsers
1593 }
1594 for i := batchStart; i < batchEnd; i++ {
1595 signupWg.Add(1)
1596 go func(idx int) {
1597 defer signupWg.Done()
1598 email := fmt.Sprintf("user%d@stress.test", idx)
1599 token, err := signUpUser(email, "tester123")
1600 if err != nil {
1601 atomic.AddInt64(&signupErrors, 1)
1602 return
1603 }
1604 tokens[idx] = token
1605 }(i)
1606 }
1607 signupWg.Wait()
1608 }
1609 signupDuration := time.Since(signupStart)
1610
1611 var validUsers int
1612 for _, tok := range tokens {
1613 if tok != "" {
1614 validUsers++
1615 }
1616 }
1617 t.Logf(" created %d/%d users in %v (%d signup errors)", validUsers, numUsers, signupDuration, signupErrors)
1618 if validUsers < numUsers/2 {
1619 t.Fatalf("too few users created: %d/%d", validUsers, numUsers)
1620 }
1621
1622 // Phase 2: Create topics — each user gets an "inbox" topic, set permissions to allow all
1623 t.Log("Phase 2: Creating per-user topics...")
1624 setupWs := dialWS(t, tokens[0])
1625 topicNames := make([]string, numUsers)
1626 for i := 0; i < numUsers; i++ {
1627 if tokens[i] == "" {
1628 continue
1629 }
1630 topicNames[i] = fmt.Sprintf("inbox-%d-%d", time.Now().UnixNano(), i)
1631 _ = websocket.JSON.Send(setupWs, wsPayload{Id: nextReqId(), Method: "create-topicName", Payload: map[string]interface{}{"name": topicNames[i]}})
1632 tryRecvJSON(setupWs, 5*time.Second) // create response

Callers

nothing calls this directly

Calls 10

ensureServerFunction · 0.85
makeFunction · 0.85
signUpUserFunction · 0.85
dialWSFunction · 0.85
nextReqIdFunction · 0.85
tryRecvJSONFunction · 0.85
AddMethod · 0.80
SetMethod · 0.80
CloseMethod · 0.65
DoneMethod · 0.45

Tested by

no test coverage detected