mirror of
https://github.com/henrygd/beszel.git
synced 2026-09-21 08:57:48 +02:00
test: use virtual time to eliminate MQTT notification timeout waits
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
|
"testing/synctest"
|
||||||
|
|
||||||
beszelTests "github.com/henrygd/beszel/internal/tests"
|
beszelTests "github.com/henrygd/beszel/internal/tests"
|
||||||
pbTests "github.com/pocketbase/pocketbase/tests"
|
pbTests "github.com/pocketbase/pocketbase/tests"
|
||||||
@@ -533,6 +534,20 @@ func TestSendTestNotification(t *testing.T) {
|
|||||||
|
|
||||||
for _, url := range []string{localURL, "smtp://user:pass@127.0.0.1/?fromAddress=sender@example.com&toAddresses=recipient@example.com", "mqtt://127.0.0.1/topic"} {
|
for _, url := range []string{localURL, "smtp://user:pass@127.0.0.1/?fromAddress=sender@example.com&toAddresses=recipient@example.com", "mqtt://127.0.0.1/topic"} {
|
||||||
scenarios = append(scenarios, beszelTests.ApiScenario{
|
scenarios = append(scenarios, beszelTests.ApiScenario{
|
||||||
|
BeforeTestFunc: func(tb testing.TB, _ *pbTests.TestApp, e *core.ServeEvent) {
|
||||||
|
if !strings.HasPrefix(url, "mqtt://") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Keep the real MQTT rejection path, but advance its library's
|
||||||
|
// fixed timeout using virtual time instead of waiting 10 seconds.
|
||||||
|
e.Router.BindFunc(func(re *core.RequestEvent) error {
|
||||||
|
var err error
|
||||||
|
synctest.Test(tb.(*testing.T), func(t *testing.T) {
|
||||||
|
err = re.Next()
|
||||||
|
})
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
},
|
||||||
Name: "readonly cannot send to " + url,
|
Name: "readonly cannot send to " + url,
|
||||||
Method: http.MethodPost,
|
Method: http.MethodPost,
|
||||||
URL: "/api/beszel/test-notification",
|
URL: "/api/beszel/test-notification",
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
|
"testing/synctest"
|
||||||
|
|
||||||
"github.com/nicholas-fedor/shoutrrr/pkg/types"
|
"github.com/nicholas-fedor/shoutrrr/pkg/types"
|
||||||
"golang.org/x/net/dns/dnsmessage"
|
"golang.org/x/net/dns/dnsmessage"
|
||||||
@@ -178,13 +179,18 @@ func TestPublicNotificationTCP(t *testing.T) {
|
|||||||
} {
|
} {
|
||||||
t.Run(rawURL, func(t *testing.T) {
|
t.Run(rawURL, func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
// MQTT waits for a fixed library timeout even after a dial failure.
|
||||||
|
// Virtual time preserves the full send/cleanup path without that delay.
|
||||||
t.Run("internal destination", func(t *testing.T) {
|
t.Run("internal destination", func(t *testing.T) {
|
||||||
|
synctest.Test(t, func(t *testing.T) {
|
||||||
err := sendPublicNotification(strings.ReplaceAll(rawURL, "HOST", "127.0.0.1"), "test")
|
err := sendPublicNotification(strings.ReplaceAll(rawURL, "HOST", "127.0.0.1"), "test")
|
||||||
if !errors.Is(err, errInternalDestination) {
|
if !errors.Is(err, errInternalDestination) {
|
||||||
t.Fatalf("expected blocked destination, got %v", err)
|
t.Fatalf("expected blocked destination, got %v", err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
})
|
||||||
t.Run("public destination uses injected dialer", func(t *testing.T) {
|
t.Run("public destination uses injected dialer", func(t *testing.T) {
|
||||||
|
synctest.Test(t, func(t *testing.T) {
|
||||||
var calls atomic.Int32
|
var calls atomic.Int32
|
||||||
stopped := errors.New("test dial stopped")
|
stopped := errors.New("test dial stopped")
|
||||||
service, err := newPublicNotificationService(strings.ReplaceAll(rawURL, "HOST", "8.8.8.8"), types.SenderOptions{
|
service, err := newPublicNotificationService(strings.ReplaceAll(rawURL, "HOST", "8.8.8.8"), types.SenderOptions{
|
||||||
@@ -213,5 +219,6 @@ func TestPublicNotificationTCP(t *testing.T) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user