Example #1
0
func TestAddBinding(t *testing.T) {
	var ex = NewExchange("ex1", EX_TYPE_TOPIC, true, true, false, amqp.NewTable(), false, make(chan *Exchange))
	ex.deleteActive = time.Now()
	// bad binding
	_, err := binding.NewBinding("q1", "ex1", "~!@", amqp.NewTable(), true)
	if err == nil {
		t.Errorf("No error with bad binding!")
	}
	if len(ex.bindings) != 0 {
		t.Errorf("Bad binding was added despite error")
	}
	// duplicate binding
	var b = bindingHelper("q1", "ex1", "a.b.c", true)
	err = ex.AddBinding(b, -1)
	if err != nil {
		t.Errorf(err.Error())
	}
	if len(ex.bindings) != 1 {
		t.Errorf("Wrong number of bindings")
	}
	err = ex.AddBinding(b, -1)
	if err != nil {
		t.Errorf(err.Error())
	}
	if len(ex.bindings) != 1 {
		t.Errorf("Wrong number of bindings")
	}
	if ex.deleteActive != time.Unix(0, 0) {
		t.Errorf("Error did not reset time")
	}

}
Example #2
0
func bindingHelper(queue string, exchange string, key string, topic bool) *binding.Binding {
	b, err := binding.NewBinding(queue, exchange, key, amqp.NewTable(), topic)
	if err != nil {
		panic(err.Error())
	}
	return b
}
Example #3
0
func TestAutoDeleteTimeout(t *testing.T) {
	var deleter = make(chan *Exchange)
	var ex = NewExchange("ex1", EX_TYPE_TOPIC, true, true, false, amqp.NewTable(), false, deleter)
	ex.autodeletePeriod = 10 * time.Millisecond
	ex.AddBinding(bindingHelper("q1", "ex1", "a.b.c", true), -1)
	var b, _ = binding.NewBinding("q1", "ex1", "a.b.c", amqp.NewTable(), true)
	ex.RemoveBinding(b)
	var toDelete = <-deleter
	if ex.Name != toDelete.Name {
		t.Errorf("Integrity error in delete")
	}
}
Example #4
0
func (channel *Channel) queueBind(method *amqp.QueueBind) *amqp.AMQPError {
	var classId, methodId = method.MethodIdentifier()

	if len(method.Queue) == 0 {
		if len(channel.lastQueueName) == 0 {
			return amqp.NewSoftError(404, "Queue not found", classId, methodId)
		} else {
			method.Queue = channel.lastQueueName
		}
	}

	// Check exchange
	var exchange, foundExchange = channel.server.exchanges[method.Exchange]
	if !foundExchange {
		return amqp.NewSoftError(404, "Exchange not found", classId, methodId)
	}

	// Check queue
	var queue, foundQueue = channel.server.queues[method.Queue]
	if !foundQueue || queue.Closed {
		return amqp.NewSoftError(404, fmt.Sprintf("Queue not found: %s", method.Queue), classId, methodId)
	}

	if queue.ConnId != -1 && queue.ConnId != channel.conn.id {
		return amqp.NewSoftError(405, fmt.Sprintf("Queue is locked to another connection"), classId, methodId)
	}

	// Create binding
	b, err := binding.NewBinding(method.Queue, method.Exchange, method.RoutingKey, method.Arguments, exchange.IsTopic())
	if err != nil {
		return amqp.NewSoftError(500, err.Error(), classId, methodId)
	}

	// Add binding
	err = exchange.AddBinding(b, channel.conn.id)
	if err != nil {
		return amqp.NewSoftError(500, err.Error(), classId, methodId)
	}

	// Persist durable bindings
	if exchange.Durable && queue.Durable {
		var err = b.Persist(channel.server.db)
		if err != nil {
			return amqp.NewSoftError(500, err.Error(), classId, methodId)
		}
	}

	if !method.NoWait {
		channel.SendMethod(&amqp.QueueBindOk{})
	}
	return nil
}
Example #5
0
func (channel *Channel) queueUnbind(method *amqp.QueueUnbind) *amqp.AMQPError {
	var classId, methodId = method.MethodIdentifier()

	// Check queue
	if len(method.Queue) == 0 {
		if len(channel.lastQueueName) == 0 {
			return amqp.NewSoftError(404, "Queue not found", classId, methodId)
		} else {
			method.Queue = channel.lastQueueName
		}
	}

	var queue, foundQueue = channel.server.queues[method.Queue]
	if !foundQueue {
		return amqp.NewSoftError(404, "Queue not found", classId, methodId)
	}

	if queue.ConnId != -1 && queue.ConnId != channel.conn.id {
		return amqp.NewSoftError(405, "Queue is locked to another connection", classId, methodId)
	}

	// Check exchange
	var exchange, foundExchange = channel.server.exchanges[method.Exchange]
	if !foundExchange {
		return amqp.NewSoftError(404, "Exchange not found", classId, methodId)
	}

	var binding, err = binding.NewBinding(
		method.Queue,
		method.Exchange,
		method.RoutingKey,
		method.Arguments,
		exchange.IsTopic(),
	)

	if err != nil {
		return amqp.NewSoftError(500, err.Error(), classId, methodId)
	}

	if queue.Durable && exchange.Durable {
		err := binding.Depersist(channel.server.db)
		if err != nil {
			return amqp.NewSoftError(500, "Could not de-persist binding!", classId, methodId)
		}
	}

	if err := exchange.RemoveBinding(binding); err != nil {
		return amqp.NewSoftError(500, err.Error(), classId, methodId)
	}
	channel.SendMethod(&amqp.QueueUnbindOk{})
	return nil
}
Example #6
0
func (server *Server) addQueue(q *queue.Queue) error {
	server.serverLock.Lock()
	defer server.serverLock.Unlock()
	server.queues[q.Name] = q
	var defaultExchange = server.exchanges[""]
	var defaultBinding, err = binding.NewBinding(q.Name, "", q.Name, amqp.NewTable(), false)
	if err != nil {
		return err
	}
	defaultExchange.AddBinding(defaultBinding, q.ConnId)
	q.Start()
	return nil
}