Ejemplo n.º 1
0
// BackendFactory creates a new object with backends.Backend interface
// Currently supported backends are AMQP and Memcache
func BackendFactory(cnf *config.Config) (backends.Backend, error) {
	if strings.HasPrefix(cnf.ResultBackend, "amqp://") {
		return backends.NewAMQPBackend(cnf), nil
	}

	if strings.HasPrefix(cnf.ResultBackend, "memcache://") {
		parts := strings.Split(cnf.ResultBackend, "memcache://")
		if len(parts) != 2 {
			return nil, fmt.Errorf(
				"Memcache result backend connection string should be in format memcache://server1:port,server2:port, instead got %s",
				cnf.ResultBackend,
			)
		}
		servers := strings.Split(parts[1], ",")
		return backends.NewMemcacheBackend(cnf, servers), nil
	}

	if strings.HasPrefix(cnf.ResultBackend, "redis://") {
		parts := strings.Split(cnf.ResultBackend, "redis://")
		if len(parts) != 2 {
			return nil, fmt.Errorf(
				"Redis result backend connection string should be in format redis://password@host:port, instead got %s",
				cnf.ResultBackend,
			)
		}

		var redisHost, redisPassword string

		parts = strings.Split(parts[1], "@")
		if len(parts) == 2 {
			redisHost = parts[1]
			redisPassword = parts[0]
		} else {
			redisHost = parts[0]
		}

		return backends.NewRedisBackend(cnf, redisHost, redisPassword), nil
	}

	if strings.HasPrefix(cnf.ResultBackend, "eager") {
		return backends.NewEagerBackend(), nil
	}

	return nil, fmt.Errorf("Factory failed with result backend: %v", cnf.ResultBackend)
}
Ejemplo n.º 2
0
func TestBackendFactory(t *testing.T) {
	var cnf config.Config

	// 1) AMQP backend test

	cnf = config.Config{ResultBackend: "amqp://*****:*****@localhost:5672/"}
	actual, err := BackendFactory(&cnf)

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

	expected := backends.NewAMQPBackend(&cnf)

	if !reflect.DeepEqual(actual, expected) {
		t.Errorf("conn = %v, want %v", actual, expected)
	}

	// 2) Memcache backend test

	cnf = config.Config{
		ResultBackend: "memcache://10.0.0.1:11211,10.0.0.2:11211",
	}
	actual, err = BackendFactory(&cnf)

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

	servers := []string{"10.0.0.1:11211", "10.0.0.2:11211"}
	expected = backends.NewMemcacheBackend(&cnf, servers)

	if !reflect.DeepEqual(actual, expected) {
		t.Errorf("conn = %v, want %v", actual, expected)
	}

	// 2) Redis backend test

	// with password
	cnf = config.Config{
		ResultBackend: "redis://password@localhost:6379",
	}
	actual, err = BackendFactory(&cnf)

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

	expected = backends.NewRedisBackend(&cnf, "localhost:6379", "password")

	if !reflect.DeepEqual(actual, expected) {
		t.Errorf("conn = %v, want %v", actual, expected)
	}

	// without password
	cnf = config.Config{
		ResultBackend: "redis://localhost:6379",
	}
	actual, err = BackendFactory(&cnf)

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

	expected = backends.NewRedisBackend(&cnf, "localhost:6379", "")

	if !reflect.DeepEqual(actual, expected) {
		t.Errorf("conn = %v, want %v", actual, expected)
	}
}