Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions helpers/txnid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package helpers

import (
"fmt"
"sync/atomic"
"time"
)

var txnCounter atomic.Int64
var start = time.Now().Unix()

// GetTxnID generates a unique transaction ID for use with endpoints like /send/{eventType/{txnId}.
// The prefix is only for debugging purposes.
func GetTxnID(prefix string) (txnID string) {
return fmt.Sprintf("%s-%d-%d", prefix, start, txnCounter.Add(1))
}
4 changes: 2 additions & 2 deletions tests/csapi/invalid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestJson(t *testing.T) {
}

for _, testCase := range testCases {
res := alice.Do(t, "POST", []string{"_matrix", "client", "v3", "rooms", roomID, "send", "complement.dummy"}, client.WithJSONBody(t, testCase))
res := alice.Do(t, "PUT", []string{"_matrix", "client", "v3", "rooms", roomID, "send", "complement.dummy", helpers.GetTxnID("TestJson-InvalidNum")}, client.WithJSONBody(t, testCase))

must.MatchResponse(t, res, match.HTTPResponse{
StatusCode: 400,
Expand All @@ -57,7 +57,7 @@ func TestJson(t *testing.T) {
}

for _, testCase := range testCases {
res := alice.Do(t, "POST", []string{"_matrix", "client", "v3", "rooms", roomID, "send", "complement.dummy"}, client.WithJSONBody(t, testCase))
res := alice.Do(t, "PUT", []string{"_matrix", "client", "v3", "rooms", roomID, "send", "complement.dummy", helpers.GetTxnID("TestJson-InvalidVal")}, client.WithJSONBody(t, testCase))

must.MatchResponse(t, res, match.HTTPResponse{
StatusCode: 400,
Expand Down
5 changes: 2 additions & 3 deletions tests/csapi/room_messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/matrix-org/gomatrixserverlib/spec"
)

// sytest: POST /rooms/:room_id/send/:event_type sends a message
// sytest: GET /rooms/:room_id/messages returns a message
func TestSendAndFetchMessage(t *testing.T) {
runtime.SkipIf(t, runtime.Dendrite) // flakey
Expand All @@ -37,8 +36,8 @@ func TestSendAndFetchMessage(t *testing.T) {

_, token := alice.MustSync(t, client.SyncReq{})

// first use the non-txn endpoint
alice.MustDo(t, "POST", []string{"_matrix", "client", "v3", "rooms", roomID, "send", "m.room.message"}, client.WithJSONBody(t, map[string]interface{}{
// first use the send endpoint
alice.MustDo(t, "PUT", []string{"_matrix", "client", "v3", "rooms", roomID, "send", "m.room.message", helpers.GetTxnID("TestSendAndFetchMessage")}, client.WithJSONBody(t, map[string]interface{}{
"msgtype": "m.text",
"body": testMessage,
}))
Expand Down
13 changes: 1 addition & 12 deletions tests/room_timestamp_to_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"fmt"
"net/url"
"strconv"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -253,16 +252,6 @@ type eventTime struct {
AfterTimestamp time.Time
}

var txnCounter int64 = 0

func getTxnID(prefix string) (txnID string) {
txnId := fmt.Sprintf("%s-%d", prefix, atomic.LoadInt64(&txnCounter))

atomic.AddInt64(&txnCounter, 1)

return txnId
}

func createTestRoom(t *testing.T, c *client.CSAPI) (roomID string, eventA, eventB *eventTime) {
t.Helper()

Expand Down Expand Up @@ -305,7 +294,7 @@ func sendMessageWithTimestamp(t *testing.T, as *client.CSAPI, c *client.CSAPI, r
//
// We can't use as.SendEventSynced(...) because application services can't use
// the /sync API.
sendRes := as.Do(t, "PUT", []string{"_matrix", "client", "v3", "rooms", roomID, "send", "m.room.message", getTxnID("sendMessageWithTimestamp-txn")}, client.WithContentType("application/json"), client.WithJSONBody(t, map[string]interface{}{
sendRes := as.Do(t, "PUT", []string{"_matrix", "client", "v3", "rooms", roomID, "send", "m.room.message", helpers.GetTxnID("sendMessageWithTimestamp-txn")}, client.WithContentType("application/json"), client.WithJSONBody(t, map[string]interface{}{
"body": message,
"msgtype": "m.text",
}), client.WithQueries(url.Values{
Expand Down
Loading