Example #1
0
// BrokerFactory creates a new object with brokers.Broker interface
// Currently only AMQP broker is supported
func BrokerFactory(cnf *config.Config) (brokers.Broker, error) {
	if strings.HasPrefix(cnf.Broker, "amqp://") {
		return brokers.NewAMQPBroker(cnf), nil
	}

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

		var redisHost, redisPassword string

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

		return brokers.NewRedisBroker(cnf, redisHost, redisPassword), nil
	}

	if strings.HasPrefix(cnf.Broker, "eager") {
		return brokers.NewEagerBroker(), nil
	}

	return nil, fmt.Errorf("Factory failed with broker URL: %v", cnf.Broker)
}
Example #2
0
func TestBrokerFactory(t *testing.T) {
	var cnf config.Config

	// 1) AMQP broker test

	cnf = config.Config{
		Broker:       "amqp://*****:*****@localhost:5672/",
		Exchange:     "machinery_exchange",
		ExchangeType: "direct",
		DefaultQueue: "machinery_tasks",
		BindingKey:   "machinery_task",
	}

	actual, err := BrokerFactory(&cnf)
	if err != nil {
		t.Errorf(err.Error())
	}

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

	// 1) Redis broker test

	// with password
	cnf = config.Config{
		Broker:       "redis://[email protected]:6379",
		DefaultQueue: "machinery_tasks",
	}

	actual, err = BrokerFactory(&cnf)
	if err != nil {
		t.Errorf(err.Error())
	}

	expected = brokers.NewRedisBroker(&cnf, "localhost:6379", "password")
	if !reflect.DeepEqual(actual, expected) {
		t.Errorf("conn = %v, want %v", actual, expected)
	}

	// without password
	cnf = config.Config{
		Broker:       "redis://localhost:6379",
		DefaultQueue: "machinery_tasks",
	}

	actual, err = BrokerFactory(&cnf)
	if err != nil {
		t.Errorf(err.Error())
	}

	expected = brokers.NewRedisBroker(&cnf, "localhost:6379", "")
	if !reflect.DeepEqual(actual, expected) {
		t.Errorf("conn = %v, want %v", actual, expected)
	}
}