vtworkerclient: Add UnregisterFactoryForTest().

This way, different fakes can be used in the same package. This was already done for vtctlclient.
This commit is contained in:
Michael Berlin 2016-01-12 00:39:34 +01:00
Родитель 43e6b50059
Коммит 526aca8e2e
2 изменённых файлов: 12 добавлений и 0 удалений

Просмотреть файл

@ -15,6 +15,7 @@ import (
func TestSplitCloneTask(t *testing.T) {
fake := fakevtworkerclient.NewFakeVtworkerClient()
vtworkerclient.RegisterFactory("fake", fake.FakeVtworkerClientFactory)
defer vtworkerclient.UnregisterFactoryForTest("fake")
flag.Set("vtworker_client_protocol", "fake")
fake.RegisterResult([]string{"SplitClone", "--strategy=-populate_blp_checkpoint", "test_keyspace/0"},
"", // No output.

Просмотреть файл

@ -46,6 +46,17 @@ func RegisterFactory(name string, factory Factory) {
factories[name] = factory
}
// UnregisterFactoryForTest allows to unregister a client implementation from the static map.
// This function is used by unit tests to cleanly unregister any fake implementations.
// This way, a test package can use the same name for different fakes and no dangling fakes are
// left behind in the static factories map after the test.
func UnregisterFactoryForTest(name string) {
if _, ok := factories[name]; !ok {
log.Fatalf("UnregisterFactoryForTest: %s is not registered", name)
}
delete(factories, name)
}
// New allows a user of the client library to get its implementation.
func New(addr string, connectTimeout time.Duration) (Client, error) {
factory, ok := factories[*protocol]