模拟 amqp091-go 接口困难
golang学习网今天将给大家带来《模拟 amqp091-go 接口困难》,感兴趣的朋友请继续看下去吧!以下内容将会涉及到等等知识点,如果你是正在学习Golang或者已经是大佬级别了,都非常欢迎也希望大家都能给我建议评论哈~希望能帮助到大家!
我正在尝试为 go amqp 消费者 https://github.com/rabbitmq/amqp091-go/blob/main/_examples/consumer/consumer.go 的精简版和稍作修改的版本编写单元测试一个简单的实用程序,用于使用 rabbitmq 队列中的消息并将它们中继到 aws sqs 队列。我在模拟诸如 connection 和 channel 结构之类的东西时遇到困难 - 对于 go 来说相当新 - 关于如何解决这个问题有什么想法吗?我在操场上写了代码的要点 - 删除了大部分代码以了解显着的部分。问题是这样的:
https://go.dev/play/p/ybsb6eu3sio
go: finding module for package github.com/rabbitmq/amqp091-go
go: downloading github.com/rabbitmq/amqp091-go v1.8.1
go: found github.com/rabbitmq/amqp091-go in github.com/rabbitmq/amqp091-go v1.8.1
# play
./prog.go:23:16: cannot use dial(url) (value of type *amqp091.connection) as connection value in assignment: *amqp091.connection does not implement connection (wrong type for method channel)
have channel() (*amqp091.channel, error)
want channel() (channel, error)
go build failed.
我试图模拟的实际功能: https://github.com/rabbitmq/amqp091-go/blob/579207b03cecc66c1b206679b79f267c8c734db7/connection.go#l843
说明问题的示例代码。
package main
import amqp "github.com/rabbitmq/amqp091-go"
type channel interface {
cancel(consumer string, nowait bool) error
}
type connection interface {
channel() (channel, error)
close() error
}
type consumer struct {
conn connection
channel channel
tag string
done chan error
}
func (c *consumer) consume(url string) error {
var err error
c.conn, err = dial(url)
return err
}
var dial = func(url string) (*amqp.connection, error) {
return &amqp.connection{}, nil
}
func main() {
}
// mocks in a test file
type mockconnection struct{}
func (m *mockconnection) channel() (channel, error) {
return &mockchannel{}, nil
}
func (m *mockconnection) close() error {
return nil
}
type mockchannel struct{}
func (m *mockchannel) cancel(consumer string, nowait bool) error {
return nil
}
//dial = func(url string) (*mockconnection, error) {
// return &mockconnection{}, nil
//}
因此,在模拟代码中,我可以使用 channel 而不是 *mockchannel:
func (m *mockConnection) Channel() (Channel, error) {
return &mockChannel{}, nil
}
但显然我无法更改 amqp091 代码来执行此操作,因此我被难住了。
正确答案
我在rabbitmq/amqp091-go connection.go一个>:
/*
channel opens a unique, concurrent server channel to process the bulk of amqp
messages. any error from methods on this receiver will render the receiver
invalid and a new channel should be opened.
*/
func (c *connection) channel() (*channel, error) {
return c.openchannel()
}
但是你有:
type connection interface {
channel() (channel, error)
close() error
}
我更喜欢使用 myconnection 或 mychannel,以避免任何混淆。
并使用 dial 函数返回 (myconnection, error) 而不是 (*amqp.connection, error):这将允许您在测试中使用真实的 amqp 连接或通过分配 dial 函数返回的模拟连接您的模拟实现。
然后,您可以在实际代码中使用类型断言来处理需要使用真实 amqp091 结构的情况。
例如:
func dosomethingwithchannel(channel mychannel) {
if realchannel, ok := channel.(*amqp.channel); ok {
// do something with realchannel
} else {
// handle mockchannel
}
}
这允许您通过模拟 connection 和 channel 接口来编写单元测试,同时仍然能够在实际代码中使用真正的 amqp091 结构。
但是...这意味着您的测试代码在应用程序代码中泄漏,这不是最佳实践。
避免这种情况的一种方法是让您的生产代码和测试代码都实现相同的接口,然后在实际代码中,您只与该接口交互。
这样,实际代码就不知道它是在处理真实实现还是模拟,并且您不会有任何测试代码泄漏到实际应用程序中。
package main
import (
"fmt"
amqp "github.com/rabbitmq/amqp091-go"
)
// define the interfaces
type mychannel interface {
cancel(consumer string, nowait bool) error
}
type myconnection interface {
channel() (mychannel, error)
close() error
}
// consumer struct
type consumer struct {
conn myconnection
channel mychannel
tag string
done chan error
}
func (c *consumer) consume(url string) error {
var err error
c.conn, err = dial(url)
if err != nil {
return err
}
c.channel, err = c.conn.channel()
return err
}
// use function variable for dial so it can be overridden in tests
var dial = func(url string) (myconnection, error) {
conn, err := amqp.dial(url)
if err != nil {
return nil, err
}
return &amqpconnection{conn}, nil
}
// wrappers around real amqp types to implement your interfaces
type amqpconnection struct {
*amqp.connection
}
func (a *amqpconnection) channel() (mychannel, error) {
ch, err := a.connection.channel()
if err != nil {
return nil, err
}
return &amqpchannel{ch}, nil
}
type amqpchannel struct {
*amqp.channel
}
func (a *amqpchannel) cancel(consumer string, nowait bool) error {
return a.channel.cancel(consumer, nowait)
}
func main() {
c := &consumer{}
err := c.consume("amqp://guest:guest@localhost:5672/")
fmt.println(err)
}
// in a separate _test.go file
type mockconnection struct{}
func (m *mockconnection) channel() (mychannel, error) {
return &mockchannel{}, nil
}
func (m *mockconnection) close() error {
return nil
}
type mockchannel struct{}
func (m *mockchannel) cancel(consumer string, nowait bool) error {
return nil
}
在测试中,您可以重写 dial 函数以返回模拟实现:
func testconsumer(t *testing.t) {
dial = func(url string) (myconnection, error) {
return &mockconnection{}, nil
}
// rest of your test code
}
这样,您的生产代码对测试代码没有任何了解,并且仅通过真实版本和模拟版本都实现的接口进行交互。
测试代码可以通过重写 dial 函数来注入模拟实现。
模拟closechannel方法为了进行测试,您实际上不需要模拟该特定方法,而是通常调用它的结构和方法。
closechannel 是 connection 结构体的未导出方法,这意味着无法从包外部直接访问它。该方法是amqp091-go库内部逻辑的一部分。
但是,您仍然可以通过模拟与其交互的公共方法和结构来测试依赖于该方法的行为。
在您的测试文件中:
type mockConnection struct {
// ... you can keep state here if needed to simulate connection behavior ...
}
func (m *mockConnection) Channel() (MyChannel, error) {
// Simulate behavior of opening a new channel
return &mockChannel{}, nil
}
func (m *mockConnection) Close() error {
// Simulate behavior of closing the connection, which would internally call closeChannel
return nil
}
type mockChannel struct{}
func (m *mockChannel) Close() error {
// Simulate behavior of closing the channel, which would internally call closeChannel
return nil
}
func TestSomething(t *testing.T) {
// Use the mockConnection in place of the real connection
conn := &mockConnection{}
// Your test logic here, e.g., opening and closing channels, and asserting the expected behavior
}
您的模拟实现模拟真实 connection 和 channel 类型的行为。
虽然这不允许您直接测试内部 closechannel 方法,但它确实允许您使用公共接口测试依赖于它的行为。
这种方法遵循单元测试的最佳实践,您应该测试单元的公共接口而不是其内部实现细节。
今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~
Vue3中echarts无法缩放如何解决
- 上一篇
- Vue3中echarts无法缩放如何解决
- 下一篇
- 西部数据重磅推出368TB“移动硬盘”,重量达13-15千克
-
- Golang · Go问答 | 15分钟前 | go · 垃圾回收 ·
- unique.Make 长期运行时会不会无限保留值
- 257浏览 收藏
-
- Golang · Go问答 | 44分钟前 |
- Handle 值相等是否意味着原始对象地址相同
- 390浏览 收藏
-
- Golang · Go问答 | 1小时前 |
- unique.Handle 能否用于包含切片的值
- 388浏览 收藏
-
- Golang · Go问答 | 1小时前 | GC · Go问答 · Go 垃圾回收 weak.Pointer runtime.KeepAlive 对象可达性
- 对象仍被使用时 weak 指针为什么可能已经失效
- 195浏览 收藏
-
- Golang · Go问答 | 2小时前 |
- weak.Pointer.Value 偶尔返回零值是不是数据竞争
- 153浏览 收藏
-
- Golang · Go问答 | 2小时前 | 并发 · 基准测试 · go · RunParallel B.Loop Go并行基准测试 PB.Next testing.Benchmark
- 并行基准测试能否直接改用 B.Loop
- 142浏览 收藏
-
- Golang · Go问答 | 2小时前 |
- testing.B.Loop 为什么不再需要手动读取 b.N
- 497浏览 收藏
-
- Golang · Go问答 | 3小时前 | go · testing · Go问答 · testing.B.Loop Go基准测试 循环外变量 benchmark状态
- B.Loop 中修改循环外变量为什么会影响基准结果
- 105浏览 收藏
-
- Golang · Go问答 | 3小时前 | 故障排查 · net/http · Go问答 · 反向代理 Sec-Fetch-Site Go CrossOriginProtection Origin Host 403误判
- 反向代理后 CrossOriginProtection 误判来源怎么办
- 201浏览 收藏
-
- Golang · Go问答 | 4小时前 | go · net/http ·
- CrossOriginProtection 为什么拒绝没有 Origin 的请求
- 186浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
-
- PubMedQA
- 深入了解PubMedQA生物医学问答数据集,涵盖其核心功能、使用方法及在临床决策、药物研发等场景的应用,助力提升NLP模型性能。
- 386次使用
-
- H2O EvalGPT
- H2O EvalGPT是H2O.ai推出的开源LLM评估平台,提供详细的大模型性能排行榜、行业特定基准测试及A/B测试功能,助您快速选择最适合项目的高性能大语言模型。
- 468次使用
-
- LMArena
- LMArena是加州大学伯克利分校推出的AI模型匿名评测平台。通过盲测投票机制,用户可对比不同大模型回答并生成实时排行榜,助力开发者优化模型及用户选择最佳AI工具。
- 475次使用
-
- HELM
- 深入了解斯坦福推出的HELM(Holistic Evaluation of Language Models)大模型评测体系。本文解析其核心功能、安装配置步骤及应用场景,涵盖准确性、公平性、鲁棒性等多维度指标,助力开发者全面优化语言模型性能。
- 415次使用
-
- MMBench
- MMBench是由上海人工智能实验室等机构联合推出的多模态基准测试平台,提供细粒度能力评估、大规模数据集及VLMEvalKit工具。本文详细介绍其核心功能、安装使用方法及应用场景,助力开发者全面评估多模态模型性能。
- 241次使用
-
- 用Nginx反向代理部署go写的网站。
- 2023-01-17 502浏览
-
- GoLand调式动态执行代码
- 2023-01-13 502浏览
-
- Go crypto/rand.Text 的长度为什么不是固定字符数
- 2026-10-04 501浏览
-
- Go strings.ToValidUTF8 清洗日志内容的边界
- 2026-10-03 501浏览
-
- Go tls.GetCertificate 为什么收不到空 ServerName 请求
- 2026-09-27 501浏览

