5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-04 13:10:14 +08:00
wails/v3/pkg/application/internal/tests/services/startup/startup_test.go
Fabio Massaioli e7c134de4e
[v3] Late service registration and error handling overhaul (#4066)
* Add service registration method

* Fix error handling and formatting in messageprocessor

* Add configurable error handling

* Improve error strings

* Fix service shutdown on macOS

* Add post shutdown hook

* Better fatal errors

* Add startup/shutdown sequence tests

* Improve debug messages

* Update JS runtime

* Update docs

* Update changelog

* Fix log message in clipboard message processor

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* Remove panic in RegisterService

* Fix linux tests (hopefully)

* Fix error formatting everywhere

* Fix typo in windows webview

* Tidy example mods

* Set application name in tests

* Fix ubuntu test workflow

* Cleanup template test pipeline

* Fix dev build detection on Go 1.24

* Update template go.mod/sum to Go 1.24

* Remove redundant caching in template tests

* Final format string cleanup

* Fix wails3 tool references

* Fix legacy log calls

* Remove formatJS and simplify format strings

* Fix indirect import

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
2025-02-19 09:27:41 +01:00

103 lines
2.6 KiB
Go

package startup
import (
"sync"
"sync/atomic"
"testing"
"github.com/wailsapp/wails/v3/pkg/application"
apptest "github.com/wailsapp/wails/v3/pkg/application/internal/tests"
svctest "github.com/wailsapp/wails/v3/pkg/application/internal/tests/services"
"github.com/wailsapp/wails/v3/pkg/events"
)
func TestMain(m *testing.M) {
apptest.Main(m)
}
type (
Service1 struct{ svctest.Startupper }
Service2 struct{ svctest.Startupper }
Service3 struct{ svctest.Startupper }
Service4 struct{ svctest.Startupper }
Service5 struct{ svctest.Startupper }
Service6 struct{ svctest.Startupper }
)
func TestServiceStartup(t *testing.T) {
var seq atomic.Int64
services := []application.Service{
svctest.Configure(&Service1{}, svctest.Config{Id: 0, T: t, Seq: &seq}),
svctest.Configure(&Service2{}, svctest.Config{Id: 1, T: t, Seq: &seq, Options: application.ServiceOptions{
Name: "I am service 2",
}}),
svctest.Configure(&Service3{}, svctest.Config{Id: 2, T: t, Seq: &seq, Options: application.ServiceOptions{
Route: "/mounted/here",
}}),
svctest.Configure(&Service4{}, svctest.Config{Id: 3, T: t, Seq: &seq}),
svctest.Configure(&Service5{}, svctest.Config{Id: 4, Seq: &seq, Options: application.ServiceOptions{
Name: "I am service 5",
Route: "/mounted/there",
}}),
svctest.Configure(&Service6{}, svctest.Config{Id: 5, T: t, Seq: &seq, Options: application.ServiceOptions{
Name: "I am service 6",
Route: "/mounted/elsewhere",
}}),
}
app := apptest.New(t, application.Options{
Services: services[:3],
})
var wg sync.WaitGroup
wg.Add(2)
go func() {
app.RegisterService(services[3])
wg.Done()
}()
go func() {
app.RegisterService(services[4])
wg.Done()
}()
wg.Wait()
app.RegisterService(services[5])
app.OnApplicationEvent(events.Common.ApplicationStarted, func(*application.ApplicationEvent) {
app.Quit()
})
err := apptest.Run(t, app)
if err != nil {
t.Fatal(err)
}
if count := seq.Load(); count != int64(len(services)) {
t.Errorf("Wrong startup call count: wanted %d, got %d", len(services), count)
}
validate(t, services[0], 0)
validate(t, services[1], 1)
validate(t, services[2], 2)
validate(t, services[3], 3)
validate(t, services[4], 3)
validate(t, services[5], 4, 5)
}
func validate(t *testing.T, svc application.Service, prev ...int64) {
id := svc.Instance().(interface{ Id() int }).Id()
seq := svc.Instance().(interface{ StartupSeq() int64 }).StartupSeq()
if seq == 0 {
t.Errorf("Service #%d did not start up", id)
return
}
for _, p := range prev {
if seq <= p {
t.Errorf("Wrong startup sequence number for service #%d: wanted >%d, got %d", id, p, seq)
}
}
}