登录
首页 >  Golang >  Go问答

模拟 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
}

我更喜欢使用 myconnectionmychannel,以避免任何混淆。
并使用 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
    }
}

这允许您通过模拟 connectionchannel 接口来编写单元测试,同时仍然能够在实际代码中使用真正的 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方法为了进行测试,您实际上不需要模拟该特定方法,而是通常调用它的结构和方法。

closechannelconnection 结构体的未导出方法,这意味着无法从包外部直接访问它。该方法是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
}

您的模拟实现模拟真实 connectionchannel 类型的行为。
虽然这不允许您直接测试内部 closechannel 方法,但它确实允许您使用公共接口测试依赖于它的行为。
这种方法遵循单元测试的最佳实践,您应该测试单元的公共接口而不是其内部实现细节。

今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>