- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个小型 AMQP 消费者,我想测试我的消费者代码,但我很难模拟 amqp.Dial
。我添加了一些接口(interface)以便我可以模拟 Connection
和 Channel
并添加了一个属性以便我可以控制拨号功能:
//consumer.go
type AmqpChannel interface {
ExchangeDeclare(name, kind string, durable, autoDelete, internal, noWait bool, args amqp.Table) error
QueueDeclare(name string, durable, autoDelete, exclusive, noWait bool, args amqp.Table) (amqp.Queue, error)
QueueBind(name, key, exchange string, noWait bool, args amqp.Table) error
Consume(queue, consumer string, autoAck, exclusive, noLocal, noWait bool, args amqp.Table) (<-chan amqp.Delivery, error)
Publish(exchange, key string, mandatory, immediate bool, msg amqp.Publishing) error
}
type AmqpConnection interface {
Channel() (AmqpChannel, error)
Close() error
}
type AmqpDial func(url string) (AmqpConnection, error)
type MyConsumer struct {
HostDsn string
channel AmqpChannel
queue amqp.Queue
connection AmqpConnection
DialFunc AmqpDial
}
func (c *MyConsumer) Connect() error {
var err error
c.connection, err = c.DialFunc(c.HostDsn)
...
这似乎接近我想要达到的目标,我可以这样指定我的测试:
func TestConsumer(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
var myConsumer = consumer.MyConsumer{
HostDsn: "test",
DialFunc: func(url string) (consumer.AmqpConnection, error) {
return mocks.NewMockAmqpConnection(mockCtrl), nil
},
}
_ = myConsumer.Connect()
}
但我无法在主例程中将原始 amqp.Dial
作为 dial-func 传递:
myConsumer := consumer.MyConsumer{
HostDsn: fmt.Sprintf(
"amqp://%s:%s@rabbitmq:5672/?heartbeat=5s",
os.Getenv("RABBITMQ_USER"),
url.QueryEscape(os.Getenv("RABBITMQ_PASSWORD")),
),
DialFunc: amqp.Dial,
}
给予
./main.go:28:9: cannot use amqp.Dial (type func(string) (*amqp.Connection, error)) as type consumer.AmqpDial in field value
我希望/认为,作为 amqp.Connection
实现 AmqpConnection
接口(interface),这会起作用:/模拟 amqp.Dial
等方法的正确方法是什么?
附言:我知道 https://github.com/NeowayLabs/wabbit但我更愿意在较低层次上解决这个问题:)
更新:@mkopriva 建议使用另一种包装方法,所以我尝试了:
func getDialer(url string) (consumer.AmqpConnection, error) {
var connection, err = amqp.Dial(url)
return connection, err
}
myConsumer := consumer.myConsumer{
HostDsn: fmt.Sprintf(
"amqp://%s:%s@rabbitmq:5672/?heartbeat=5s",
os.Getenv("RABBITMQ_USER"),
url.QueryEscape(os.Getenv("RABBITMQ_PASSWORD")),
),
DialFunc: getDialer,
}
但这会导致:
cannot use connection (type *amqp.Connection) as type consumer.AmqpConnection in return argument:
*amqp.Connection does not implement consumer.AmqpConnection (wrong type for Channel method)
have Channel() (*amqp.Channel, error)
want Channel() (consumer.AmqpChannel, error)
make: *** [Makefile:4: build-consumer] Error 2
最佳答案
给定以下类型:
type AmqpChannel interface {
ExchangeDeclare(name, kind string, durable, autoDelete, internal, noWait bool, args amqp.Table) error
QueueDeclare(name string, durable, autoDelete, exclusive, noWait bool, args amqp.Table) (amqp.Queue, error)
QueueBind(name, key, exchange string, noWait bool, args amqp.Table) error
Consume(queue, consumer string, autoAck, exclusive, noLocal, noWait bool, args amqp.Table) (<-chan amqp.Delivery, error)
Publish(exchange, key string, mandatory, immediate bool, msg amqp.Publishing) error
}
type AmqpConnection interface {
Channel() (AmqpChannel, error)
Close() error
}
type AmqpDial func(url string) (AmqpConnection, error)
您可以创建委托(delegate)给实际代码的简单包装器:
func AmqpDialWrapper(url string) (AmqpConnection, error) {
conn, err := amqp.Dial(url)
if err != nil {
return nil, err
}
return AmqpConnectionWrapper{conn}, nil
}
type AmqpConnectionWrapper struct {
conn *amqp.Connection
}
// If *amqp.Channel does not satisfy the consumer.AmqpChannel interface
// then you'll need another wrapper, a AmqpChannelWrapper, that implements
// the consumer.AmqpChannel interface and delegates to *amqp.Channel.
func (w AmqpConnectionWrapper) Channel() (AmqpChannel, error) {
return w.conn.Channel()
}
func (w AmqpConnectionWrapper) Close() error {
return w.conn.Close()
}
关于go - 如何模拟 amqp.Dial 等库中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64501096/
在 Spring AMQP 项目中, 如果 messageProperties 没有 messageId,它们总是创建 messageId。 像这样.. if (this.createMessageI
我不确定我对 errorHandler 和 returnExceptions 的理解是否正确。 但这是我的目标:我从 App_A 发送了一条消息,使用 @RabbitListener 在 App_B
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我们希望将大型机放在总线上。我相信是AS400。为此,我想让 CICS 大型机向代理发送 AMQP 消息。有几十个AMQP客户端,包括JMS客户端。我对大型机上可能发生的事情了解不够,无法判断是否可以
我们希望将大型机放在公共(public)汽车上。我相信它是AS400。为此,我想让 CICS 大型机向代理发送 AMQP 消息。有几十个 AMQP 客户端,包括 JMS 客户端。我不太了解大型机上的可
我即将实现一个基于 PHP 的系统,该系统使用 RabbitMQ。我可以看出那里有 2 个成熟的库:PECL AMQP和 php-amqp . 我将同时为客户端和工作人员使用 PHP。 有人对这两个库
我正在使用 RabbitMQ 和 ruby-amqp与 Rails。当 Controller 收到消息时,我执行以下操作: def create AMQP.start("amqp://localh
我有两个困惑。 1.如果消息监听器抛出RuntimeException,SimpleMessageListenrContainer会停止吗?2.如果SimpleMessageListenerConta
我们在绑定(bind)到 Rabbit MQ 实例的 Java 应用程序的日志中遇到以下异常。 这是必须要注意的事情,表示Spring AMQP的实现中存在问题,还是可以忽略的事情?在后一种情况下,此
场景:微服务从 RabbitMQ 队列中获取消息,将其转换为对象,然后微服务对外部服务进行 REST 调用。 它将处理成千上万条这样的消息,如果我们知道外部 Rest 服务已关闭,有没有办法告诉我的消
我有一个使用带有 webflux 的 Boot 2.0 的应用程序,并且有一个端点返回 ServerSentEvent 的 Flux。这些事件是通过利用 spring-amqp 从 RabbitMQ
我正在尝试使用 streadway/amqp 连接到 RabbitMQ 总线Go 的驱动程序。我正在处理重新连接例程,为此,我有一个 rabbitMQConsume 函数调用 rabbitMQConn
我正在尝试通过 SSL 连接到 RabbitMQ。我遵循了此处链接的 RabbitMQ SSL 文档 https://www.rabbitmq.com/ssl.html根据 RabbitMQ SSL
这些 amqp 客户端库之间有什么区别?哪一个是最推荐的?主要区别是什么? 最佳答案 我会推荐 amqp.node和 bramqp通过 node-amqp。 node-amqp 有很多错误并且维护不善
我得到的错误: 2019-12-09 06:39:33.189 ERROR 107132 --- [http-nio-8082-exec-5] o.a.c.c.C.[.[.[/].[dispatche
所以我已经让 MQTT -> MQTT 和 AMQP -> AMQP 工作;不过,MQTT -> AMQP 的翻译似乎在某处不起作用。这是我的测试,如果我的“监听器”也在使用 paho 的 MQTT
我得到的错误: 2019-12-09 06:39:33.189 ERROR 107132 --- [http-nio-8082-exec-5] o.a.c.c.C.[.[.[/].[dispatche
几天前我问了同样的问题:Unable to "Peek" messages from an Azure Service Bus Queue using AMQP and Node 。我再次问同样的问题
我有一个当前与 RabbitMQ 集成的组件。我想将 RabbitMQ 替换为 Azure Event Hub,因为我们现在位于云中。 AMQP 0.9.1 与 AMQP 1.0 兼容吗?交换会无缝进
我有一个当前与 RabbitMQ 集成的组件。我想将 RabbitMQ 替换为 Azure Event Hub,因为我们现在位于云中。 AMQP 0.9.1 与 AMQP 1.0 兼容吗?交换会无缝进
我是一名优秀的程序员,十分优秀!