Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,21 @@ jobs:
echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p
ulimit -n 5000

- name: Create folders
run: |
mkdir ./tests/coverage-ci

- name: Install Go dependencies
run: go mod download

- name: Run service unit tests with coverage
run: |
mkdir ./tests/coverage-ci
go test -timeout 20m -v -race -cover -tags=debug -failfast -coverpkg=github.com/roadrunner-server/service/v6 -coverprofile=./tests/coverage-ci/service_u.out -covermode=atomic .
go test -timeout 20m -v -race -cover -tags=debug -coverpkg=./... -coverprofile=./tests/coverage-ci/service_u.out -covermode=atomic ./...

- name: Run service e2e tests with coverage
run: |
cd tests

chmod 777 php_test_files/test_binary
chmod +x php_test_files/test_binary
go test -timeout 20m -v -race -cover -tags=debug -failfast -coverpkg=github.com/roadrunner-server/service/v6 -coverprofile=./coverage-ci/service.out -covermode=atomic .
go test -timeout 20m -v -race -cover -tags=debug -coverpkg=github.com/roadrunner-server/service/v6/... -coverprofile=./coverage-ci/service.out -covermode=atomic .

- name: Archive code coverage results
uses: actions/upload-artifact@v7
Expand Down
37 changes: 37 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package service

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestConfigInitDefault(t *testing.T) {
cfg := &Config{Services: map[string]*Service{
"unset": {},
"negative": {ProcessNum: -3},
"explicit": {ProcessNum: 4, RestartSec: 7, TimeoutStopSec: 9},
}}

cfg.InitDefault()

tests := []struct {
name string
processNum int
restartSec uint64
timeoutStopSec uint64
}{
{name: "unset", processNum: 1, restartSec: 30, timeoutStopSec: 5},
{name: "negative", processNum: 1, restartSec: 30, timeoutStopSec: 5},
{name: "explicit", processNum: 4, restartSec: 7, timeoutStopSec: 9},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
svc := cfg.Services[tt.name]
require.Equal(t, tt.processNum, svc.ProcessNum)
require.Equal(t, tt.restartSec, svc.RestartSec)
require.Equal(t, tt.timeoutStopSec, svc.TimeoutStopSec)
})
}
}
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,25 @@ go 1.26
toolchain go1.26.5

require (
github.com/roadrunner-server/api-go/v6 v6.0.0-beta.13
github.com/roadrunner-server/api-go/v6 v6.0.0-beta.14
github.com/roadrunner-server/errors v1.5.0
github.com/roadrunner-server/pool/v2 v2.0.0-beta.1
github.com/shirou/gopsutil v3.21.11+incompatible
github.com/stretchr/testify v1.11.1
)

require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/roadrunner-server/goridge/v4 v4.0.0-beta.3 // indirect
github.com/rogpeppe/go-internal v1.14.1 // indirect
github.com/tklauser/go-sysconf v0.4.0 // indirect
github.com/tklauser/numcpus v0.12.0 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
golang.org/x/sys v0.47.0 // indirect
google.golang.org/protobuf v1.36.12 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
19 changes: 17 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE=
github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78=
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/roadrunner-server/api-go/v6 v6.0.0-beta.13 h1:BAV1aKkRp51C1OXDfEYZXgfrXqn4O7bpr6Z/m5otwd8=
github.com/roadrunner-server/api-go/v6 v6.0.0-beta.13/go.mod h1:Y4rsabWjr4Y10Jg6H8J5NDitQqlnXmGhCdgR+zyLYkI=
github.com/roadrunner-server/api-go/v6 v6.0.0-beta.14 h1:sTskv/3ImOZlUdtHuj9uT24gm1gQl/qU8rFNvn3MzhU=
github.com/roadrunner-server/api-go/v6 v6.0.0-beta.14/go.mod h1:Y4rsabWjr4Y10Jg6H8J5NDitQqlnXmGhCdgR+zyLYkI=
github.com/roadrunner-server/errors v1.5.0 h1:unG7LKIZrSzkCCF3YLRLA5VyqE0KKomofXVJUXJe00g=
github.com/roadrunner-server/errors v1.5.0/go.mod h1:g9fo/T2C13cWRDR9PW1r0ZAOSQfNhWAZawyfkGiaHuI=
github.com/roadrunner-server/goridge/v4 v4.0.0-beta.3 h1:+kUw00/fpqwdMWrPMYW+OZH3O4gEar8hqrY7I+nAztA=
github.com/roadrunner-server/goridge/v4 v4.0.0-beta.3/go.mod h1:1aHppV68y/VqRED/AsfNg59sft9aQOhqgr5Z5n49jbM=
github.com/roadrunner-server/pool/v2 v2.0.0-beta.1 h1:jpYXFtdD6QGAdAGPgMxrNi3j1CegCRpb2y+A+3GnXFA=
github.com/roadrunner-server/pool/v2 v2.0.0-beta.1/go.mod h1:Bo1wT7RtL3eyQHXBUohNhtj/yAmRt6Rq8smuBg5pWkY=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI=
github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
Expand All @@ -31,5 +43,8 @@ golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs=
golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
google.golang.org/protobuf v1.36.12 h1:pJOKDDOyeXErUroCihFAd5LQuwXBSpVnKGrj5o/fwxc=
google.golang.org/protobuf v1.36.12/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
108 changes: 108 additions & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package service

import (
"context"
"log/slog"
"maps"
"sync"
)

// capturedRecord is a log record with the attributes carried by its handler.
type capturedRecord struct {
message string
attrs map[string]string
}

// captureStore collects the records written through a captureHandler.
type captureStore struct {
mu sync.Mutex
records []capturedRecord
}

func (s *captureStore) add(r capturedRecord) {
s.mu.Lock()
s.records = append(s.records, r)
s.mu.Unlock()
}

// messages returns the messages in the order they were written.
func (s *captureStore) messages() []string {
s.mu.Lock()
defer s.mu.Unlock()

out := make([]string, 0, len(s.records))
for _, r := range s.records {
out = append(out, r.message)
}

return out
}

// count returns how many records carry the given message.
func (s *captureStore) count(message string) int {
s.mu.Lock()
defer s.mu.Unlock()

var n int
for _, r := range s.records {
if r.message == message {
n++
}
}

return n
}

// find returns the first record with the given message.
func (s *captureStore) find(message string) (capturedRecord, bool) {
s.mu.Lock()
defer s.mu.Unlock()

for _, r := range s.records {
if r.message == message {
return r, true
}
}

return capturedRecord{}, false
}

// captureHandler is an slog.Handler that keeps every record in memory.
type captureHandler struct {
store *captureStore
attrs map[string]string
}

// newCaptureLogger returns a logger writing into the returned store.
func newCaptureLogger() (*slog.Logger, *captureStore) {
store := &captureStore{}
return slog.New(&captureHandler{store: store, attrs: map[string]string{}}), store
}

func (h *captureHandler) Enabled(context.Context, slog.Level) bool {
return true
}

func (h *captureHandler) Handle(_ context.Context, r slog.Record) error {
rec := capturedRecord{message: r.Message, attrs: maps.Clone(h.attrs)}
r.Attrs(func(a slog.Attr) bool {
rec.attrs[a.Key] = a.Value.String()
return true
})

h.store.add(rec)
return nil
}

func (h *captureHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
next := maps.Clone(h.attrs)
for _, a := range attrs {
next[a.Key] = a.Value.String()
}

return &captureHandler{store: h.store, attrs: next}
}

func (h *captureHandler) WithGroup(string) slog.Handler {
return h
}
Loading
Loading