github.com/decred/dcrlnd@v0.7.6/lncfg/workers_test.go (about)

     1  package lncfg_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/decred/dcrlnd/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  }