samuel / go-zookeeper

Native ZooKeeper client for Go. This project is no longer maintained. Please use https://github.com/go-zookeeper/zk instead.
BSD 3-Clause "New" or "Revised" License
1.64k stars 672 forks source link

How to mock Connect API for test? #212

Open max-min opened 5 years ago

max-min commented 5 years ago

https://github.com/samuel/go-zookeeper/blob/c4fab1ac1bec58281ad0667dc3f0907a9476ac47/zk/conn.go#L175

connOption is not friendly ...

khuddlefish commented 5 years ago

We just left the connOption part out in our mock since it is optional. This is kind of what our connect looks like

type stubZkClient struct {
    ConnectStub struct {
        called         int
        servers        []string
        sessionTimeout time.Duration
        zkConn         ZkConn
        eventChan      chan zk.Event
        err            error
    }
}

func (c *stubZkClient) Connect(servers []string, sessionTimeout time.Duration) (ZkConn, <-chan zk.Event, error) {
        c.ConnectStub.called++
        c.ConnectStub.servers = servers
    c.ConnectStub.sessionTimeout = sessionTimeout
    return c.ConnectStub.zkConn, c.ConnectStub.eventChan, c.ConnectStub.err
}