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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
package tables_test
import (
"context"
"testing"
"github.com/matrix-org/dendrite/internal/sqlutil"
"github.com/matrix-org/dendrite/roomserver/storage/postgres"
"github.com/matrix-org/dendrite/roomserver/storage/sqlite3"
"github.com/matrix-org/dendrite/roomserver/storage/tables"
"github.com/matrix-org/dendrite/roomserver/types"
"github.com/matrix-org/dendrite/setup/config"
"github.com/matrix-org/dendrite/test"
"github.com/stretchr/testify/assert"
)
func mustCreateEventsTable(t *testing.T, dbType test.DBType) (tables.Events, func()) {
t.Helper()
connStr, close := test.PrepareDBConnectionString(t, dbType)
db, err := sqlutil.Open(&config.DatabaseOptions{
ConnectionString: config.DataSource(connStr),
}, sqlutil.NewExclusiveWriter())
assert.NoError(t, err)
var tab tables.Events
switch dbType {
case test.DBTypePostgres:
err = postgres.CreateEventsTable(db)
assert.NoError(t, err)
tab, err = postgres.PrepareEventsTable(db)
case test.DBTypeSQLite:
err = sqlite3.CreateEventsTable(db)
assert.NoError(t, err)
tab, err = sqlite3.PrepareEventsTable(db)
}
assert.NoError(t, err)
return tab, close
}
func Test_EventsTable(t *testing.T) {
alice := test.NewUser(t)
room := test.NewRoom(t, alice)
ctx := context.Background()
test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
tab, close := mustCreateEventsTable(t, dbType)
defer close()
// create some dummy data
eventIDs := make([]string, 0, len(room.Events()))
wantStateAtEvent := make([]types.StateAtEvent, 0, len(room.Events()))
wantStateAtEventAndRefs := make([]types.StateAtEventAndReference, 0, len(room.Events()))
for _, ev := range room.Events() {
eventNID, snapNID, err := tab.InsertEvent(ctx, nil, 1, 1, 1, ev.EventID(), nil, ev.Depth(), false)
assert.NoError(t, err)
gotEventNID, gotSnapNID, err := tab.SelectEvent(ctx, nil, ev.EventID())
assert.NoError(t, err)
assert.Equal(t, eventNID, gotEventNID)
assert.Equal(t, snapNID, gotSnapNID)
eventID, err := tab.SelectEventID(ctx, nil, eventNID)
assert.NoError(t, err)
assert.Equal(t, eventID, ev.EventID())
// The events shouldn't be sent to output yet
sentToOutput, err := tab.SelectEventSentToOutput(ctx, nil, gotEventNID)
assert.NoError(t, err)
assert.False(t, sentToOutput)
err = tab.UpdateEventSentToOutput(ctx, nil, gotEventNID)
assert.NoError(t, err)
// Now they should be sent to output
sentToOutput, err = tab.SelectEventSentToOutput(ctx, nil, gotEventNID)
assert.NoError(t, err)
assert.True(t, sentToOutput)
eventIDs = append(eventIDs, ev.EventID())
// Set the stateSnapshot to 2 for some events to verify they are returned later
stateSnapshot := 0
if eventNID < 3 {
stateSnapshot = 2
err = tab.UpdateEventState(ctx, nil, eventNID, 2)
assert.NoError(t, err)
}
stateAtEvent := types.StateAtEvent{
BeforeStateSnapshotNID: types.StateSnapshotNID(stateSnapshot),
IsRejected: false,
StateEntry: types.StateEntry{
EventNID: eventNID,
StateKeyTuple: types.StateKeyTuple{
EventTypeNID: 1,
EventStateKeyNID: 1,
},
},
}
wantStateAtEvent = append(wantStateAtEvent, stateAtEvent)
wantStateAtEventAndRefs = append(wantStateAtEventAndRefs, types.StateAtEventAndReference{
StateAtEvent: stateAtEvent,
EventID: ev.EventID(),
})
}
stateEvents, err := tab.BulkSelectStateEventByID(ctx, nil, eventIDs, false)
assert.NoError(t, err)
assert.Equal(t, len(stateEvents), len(eventIDs))
nids := make([]types.EventNID, 0, len(stateEvents))
for _, ev := range stateEvents {
nids = append(nids, ev.EventNID)
}
stateEvents2, err := tab.BulkSelectStateEventByNID(ctx, nil, nids, nil)
assert.NoError(t, err)
// somehow SQLite doesn't return the values ordered as requested by the query
assert.ElementsMatch(t, stateEvents, stateEvents2)
roomNIDs, err := tab.SelectRoomNIDsForEventNIDs(ctx, nil, nids)
assert.NoError(t, err)
// We only inserted one room, so the RoomNID should be the same for all evendNIDs
for _, roomNID := range roomNIDs {
assert.Equal(t, types.RoomNID(1), roomNID)
}
stateAtEvent, err := tab.BulkSelectStateAtEventByID(ctx, nil, eventIDs)
assert.NoError(t, err)
assert.Equal(t, len(eventIDs), len(stateAtEvent))
assert.ElementsMatch(t, wantStateAtEvent, stateAtEvent)
evendNIDMap, err := tab.BulkSelectEventID(ctx, nil, nids)
assert.NoError(t, err)
t.Logf("%+v", evendNIDMap)
assert.Equal(t, len(evendNIDMap), len(nids))
nidMap, err := tab.BulkSelectEventNID(ctx, nil, eventIDs)
assert.NoError(t, err)
// check that we got all expected eventNIDs
for _, eventID := range eventIDs {
_, ok := nidMap[eventID]
assert.True(t, ok)
}
stateAndRefs, err := tab.BulkSelectStateAtEventAndReference(ctx, nil, nids)
assert.NoError(t, err)
assert.Equal(t, wantStateAtEventAndRefs, stateAndRefs)
// check we get the expected event depth
maxDepth, err := tab.SelectMaxEventDepth(ctx, nil, nids)
assert.NoError(t, err)
assert.Equal(t, int64(len(room.Events())+1), maxDepth)
})
}
|