예제 #1
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

	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)
	}
}
예제 #2
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,
			)
		}
		return brokers.NewRedisBroker(cnf, parts[1]), nil
	}

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

	return nil, fmt.Errorf("Factory failed with broker URL: %v", cnf.Broker)
}