Skip to content

Commit d1c4a7d

Browse files
stariushieblmi
authored andcommitted
loopd: test error reporting logic
1 parent 360af61 commit d1c4a7d

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

loopd/daemon.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ var (
4747
errOnlyStartOnce = fmt.Errorf("daemon can only be started once")
4848
)
4949

50+
// shouldReportManagerErr determines whether a manager error should be forwarded
51+
// to the internal error channel. Context cancellations are treated as
52+
// non-fatal.
53+
func shouldReportManagerErr(err error) bool {
54+
return err != nil && !errors.Is(err, context.Canceled)
55+
}
56+
5057
// ListenerCfg holds closures used to retrieve listeners for the gRPC services.
5158
type ListenerCfg struct {
5259
// grpcListener returns a TLS listener to use for the gRPC server, based
@@ -892,7 +899,7 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
892899
defer infof("Static address manager stopped")
893900

894901
err := staticAddressManager.Run(d.mainCtx, initChan)
895-
if err != nil && !errors.Is(err, context.Canceled) {
902+
if shouldReportManagerErr(err) {
896903
d.internalErrChan <- err
897904
}
898905
}()
@@ -924,7 +931,7 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
924931
defer infof("Static address deposit manager stopped")
925932

926933
err := depositManager.Run(d.mainCtx, initChan)
927-
if err != nil && !errors.Is(err, context.Canceled) {
934+
if shouldReportManagerErr(err) {
928935
d.internalErrChan <- err
929936
}
930937
}()
@@ -956,7 +963,7 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
956963
defer infof("Static address withdrawal manager stopped")
957964

958965
err := withdrawalManager.Run(d.mainCtx, initChan)
959-
if err != nil && !errors.Is(err, context.Canceled) {
966+
if shouldReportManagerErr(err) {
960967
d.internalErrChan <- err
961968
}
962969
}()
@@ -992,7 +999,7 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
992999
infof("Starting static address loop-in manager...")
9931000
defer infof("Static address loop-in manager stopped")
9941001
err := staticLoopInManager.Run(d.mainCtx, initChan)
995-
if err != nil && !errors.Is(err, context.Canceled) {
1002+
if shouldReportManagerErr(err) {
9961003
d.internalErrChan <- err
9971004
}
9981005
}()

loopd/daemon_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package loopd
2+
3+
import (
4+
"context"
5+
"errors"
6+
"fmt"
7+
"testing"
8+
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
// TestShouldReportManagerErr verifies that context cancellations are treated as
13+
// non-fatal while other errors are reported.
14+
func TestShouldReportManagerErr(t *testing.T) {
15+
t.Parallel()
16+
17+
tests := []struct {
18+
name string
19+
err error
20+
expected bool
21+
}{
22+
{
23+
name: "nil error",
24+
err: nil,
25+
expected: false,
26+
},
27+
{
28+
name: "context canceled",
29+
err: context.Canceled,
30+
expected: false,
31+
},
32+
{
33+
name: "wrapped context canceled",
34+
err: fmt.Errorf("wrap: %w", context.Canceled),
35+
expected: false,
36+
},
37+
{
38+
name: "other error",
39+
err: errors.New("boom"),
40+
expected: true,
41+
},
42+
}
43+
44+
for _, tt := range tests {
45+
t.Run(tt.name, func(t *testing.T) {
46+
got := shouldReportManagerErr(tt.err)
47+
require.Equal(t, tt.expected, got)
48+
})
49+
}
50+
}

0 commit comments

Comments
 (0)