mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-17 02:53:11 -06:00
Flush out relayapi interface tests
This commit is contained in:
parent
21a63203df
commit
1f67804db5
|
|
@ -16,6 +16,7 @@ package internal
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
fedAPI "github.com/matrix-org/dendrite/federationapi/api"
|
||||
|
|
@ -29,14 +30,29 @@ import (
|
|||
|
||||
type testFedClient struct {
|
||||
fedAPI.FederationClient
|
||||
shouldFail bool
|
||||
queryCount uint
|
||||
queueDepth uint
|
||||
}
|
||||
|
||||
func (f *testFedClient) GetAsyncEvents(ctx context.Context, u gomatrixserverlib.UserID, prev gomatrixserverlib.RelayEntry, relayServer gomatrixserverlib.ServerName) (res gomatrixserverlib.RespGetAsyncEvents, err error) {
|
||||
return gomatrixserverlib.RespGetAsyncEvents{
|
||||
Txn: gomatrixserverlib.Transaction{},
|
||||
EntryID: 0,
|
||||
EntriesQueued: false,
|
||||
}, nil
|
||||
f.queryCount++
|
||||
if !f.shouldFail {
|
||||
res = gomatrixserverlib.RespGetAsyncEvents{
|
||||
Txn: gomatrixserverlib.Transaction{},
|
||||
EntryID: 0,
|
||||
}
|
||||
if f.queueDepth > 0 {
|
||||
res.EntriesQueued = true
|
||||
} else {
|
||||
res.EntriesQueued = false
|
||||
}
|
||||
f.queueDepth -= 1
|
||||
} else {
|
||||
err = fmt.Errorf("Error")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func TestPerformRelayServerSync(t *testing.T) {
|
||||
|
|
@ -64,5 +80,53 @@ func TestPerformRelayServerSync(t *testing.T) {
|
|||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
// TODO : called twice when queued
|
||||
// TODO : returns err if fed returns err
|
||||
func TestPerformRelayServerSyncFedError(t *testing.T) {
|
||||
testDB := storage.NewFakeRelayDatabase()
|
||||
db := shared.Database{
|
||||
Writer: sqlutil.NewDummyWriter(),
|
||||
RelayQueue: testDB,
|
||||
RelayQueueJSON: testDB,
|
||||
}
|
||||
|
||||
userID, err := gomatrixserverlib.NewUserID("@local:domain", false)
|
||||
assert.Nil(t, err, "Invalid userID")
|
||||
|
||||
fedClient := &testFedClient{shouldFail: true}
|
||||
relayAPI := NewRelayInternalAPI(
|
||||
&db, fedClient, nil, nil, nil, false, "",
|
||||
)
|
||||
|
||||
req := api.PerformRelayServerSyncRequest{
|
||||
UserID: *userID,
|
||||
RelayServer: gomatrixserverlib.ServerName("relay"),
|
||||
}
|
||||
res := api.PerformRelayServerSyncResponse{}
|
||||
err = relayAPI.PerformRelayServerSync(context.Background(), &req, &res)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestPerformRelayServerSyncRunsUntilQueueEmpty(t *testing.T) {
|
||||
testDB := storage.NewFakeRelayDatabase()
|
||||
db := shared.Database{
|
||||
Writer: sqlutil.NewDummyWriter(),
|
||||
RelayQueue: testDB,
|
||||
RelayQueueJSON: testDB,
|
||||
}
|
||||
|
||||
userID, err := gomatrixserverlib.NewUserID("@local:domain", false)
|
||||
assert.Nil(t, err, "Invalid userID")
|
||||
|
||||
fedClient := &testFedClient{queueDepth: 2}
|
||||
relayAPI := NewRelayInternalAPI(
|
||||
&db, fedClient, nil, nil, nil, false, "",
|
||||
)
|
||||
|
||||
req := api.PerformRelayServerSyncRequest{
|
||||
UserID: *userID,
|
||||
RelayServer: gomatrixserverlib.ServerName("relay"),
|
||||
}
|
||||
res := api.PerformRelayServerSyncResponse{}
|
||||
err = relayAPI.PerformRelayServerSync(context.Background(), &req, &res)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, uint(3), fedClient.queryCount)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,12 +35,12 @@ func TestGetAsyncEmptyDatabaseReturnsNothing(t *testing.T) {
|
|||
}
|
||||
httpReq := &http.Request{}
|
||||
userID, err := gomatrixserverlib.NewUserID("@local:domain", false)
|
||||
assert.Nil(t, err, "Invalid userID")
|
||||
assert.NoError(t, err, "Invalid userID")
|
||||
|
||||
transaction := createTransaction()
|
||||
|
||||
_, err = db.StoreAsyncTransaction(context.Background(), transaction)
|
||||
assert.Nil(t, err, "Failed to store transaction")
|
||||
assert.NoError(t, err, "Failed to store transaction")
|
||||
|
||||
relayAPI := internal.NewRelayInternalAPI(
|
||||
&db, nil, nil, nil, nil, false, "",
|
||||
|
|
@ -55,7 +55,7 @@ func TestGetAsyncEmptyDatabaseReturnsNothing(t *testing.T) {
|
|||
assert.Equal(t, gomatrixserverlib.Transaction{}, jsonResponse.Txn)
|
||||
|
||||
count, err := db.GetAsyncTransactionCount(context.Background(), *userID)
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Zero(t, count)
|
||||
}
|
||||
|
||||
|
|
@ -68,11 +68,11 @@ func TestGetAsyncReturnsSavedTransaction(t *testing.T) {
|
|||
}
|
||||
httpReq := &http.Request{}
|
||||
userID, err := gomatrixserverlib.NewUserID("@local:domain", false)
|
||||
assert.Nil(t, err, "Invalid userID")
|
||||
assert.NoError(t, err, "Invalid userID")
|
||||
|
||||
transaction := createTransaction()
|
||||
receipt, err := db.StoreAsyncTransaction(context.Background(), transaction)
|
||||
assert.Nil(t, err, "Failed to store transaction")
|
||||
assert.NoError(t, err, "Failed to store transaction")
|
||||
|
||||
err = db.AssociateAsyncTransactionWithDestinations(
|
||||
context.Background(),
|
||||
|
|
@ -81,7 +81,7 @@ func TestGetAsyncReturnsSavedTransaction(t *testing.T) {
|
|||
},
|
||||
transaction.TransactionID,
|
||||
receipt)
|
||||
assert.Nil(t, err, "Failed to associate transaction with user")
|
||||
assert.NoError(t, err, "Failed to associate transaction with user")
|
||||
|
||||
relayAPI := internal.NewRelayInternalAPI(
|
||||
&db, nil, nil, nil, nil, false, "",
|
||||
|
|
@ -105,7 +105,7 @@ func TestGetAsyncReturnsSavedTransaction(t *testing.T) {
|
|||
assert.Equal(t, gomatrixserverlib.Transaction{}, jsonResponse.Txn)
|
||||
|
||||
count, err := db.GetAsyncTransactionCount(context.Background(), *userID)
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Zero(t, count)
|
||||
}
|
||||
|
||||
|
|
@ -118,11 +118,11 @@ func TestGetAsyncReturnsMultipleSavedTransactions(t *testing.T) {
|
|||
}
|
||||
httpReq := &http.Request{}
|
||||
userID, err := gomatrixserverlib.NewUserID("@local:domain", false)
|
||||
assert.Nil(t, err, "Invalid userID")
|
||||
assert.NoError(t, err, "Invalid userID")
|
||||
|
||||
transaction := createTransaction()
|
||||
receipt, err := db.StoreAsyncTransaction(context.Background(), transaction)
|
||||
assert.Nil(t, err, "Failed to store transaction")
|
||||
assert.NoError(t, err, "Failed to store transaction")
|
||||
|
||||
err = db.AssociateAsyncTransactionWithDestinations(
|
||||
context.Background(),
|
||||
|
|
@ -131,11 +131,11 @@ func TestGetAsyncReturnsMultipleSavedTransactions(t *testing.T) {
|
|||
},
|
||||
transaction.TransactionID,
|
||||
receipt)
|
||||
assert.Nil(t, err, "Failed to associate transaction with user")
|
||||
assert.NoError(t, err, "Failed to associate transaction with user")
|
||||
|
||||
transaction2 := createTransaction()
|
||||
receipt2, err := db.StoreAsyncTransaction(context.Background(), transaction2)
|
||||
assert.Nil(t, err, "Failed to store transaction")
|
||||
assert.NoError(t, err, "Failed to store transaction")
|
||||
|
||||
err = db.AssociateAsyncTransactionWithDestinations(
|
||||
context.Background(),
|
||||
|
|
@ -144,7 +144,7 @@ func TestGetAsyncReturnsMultipleSavedTransactions(t *testing.T) {
|
|||
},
|
||||
transaction2.TransactionID,
|
||||
receipt2)
|
||||
assert.Nil(t, err, "Failed to associate transaction with user")
|
||||
assert.NoError(t, err, "Failed to associate transaction with user")
|
||||
|
||||
relayAPI := internal.NewRelayInternalAPI(
|
||||
&db, nil, nil, nil, nil, false, "",
|
||||
|
|
@ -176,6 +176,6 @@ func TestGetAsyncReturnsMultipleSavedTransactions(t *testing.T) {
|
|||
assert.Equal(t, gomatrixserverlib.Transaction{}, jsonResponse.Txn)
|
||||
|
||||
count, err := db.GetAsyncTransactionCount(context.Background(), *userID)
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Zero(t, count)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -57,7 +57,7 @@ func TestForwardEmptyReturnsOk(t *testing.T) {
|
|||
}
|
||||
httpReq := &http.Request{}
|
||||
userID, err := gomatrixserverlib.NewUserID("@local:domain", false)
|
||||
assert.Nil(t, err, "Invalid userID")
|
||||
assert.NoError(t, err, "Invalid userID")
|
||||
|
||||
txn := createTransaction()
|
||||
request := createFederationRequest(*userID, txn.TransactionID, txn.Origin, txn.Destination, txn)
|
||||
|
|
@ -80,7 +80,7 @@ func TestForwardBadJSONReturnsError(t *testing.T) {
|
|||
}
|
||||
httpReq := &http.Request{}
|
||||
userID, err := gomatrixserverlib.NewUserID("@local:domain", false)
|
||||
assert.Nil(t, err, "Invalid userID")
|
||||
assert.NoError(t, err, "Invalid userID")
|
||||
|
||||
type BadData struct {
|
||||
Field bool `json:"pdus"`
|
||||
|
|
@ -109,7 +109,7 @@ func TestForwardTooManyPDUsReturnsError(t *testing.T) {
|
|||
}
|
||||
httpReq := &http.Request{}
|
||||
userID, err := gomatrixserverlib.NewUserID("@local:domain", false)
|
||||
assert.Nil(t, err, "Invalid userID")
|
||||
assert.NoError(t, err, "Invalid userID")
|
||||
|
||||
type BadData struct {
|
||||
Field []json.RawMessage `json:"pdus"`
|
||||
|
|
@ -143,7 +143,7 @@ func TestForwardTooManyEDUsReturnsError(t *testing.T) {
|
|||
}
|
||||
httpReq := &http.Request{}
|
||||
userID, err := gomatrixserverlib.NewUserID("@local:domain", false)
|
||||
assert.Nil(t, err, "Invalid userID")
|
||||
assert.NoError(t, err, "Invalid userID")
|
||||
|
||||
type BadData struct {
|
||||
Field []gomatrixserverlib.EDU `json:"edus"`
|
||||
|
|
@ -177,7 +177,7 @@ func TestUniqueTransactionStoredInDatabase(t *testing.T) {
|
|||
}
|
||||
httpReq := &http.Request{}
|
||||
userID, err := gomatrixserverlib.NewUserID("@local:domain", false)
|
||||
assert.Nil(t, err, "Invalid userID")
|
||||
assert.NoError(t, err, "Invalid userID")
|
||||
|
||||
txn := createTransaction()
|
||||
request := createFederationRequest(*userID, txn.TransactionID, txn.Origin, txn.Destination, txn)
|
||||
|
|
@ -189,10 +189,10 @@ func TestUniqueTransactionStoredInDatabase(t *testing.T) {
|
|||
response := routing.ForwardAsync(
|
||||
httpReq, &request, &relayAPI, txn.TransactionID, *userID)
|
||||
transaction, _, err := db.GetAsyncTransaction(context.TODO(), *userID)
|
||||
assert.Nil(t, err, "Failed retrieving transaction")
|
||||
assert.NoError(t, err, "Failed retrieving transaction")
|
||||
|
||||
transactionCount, err := db.GetAsyncTransactionCount(context.TODO(), *userID)
|
||||
assert.Nil(t, err, "Failed retrieving transaction count")
|
||||
assert.NoError(t, err, "Failed retrieving transaction count")
|
||||
|
||||
assert.Equal(t, 200, response.Code)
|
||||
assert.Equal(t, int64(1), transactionCount)
|
||||
|
|
|
|||
Loading…
Reference in a new issue