Пример #1
0
func CheckExperiments(experiments map[string][]string, uid string, cfg config.Config) map[string]string {
	r := make(map[string]string)
	// if we don't specify experiments to participate in return all
	if len(experiments) == 0 {
		for experimentName, exp := range cfg.ExperimentMap {
			r[experimentName] = exp.GetAlternative(uid)
		}
		return r
	}

	// else only return requested experiments
	for experimentName, _ := range experiments {
		if e, ok := cfg.ExperimentMap[experimentName]; ok {
			r[experimentName] = e.GetAlternative(uid)
		} else {
			// Un-configured feature specified by the client.
			alts := make([]experiment.Alternative, len(experiments[experimentName]))
			for _, alternativeName := range experiments[experimentName] {
				alts = append(alts, experiment.Alternative{alternativeName, 1})
			}

			e := experiment.NewExperiment(experimentName, make(map[string]string), alts...)
			r[experimentName] = e.GetAlternative(uid)
		}
	}
	return r
}
Пример #2
0
func TestGroupMemberGetsCorrectAlternate(t *testing.T) {
	groupConfig := map[string]string{
		"group_a": "yellow_button",
		"group_b": "red_button",
	}

	groupMapping := map[string][]string{
		"group_a": {"1", "2"},
		"group_b": {"3", "4"},
	}

	newTest := experiment.NewExperiment("test",
		groupConfig,
		experiment.Alternative{Name: "yellow_button", Weight: 1},
		experiment.Alternative{Name: "red_button", Weight: 1})

	newTest.SetupGroups(groupMapping)

	if newTest.GetAlternative("1") != "yellow_button" {
		t.Errorf("Expecting yellow button for uid 1 in group a")
	}

	if newTest.GetAlternative("3") != "red_button" {
		t.Errorf("Expecting red button for uid 3 in group b")
	}
}
Пример #3
0
func mockConfig() *config.Config {
	cfg := config.Config{}
	cfg.Experiments = append(cfg.Experiments, experiment.NewExperiment("test1",
		make(map[string]string),
		experiment.Alternative{Name: "a", Weight: 1},
		experiment.Alternative{Name: "b", Weight: 1}))

	cfg.Experiments = append(cfg.Experiments, experiment.NewExperiment("test2",
		make(map[string]string),
		experiment.Alternative{Name: "a", Weight: 1},
		experiment.Alternative{Name: "b", Weight: 1}))

	cfg.Features = append(cfg.Features, feature.NewFeature("scrolling", 1, make(map[string]int)))

	config.InitConfig(&cfg)

	return &cfg
}
Пример #4
0
func TestBasicAB(t *testing.T) {
	newTest := experiment.NewExperiment("test",
		make(map[string]string),
		experiment.Alternative{Name: "a", Weight: 1},
		experiment.Alternative{Name: "b", Weight: 1})

	if newTest.Name != "test" {
		t.Errorf("Incorrect test name")
	}
}
Пример #5
0
func TestGetAlternate(t *testing.T) {
	newTest := experiment.NewExperiment("test", make(map[string]string),
		experiment.Alternative{Name: "a", Weight: 1}, experiment.Alternative{Name: "b", Weight: 1})

	alternate := newTest.GetAlternative("1")

	if !(alternate == "a" || alternate == "b") {
		t.Errorf("Invalid alternate returned %s", alternate)
	}

	alternate1 := newTest.GetAlternative("1")

	if alternate != alternate1 {
		t.Errorf("Inconsistent hash")
	}
}
Пример #6
0
func TestValidJson(t *testing.T) {
	testConfig, err := config.LoadConfig(exampleJsonConfig)

	if err != nil {
		t.Error(err)
	}

	if len(testConfig.Experiments) != 1 {
		t.Error("Example config should contain 1 experiments. Found:", len(testConfig.Experiments))
	}

	if len(testConfig.Groups) != 2 {
		t.Error("Example config should contain 2 groups. Found:", len(testConfig.Groups))
	}

	if reflect.DeepEqual(testConfig.Experiments[0],
		experiment.NewExperiment("progress_bar",
			make(map[string]string),
			experiment.Alternative{"green", 1},
			experiment.Alternative{"red", 1})) {
		t.Error("Wrong experiment for example config")
	}
}