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://") {
		return backends.NewRedisBackend(cnf, cnf.ResultBackend), 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://guest:gu[email protected]: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

	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)
	}
}