模拟 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无法缩放如何解决

- 下一篇
- 西部数据重磅推出368TB“移动硬盘”,重量达13-15千克
-
- Golang · Go问答 | 1年前 |
- 在读取缓冲通道中的内容之前退出
- 139浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 戈兰岛的全球 GOPRIVATE 设置
- 204浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何将结构作为参数传递给 xml-rpc
- 325浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何用golang获得小数点以下两位长度?
- 477浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何通过 client-go 和 golang 检索 Kubernetes 指标
- 486浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 将多个“参数”映射到单个可变参数的习惯用法
- 439浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 将 HTTP 响应正文写入文件后出现 EOF 错误
- 357浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 结构中映射的匿名列表的“复合文字中缺少类型”
- 352浏览 收藏
-
- Golang · Go问答 | 1年前 |
- NATS Jetstream 的性能
- 101浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何将复杂的字符串输入转换为mapstring?
- 440浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 相当于GoLang中Java将Object作为方法参数传递
- 212浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何确保所有 goroutine 在没有 time.Sleep 的情况下终止?
- 143浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 484次学习
-
- AI Make Song
- AI Make Song是一款革命性的AI音乐生成平台,提供文本和歌词转音乐的双模式输入,支持多语言及商业友好版权体系。无论你是音乐爱好者、内容创作者还是广告从业者,都能在这里实现“用文字创造音乐”的梦想。平台已生成超百万首原创音乐,覆盖全球20个国家,用户满意度高达95%。
- 16次使用
-
- SongGenerator
- 探索SongGenerator.io,零门槛、全免费的AI音乐生成器。无需注册,通过简单文本输入即可生成多风格音乐,适用于内容创作者、音乐爱好者和教育工作者。日均生成量超10万次,全球50国家用户信赖。
- 13次使用
-
- BeArt AI换脸
- 探索BeArt AI换脸工具,免费在线使用,无需下载软件,即可对照片、视频和GIF进行高质量换脸。体验快速、流畅、无水印的换脸效果,适用于娱乐创作、影视制作、广告营销等多种场景。
- 12次使用
-
- 协启动
- SEO摘要协启动(XieQiDong Chatbot)是由深圳协启动传媒有限公司运营的AI智能服务平台,提供多模型支持的对话服务、文档处理和图像生成工具,旨在提升用户内容创作与信息处理效率。平台支持订阅制付费,适合个人及企业用户,满足日常聊天、文案生成、学习辅助等需求。
- 16次使用
-
- Brev AI
- 探索Brev AI,一个无需注册即可免费使用的AI音乐创作平台,提供多功能工具如音乐生成、去人声、歌词创作等,适用于内容创作、商业配乐和个人创作,满足您的音乐需求。
- 17次使用
-
- GoLand调式动态执行代码
- 2023-01-13 502浏览
-
- 用Nginx反向代理部署go写的网站。
- 2023-01-17 502浏览
-
- Golang取得代码运行时间的问题
- 2023-02-24 501浏览
-
- 请问 go 代码如何实现在代码改动后不需要Ctrl+c,然后重新 go run *.go 文件?
- 2023-01-08 501浏览
-
- 如何从同一个 io.Reader 读取多次
- 2023-04-11 501浏览