亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何模擬 amqp.Dial 等庫中的函數

如何模擬 amqp.Dial 等庫中的函數

Go
大話西游666 2022-07-11 15:05:09
我正在研究一個小型 AMQP 消費者,我想測試我的消費者代碼,但我很難模擬amqp.Dial. 我添加了一些接口,以便我可以模擬Connection并Channel添加一個屬性,以便我可以控制撥號功能://consumer.gotype 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接口,這會起作用:/模擬方法的正確方法是amqp.Dial什么?
查看完整描述

1 回答

?
MMMHUHU

TA貢獻1834條經驗 獲得超8個贊

給定以下類型:


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)

您可以創建委托給實際代碼的簡單包裝器:


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()

}


查看完整回答
反對 回復 2022-07-11
  • 1 回答
  • 0 關注
  • 107 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號