-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsettings_test.go
More file actions
147 lines (132 loc) · 4.25 KB
/
settings_test.go
File metadata and controls
147 lines (132 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package config
import (
"encoding/json"
"testing"
"time"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
)
func TestArgumentPrecedence(t *testing.T) {
tests := []struct {
name string
configContent string
cliArgs []string
expectedStats time.Duration
expectedWorkers int
expectedTPS float64
}{
{
name: "config file only",
configContent: `{
"statsInterval": "5s",
"workers": 3,
"tps": 100.5
}`,
cliArgs: []string{},
expectedStats: 5 * time.Second,
expectedWorkers: 3,
expectedTPS: 100.5,
},
{
name: "CLI overrides config",
configContent: `{
"statsInterval": "5s",
"workers": 3,
"tps": 100.5
}`,
cliArgs: []string{"--stats-interval", "3s", "--workers", "7"},
expectedStats: 3 * time.Second,
expectedWorkers: 7,
expectedTPS: 100.5, // Not overridden by CLI
},
{
name: "defaults when neither CLI nor config",
configContent: `{
"endpoints": ["http://localhost:8545"]
}`,
cliArgs: []string{},
expectedStats: 10 * time.Second, // Default
expectedWorkers: 1, // Default
expectedTPS: 0.0, // Default
},
{
name: "CLI overrides defaults",
configContent: `{
"endpoints": ["http://localhost:8545"]
}`,
cliArgs: []string{"--stats-interval", "15s", "--tps", "50"},
expectedStats: 15 * time.Second,
expectedWorkers: 1, // Default (not overridden)
expectedTPS: 50.0, // CLI override
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Reset viper for each test
viper.Reset()
// create Settings struct
configSettings := &Settings{}
err := json.Unmarshal([]byte(tt.configContent), configSettings)
require.NoError(t, err, "Failed to unmarshal config file")
// Create test command with flags
cmd := &cobra.Command{
Use: "test",
}
// Add flags (with zero defaults to avoid precedence issues)
cmd.Flags().Duration("stats-interval", 0, "Stats interval")
cmd.Flags().Int("workers", 0, "Number of workers")
cmd.Flags().Float64("tps", 0, "TPS")
cmd.Flags().Bool("dry-run", false, "Dry run")
cmd.Flags().Bool("debug", false, "Debug")
cmd.Flags().Bool("track-receipts", false, "Track receipts")
cmd.Flags().Bool("track-blocks", false, "Track blocks")
cmd.Flags().Bool("prewarm", false, "Prewarm")
cmd.Flags().Bool("track-user-latency", false, "Track user latency")
cmd.Flags().Int("buffer-size", 0, "Buffer size")
cmd.Flags().Bool("ramp-up", false, "Ramp up loadtest")
cmd.Flags().String("report-path", "", "Report path")
cmd.Flags().String("txs-dir", "", "Txs dir")
cmd.Flags().Uint64("target-gas", 0, "Target gas")
cmd.Flags().Int("num-blocks-to-write", 0, "Number of blocks to write")
// Parse CLI args
if len(tt.cliArgs) > 0 {
cmd.SetArgs(tt.cliArgs)
require.NoError(t, cmd.Execute(), "Failed to parse CLI args")
}
// Initialize Viper
require.NoError(t, InitializeViper(cmd), "Failed to initialize Viper")
// Load settings
require.NoError(t, LoadSettings(configSettings), "Failed to load settings")
// Resolve settings
settings := ResolveSettings()
// Verify expectations
require.Equal(t, tt.expectedStats, settings.StatsInterval.ToDuration(), "StatsInterval: expected %v, got %v", tt.expectedStats, settings.StatsInterval.ToDuration())
require.Equal(t, tt.expectedWorkers, settings.Workers, "Workers: expected %d, got %d", tt.expectedWorkers, settings.Workers)
require.Equal(t, tt.expectedTPS, settings.TPS, "TPS: expected %f, got %f", tt.expectedTPS, settings.TPS)
})
}
}
func TestDefaultSettings(t *testing.T) {
defaults := DefaultSettings()
expected := Settings{
Workers: 1,
TPS: 0.0,
StatsInterval: Duration(10 * time.Second),
BufferSize: 1000,
DryRun: false,
Debug: false,
TrackReceipts: false,
TrackBlocks: false,
TrackUserLatency: false,
Prewarm: false,
RampUp: false,
ReportPath: "",
TxsDir: "",
TargetGas: 10_000_000,
NumBlocksToWrite: 100,
}
if defaults != expected {
t.Errorf("DefaultSettings mismatch.\nExpected: %+v\nGot: %+v", expected, defaults)
}
}