Skip to content

Commit

Permalink
refactor: parser logic
Browse files Browse the repository at this point in the history
  • Loading branch information
vinitparekh17 committed Nov 15, 2024
1 parent cd1bfc6 commit 765510d
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 8 deletions.
4 changes: 3 additions & 1 deletion config/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"bufio"
"fmt"
"log/slog"
"os"
"reflect"
"strconv"
Expand Down Expand Up @@ -89,7 +90,8 @@ func processConfigData(scanner *bufio.Scanner, p *ConfigParser) error {

parts := strings.SplitN(line, "=", 2)
if len(parts) != 2 {
return fmt.Errorf("invalid config line: %s", line)
slog.Warn("invalid config line", slog.String("line", line))
continue
}

key := strings.TrimSpace(parts[0])
Expand Down
1 change: 1 addition & 0 deletions integration_tests/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func TestCreateConfigFile_InvalidPath(t *testing.T) {
}
}

// TestCreateConfigFile_NoPermission tests creation without write permissions
func TestCreateConfigFile_NoPermission(t *testing.T) {
if os.Getuid() == 0 {
t.Skip("Skipping test when running as root")
Expand Down
88 changes: 81 additions & 7 deletions integration_tests/config/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,89 @@ import (
"os"
"path/filepath"
"testing"
"time"

"github.com/dicedb/dice/config"
)

// TestConfig is a test struct that mimics your actual config structure
type TestConfig struct {
Host string `default:"localhost"`
Port int `default:"8080"`
LogLevel string `default:"info"`
Version string `config:"version" default:"0.0.5"`
InstanceID string `config:"instance_id"`
Auth auth `config:"auth"`
AsyncServer asyncServer `config:"async_server"`
HTTP http `config:"http"`
WebSocket websocket `config:"websocket"`
Performance performance `config:"performance"`
Memory memory `config:"memory"`
Persistence persistence `config:"persistence"`
Logging logging `config:"logging"`
Network network `config:"network"`
}

type auth struct {
UserName string `config:"username" default:"dice"`
Password string `config:"password"`
}

type asyncServer struct {
Addr string `config:"addr" default:"0.0.0.0"`
Port int `config:"port" default:"7379" validate:"min=1024,max=65535"`
KeepAlive int32 `config:"keepalive" default:"300"`
Timeout int32 `config:"timeout" default:"300"`
MaxConn int32 `config:"max_conn" default:"0"`
}

type http struct {
Enabled bool `config:"enabled" default:"true"`
Port int `config:"port" default:"8082" validate:"min=1024,max=65535"`
}

type websocket struct {
Enabled bool `config:"enabled" default:"true"`
Port int `config:"port" default:"8379" validate:"min=1024,max=65535"`
MaxWriteResponseRetries int `config:"max_write_response_retries" default:"3" validate:"min=0"`
WriteResponseTimeout time.Duration `config:"write_response_timeout" default:"10s"`
}

type performance struct {
WatchChanBufSize int `config:"watch_chan_buf_size" default:"20000"`
ShardCronFrequency time.Duration `config:"shard_cron_frequency" default:"1s"`
MultiplexerPollTimeout time.Duration `config:"multiplexer_poll_timeout" default:"100ms"`
MaxClients int32 `config:"max_clients" default:"20000" validate:"min=0"`
EnableMultiThreading bool `config:"enable_multithreading" default:"false"`
StoreMapInitSize int `config:"store_map_init_size" default:"1024000"`
AdhocReqChanBufSize int `config:"adhoc_req_chan_buf_size" default:"20"`
EnableProfiling bool `config:"profiling" default:"false"`
EnableWatch bool `config:"enable_watch" default:"false"`
NumShards int `config:"num_shards" default:"-1" validate:"oneof=-1|min=1,lte=128"`
}

type memory struct {
MaxMemory int64 `config:"max_memory" default:"0"`
EvictionPolicy string `config:"eviction_policy" default:"allkeys-lfu" validate:"oneof=simple-first allkeys-random allkeys-lru allkeys-lfu"`
EvictionRatio float64 `config:"eviction_ratio" default:"0.9" validate:"min=0,lte=1"`
KeysLimit int `config:"keys_limit" default:"200000000" validate:"min=0"`
LFULogFactor int `config:"lfu_log_factor" default:"10" validate:"min=0"`
}

type persistence struct {
AOFFile string `config:"aof_file" default:"./dice-master.aof" validate:"filepath"`
PersistenceEnabled bool `config:"persistence_enabled" default:"true"`
WriteAOFOnCleanup bool `config:"write_aof_on_cleanup" default:"false"`
EnableWAL bool `config:"enable-wal" default:"false"`
WALDir string `config:"wal-dir" default:"./" validate:"dirpath"`
RestoreFromWAL bool `config:"restore-wal" default:"false"`
WALEngine string `config:"wal-engine" default:"aof" validate:"oneof=sqlite aof"`
}

type logging struct {
LogLevel string `config:"log_level" default:"info" validate:"oneof=debug info warn error"`
}

type network struct {
IOBufferLengthMAX int `config:"io_buffer_length_max" default:"51200" validate:"min=0,max=1048576"` // max is 1MB'
IOBufferLength int `config:"io_buffer_length" default:"512" validate:"min=0"`
}

func TestNewConfigParser(t *testing.T) {
Expand Down Expand Up @@ -46,7 +120,7 @@ log_level=debug`,
content: `host=testhost
invalid-line
port=9090`,
wantErr: true,
wantErr: false,
},
{
name: "non-existent file",
Expand Down Expand Up @@ -101,7 +175,7 @@ log_level=debug`,
input: `host=testhost
invalid-line
port=9090`,
wantErr: true,
wantErr: false,
},
}

Expand Down Expand Up @@ -172,7 +246,7 @@ func TestParseDefaults(t *testing.T) {

if !tt.wantErr && tt.cfg != nil {
cfg := tt.cfg.(*TestConfig)
if cfg.Host != "localhost" || cfg.Port != 8080 || cfg.LogLevel != "info" {
if cfg.AsyncServer.Addr != "0.0.0.0" || cfg.AsyncServer.Port != 7379 || cfg.Logging.LogLevel != "info" {
t.Error("Default values were not properly set")
}
}
Expand Down Expand Up @@ -228,7 +302,7 @@ func TestLoadconfig(t *testing.T) {

if !tt.wantErr && tt.cfg != nil {
cfg := tt.cfg.(*TestConfig)
if tt.content != "" && (cfg.Host != "customhost" || cfg.Port != 9090 || cfg.LogLevel != "debug") {
if tt.content != "" && (cfg.AsyncServer.Addr != "customhost" || cfg.AsyncServer.Port != 9090 || cfg.Logging.LogLevel != "debug") {
t.Error("Config values were not properly loaded")
}
}
Expand Down

0 comments on commit 765510d

Please sign in to comment.