登录
首页 >  Golang >  Go问答

测试 Go 中封装多个客户端的方法的优雅方式是什么?

来源:stackoverflow

时间:2024-03-13 15:39:29 286浏览 收藏

哈喽!今天心血来潮给大家带来了《测试 Go 中封装多个客户端的方法的优雅方式是什么?》,想必大家应该对Golang都不陌生吧,那么阅读本文就都不会很困难,以下内容主要涉及到,若是你正在学习Golang,千万别错过这篇文章~希望能帮助到你!

问题内容

我有一个 client 结构,它包装了多个客户端(etcd 和 libvirt)。像这样的东西:

type client struct {
  etcd    *clientv3
  libvirt *libvirt.connect
}

一旦我的库的客户端想要关闭其句柄,我就想关闭这两个句柄。所以我有:

func (c *Client) Close() error {
    c.etcd.Close()
    c.libvirt.Close()
    // Error handling excluded for brevity
}

测试这个的优雅方法是什么?我当前最好的选择是创建两个接口,一个用于两个包装的客户端中的每一个。这些接口将包括我的库使用的两个客户端的每一个方法。这应该使得传递某种模拟而不是真正的客户变得相对容易。这可能是前进的方向,但感觉很尴尬。

我还有哪些其他选择?


解决方案


受到 powar 的评论的鼓励,我的想法很好,我继续这样做:

我更改了 client 结构以使用 libvirt 和 etcd 连接的接口:

type etcdclient interface {
}

type libvirtclient interface {
}

type client struct {
    etcd    etcdclient
    libvirt libvirtclient
}

当我尝试编译该包时,我收到一条类似如下的错误消息:

./main.go:17:18: c.etcd.close undefined (type etcdclient is interface with no methods)
./main.go:21:24: c.libvirt.close undefined (type libvirtclient is interface with no methods)

不足为奇。然后,我向接口添加了最简单的 close() 方法:

type etcdclient interface {
    close()
}

type libvirtclient interface {
    close()
}

再次编译给了我:

./main.go:56:10: cannot use etcd (type *clientv3.client) as type etcdclient in assignment:
    *clientv3.client does not implement etcdclient (wrong type for close method)
        have close() error
        want close()
./main.go:62:13: cannot use lv (type *libvirt.connect) as type libvirtclient in assignment:
    *libvirt.connect does not implement libvirtclient (wrong type for close method)
        have close() (int, error)
        want close()

然后我用它来填写接口定义:

type etcdclient interface {
    close() error
}

type libvirtclient interface {
    close() (int, error)
}

当然,clos​​e 非常简单,我不必经历这个,但正如我之前提到的,我在这些接口上调用许多方法,这样就可以让编译器帮助我变得非常简单填写接口定义。

对于测试,我可以制作假货(模拟?存根?我总是忘记其中的区别)。这是完整的测试文件:

package main

import (
    "errors"
    "testing"
)

type fakeetcdclient struct {
    wasclosed   bool
    failtoclose bool
}

func (f *fakeetcdclient) close() error {
    if f.failtoclose {
        return errors.new("fake etcd failed to close")
    }
    f.wasclosed = true
    return nil
}

type fakelibvirtclient struct {
    wasclosed   bool
    failtoclose bool
}

func (f *fakelibvirtclient) close() (int, error) {
    if f.failtoclose {
        return 0, errors.new("fake libvirt failed to close")
    }
    f.wasclosed = true
    return 0, nil
}

func testclient_close(t *testing.t) {
    type fields struct {
        etcd    etcdclient
        libvirt libvirtclient
    }
    tests := []struct {
        name    string
        fields  fields
        wanterr bool
    }{
        {"happy path", fields{&fakeetcdclient{}, &fakelibvirtclient{}}, false},
        {"etcd fails", fields{&fakeetcdclient{failtoclose: true}, &fakelibvirtclient{}}, true},
        {"libvirt fails", fields{&fakeetcdclient{}, &fakelibvirtclient{failtoclose: true}}, true},
    }
    for _, tt := range tests {
        t.run(tt.name, func(t *testing.t) {
            c := &client{
                etcd:    tt.fields.etcd,
                libvirt: tt.fields.libvirt,
            }
            if err := c.close(); (err != nil) != tt.wanterr {
                t.errorf("client.close() error = %v, wanterr %v", err, tt.wanterr)
            } else {
                if !tt.wanterr {
                    // we only check if the clients have been closed if
                    // client.close() returns successfully.
                    if !c.etcd.(*fakeetcdclient).wasclosed {
                        t.error("etcd connection was not closed")
                    }
                    if !c.libvirt.(*fakelibvirtclient).wasclosed {
                        t.error("libvirt connection was not closed")
                    }
                }
            }
        })
    }
}

正如我在评论中提到的,您可以创建一个 clos​​ableclient ,如下所示。由于您的每个客户端都有 clos​​e 方法,因此您可以执行此操作。在您的测试文件中,您可以创建只需要实现 clos​​e 方法的模拟客户端。您不需要让接口实现所有方法。在代码中,您可以使用类型断言将 clos​​ableclient 转换为特定客户端以访问其功能。 Here 是类型断言的一个很好的例子。

我添加了代码片段来展示如何使用类型断言来获取底层结构。测试文件中的模拟客户端不需要实现 foo 和 bar 方法,因为接口 clos​​ableclient 只需要 clos​​e 方法。

type ClosableClient interface {
    Close()
}

type Etcd struct{}

func (e *Etcd) Close() {
    fmt.Println("etcd closing")
}

func (e *Etcd) Foo() {
    fmt.Println("etcd foo")
}

type Libvirt struct{}

func (l *Libvirt) Close() {
    fmt.Println("libvirt closing")
}

func (l *Libvirt) Bar() {
    fmt.Println("libvirt bar")
}

type Client struct {
    etcd    ClosableClient
    libvirt ClosableClient
}

func (c *Client) Close() {
    c.etcd.Close()
    c.libvirt.Close()
}

func (c *Client) FooBar() {
    etcd, ok := c.etcd.(*Etcd)
    if !ok {
        panic("etcd is of incorrect type")
    }

    etcd.Foo()

    libvirt, ok := c.etcd.(*Libvirt)
    if !ok {
        panic("libvirt is of incorrect type")
    }

    libvirt.Bar()
}

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

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