2022-10-19 11:03:16 +01:00
|
|
|
// Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package queue
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2024-05-01 01:38:36 +01:00
|
|
|
"sync/atomic"
|
2022-10-19 11:03:16 +01:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-03-17 11:09:45 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal/caching"
|
2023-03-22 08:21:32 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2023-03-17 11:09:45 +00:00
|
|
|
"github.com/matrix-org/dendrite/test/testrig"
|
2023-04-06 09:55:01 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/fclient"
|
2023-04-19 15:50:33 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2022-10-19 11:03:16 +01:00
|
|
|
"gotest.tools/v3/poll"
|
|
|
|
|
2022-10-21 11:50:51 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2022-10-19 11:03:16 +01:00
|
|
|
"github.com/matrix-org/dendrite/federationapi/statistics"
|
|
|
|
"github.com/matrix-org/dendrite/federationapi/storage"
|
2023-04-27 12:54:20 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
2022-10-19 11:03:16 +01:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
|
|
|
"github.com/matrix-org/dendrite/setup/process"
|
|
|
|
"github.com/matrix-org/dendrite/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
func mustCreateFederationDatabase(t *testing.T, dbType test.DBType, realDatabase bool) (storage.Database, *process.ProcessContext, func()) {
|
|
|
|
if realDatabase {
|
|
|
|
// Real Database/s
|
2023-03-22 08:21:32 +00:00
|
|
|
cfg, processCtx, close := testrig.CreateConfig(t, dbType)
|
|
|
|
cm := sqlutil.NewConnectionManager(processCtx, cfg.Global.DatabaseOptions)
|
|
|
|
caches := caching.NewRistrettoCache(128*1024*1024, time.Hour, caching.DisableMetrics)
|
2022-10-19 11:03:16 +01:00
|
|
|
connStr, dbClose := test.PrepareDBConnectionString(t, dbType)
|
2023-03-22 08:21:32 +00:00
|
|
|
db, err := storage.NewDatabase(processCtx.Context(), cm, &config.DatabaseOptions{
|
2022-10-19 11:03:16 +01:00
|
|
|
ConnectionString: config.DataSource(connStr),
|
2023-03-22 08:21:32 +00:00
|
|
|
}, caches, cfg.Global.IsLocalServerName)
|
2022-10-19 11:03:16 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("NewDatabase returned %s", err)
|
|
|
|
}
|
2023-03-22 08:21:32 +00:00
|
|
|
return db, processCtx, func() {
|
|
|
|
close()
|
2022-10-19 11:03:16 +01:00
|
|
|
dbClose()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Fake Database
|
2023-01-23 17:55:12 +00:00
|
|
|
db := test.NewInMemoryFederationDatabase()
|
2023-03-17 11:09:45 +00:00
|
|
|
return db, process.NewProcessContext(), func() {}
|
2022-10-19 11:03:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type stubFederationClient struct {
|
2023-04-24 17:23:25 +01:00
|
|
|
fclient.FederationClient
|
2023-01-23 17:55:12 +00:00
|
|
|
shouldTxSucceed bool
|
|
|
|
shouldTxRelaySucceed bool
|
|
|
|
txCount atomic.Uint32
|
|
|
|
txRelayCount atomic.Uint32
|
2022-10-19 11:03:16 +01:00
|
|
|
}
|
|
|
|
|
2023-04-06 09:55:01 +01:00
|
|
|
func (f *stubFederationClient) SendTransaction(ctx context.Context, t gomatrixserverlib.Transaction) (res fclient.RespSend, err error) {
|
2022-10-19 11:03:16 +01:00
|
|
|
var result error
|
|
|
|
if !f.shouldTxSucceed {
|
|
|
|
result = fmt.Errorf("transaction failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
f.txCount.Add(1)
|
2023-04-06 09:55:01 +01:00
|
|
|
return fclient.RespSend{}, result
|
2022-10-19 11:03:16 +01:00
|
|
|
}
|
|
|
|
|
2023-04-19 15:50:33 +01:00
|
|
|
func (f *stubFederationClient) P2PSendTransactionToRelay(ctx context.Context, u spec.UserID, t gomatrixserverlib.Transaction, forwardingServer spec.ServerName) (res fclient.EmptyResp, err error) {
|
2023-01-23 17:55:12 +00:00
|
|
|
var result error
|
|
|
|
if !f.shouldTxRelaySucceed {
|
|
|
|
result = fmt.Errorf("relay transaction failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
f.txRelayCount.Add(1)
|
2023-04-06 09:55:01 +01:00
|
|
|
return fclient.EmptyResp{}, result
|
2023-01-23 17:55:12 +00:00
|
|
|
}
|
|
|
|
|
2023-04-27 12:54:20 +01:00
|
|
|
func mustCreatePDU(t *testing.T) *types.HeaderedEvent {
|
2022-10-19 11:03:16 +01:00
|
|
|
t.Helper()
|
2023-09-15 15:39:06 +01:00
|
|
|
content := `{"type":"m.room.message", "room_id":"!room:a"}`
|
2023-04-21 17:06:29 +01:00
|
|
|
ev, err := gomatrixserverlib.MustGetRoomVersion(gomatrixserverlib.RoomVersionV10).NewEventFromTrustedJSON([]byte(content), false)
|
2022-10-19 11:03:16 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create event: %v", err)
|
|
|
|
}
|
2023-05-02 15:03:16 +01:00
|
|
|
return &types.HeaderedEvent{PDU: ev}
|
2022-10-19 11:03:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func mustCreateEDU(t *testing.T) *gomatrixserverlib.EDU {
|
|
|
|
t.Helper()
|
2023-04-19 15:50:33 +01:00
|
|
|
return &gomatrixserverlib.EDU{Type: spec.MTyping}
|
2022-10-19 11:03:16 +01:00
|
|
|
}
|
|
|
|
|
2023-01-23 17:55:12 +00:00
|
|
|
func testSetup(failuresUntilBlacklist uint32, failuresUntilAssumedOffline uint32, shouldTxSucceed bool, shouldTxRelaySucceed bool, t *testing.T, dbType test.DBType, realDatabase bool) (storage.Database, *stubFederationClient, *OutgoingQueues, *process.ProcessContext, func()) {
|
2022-10-19 11:03:16 +01:00
|
|
|
db, processContext, close := mustCreateFederationDatabase(t, dbType, realDatabase)
|
|
|
|
|
|
|
|
fc := &stubFederationClient{
|
2023-01-23 17:55:12 +00:00
|
|
|
shouldTxSucceed: shouldTxSucceed,
|
|
|
|
shouldTxRelaySucceed: shouldTxRelaySucceed,
|
2024-05-01 01:38:36 +01:00
|
|
|
txCount: atomic.Uint32{},
|
|
|
|
txRelayCount: atomic.Uint32{},
|
2022-10-19 11:03:16 +01:00
|
|
|
}
|
2023-01-23 17:55:12 +00:00
|
|
|
|
2024-02-28 19:59:34 +00:00
|
|
|
stats := statistics.NewStatistics(db, failuresUntilBlacklist, failuresUntilAssumedOffline, false)
|
2023-04-06 09:55:01 +01:00
|
|
|
signingInfo := []*fclient.SigningIdentity{
|
2022-11-15 15:05:23 +00:00
|
|
|
{
|
2022-11-11 16:41:37 +00:00
|
|
|
KeyID: "ed21019:auto",
|
|
|
|
PrivateKey: test.PrivateKeyA,
|
|
|
|
ServerName: "localhost",
|
|
|
|
},
|
2022-10-19 11:03:16 +01:00
|
|
|
}
|
2023-11-22 14:38:04 +00:00
|
|
|
queues := NewOutgoingQueues(db, processContext, false, "localhost", fc, &stats, signingInfo)
|
2022-10-19 11:03:16 +01:00
|
|
|
|
|
|
|
return db, fc, queues, processContext, close
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSendPDUOnSuccessRemovedFromDB(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
failuresUntilBlacklist := uint32(16)
|
2023-04-19 15:50:33 +01:00
|
|
|
destination := spec.ServerName("remotehost")
|
2023-01-23 17:55:12 +00:00
|
|
|
db, fc, queues, pc, close := testSetup(failuresUntilBlacklist, failuresUntilBlacklist+1, true, false, t, test.DBTypeSQLite, false)
|
2022-10-19 11:03:16 +01:00
|
|
|
defer close()
|
|
|
|
defer func() {
|
|
|
|
pc.ShutdownDendrite()
|
|
|
|
<-pc.WaitForShutdown()
|
|
|
|
}()
|
|
|
|
|
|
|
|
ev := mustCreatePDU(t)
|
2023-04-19 15:50:33 +01:00
|
|
|
err := queues.SendEvent(ev, "localhost", []spec.ServerName{destination})
|
2022-10-19 11:03:16 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
check := func(log poll.LogT) poll.Result {
|
|
|
|
if fc.txCount.Load() == 1 {
|
|
|
|
data, dbErr := db.GetPendingPDUs(pc.Context(), destination, 100)
|
|
|
|
assert.NoError(t, dbErr)
|
|
|
|
if len(data) == 0 {
|
|
|
|
return poll.Success()
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for event to be removed from database. Currently present PDU: %d", len(data))
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for more send attempts before checking database. Currently %d", fc.txCount.Load())
|
|
|
|
}
|
|
|
|
poll.WaitOn(t, check, poll.WithTimeout(5*time.Second), poll.WithDelay(100*time.Millisecond))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSendEDUOnSuccessRemovedFromDB(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
failuresUntilBlacklist := uint32(16)
|
2023-04-19 15:50:33 +01:00
|
|
|
destination := spec.ServerName("remotehost")
|
2023-01-23 17:55:12 +00:00
|
|
|
db, fc, queues, pc, close := testSetup(failuresUntilBlacklist, failuresUntilBlacklist+1, true, false, t, test.DBTypeSQLite, false)
|
2022-10-19 11:03:16 +01:00
|
|
|
defer close()
|
|
|
|
defer func() {
|
|
|
|
pc.ShutdownDendrite()
|
|
|
|
<-pc.WaitForShutdown()
|
|
|
|
}()
|
|
|
|
|
|
|
|
ev := mustCreateEDU(t)
|
2023-04-19 15:50:33 +01:00
|
|
|
err := queues.SendEDU(ev, "localhost", []spec.ServerName{destination})
|
2022-10-19 11:03:16 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
check := func(log poll.LogT) poll.Result {
|
|
|
|
if fc.txCount.Load() == 1 {
|
|
|
|
data, dbErr := db.GetPendingEDUs(pc.Context(), destination, 100)
|
|
|
|
assert.NoError(t, dbErr)
|
|
|
|
if len(data) == 0 {
|
|
|
|
return poll.Success()
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for event to be removed from database. Currently present EDU: %d", len(data))
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for more send attempts before checking database. Currently %d", fc.txCount.Load())
|
|
|
|
}
|
|
|
|
poll.WaitOn(t, check, poll.WithTimeout(5*time.Second), poll.WithDelay(100*time.Millisecond))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSendPDUOnFailStoredInDB(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
failuresUntilBlacklist := uint32(16)
|
2023-04-19 15:50:33 +01:00
|
|
|
destination := spec.ServerName("remotehost")
|
2023-01-23 17:55:12 +00:00
|
|
|
db, fc, queues, pc, close := testSetup(failuresUntilBlacklist, failuresUntilBlacklist+1, false, false, t, test.DBTypeSQLite, false)
|
2022-10-19 11:03:16 +01:00
|
|
|
defer close()
|
|
|
|
defer func() {
|
|
|
|
pc.ShutdownDendrite()
|
|
|
|
<-pc.WaitForShutdown()
|
|
|
|
}()
|
|
|
|
|
|
|
|
ev := mustCreatePDU(t)
|
2023-04-19 15:50:33 +01:00
|
|
|
err := queues.SendEvent(ev, "localhost", []spec.ServerName{destination})
|
2022-10-19 11:03:16 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
check := func(log poll.LogT) poll.Result {
|
|
|
|
// Wait for 2 backoff attempts to ensure there was adequate time to attempt sending
|
|
|
|
if fc.txCount.Load() >= 2 {
|
|
|
|
data, dbErr := db.GetPendingPDUs(pc.Context(), destination, 100)
|
|
|
|
assert.NoError(t, dbErr)
|
|
|
|
if len(data) == 1 {
|
|
|
|
return poll.Success()
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for event to be added to database. Currently present PDU: %d", len(data))
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for more send attempts before checking database. Currently %d", fc.txCount.Load())
|
|
|
|
}
|
|
|
|
poll.WaitOn(t, check, poll.WithTimeout(5*time.Second), poll.WithDelay(100*time.Millisecond))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSendEDUOnFailStoredInDB(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
failuresUntilBlacklist := uint32(16)
|
2023-04-19 15:50:33 +01:00
|
|
|
destination := spec.ServerName("remotehost")
|
2023-01-23 17:55:12 +00:00
|
|
|
db, fc, queues, pc, close := testSetup(failuresUntilBlacklist, failuresUntilBlacklist+1, false, false, t, test.DBTypeSQLite, false)
|
2022-10-19 11:03:16 +01:00
|
|
|
defer close()
|
|
|
|
defer func() {
|
|
|
|
pc.ShutdownDendrite()
|
|
|
|
<-pc.WaitForShutdown()
|
|
|
|
}()
|
|
|
|
|
|
|
|
ev := mustCreateEDU(t)
|
2023-04-19 15:50:33 +01:00
|
|
|
err := queues.SendEDU(ev, "localhost", []spec.ServerName{destination})
|
2022-10-19 11:03:16 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
check := func(log poll.LogT) poll.Result {
|
|
|
|
// Wait for 2 backoff attempts to ensure there was adequate time to attempt sending
|
|
|
|
if fc.txCount.Load() >= 2 {
|
|
|
|
data, dbErr := db.GetPendingEDUs(pc.Context(), destination, 100)
|
|
|
|
assert.NoError(t, dbErr)
|
|
|
|
if len(data) == 1 {
|
|
|
|
return poll.Success()
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for event to be added to database. Currently present EDU: %d", len(data))
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for more send attempts before checking database. Currently %d", fc.txCount.Load())
|
|
|
|
}
|
|
|
|
poll.WaitOn(t, check, poll.WithTimeout(5*time.Second), poll.WithDelay(100*time.Millisecond))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSendPDUAgainDoesntInterruptBackoff(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
failuresUntilBlacklist := uint32(16)
|
2023-04-19 15:50:33 +01:00
|
|
|
destination := spec.ServerName("remotehost")
|
2023-01-23 17:55:12 +00:00
|
|
|
db, fc, queues, pc, close := testSetup(failuresUntilBlacklist, failuresUntilBlacklist+1, false, false, t, test.DBTypeSQLite, false)
|
2022-10-19 11:03:16 +01:00
|
|
|
defer close()
|
|
|
|
defer func() {
|
|
|
|
pc.ShutdownDendrite()
|
|
|
|
<-pc.WaitForShutdown()
|
|
|
|
}()
|
|
|
|
|
|
|
|
ev := mustCreatePDU(t)
|
2023-04-19 15:50:33 +01:00
|
|
|
err := queues.SendEvent(ev, "localhost", []spec.ServerName{destination})
|
2022-10-19 11:03:16 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
check := func(log poll.LogT) poll.Result {
|
|
|
|
// Wait for 2 backoff attempts to ensure there was adequate time to attempt sending
|
|
|
|
if fc.txCount.Load() >= 2 {
|
|
|
|
data, dbErr := db.GetPendingPDUs(pc.Context(), destination, 100)
|
|
|
|
assert.NoError(t, dbErr)
|
|
|
|
if len(data) == 1 {
|
|
|
|
return poll.Success()
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for event to be added to database. Currently present PDU: %d", len(data))
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for more send attempts before checking database. Currently %d", fc.txCount.Load())
|
|
|
|
}
|
|
|
|
poll.WaitOn(t, check, poll.WithTimeout(5*time.Second), poll.WithDelay(100*time.Millisecond))
|
|
|
|
|
|
|
|
fc.shouldTxSucceed = true
|
|
|
|
ev = mustCreatePDU(t)
|
2023-04-19 15:50:33 +01:00
|
|
|
err = queues.SendEvent(ev, "localhost", []spec.ServerName{destination})
|
2022-10-19 11:03:16 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
pollEnd := time.Now().Add(1 * time.Second)
|
|
|
|
immediateCheck := func(log poll.LogT) poll.Result {
|
|
|
|
data, dbErr := db.GetPendingPDUs(pc.Context(), destination, 100)
|
|
|
|
assert.NoError(t, dbErr)
|
|
|
|
if len(data) == 0 {
|
|
|
|
return poll.Error(fmt.Errorf("The backoff was interrupted early"))
|
|
|
|
}
|
|
|
|
if time.Now().After(pollEnd) {
|
|
|
|
// Allow more than enough time for the backoff to be interrupted before
|
|
|
|
// reporting that it wasn't.
|
|
|
|
return poll.Success()
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for events to be removed from database. Currently present PDU: %d", len(data))
|
|
|
|
}
|
|
|
|
poll.WaitOn(t, immediateCheck, poll.WithTimeout(2*time.Second), poll.WithDelay(100*time.Millisecond))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSendEDUAgainDoesntInterruptBackoff(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
failuresUntilBlacklist := uint32(16)
|
2023-04-19 15:50:33 +01:00
|
|
|
destination := spec.ServerName("remotehost")
|
2023-01-23 17:55:12 +00:00
|
|
|
db, fc, queues, pc, close := testSetup(failuresUntilBlacklist, failuresUntilBlacklist+1, false, false, t, test.DBTypeSQLite, false)
|
2022-10-19 11:03:16 +01:00
|
|
|
defer close()
|
|
|
|
defer func() {
|
|
|
|
pc.ShutdownDendrite()
|
|
|
|
<-pc.WaitForShutdown()
|
|
|
|
}()
|
|
|
|
|
|
|
|
ev := mustCreateEDU(t)
|
2023-04-19 15:50:33 +01:00
|
|
|
err := queues.SendEDU(ev, "localhost", []spec.ServerName{destination})
|
2022-10-19 11:03:16 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
check := func(log poll.LogT) poll.Result {
|
|
|
|
// Wait for 2 backoff attempts to ensure there was adequate time to attempt sending
|
|
|
|
if fc.txCount.Load() >= 2 {
|
|
|
|
data, dbErr := db.GetPendingEDUs(pc.Context(), destination, 100)
|
|
|
|
assert.NoError(t, dbErr)
|
|
|
|
if len(data) == 1 {
|
|
|
|
return poll.Success()
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for event to be added to database. Currently present EDU: %d", len(data))
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for more send attempts before checking database. Currently %d", fc.txCount.Load())
|
|
|
|
}
|
|
|
|
poll.WaitOn(t, check, poll.WithTimeout(5*time.Second), poll.WithDelay(100*time.Millisecond))
|
|
|
|
|
|
|
|
fc.shouldTxSucceed = true
|
|
|
|
ev = mustCreateEDU(t)
|
2023-04-19 15:50:33 +01:00
|
|
|
err = queues.SendEDU(ev, "localhost", []spec.ServerName{destination})
|
2022-10-19 11:03:16 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
pollEnd := time.Now().Add(1 * time.Second)
|
|
|
|
immediateCheck := func(log poll.LogT) poll.Result {
|
|
|
|
data, dbErr := db.GetPendingEDUs(pc.Context(), destination, 100)
|
|
|
|
assert.NoError(t, dbErr)
|
|
|
|
if len(data) == 0 {
|
|
|
|
return poll.Error(fmt.Errorf("The backoff was interrupted early"))
|
|
|
|
}
|
|
|
|
if time.Now().After(pollEnd) {
|
|
|
|
// Allow more than enough time for the backoff to be interrupted before
|
|
|
|
// reporting that it wasn't.
|
|
|
|
return poll.Success()
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for events to be removed from database. Currently present EDU: %d", len(data))
|
|
|
|
}
|
|
|
|
poll.WaitOn(t, immediateCheck, poll.WithTimeout(2*time.Second), poll.WithDelay(100*time.Millisecond))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSendPDUMultipleFailuresBlacklisted(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
failuresUntilBlacklist := uint32(2)
|
2023-04-19 15:50:33 +01:00
|
|
|
destination := spec.ServerName("remotehost")
|
2023-01-23 17:55:12 +00:00
|
|
|
db, fc, queues, pc, close := testSetup(failuresUntilBlacklist, failuresUntilBlacklist+1, false, false, t, test.DBTypeSQLite, false)
|
2022-10-19 11:03:16 +01:00
|
|
|
defer close()
|
|
|
|
defer func() {
|
|
|
|
pc.ShutdownDendrite()
|
|
|
|
<-pc.WaitForShutdown()
|
|
|
|
}()
|
|
|
|
|
|
|
|
ev := mustCreatePDU(t)
|
2023-04-19 15:50:33 +01:00
|
|
|
err := queues.SendEvent(ev, "localhost", []spec.ServerName{destination})
|
2022-10-19 11:03:16 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
check := func(log poll.LogT) poll.Result {
|
|
|
|
if fc.txCount.Load() == failuresUntilBlacklist {
|
|
|
|
data, dbErr := db.GetPendingPDUs(pc.Context(), destination, 100)
|
|
|
|
assert.NoError(t, dbErr)
|
|
|
|
if len(data) == 1 {
|
|
|
|
if val, _ := db.IsServerBlacklisted(destination); val {
|
|
|
|
return poll.Success()
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for server to be blacklisted")
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for event to be added to database. Currently present PDU: %d", len(data))
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for more send attempts before checking database. Currently %d", fc.txCount.Load())
|
|
|
|
}
|
|
|
|
poll.WaitOn(t, check, poll.WithTimeout(5*time.Second), poll.WithDelay(100*time.Millisecond))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSendEDUMultipleFailuresBlacklisted(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
failuresUntilBlacklist := uint32(2)
|
2023-04-19 15:50:33 +01:00
|
|
|
destination := spec.ServerName("remotehost")
|
2023-01-23 17:55:12 +00:00
|
|
|
db, fc, queues, pc, close := testSetup(failuresUntilBlacklist, failuresUntilBlacklist+1, false, false, t, test.DBTypeSQLite, false)
|
2022-10-19 11:03:16 +01:00
|
|
|
defer close()
|
|
|
|
defer func() {
|
|
|
|
pc.ShutdownDendrite()
|
|
|
|
<-pc.WaitForShutdown()
|
|
|
|
}()
|
|
|
|
|
|
|
|
ev := mustCreateEDU(t)
|
2023-04-19 15:50:33 +01:00
|
|
|
err := queues.SendEDU(ev, "localhost", []spec.ServerName{destination})
|
2022-10-19 11:03:16 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
check := func(log poll.LogT) poll.Result {
|
|
|
|
if fc.txCount.Load() == failuresUntilBlacklist {
|
|
|
|
data, dbErr := db.GetPendingEDUs(pc.Context(), destination, 100)
|
|
|
|
assert.NoError(t, dbErr)
|
|
|
|
if len(data) == 1 {
|
|
|
|
if val, _ := db.IsServerBlacklisted(destination); val {
|
|
|
|
return poll.Success()
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for server to be blacklisted")
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for event to be added to database. Currently present EDU: %d", len(data))
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for more send attempts before checking database. Currently %d", fc.txCount.Load())
|
|
|
|
}
|
|
|
|
poll.WaitOn(t, check, poll.WithTimeout(5*time.Second), poll.WithDelay(100*time.Millisecond))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSendPDUBlacklistedWithPriorExternalFailure(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
failuresUntilBlacklist := uint32(2)
|
2023-04-19 15:50:33 +01:00
|
|
|
destination := spec.ServerName("remotehost")
|
2023-01-23 17:55:12 +00:00
|
|
|
db, fc, queues, pc, close := testSetup(failuresUntilBlacklist, failuresUntilBlacklist+1, false, false, t, test.DBTypeSQLite, false)
|
2022-10-19 11:03:16 +01:00
|
|
|
defer close()
|
|
|
|
defer func() {
|
|
|
|
pc.ShutdownDendrite()
|
|
|
|
<-pc.WaitForShutdown()
|
|
|
|
}()
|
|
|
|
|
|
|
|
queues.statistics.ForServer(destination).Failure()
|
|
|
|
|
|
|
|
ev := mustCreatePDU(t)
|
2023-04-19 15:50:33 +01:00
|
|
|
err := queues.SendEvent(ev, "localhost", []spec.ServerName{destination})
|
2022-10-19 11:03:16 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
check := func(log poll.LogT) poll.Result {
|
|
|
|
if fc.txCount.Load() == failuresUntilBlacklist {
|
|
|
|
data, dbErr := db.GetPendingPDUs(pc.Context(), destination, 100)
|
|
|
|
assert.NoError(t, dbErr)
|
|
|
|
if len(data) == 1 {
|
|
|
|
if val, _ := db.IsServerBlacklisted(destination); val {
|
|
|
|
return poll.Success()
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for server to be blacklisted")
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for event to be added to database. Currently present PDU: %d", len(data))
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for more send attempts before checking database. Currently %d", fc.txCount.Load())
|
|
|
|
}
|
|
|
|
poll.WaitOn(t, check, poll.WithTimeout(5*time.Second), poll.WithDelay(100*time.Millisecond))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSendEDUBlacklistedWithPriorExternalFailure(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
failuresUntilBlacklist := uint32(2)
|
2023-04-19 15:50:33 +01:00
|
|
|
destination := spec.ServerName("remotehost")
|
2023-01-23 17:55:12 +00:00
|
|
|
db, fc, queues, pc, close := testSetup(failuresUntilBlacklist, failuresUntilBlacklist+1, false, false, t, test.DBTypeSQLite, false)
|
2022-10-19 11:03:16 +01:00
|
|
|
defer close()
|
|
|
|
defer func() {
|
|
|
|
pc.ShutdownDendrite()
|
|
|
|
<-pc.WaitForShutdown()
|
|
|
|
}()
|
|
|
|
|
|
|
|
queues.statistics.ForServer(destination).Failure()
|
|
|
|
|
|
|
|
ev := mustCreateEDU(t)
|
2023-04-19 15:50:33 +01:00
|
|
|
err := queues.SendEDU(ev, "localhost", []spec.ServerName{destination})
|
2022-10-19 11:03:16 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
check := func(log poll.LogT) poll.Result {
|
|
|
|
if fc.txCount.Load() == failuresUntilBlacklist {
|
|
|
|
data, dbErr := db.GetPendingEDUs(pc.Context(), destination, 100)
|
|
|
|
assert.NoError(t, dbErr)
|
|
|
|
if len(data) == 1 {
|
|
|
|
if val, _ := db.IsServerBlacklisted(destination); val {
|
|
|
|
return poll.Success()
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for server to be blacklisted")
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for event to be added to database. Currently present EDU: %d", len(data))
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for more send attempts before checking database. Currently %d", fc.txCount.Load())
|
|
|
|
}
|
|
|
|
poll.WaitOn(t, check, poll.WithTimeout(5*time.Second), poll.WithDelay(100*time.Millisecond))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRetryServerSendsPDUSuccessfully(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
failuresUntilBlacklist := uint32(1)
|
2023-04-19 15:50:33 +01:00
|
|
|
destination := spec.ServerName("remotehost")
|
2023-01-23 17:55:12 +00:00
|
|
|
db, fc, queues, pc, close := testSetup(failuresUntilBlacklist, failuresUntilBlacklist+1, false, false, t, test.DBTypeSQLite, false)
|
2022-10-19 11:03:16 +01:00
|
|
|
defer close()
|
|
|
|
defer func() {
|
|
|
|
pc.ShutdownDendrite()
|
|
|
|
<-pc.WaitForShutdown()
|
|
|
|
}()
|
|
|
|
|
|
|
|
// NOTE : getQueue before sending event to ensure we grab the same queue reference
|
|
|
|
// before it is blacklisted and deleted.
|
|
|
|
dest := queues.getQueue(destination)
|
|
|
|
ev := mustCreatePDU(t)
|
2023-04-19 15:50:33 +01:00
|
|
|
err := queues.SendEvent(ev, "localhost", []spec.ServerName{destination})
|
2022-10-19 11:03:16 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
checkBlacklisted := func(log poll.LogT) poll.Result {
|
|
|
|
if fc.txCount.Load() == failuresUntilBlacklist {
|
|
|
|
data, dbErr := db.GetPendingPDUs(pc.Context(), destination, 100)
|
|
|
|
assert.NoError(t, dbErr)
|
|
|
|
if len(data) == 1 {
|
|
|
|
if val, _ := db.IsServerBlacklisted(destination); val {
|
|
|
|
if !dest.running.Load() {
|
|
|
|
return poll.Success()
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for queue to stop completely")
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for server to be blacklisted")
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for event to be added to database. Currently present PDU: %d", len(data))
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for more send attempts before checking database. Currently %d", fc.txCount.Load())
|
|
|
|
}
|
|
|
|
poll.WaitOn(t, checkBlacklisted, poll.WithTimeout(5*time.Second), poll.WithDelay(100*time.Millisecond))
|
|
|
|
|
|
|
|
fc.shouldTxSucceed = true
|
2023-01-23 17:55:12 +00:00
|
|
|
wasBlacklisted := dest.statistics.MarkServerAlive()
|
|
|
|
queues.RetryServer(destination, wasBlacklisted)
|
2022-10-19 11:03:16 +01:00
|
|
|
checkRetry := func(log poll.LogT) poll.Result {
|
|
|
|
data, dbErr := db.GetPendingPDUs(pc.Context(), destination, 100)
|
|
|
|
assert.NoError(t, dbErr)
|
|
|
|
if len(data) == 0 {
|
|
|
|
return poll.Success()
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for event to be removed from database. Currently present PDU: %d", len(data))
|
|
|
|
}
|
|
|
|
poll.WaitOn(t, checkRetry, poll.WithTimeout(5*time.Second), poll.WithDelay(100*time.Millisecond))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRetryServerSendsEDUSuccessfully(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
failuresUntilBlacklist := uint32(1)
|
2023-04-19 15:50:33 +01:00
|
|
|
destination := spec.ServerName("remotehost")
|
2023-01-23 17:55:12 +00:00
|
|
|
db, fc, queues, pc, close := testSetup(failuresUntilBlacklist, failuresUntilBlacklist+1, false, false, t, test.DBTypeSQLite, false)
|
2022-10-19 11:03:16 +01:00
|
|
|
defer close()
|
|
|
|
defer func() {
|
|
|
|
pc.ShutdownDendrite()
|
|
|
|
<-pc.WaitForShutdown()
|
|
|
|
}()
|
|
|
|
|
|
|
|
// NOTE : getQueue before sending event to ensure we grab the same queue reference
|
|
|
|
// before it is blacklisted and deleted.
|
|
|
|
dest := queues.getQueue(destination)
|
|
|
|
ev := mustCreateEDU(t)
|
2023-04-19 15:50:33 +01:00
|
|
|
err := queues.SendEDU(ev, "localhost", []spec.ServerName{destination})
|
2022-10-19 11:03:16 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
checkBlacklisted := func(log poll.LogT) poll.Result {
|
|
|
|
if fc.txCount.Load() == failuresUntilBlacklist {
|
|
|
|
data, dbErr := db.GetPendingEDUs(pc.Context(), destination, 100)
|
|
|
|
assert.NoError(t, dbErr)
|
|
|
|
if len(data) == 1 {
|
|
|
|
if val, _ := db.IsServerBlacklisted(destination); val {
|
|
|
|
if !dest.running.Load() {
|
|
|
|
return poll.Success()
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for queue to stop completely")
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for server to be blacklisted")
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for event to be added to database. Currently present EDU: %d", len(data))
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for more send attempts before checking database. Currently %d", fc.txCount.Load())
|
|
|
|
}
|
|
|
|
poll.WaitOn(t, checkBlacklisted, poll.WithTimeout(5*time.Second), poll.WithDelay(100*time.Millisecond))
|
|
|
|
|
|
|
|
fc.shouldTxSucceed = true
|
2023-01-23 17:55:12 +00:00
|
|
|
wasBlacklisted := dest.statistics.MarkServerAlive()
|
|
|
|
queues.RetryServer(destination, wasBlacklisted)
|
2022-10-19 11:03:16 +01:00
|
|
|
checkRetry := func(log poll.LogT) poll.Result {
|
|
|
|
data, dbErr := db.GetPendingEDUs(pc.Context(), destination, 100)
|
|
|
|
assert.NoError(t, dbErr)
|
|
|
|
if len(data) == 0 {
|
|
|
|
return poll.Success()
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for event to be removed from database. Currently present EDU: %d", len(data))
|
|
|
|
}
|
|
|
|
poll.WaitOn(t, checkRetry, poll.WithTimeout(5*time.Second), poll.WithDelay(100*time.Millisecond))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSendPDUBatches(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
failuresUntilBlacklist := uint32(16)
|
2023-04-19 15:50:33 +01:00
|
|
|
destination := spec.ServerName("remotehost")
|
2022-10-19 11:03:16 +01:00
|
|
|
|
|
|
|
// test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
|
|
|
|
// db, fc, queues, pc, close := testSetup(failuresUntilBlacklist, true, t, dbType, true)
|
2023-01-23 17:55:12 +00:00
|
|
|
db, fc, queues, pc, close := testSetup(failuresUntilBlacklist, failuresUntilBlacklist+1, true, false, t, test.DBTypeSQLite, false)
|
2022-10-19 11:03:16 +01:00
|
|
|
defer close()
|
|
|
|
defer func() {
|
|
|
|
pc.ShutdownDendrite()
|
|
|
|
<-pc.WaitForShutdown()
|
|
|
|
}()
|
|
|
|
|
2023-04-19 15:50:33 +01:00
|
|
|
destinations := map[spec.ServerName]struct{}{destination: {}}
|
2022-10-19 11:03:16 +01:00
|
|
|
// Populate database with > maxPDUsPerTransaction
|
|
|
|
pduMultiplier := uint32(3)
|
|
|
|
for i := 0; i < maxPDUsPerTransaction*int(pduMultiplier); i++ {
|
|
|
|
ev := mustCreatePDU(t)
|
|
|
|
headeredJSON, _ := json.Marshal(ev)
|
|
|
|
nid, _ := db.StoreJSON(pc.Context(), string(headeredJSON))
|
2022-10-21 11:50:51 +01:00
|
|
|
err := db.AssociatePDUWithDestinations(pc.Context(), destinations, nid)
|
|
|
|
assert.NoError(t, err, "failed to associate PDU with destinations")
|
2022-10-19 11:03:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ev := mustCreatePDU(t)
|
2023-04-19 15:50:33 +01:00
|
|
|
err := queues.SendEvent(ev, "localhost", []spec.ServerName{destination})
|
2022-10-19 11:03:16 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
check := func(log poll.LogT) poll.Result {
|
|
|
|
if fc.txCount.Load() == pduMultiplier+1 { // +1 for the extra SendEvent()
|
|
|
|
data, dbErr := db.GetPendingPDUs(pc.Context(), destination, 200)
|
|
|
|
assert.NoError(t, dbErr)
|
|
|
|
if len(data) == 0 {
|
|
|
|
return poll.Success()
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for all events to be removed from database. Currently present PDU: %d", len(data))
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for the right amount of send attempts before checking database. Currently %d", fc.txCount.Load())
|
|
|
|
}
|
|
|
|
poll.WaitOn(t, check, poll.WithTimeout(5*time.Second), poll.WithDelay(100*time.Millisecond))
|
|
|
|
// })
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSendEDUBatches(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
failuresUntilBlacklist := uint32(16)
|
2023-04-19 15:50:33 +01:00
|
|
|
destination := spec.ServerName("remotehost")
|
2022-10-19 11:03:16 +01:00
|
|
|
|
|
|
|
// test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
|
|
|
|
// db, fc, queues, pc, close := testSetup(failuresUntilBlacklist, true, t, dbType, true)
|
2023-01-23 17:55:12 +00:00
|
|
|
db, fc, queues, pc, close := testSetup(failuresUntilBlacklist, failuresUntilBlacklist+1, true, false, t, test.DBTypeSQLite, false)
|
2022-10-19 11:03:16 +01:00
|
|
|
defer close()
|
|
|
|
defer func() {
|
|
|
|
pc.ShutdownDendrite()
|
|
|
|
<-pc.WaitForShutdown()
|
|
|
|
}()
|
|
|
|
|
2023-04-19 15:50:33 +01:00
|
|
|
destinations := map[spec.ServerName]struct{}{destination: {}}
|
2022-10-19 11:03:16 +01:00
|
|
|
// Populate database with > maxEDUsPerTransaction
|
|
|
|
eduMultiplier := uint32(3)
|
|
|
|
for i := 0; i < maxEDUsPerTransaction*int(eduMultiplier); i++ {
|
|
|
|
ev := mustCreateEDU(t)
|
|
|
|
ephemeralJSON, _ := json.Marshal(ev)
|
|
|
|
nid, _ := db.StoreJSON(pc.Context(), string(ephemeralJSON))
|
2022-10-21 11:50:51 +01:00
|
|
|
err := db.AssociateEDUWithDestinations(pc.Context(), destinations, nid, ev.Type, nil)
|
|
|
|
assert.NoError(t, err, "failed to associate EDU with destinations")
|
2022-10-19 11:03:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ev := mustCreateEDU(t)
|
2023-04-19 15:50:33 +01:00
|
|
|
err := queues.SendEDU(ev, "localhost", []spec.ServerName{destination})
|
2022-10-19 11:03:16 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
check := func(log poll.LogT) poll.Result {
|
|
|
|
if fc.txCount.Load() == eduMultiplier+1 { // +1 for the extra SendEvent()
|
|
|
|
data, dbErr := db.GetPendingEDUs(pc.Context(), destination, 200)
|
|
|
|
assert.NoError(t, dbErr)
|
|
|
|
if len(data) == 0 {
|
|
|
|
return poll.Success()
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for all events to be removed from database. Currently present EDU: %d", len(data))
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for the right amount of send attempts before checking database. Currently %d", fc.txCount.Load())
|
|
|
|
}
|
|
|
|
poll.WaitOn(t, check, poll.WithTimeout(5*time.Second), poll.WithDelay(100*time.Millisecond))
|
|
|
|
// })
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSendPDUAndEDUBatches(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
failuresUntilBlacklist := uint32(16)
|
2023-04-19 15:50:33 +01:00
|
|
|
destination := spec.ServerName("remotehost")
|
2022-10-19 11:03:16 +01:00
|
|
|
|
|
|
|
// test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
|
|
|
|
// db, fc, queues, pc, close := testSetup(failuresUntilBlacklist, true, t, dbType, true)
|
2023-01-23 17:55:12 +00:00
|
|
|
db, fc, queues, pc, close := testSetup(failuresUntilBlacklist, failuresUntilBlacklist+1, true, false, t, test.DBTypeSQLite, false)
|
2022-10-19 11:03:16 +01:00
|
|
|
defer close()
|
|
|
|
defer func() {
|
|
|
|
pc.ShutdownDendrite()
|
|
|
|
<-pc.WaitForShutdown()
|
|
|
|
}()
|
|
|
|
|
2023-04-19 15:50:33 +01:00
|
|
|
destinations := map[spec.ServerName]struct{}{destination: {}}
|
2022-10-19 11:03:16 +01:00
|
|
|
// Populate database with > maxEDUsPerTransaction
|
|
|
|
multiplier := uint32(3)
|
|
|
|
for i := 0; i < maxPDUsPerTransaction*int(multiplier)+1; i++ {
|
|
|
|
ev := mustCreatePDU(t)
|
|
|
|
headeredJSON, _ := json.Marshal(ev)
|
|
|
|
nid, _ := db.StoreJSON(pc.Context(), string(headeredJSON))
|
2022-10-21 11:50:51 +01:00
|
|
|
err := db.AssociatePDUWithDestinations(pc.Context(), destinations, nid)
|
|
|
|
assert.NoError(t, err, "failed to associate PDU with destinations")
|
2022-10-19 11:03:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < maxEDUsPerTransaction*int(multiplier); i++ {
|
|
|
|
ev := mustCreateEDU(t)
|
|
|
|
ephemeralJSON, _ := json.Marshal(ev)
|
|
|
|
nid, _ := db.StoreJSON(pc.Context(), string(ephemeralJSON))
|
2022-10-21 11:50:51 +01:00
|
|
|
err := db.AssociateEDUWithDestinations(pc.Context(), destinations, nid, ev.Type, nil)
|
|
|
|
assert.NoError(t, err, "failed to associate EDU with destinations")
|
2022-10-19 11:03:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ev := mustCreateEDU(t)
|
2023-04-19 15:50:33 +01:00
|
|
|
err := queues.SendEDU(ev, "localhost", []spec.ServerName{destination})
|
2022-10-19 11:03:16 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
check := func(log poll.LogT) poll.Result {
|
|
|
|
if fc.txCount.Load() == multiplier+1 { // +1 for the extra SendEvent()
|
|
|
|
pduData, dbErrPDU := db.GetPendingPDUs(pc.Context(), destination, 200)
|
|
|
|
assert.NoError(t, dbErrPDU)
|
|
|
|
eduData, dbErrEDU := db.GetPendingEDUs(pc.Context(), destination, 200)
|
|
|
|
assert.NoError(t, dbErrEDU)
|
|
|
|
if len(pduData) == 0 && len(eduData) == 0 {
|
|
|
|
return poll.Success()
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for all events to be removed from database. Currently present PDU: %d EDU: %d", len(pduData), len(eduData))
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for the right amount of send attempts before checking database. Currently %d", fc.txCount.Load())
|
|
|
|
}
|
|
|
|
poll.WaitOn(t, check, poll.WithTimeout(5*time.Second), poll.WithDelay(100*time.Millisecond))
|
|
|
|
// })
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExternalFailureBackoffDoesntStartQueue(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
failuresUntilBlacklist := uint32(16)
|
2023-04-19 15:50:33 +01:00
|
|
|
destination := spec.ServerName("remotehost")
|
2023-01-23 17:55:12 +00:00
|
|
|
db, fc, queues, pc, close := testSetup(failuresUntilBlacklist, failuresUntilBlacklist+1, true, false, t, test.DBTypeSQLite, false)
|
2022-10-19 11:03:16 +01:00
|
|
|
defer close()
|
|
|
|
defer func() {
|
|
|
|
pc.ShutdownDendrite()
|
|
|
|
<-pc.WaitForShutdown()
|
|
|
|
}()
|
|
|
|
|
|
|
|
dest := queues.getQueue(destination)
|
|
|
|
queues.statistics.ForServer(destination).Failure()
|
2023-04-19 15:50:33 +01:00
|
|
|
destinations := map[spec.ServerName]struct{}{destination: {}}
|
2022-10-19 11:03:16 +01:00
|
|
|
ev := mustCreatePDU(t)
|
|
|
|
headeredJSON, _ := json.Marshal(ev)
|
|
|
|
nid, _ := db.StoreJSON(pc.Context(), string(headeredJSON))
|
2022-10-21 11:50:51 +01:00
|
|
|
err := db.AssociatePDUWithDestinations(pc.Context(), destinations, nid)
|
|
|
|
assert.NoError(t, err, "failed to associate PDU with destinations")
|
2022-10-19 11:03:16 +01:00
|
|
|
|
|
|
|
pollEnd := time.Now().Add(3 * time.Second)
|
|
|
|
runningCheck := func(log poll.LogT) poll.Result {
|
|
|
|
if dest.running.Load() || fc.txCount.Load() > 0 {
|
|
|
|
return poll.Error(fmt.Errorf("The queue was started"))
|
|
|
|
}
|
|
|
|
if time.Now().After(pollEnd) {
|
|
|
|
// Allow more than enough time for the queue to be started in the case
|
|
|
|
// of backoff triggering it to start.
|
|
|
|
return poll.Success()
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting to ensure queue doesn't start.")
|
|
|
|
}
|
|
|
|
poll.WaitOn(t, runningCheck, poll.WithTimeout(4*time.Second), poll.WithDelay(100*time.Millisecond))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQueueInteractsWithRealDatabasePDUAndEDU(t *testing.T) {
|
|
|
|
// NOTE : Only one test case against real databases can be run at a time.
|
|
|
|
t.Parallel()
|
|
|
|
failuresUntilBlacklist := uint32(1)
|
2023-04-19 15:50:33 +01:00
|
|
|
destination := spec.ServerName("remotehost")
|
|
|
|
destinations := map[spec.ServerName]struct{}{destination: {}}
|
2022-10-19 11:03:16 +01:00
|
|
|
test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
|
2023-01-23 17:55:12 +00:00
|
|
|
db, fc, queues, pc, close := testSetup(failuresUntilBlacklist, failuresUntilBlacklist+1, false, false, t, dbType, true)
|
2022-10-19 11:03:16 +01:00
|
|
|
// NOTE : These defers aren't called if go test is killed so the dbs may not get cleaned up.
|
|
|
|
defer close()
|
|
|
|
defer func() {
|
|
|
|
pc.ShutdownDendrite()
|
|
|
|
<-pc.WaitForShutdown()
|
|
|
|
}()
|
|
|
|
|
|
|
|
// NOTE : getQueue before sending event to ensure we grab the same queue reference
|
|
|
|
// before it is blacklisted and deleted.
|
|
|
|
dest := queues.getQueue(destination)
|
|
|
|
ev := mustCreatePDU(t)
|
2023-04-19 15:50:33 +01:00
|
|
|
err := queues.SendEvent(ev, "localhost", []spec.ServerName{destination})
|
2022-10-19 11:03:16 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-10-20 16:37:35 +01:00
|
|
|
// NOTE : The server can be blacklisted before this, so manually inject the event
|
|
|
|
// into the database.
|
2022-10-19 11:03:16 +01:00
|
|
|
edu := mustCreateEDU(t)
|
2022-10-20 16:37:35 +01:00
|
|
|
ephemeralJSON, _ := json.Marshal(edu)
|
|
|
|
nid, _ := db.StoreJSON(pc.Context(), string(ephemeralJSON))
|
2022-10-21 11:50:51 +01:00
|
|
|
err = db.AssociateEDUWithDestinations(pc.Context(), destinations, nid, edu.Type, nil)
|
|
|
|
assert.NoError(t, err, "failed to associate EDU with destinations")
|
2022-10-19 11:03:16 +01:00
|
|
|
|
|
|
|
checkBlacklisted := func(log poll.LogT) poll.Result {
|
|
|
|
if fc.txCount.Load() == failuresUntilBlacklist {
|
|
|
|
pduData, dbErrPDU := db.GetPendingPDUs(pc.Context(), destination, 200)
|
|
|
|
assert.NoError(t, dbErrPDU)
|
|
|
|
eduData, dbErrEDU := db.GetPendingEDUs(pc.Context(), destination, 200)
|
|
|
|
assert.NoError(t, dbErrEDU)
|
|
|
|
if len(pduData) == 1 && len(eduData) == 1 {
|
|
|
|
if val, _ := db.IsServerBlacklisted(destination); val {
|
|
|
|
if !dest.running.Load() {
|
|
|
|
return poll.Success()
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for queue to stop completely")
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for server to be blacklisted")
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for events to be added to database. Currently present PDU: %d EDU: %d", len(pduData), len(eduData))
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for more send attempts before checking database. Currently %d", fc.txCount.Load())
|
|
|
|
}
|
|
|
|
poll.WaitOn(t, checkBlacklisted, poll.WithTimeout(10*time.Second), poll.WithDelay(100*time.Millisecond))
|
|
|
|
|
|
|
|
fc.shouldTxSucceed = true
|
2023-01-23 17:55:12 +00:00
|
|
|
wasBlacklisted := dest.statistics.MarkServerAlive()
|
|
|
|
queues.RetryServer(destination, wasBlacklisted)
|
2022-10-19 11:03:16 +01:00
|
|
|
checkRetry := func(log poll.LogT) poll.Result {
|
|
|
|
pduData, dbErrPDU := db.GetPendingPDUs(pc.Context(), destination, 200)
|
|
|
|
assert.NoError(t, dbErrPDU)
|
|
|
|
eduData, dbErrEDU := db.GetPendingEDUs(pc.Context(), destination, 200)
|
|
|
|
assert.NoError(t, dbErrEDU)
|
|
|
|
if len(pduData) == 0 && len(eduData) == 0 {
|
|
|
|
return poll.Success()
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for events to be removed from database. Currently present PDU: %d EDU: %d", len(pduData), len(eduData))
|
|
|
|
}
|
|
|
|
poll.WaitOn(t, checkRetry, poll.WithTimeout(10*time.Second), poll.WithDelay(100*time.Millisecond))
|
|
|
|
})
|
|
|
|
}
|
2023-01-23 17:55:12 +00:00
|
|
|
|
|
|
|
func TestSendPDUMultipleFailuresAssumedOffline(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
failuresUntilBlacklist := uint32(7)
|
|
|
|
failuresUntilAssumedOffline := uint32(2)
|
2023-04-19 15:50:33 +01:00
|
|
|
destination := spec.ServerName("remotehost")
|
2023-01-23 17:55:12 +00:00
|
|
|
db, fc, queues, pc, close := testSetup(failuresUntilBlacklist, failuresUntilAssumedOffline, false, false, t, test.DBTypeSQLite, false)
|
|
|
|
defer close()
|
|
|
|
defer func() {
|
|
|
|
pc.ShutdownDendrite()
|
|
|
|
<-pc.WaitForShutdown()
|
|
|
|
}()
|
|
|
|
|
|
|
|
ev := mustCreatePDU(t)
|
2023-04-19 15:50:33 +01:00
|
|
|
err := queues.SendEvent(ev, "localhost", []spec.ServerName{destination})
|
2023-01-23 17:55:12 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
check := func(log poll.LogT) poll.Result {
|
|
|
|
if fc.txCount.Load() == failuresUntilAssumedOffline {
|
|
|
|
data, dbErr := db.GetPendingPDUs(pc.Context(), destination, 100)
|
|
|
|
assert.NoError(t, dbErr)
|
|
|
|
if len(data) == 1 {
|
|
|
|
if val, _ := db.IsServerAssumedOffline(context.Background(), destination); val {
|
|
|
|
return poll.Success()
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for server to be assumed offline")
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for event to be added to database. Currently present PDU: %d", len(data))
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for more send attempts before checking database. Currently %d", fc.txCount.Load())
|
|
|
|
}
|
|
|
|
poll.WaitOn(t, check, poll.WithTimeout(5*time.Second), poll.WithDelay(100*time.Millisecond))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSendEDUMultipleFailuresAssumedOffline(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
failuresUntilBlacklist := uint32(7)
|
|
|
|
failuresUntilAssumedOffline := uint32(2)
|
2023-04-19 15:50:33 +01:00
|
|
|
destination := spec.ServerName("remotehost")
|
2023-01-23 17:55:12 +00:00
|
|
|
db, fc, queues, pc, close := testSetup(failuresUntilBlacklist, failuresUntilAssumedOffline, false, false, t, test.DBTypeSQLite, false)
|
|
|
|
defer close()
|
|
|
|
defer func() {
|
|
|
|
pc.ShutdownDendrite()
|
|
|
|
<-pc.WaitForShutdown()
|
|
|
|
}()
|
|
|
|
|
|
|
|
ev := mustCreateEDU(t)
|
2023-04-19 15:50:33 +01:00
|
|
|
err := queues.SendEDU(ev, "localhost", []spec.ServerName{destination})
|
2023-01-23 17:55:12 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
check := func(log poll.LogT) poll.Result {
|
|
|
|
if fc.txCount.Load() == failuresUntilAssumedOffline {
|
|
|
|
data, dbErr := db.GetPendingEDUs(pc.Context(), destination, 100)
|
|
|
|
assert.NoError(t, dbErr)
|
|
|
|
if len(data) == 1 {
|
|
|
|
if val, _ := db.IsServerAssumedOffline(context.Background(), destination); val {
|
|
|
|
return poll.Success()
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for server to be assumed offline")
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for event to be added to database. Currently present EDU: %d", len(data))
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for more send attempts before checking database. Currently %d", fc.txCount.Load())
|
|
|
|
}
|
|
|
|
poll.WaitOn(t, check, poll.WithTimeout(5*time.Second), poll.WithDelay(100*time.Millisecond))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSendPDUOnRelaySuccessRemovedFromDB(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
failuresUntilBlacklist := uint32(16)
|
|
|
|
failuresUntilAssumedOffline := uint32(1)
|
2023-04-19 15:50:33 +01:00
|
|
|
destination := spec.ServerName("remotehost")
|
2023-01-23 17:55:12 +00:00
|
|
|
db, fc, queues, pc, close := testSetup(failuresUntilBlacklist, failuresUntilAssumedOffline, false, true, t, test.DBTypeSQLite, false)
|
|
|
|
defer close()
|
|
|
|
defer func() {
|
|
|
|
pc.ShutdownDendrite()
|
|
|
|
<-pc.WaitForShutdown()
|
|
|
|
}()
|
|
|
|
|
2023-04-19 15:50:33 +01:00
|
|
|
relayServers := []spec.ServerName{"relayserver"}
|
2023-01-23 17:55:12 +00:00
|
|
|
queues.statistics.ForServer(destination).AddRelayServers(relayServers)
|
|
|
|
|
|
|
|
ev := mustCreatePDU(t)
|
2023-04-19 15:50:33 +01:00
|
|
|
err := queues.SendEvent(ev, "localhost", []spec.ServerName{destination})
|
2023-01-23 17:55:12 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
check := func(log poll.LogT) poll.Result {
|
2023-01-28 23:27:53 +00:00
|
|
|
if fc.txCount.Load() >= 1 {
|
2023-01-23 17:55:12 +00:00
|
|
|
if fc.txRelayCount.Load() == 1 {
|
|
|
|
data, dbErr := db.GetPendingPDUs(pc.Context(), destination, 100)
|
|
|
|
assert.NoError(t, dbErr)
|
|
|
|
if len(data) == 0 {
|
|
|
|
return poll.Success()
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for event to be removed from database. Currently present PDU: %d", len(data))
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for more relay send attempts before checking database. Currently %d", fc.txRelayCount.Load())
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for more send attempts before checking database. Currently %d", fc.txCount.Load())
|
|
|
|
}
|
|
|
|
poll.WaitOn(t, check, poll.WithTimeout(5*time.Second), poll.WithDelay(100*time.Millisecond))
|
|
|
|
|
|
|
|
assumedOffline, _ := db.IsServerAssumedOffline(context.Background(), destination)
|
|
|
|
assert.Equal(t, true, assumedOffline)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSendEDUOnRelaySuccessRemovedFromDB(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
failuresUntilBlacklist := uint32(16)
|
|
|
|
failuresUntilAssumedOffline := uint32(1)
|
2023-04-19 15:50:33 +01:00
|
|
|
destination := spec.ServerName("remotehost")
|
2023-01-23 17:55:12 +00:00
|
|
|
db, fc, queues, pc, close := testSetup(failuresUntilBlacklist, failuresUntilAssumedOffline, false, true, t, test.DBTypeSQLite, false)
|
|
|
|
defer close()
|
|
|
|
defer func() {
|
|
|
|
pc.ShutdownDendrite()
|
|
|
|
<-pc.WaitForShutdown()
|
|
|
|
}()
|
|
|
|
|
2023-04-19 15:50:33 +01:00
|
|
|
relayServers := []spec.ServerName{"relayserver"}
|
2023-01-23 17:55:12 +00:00
|
|
|
queues.statistics.ForServer(destination).AddRelayServers(relayServers)
|
|
|
|
|
|
|
|
ev := mustCreateEDU(t)
|
2023-04-19 15:50:33 +01:00
|
|
|
err := queues.SendEDU(ev, "localhost", []spec.ServerName{destination})
|
2023-01-23 17:55:12 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
check := func(log poll.LogT) poll.Result {
|
2023-01-28 23:27:53 +00:00
|
|
|
if fc.txCount.Load() >= 1 {
|
2023-01-23 17:55:12 +00:00
|
|
|
if fc.txRelayCount.Load() == 1 {
|
|
|
|
data, dbErr := db.GetPendingEDUs(pc.Context(), destination, 100)
|
|
|
|
assert.NoError(t, dbErr)
|
|
|
|
if len(data) == 0 {
|
|
|
|
return poll.Success()
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for event to be removed from database. Currently present EDU: %d", len(data))
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for more relay send attempts before checking database. Currently %d", fc.txRelayCount.Load())
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for more send attempts before checking database. Currently %d", fc.txCount.Load())
|
|
|
|
}
|
|
|
|
poll.WaitOn(t, check, poll.WithTimeout(5*time.Second), poll.WithDelay(100*time.Millisecond))
|
|
|
|
|
|
|
|
assumedOffline, _ := db.IsServerAssumedOffline(context.Background(), destination)
|
|
|
|
assert.Equal(t, true, assumedOffline)
|
|
|
|
}
|