forked from matrix-org/sliding-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_test.go
107 lines (102 loc) · 3.02 KB
/
db_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package syncv3
import (
"encoding/json"
"fmt"
"sync"
"testing"
"time"
syncv3 "github.com/matrix-org/sliding-sync"
"github.com/matrix-org/sliding-sync/sync2"
"github.com/matrix-org/sliding-sync/sync3"
"github.com/matrix-org/sliding-sync/testutils"
"github.com/matrix-org/sliding-sync/testutils/m"
)
// Test that the proxy works fine with low max conns. Low max conns can be a problem
// if a request A needs 2 conns to respond and that blocks forward progress on the server,
// and the request can only obtain 1 conn.
func TestMaxDBConns(t *testing.T) {
pqString := testutils.PrepareDBConnectionString()
// setup code
v2 := runTestV2Server(t)
opts := syncv3.Opts{
DBMaxConns: 1,
}
v3 := runTestServer(t, v2, pqString, opts)
defer v2.close()
defer v3.close()
testMaxDBConns := func() {
// make N users and drip feed some events, make sure they are all seen
numUsers := 5
var wg sync.WaitGroup
wg.Add(numUsers)
for i := 0; i < numUsers; i++ {
go func(n int) {
defer wg.Done()
userID := fmt.Sprintf("@maxconns_%d:localhost", n)
token := fmt.Sprintf("maxconns_%d", n)
roomID := fmt.Sprintf("!maxconns_%d", n)
v2.addAccount(t, userID, token)
state := createRoomState(t, userID, time.Now())
v2.queueResponse(userID, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: v2JoinTimeline(roomEvents{
roomID: roomID,
state: state[:len(state)-1],
events: []json.RawMessage{
state[len(state)-1],
},
}),
},
})
// initial sync
res := v3.mustDoV3Request(t, token, sync3.Request{
Lists: map[string]sync3.RequestList{"a": {
Ranges: sync3.SliceRanges{
[2]int64{0, 1},
},
RoomSubscription: sync3.RoomSubscription{
TimelineLimit: 1,
},
}},
})
t.Logf("user %s has done an initial /sync OK", userID)
m.MatchResponse(t, res, m.MatchList("a", m.MatchV3Count(1), m.MatchV3Ops(
m.MatchV3SyncOp(0, 0, []string{roomID}),
)), m.MatchRoomSubscriptionsStrict(map[string][]m.RoomMatcher{
roomID: {
m.MatchJoinCount(1),
},
}))
// drip feed and get update
dripMsg := testutils.NewEvent(t, "m.room.message", userID, map[string]interface{}{
"msgtype": "m.text",
"body": "drip drip",
})
v2.queueResponse(userID, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: v2JoinTimeline(roomEvents{
roomID: roomID,
events: []json.RawMessage{
dripMsg,
},
}),
},
})
t.Logf("user %s has queued the drip", userID)
v2.waitUntilEmpty(t, userID)
t.Logf("user %s poller has received the drip", userID)
res = v3.mustDoV3RequestWithPos(t, token, res.Pos, sync3.Request{})
m.MatchResponse(t, res, m.MatchRoomSubscriptionsStrict(map[string][]m.RoomMatcher{
roomID: {
m.MatchRoomTimelineMostRecent(1, []json.RawMessage{dripMsg}),
},
}))
t.Logf("user %s has received the drip", userID)
}(i)
}
wg.Wait()
}
testMaxDBConns()
v3.restart(t, v2, pqString, opts)
testMaxDBConns()
}