func TestWrite(t *testing.T) {
	// Setup
	var dbFile = "TestWrite.db"
	os.Remove(dbFile)
	defer os.Remove(dbFile)
	rhs := []amqp.MessageResourceHolder{&TestResourceHolder{}}
	ms, err := NewMessageStore(dbFile)
	// Create messages
	msg1 := amqp.RandomMessage(true)
	msg2 := amqp.RandomMessage(true)
	fmt.Printf("Creating ids: %d, %d\n", msg1.Id, msg2.Id)

	// Store messages and delete one
	fmt.Println("Adding message 1")
	ms.AddMessage(msg1, []string{"some-queue", "some-other-queue"})
	fmt.Println("Adding message 2")
	qm2Map, err := ms.AddMessage(msg2, []string{"some-queue"})
	_, err = ms.GetAndDecrRef(qm2Map["some-queue"][0], "some-queue", rhs)
	if err != nil {
		t.Errorf(err.Error())
		return
	}

	// Close DB
	ms.db.Close()
	keys := map[int64]bool{
		msg1.Id: true,
		// msg2.Id: true,
	}
	// Assert that the DB is correct
	err = assertKeys(dbFile, keys)
	if err != nil {
		t.Errorf(err.Error())
		return
	}

	// try loading from disk in the message store
	ms2, err := NewMessageStore(dbFile)
	_, err = ms2.LoadQueueFromDisk("some-queue")
	if err != nil {
		t.Errorf(err.Error())
		return
	}
	_, err = ms2.LoadQueueFromDisk("some-other-queue")
	if err != nil {
		t.Errorf(err.Error())
		return
	}
}
func TestExchangeRoutingFanout(t *testing.T) {
	var exFanout = NewExchange(
		"exf",
		EX_TYPE_FANOUT,
		false,
		false,
		false,
		amqp.NewTable(),
		false,
		make(chan *Exchange),
	)
	exFanout.AddBinding(bindingHelper("q1", "exf", "rk-1", false), -1)
	exFanout.AddBinding(bindingHelper("q2", "exf", "rk-2", false), -1)

	// Create a random message, won't route by default
	var msg = amqp.RandomMessage(false)
	msg.Method.Exchange = "exf"

	res, err := exFanout.QueuesForPublish(msg)
	if err != nil {
		t.Errorf(err.Msg)
	}
	_, foundQ1 := res["q1"]
	_, foundQ2 := res["q2"]
	if !foundQ1 || !foundQ2 {
		t.Errorf("Failed to route fanout message %v %v", foundQ1, foundQ2)
	}
}
func TestExchangeRoutingTopic(t *testing.T) {
	var exTopic = NewExchange(
		"ext",
		EX_TYPE_TOPIC,
		false,
		false,
		false,
		amqp.NewTable(),
		false,
		make(chan *Exchange),
	)
	exTopic.AddBinding(bindingHelper("q1", "ext", "api.msg.*.json", true), -1)
	exTopic.AddBinding(bindingHelper("q1", "ext", "api.*.home.json", true), -1)
	exTopic.AddBinding(bindingHelper("q2", "ext", "api.msg.home.json", true), -1)
	exTopic.AddBinding(bindingHelper("q3", "ext", "log.#", true), -1)

	// Create a random message, won't route by default
	var msg = amqp.RandomMessage(false)
	msg.Method.Exchange = "ext"

	// no match
	res, err := exTopic.QueuesForPublish(msg)
	if err != nil {
		t.Errorf(err.Msg)
	}
	if len(res) > 0 {
		t.Errorf("Routed message which should not have routed", res)
	}

	// one match on #
	msg.Method.RoutingKey = "log.msg.home.json"
	res, err = exTopic.QueuesForPublish(msg)
	if err != nil {
		t.Errorf(err.Msg)
	}
	_, foundLog := res["q3"]
	if !foundLog || len(res) != 1 {
		t.Errorf("Bad results routing to # key")
	}

	// one queue on two matches
	msg.Method.RoutingKey = "api.msg.home.json"
	res, err = exTopic.QueuesForPublish(msg)
	if err != nil {
		t.Errorf(err.Msg)
	}
	_, foundQ1 := res["q1"]
	_, foundQ2 := res["q2"]
	if !foundQ1 || !foundQ2 || len(res) != 2 {
		t.Errorf("Bad results routing to multiply-bound * key")
	}

}
func TestExchangeRoutingDirect(t *testing.T) {
	// Make exchange ang binding
	var exDirect = NewExchange(
		"exd",
		EX_TYPE_DIRECT,
		false,
		false,
		false,
		amqp.NewTable(),
		false,
		make(chan *Exchange),
	)
	exDirect.AddBinding(bindingHelper("q1", "exd", "rk-1", false), -1)

	// Create a random message, won't route by default
	var msg = amqp.RandomMessage(false)
	// Test wrong exchange for coverage
	res, err := exDirect.QueuesForPublish(msg)
	if err != nil {
		t.Errorf(err.Msg)
	}
	if len(res) > 0 {
		t.Errorf("Routed message which should not have routed", res)
	}

	// Test right exchange, wrong key
	msg.Method.Exchange = "exd"

	res, err = exDirect.QueuesForPublish(msg)
	if err != nil {
		t.Errorf(err.Msg)
	}
	if len(res) > 0 {
		t.Errorf("Routed message which should not have routed", res)
	}

	// Set the right values for routing
	msg.Method.RoutingKey = "rk-1"

	res, err = exDirect.QueuesForPublish(msg)
	if err != nil {
		t.Errorf(err.Msg)
	}
	if _, found := res["q1"]; !found {
		t.Errorf("Failed to route direct message: %s", res)
	}
}