// integration test
func TestRmqIntegration(t *testing.T) {
	mt := plugin.MetricType{
		Namespace_:          core.NewNamespace("foo", "bar"),
		LastAdvertisedTime_: time.Now(),
		Version_:            1,
		Data_:               1,
	}
	data, _, err := plugin.MarshalMetricTypes(plugin.SnapGOBContentType, []plugin.MetricType{mt})
	Convey("Metric should encode successfully", t, func() {
		Convey("So err should be nil", func() {
			So(err, ShouldBeNil)
		})
	})
	rmqPub := NewRmqPublisher()
	cp, _ := rmqPub.GetConfigPolicy()
	config := map[string]ctypes.ConfigValue{
		"uri":           ctypes.ConfigValueStr{Value: "127.0.0.1:5672"},
		"exchange_name": ctypes.ConfigValueStr{Value: "snap"},
		"routing_key":   ctypes.ConfigValueStr{Value: "metrics"},
		"exchange_type": ctypes.ConfigValueStr{Value: "fanout"},
	}
	cfg, _ := cp.Get([]string{""}).Process(config)

	cKill := make(chan struct{})
	cMetrics, errc := connectToAmqp(cKill)
	err = rmqPub.Publish(plugin.SnapGOBContentType, data, *cfg)
	Convey("Publish should successfully publish metric to RabbitMQ server", t, func() {
		Convey("Publish data to RabbitMQ should not error", func() {
			So(err, ShouldBeNil)
		})
		Convey("We should be able to retrieve metric from RabbitMQ Server and validate", func() {
			Convey("Connecting to RabbitMQ server should not error", func() {
				So(errc, ShouldBeNil)
			})
			Convey("Validate metric", func() {
				if err == nil {
					select {
					case metric := <-cMetrics:
						var metrix []plugin.MetricType
						err := json.Unmarshal(metric, &metrix)
						So(err, ShouldBeNil)
						So(metrix[0].Version(), ShouldEqual, mt.Version_)
						cKill <- struct{}{}
					case <-time.After(time.Second * 10):
						t.Fatal("Timeout when waiting for AMQP message")
					}

				}
			})
		})

	})
}
// sendToHeka sends array of snap metrics to Heka
func (shc *SnapHekaClient) sendToHeka(metrics []plugin.MetricType) error {
	pid := int32(os.Getpid())
	hostname, _ := os.Hostname()

	// Initializes Heka message encoder
	encoder := client.NewProtobufEncoder(nil)

	// Creates Heka message sender
	sender, err := client.NewNetworkSender(shc.hekaScheme, shc.hekaHost)
	if err != nil {
		logger.WithField("_block", "sendToHeka").Error("create NewNetworkSender error: ", err)
		return err
	}

	var buf []byte
	for _, m := range metrics {
		b, _, e := plugin.MarshalMetricTypes(plugin.SnapJSONContentType, []plugin.MetricType{m})
		if e != nil {
			logger.WithField("_block", "sendToHeka").Error("marshal metric error: ", m)
			continue
		}

		// Converts snap metric to Heka message
		msg, err := createHekaMessage(string(b), m, pid, hostname)
		if err != nil {
			logger.WithField("_block", "sendToHeka").Error("create message error: ", err)
			continue
		}
		err = encoder.EncodeMessageStream(msg, &buf)
		if err != nil {
			logger.WithField("_block", "sendToHeka").Error("encoding error: ", err)
			continue
		}

		err = sender.SendMessage(buf)
		if err != nil {
			logger.WithField("_block", "sendToHeka").Error("sending message error: ", err)
		}
	}
	sender.Close()
	return nil
}