Skip to content

Commit 756d388

Browse files
Use stable MSC3030 /timestamp_to_event endpoints (#559)
Synapse changes: matrix-org/synapse#14471
1 parent ab6cf55 commit 756d388

File tree

2 files changed

+59
-58
lines changed

2 files changed

+59
-58
lines changed

tests/msc2716_test.go

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,16 +1126,6 @@ func TestImportHistoricalMessages(t *testing.T) {
11261126
})
11271127
}
11281128

1129-
var txnCounter int = 0
1130-
1131-
func getTxnID(prefix string) (txnID string) {
1132-
txnId := fmt.Sprintf("%s-%d", prefix, txnCounter)
1133-
1134-
txnCounter++
1135-
1136-
return txnId
1137-
}
1138-
11391129
func makeInterfaceSlice(slice []string) []interface{} {
11401130
interfaceSlice := make([]interface{}, len(slice))
11411131
for i := range slice {
@@ -1164,42 +1154,6 @@ func includes(needle string, haystack []string) bool {
11641154
return false
11651155
}
11661156

1167-
func fetchUntilMessagesResponseHas(t *testing.T, c *client.CSAPI, roomID string, check func(gjson.Result) bool) {
1168-
t.Helper()
1169-
start := time.Now()
1170-
checkCounter := 0
1171-
for {
1172-
if time.Since(start) > c.SyncUntilTimeout {
1173-
t.Fatalf("fetchUntilMessagesResponseHas timed out. Called check function %d times", checkCounter)
1174-
}
1175-
1176-
messagesRes := c.MustDoFunc(t, "GET", []string{"_matrix", "client", "v3", "rooms", roomID, "messages"}, client.WithContentType("application/json"), client.WithQueries(url.Values{
1177-
"dir": []string{"b"},
1178-
"limit": []string{"100"},
1179-
}))
1180-
messsageResBody := client.ParseJSON(t, messagesRes)
1181-
wantKey := "chunk"
1182-
keyRes := gjson.GetBytes(messsageResBody, wantKey)
1183-
if !keyRes.Exists() {
1184-
t.Fatalf("missing key '%s'", wantKey)
1185-
}
1186-
if !keyRes.IsArray() {
1187-
t.Fatalf("key '%s' is not an array (was %s)", wantKey, keyRes.Type)
1188-
}
1189-
1190-
events := keyRes.Array()
1191-
for _, ev := range events {
1192-
if check(ev) {
1193-
return
1194-
}
1195-
}
1196-
1197-
checkCounter++
1198-
// Add a slight delay so we don't hammmer the messages endpoint
1199-
time.Sleep(500 * time.Millisecond)
1200-
}
1201-
}
1202-
12031157
// Paginate the /messages endpoint until we find all of the expectedEventIds
12041158
// (order does not matter). If any event in denyListEventIDs is found, an error
12051159
// will be thrown.

tests/msc3030_test.go renamed to tests/room_timestamp_to_event_test.go

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
//go:build msc3030
2-
// +build msc3030
1+
//go:build !dendrite_blacklist
2+
// +build !dendrite_blacklist
33

4-
// This file contains tests for a jump to date API endpoint,
5-
// currently experimental feature defined by MSC3030, which you can read here:
6-
// https://github.com/matrix-org/matrix-doc/pull/3030
4+
// This file contains tests for the `/timestamp_to_event` client and federation API
5+
// endpoints (also known as *jump to date*). As defined by MSC3030, which you can read
6+
// here: https://github.com/matrix-org/matrix-doc/pull/3030
77

88
package tests
99

@@ -118,7 +118,7 @@ func TestJumpToDateEndpoint(t *testing.T) {
118118
// Make the `/timestamp_to_event` request from Bob's perspective (non room member)
119119
timestamp := makeTimestampFromTime(timeBeforeRoomCreation)
120120
timestampString := strconv.FormatInt(timestamp, 10)
121-
timestampToEventRes := nonMemberUser.DoFunc(t, "GET", []string{"_matrix", "client", "unstable", "org.matrix.msc3030", "rooms", roomID, "timestamp_to_event"}, client.WithContentType("application/json"), client.WithQueries(url.Values{
121+
timestampToEventRes := nonMemberUser.DoFunc(t, "GET", []string{"_matrix", "client", "v1", "rooms", roomID, "timestamp_to_event"}, client.WithContentType("application/json"), client.WithQueries(url.Values{
122122
"ts": []string{timestampString},
123123
"dir": []string{"f"},
124124
}))
@@ -146,7 +146,7 @@ func TestJumpToDateEndpoint(t *testing.T) {
146146
// Make the `/timestamp_to_event` request from Bob's perspective (non room member)
147147
timestamp := makeTimestampFromTime(timeBeforeRoomCreation)
148148
timestampString := strconv.FormatInt(timestamp, 10)
149-
timestampToEventRes := nonMemberUser.DoFunc(t, "GET", []string{"_matrix", "client", "unstable", "org.matrix.msc3030", "rooms", roomID, "timestamp_to_event"}, client.WithContentType("application/json"), client.WithQueries(url.Values{
149+
timestampToEventRes := nonMemberUser.DoFunc(t, "GET", []string{"_matrix", "client", "v1", "rooms", roomID, "timestamp_to_event"}, client.WithContentType("application/json"), client.WithQueries(url.Values{
150150
"ts": []string{timestampString},
151151
"dir": []string{"f"},
152152
}))
@@ -236,6 +236,16 @@ type eventTime struct {
236236
AfterTimestamp time.Time
237237
}
238238

239+
var txnCounter int = 0
240+
241+
func getTxnID(prefix string) (txnID string) {
242+
txnId := fmt.Sprintf("%s-%d", prefix, txnCounter)
243+
244+
txnCounter++
245+
246+
return txnId
247+
}
248+
239249
func createTestRoom(t *testing.T, c *client.CSAPI) (roomID string, eventA, eventB *eventTime) {
240250
t.Helper()
241251

@@ -300,19 +310,20 @@ func mustCheckEventisReturnedForTime(t *testing.T, c *client.CSAPI, roomID strin
300310

301311
givenTimestamp := makeTimestampFromTime(givenTime)
302312
timestampString := strconv.FormatInt(givenTimestamp, 10)
303-
timestampToEventRes := c.DoFunc(t, "GET", []string{"_matrix", "client", "unstable", "org.matrix.msc3030", "rooms", roomID, "timestamp_to_event"}, client.WithContentType("application/json"), client.WithQueries(url.Values{
313+
timestampToEventRes := c.DoFunc(t, "GET", []string{"_matrix", "client", "v1", "rooms", roomID, "timestamp_to_event"}, client.WithContentType("application/json"), client.WithQueries(url.Values{
304314
"ts": []string{timestampString},
305315
"dir": []string{direction},
306316
}))
307317
timestampToEventResBody := client.ParseJSON(t, timestampToEventRes)
308318

309-
// Only allow a 200 response meaning we found an event or a 404 meaning we didn't.
310-
// Other status codes will throw and assumed to be application errors.
319+
// Only allow a 200 response meaning we found an event or when no `expectedEventId` is provided, a
320+
// 404 meaning we didn't find anything. Other status codes will throw and are assumed to
321+
// be application errors.
311322
actualEventId := ""
312323
if timestampToEventRes.StatusCode == 200 {
313324
actualEventId = client.GetJSONFieldStr(t, timestampToEventResBody, "event_id")
314-
} else if timestampToEventRes.StatusCode != 404 {
315-
t.Fatalf("mustCheckEventisReturnedForTime: /timestamp_to_event request failed with status=%d", timestampToEventRes.StatusCode)
325+
} else if timestampToEventRes.StatusCode != 404 || (timestampToEventRes.StatusCode == 404 && expectedEventId != "") {
326+
t.Fatalf("mustCheckEventisReturnedForTime: /timestamp_to_event request failed with status=%d body=%s", timestampToEventRes.StatusCode, string(timestampToEventResBody))
316327
}
317328

318329
if actualEventId != expectedEventId {
@@ -327,6 +338,42 @@ func mustCheckEventisReturnedForTime(t *testing.T, c *client.CSAPI, roomID strin
327338
}
328339
}
329340

341+
func fetchUntilMessagesResponseHas(t *testing.T, c *client.CSAPI, roomID string, check func(gjson.Result) bool) {
342+
t.Helper()
343+
start := time.Now()
344+
checkCounter := 0
345+
for {
346+
if time.Since(start) > c.SyncUntilTimeout {
347+
t.Fatalf("fetchUntilMessagesResponseHas timed out. Called check function %d times", checkCounter)
348+
}
349+
350+
messagesRes := c.MustDoFunc(t, "GET", []string{"_matrix", "client", "v3", "rooms", roomID, "messages"}, client.WithContentType("application/json"), client.WithQueries(url.Values{
351+
"dir": []string{"b"},
352+
"limit": []string{"100"},
353+
}))
354+
messsageResBody := client.ParseJSON(t, messagesRes)
355+
wantKey := "chunk"
356+
keyRes := gjson.GetBytes(messsageResBody, wantKey)
357+
if !keyRes.Exists() {
358+
t.Fatalf("missing key '%s'", wantKey)
359+
}
360+
if !keyRes.IsArray() {
361+
t.Fatalf("key '%s' is not an array (was %s)", wantKey, keyRes.Type)
362+
}
363+
364+
events := keyRes.Array()
365+
for _, ev := range events {
366+
if check(ev) {
367+
return
368+
}
369+
}
370+
371+
checkCounter++
372+
// Add a slight delay so we don't hammmer the messages endpoint
373+
time.Sleep(500 * time.Millisecond)
374+
}
375+
}
376+
330377
func getDebugMessageListFromMessagesResponse(t *testing.T, c *client.CSAPI, roomID string, expectedEventId string, actualEventId string, givenTimestamp int64) string {
331378
t.Helper()
332379

0 commit comments

Comments
 (0)