File tree 3 files changed +129
-0
lines changed
3 files changed +129
-0
lines changed Original file line number Diff line number Diff line change @@ -990,6 +990,12 @@ func loadConfig() (*config, error) {
990
990
"minbackoff" )
991
991
}
992
992
993
+ // Assert that all worker pools will have a positive number of
994
+ // workers, otherwise the pools will rendered useless.
995
+ if err := cfg .Workers .Validate (); err != nil {
996
+ return nil , err
997
+ }
998
+
993
999
// Finally, ensure that the user's color is correctly formatted,
994
1000
// otherwise the server will not be able to start after the unlocking
995
1001
// the wallet.
Original file line number Diff line number Diff line change 1
1
package lncfg
2
2
3
+ import "fmt"
4
+
3
5
const (
4
6
// DefaultReadWorkers is the default maximum number of concurrent
5
7
// workers used by the daemon's read pool.
@@ -26,3 +28,22 @@ type Workers struct {
26
28
// Sig is the maximum number of concurrent sig pool workers.
27
29
Sig int `long:"sig" description:"Maximum number of concurrent sig pool workers."`
28
30
}
31
+
32
+ // Validate checks the Workers configuration to ensure that the input values are
33
+ // sane.
34
+ func (w * Workers ) Validate () error {
35
+ if w .Read <= 0 {
36
+ return fmt .Errorf ("number of read workers (%d) must be " +
37
+ "positive" , w .Read )
38
+ }
39
+ if w .Write <= 0 {
40
+ return fmt .Errorf ("number of write workers (%d) must be " +
41
+ "positive" , w .Write )
42
+ }
43
+ if w .Sig <= 0 {
44
+ return fmt .Errorf ("number of sig workers (%d) must be " +
45
+ "positive" , w .Sig )
46
+ }
47
+
48
+ return nil
49
+ }
Original file line number Diff line number Diff line change
1
+ package lncfg_test
2
+
3
+ import (
4
+ "testing"
5
+
6
+ "github.com/lightningnetwork/lnd/lncfg"
7
+ )
8
+
9
+ const (
10
+ maxUint = ^ uint (0 )
11
+ maxInt = int (maxUint >> 1 )
12
+ minInt = - maxInt - 1
13
+ )
14
+
15
+ // TestValidateWorkers asserts that validating the Workers config only succeeds
16
+ // if all fields specify a positive number of workers.
17
+ func TestValidateWorkers (t * testing.T ) {
18
+ tests := []struct {
19
+ name string
20
+ cfg * lncfg.Workers
21
+ valid bool
22
+ }{
23
+ {
24
+ name : "min valid" ,
25
+ cfg : & lncfg.Workers {
26
+ Read : 1 ,
27
+ Write : 1 ,
28
+ Sig : 1 ,
29
+ },
30
+ valid : true ,
31
+ },
32
+ {
33
+ name : "max valid" ,
34
+ cfg : & lncfg.Workers {
35
+ Read : maxInt ,
36
+ Write : maxInt ,
37
+ Sig : maxInt ,
38
+ },
39
+ valid : true ,
40
+ },
41
+ {
42
+ name : "read max invalid" ,
43
+ cfg : & lncfg.Workers {
44
+ Read : 0 ,
45
+ Write : 1 ,
46
+ Sig : 1 ,
47
+ },
48
+ },
49
+ {
50
+ name : "write max invalid" ,
51
+ cfg : & lncfg.Workers {
52
+ Read : 1 ,
53
+ Write : 0 ,
54
+ Sig : 1 ,
55
+ },
56
+ },
57
+ {
58
+ name : "sig max invalid" ,
59
+ cfg : & lncfg.Workers {
60
+ Read : 1 ,
61
+ Write : 1 ,
62
+ Sig : 0 ,
63
+ },
64
+ },
65
+ {
66
+ name : "read min invalid" ,
67
+ cfg : & lncfg.Workers {
68
+ Read : minInt ,
69
+ Write : 1 ,
70
+ Sig : 1 ,
71
+ },
72
+ },
73
+ {
74
+ name : "write min invalid" ,
75
+ cfg : & lncfg.Workers {
76
+ Read : 1 ,
77
+ Write : minInt ,
78
+ Sig : 1 ,
79
+ },
80
+ },
81
+ {
82
+ name : "sig min invalid" ,
83
+ cfg : & lncfg.Workers {
84
+ Read : 1 ,
85
+ Write : 1 ,
86
+ Sig : minInt ,
87
+ },
88
+ },
89
+ }
90
+
91
+ for _ , test := range tests {
92
+ t .Run (test .name , func (t * testing.T ) {
93
+ err := test .cfg .Validate ()
94
+ switch {
95
+ case test .valid && err != nil :
96
+ t .Fatalf ("valid config was invalid: %v" , err )
97
+ case ! test .valid && err == nil :
98
+ t .Fatalf ("invalid config was valid" )
99
+ }
100
+ })
101
+ }
102
+ }
You can’t perform that action at this time.
0 commit comments