Files
3x-ui/internal/web/service/sublink_registry_test.go
T
25f2002654 d2a0216e01 fix(web): install the share-link provider before the panel serves (#6914)
The panel started answering requests before the share-link generator was
registered, so an "all links", client sub-links or Telegram link request that
arrived during start-up got "sub link provider not registered". NewServer now
takes the provider and installs it before anything listens, and the provider
registry is guarded by a lock, since start-up writes it while requests read it.
2026-10-24 05:50:46 +02:00

45 lines
1.0 KiB
Go

package service
import (
"sync"
"testing"
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
)
type fakeSubLinkProvider struct {
links []string
}
func (f fakeSubLinkProvider) SubLinksForSubId(host, subId string) ([]string, error) {
return f.links, nil
}
func (f fakeSubLinkProvider) LinksForClient(host string, inbound *model.Inbound, email string) []string {
return f.links
}
func (f fakeSubLinkProvider) LinksForInbounds(host string, inbounds []*model.Inbound) []string {
return f.links
}
// Startup writes the provider registry while link requests read it; under -race an
// unguarded registry fails here.
func TestSubLinkProviderRegistrationIsRaceFree(t *testing.T) {
t.Cleanup(func() { RegisterSubLinkProvider(nil) })
var wg sync.WaitGroup
for range 8 {
wg.Add(2)
go func() {
defer wg.Done()
RegisterSubLinkProvider(fakeSubLinkProvider{links: []string{"vless://example"}})
}()
go func() {
defer wg.Done()
_, _ = (&InboundService{}).GetSubLinks("panel.example", "subid")
}()
}
wg.Wait()
}