Esempio n. 1
0
func TestBuildConfig(t *testing.T) {
	conn := NewConn()

	if err := os.Unsetenv(vars.Env(vars.MaxStackSize)); err != nil {
		t.Fatal(err)
	}

	maxStackSizeFlag = 32
	conn.buildConfig()

	if s := conn.Config.Get(vars.MaxStackSize); s != 32 {
		t.Errorf("MaxStackSize is %v, expected %d", s, 32)
	}

	if err := os.Setenv(vars.Env(vars.MaxStackSize), "42"); err != nil {
		t.Fatal(err)
	}
	conn.buildConfig()

	if s := conn.Config.Get(vars.MaxStackSize); s != 42 {
		t.Errorf("MaxStackSize is %v, expected %d", s, 42)
	}

	if err := os.Setenv(vars.Env(vars.MaxStackSize), "foo"); err != nil {
		t.Fatal(err)
	}
	conn.buildConfig()

	if s := conn.Config.Get(vars.MaxStackSize); s != -1 {
		t.Errorf("MaxStackSize is %v, expected %v", s, -1)
	}
}
Esempio n. 2
0
// buildConfig sets non-default config values to the Connection
// reading from environment variables and cli flags.
func (c *Conn) buildConfig() {
	flagKeys := []flagKey{
		{maxStackSizeFlag, vars.MaxStackSize},
		{readTimeoutFlag, vars.ReadTimeout},
		{writeTimeoutFlag, vars.WriteTimeout},
		{portFlag, vars.Port},
	}

	for _, fk := range flagKeys {
		if e := os.Getenv(vars.Env(fk.key)); e != "" {
			if i, err := strconv.Atoi(e); err != nil {
				c.Config.Set(fk.key, vars.DefaultInt(fk.key))
			} else {
				c.Config.Set(fk.key, i)

			}
			continue
		}
		c.Config.Set(fk.key, fk.flag)
	}
}