模拟 amqp091-go 接口困难
来源:stackoverflow
时间:2024-04-16 08:48:32 384浏览 收藏
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学习网公众号,一起学习编程~
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习