From d8e6c68ccc83cee87f565d30b0d06171551f460b Mon Sep 17 00:00:00 2001 From: Paul Lesiak Date: Wed, 22 Jan 2020 14:44:48 -0500 Subject: [PATCH] bug: Consul lock does not receive signals if lock not acquired (#5909) --- command/commands_oss.go | 2 +- command/lock/lock.go | 4 ++-- command/lock/lock_test.go | 22 +++++++++++----------- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/command/commands_oss.go b/command/commands_oss.go index d5d04a440..cf419cad4 100644 --- a/command/commands_oss.go +++ b/command/commands_oss.go @@ -190,7 +190,7 @@ func init() { Register("kv import", func(ui cli.Ui) (cli.Command, error) { return kvimp.New(ui), nil }) Register("kv put", func(ui cli.Ui) (cli.Command, error) { return kvput.New(ui), nil }) Register("leave", func(ui cli.Ui) (cli.Command, error) { return leave.New(ui), nil }) - Register("lock", func(ui cli.Ui) (cli.Command, error) { return lock.New(ui), nil }) + Register("lock", func(ui cli.Ui) (cli.Command, error) { return lock.New(ui, MakeShutdownCh()), nil }) Register("login", func(ui cli.Ui) (cli.Command, error) { return login.New(ui), nil }) Register("logout", func(ui cli.Ui) (cli.Command, error) { return logout.New(ui), nil }) Register("maint", func(ui cli.Ui) (cli.Command, error) { return maint.New(ui), nil }) diff --git a/command/lock/lock.go b/command/lock/lock.go index 5abddb3d9..dbf06d1d8 100644 --- a/command/lock/lock.go +++ b/command/lock/lock.go @@ -61,8 +61,8 @@ type cmd struct { timeout time.Duration } -func New(ui cli.Ui) *cmd { - c := &cmd{UI: ui} +func New(ui cli.Ui, shutdownCh <-chan struct{}) *cmd { + c := &cmd{UI: ui, ShutdownCh: shutdownCh} c.init() return c } diff --git a/command/lock/lock_test.go b/command/lock/lock_test.go index 121046b8a..bc117058a 100644 --- a/command/lock/lock_test.go +++ b/command/lock/lock_test.go @@ -15,7 +15,7 @@ import ( func argFail(t *testing.T, args []string, expected string) { ui := cli.NewMockUi() - c := New(ui) + c := New(ui, nil) c.flags.SetOutput(ui.ErrorWriter) if code := c.Run(args); code != 1 { t.Fatalf("expected return code 1, got %d", code) @@ -28,7 +28,7 @@ func argFail(t *testing.T, args []string, expected string) { func TestLockCommand_noTabs(t *testing.T) { t.Parallel() - if strings.ContainsRune(New(cli.NewMockUi()).Help(), '\t') { + if strings.ContainsRune(New(cli.NewMockUi(), nil).Help(), '\t') { t.Fatal("help has tabs") } } @@ -48,7 +48,7 @@ func TestLockCommand(t *testing.T) { testrpc.WaitForTestAgent(t, a.RPC, "dc1") ui := cli.NewMockUi() - c := New(ui) + c := New(ui, nil) filePath := filepath.Join(a.Config.DataDir, "test_touch") args := []string{"-http-addr=" + a.HTTPAddr(), "test/prefix", "touch", filePath} @@ -73,7 +73,7 @@ func TestLockCommand_NoShell(t *testing.T) { testrpc.WaitForTestAgent(t, a.RPC, "dc1") ui := cli.NewMockUi() - c := New(ui) + c := New(ui, nil) filePath := filepath.Join(a.Config.DataDir, "test_touch") args := []string{"-http-addr=" + a.HTTPAddr(), "-shell=false", "test/prefix", "touch", filePath} @@ -98,7 +98,7 @@ func TestLockCommand_TryLock(t *testing.T) { testrpc.WaitForTestAgent(t, a.RPC, "dc1") ui := cli.NewMockUi() - c := New(ui) + c := New(ui, nil) filePath := filepath.Join(a.Config.DataDir, "test_touch") args := []string{"-http-addr=" + a.HTTPAddr(), "-try=10s", "test/prefix", "touch", filePath} @@ -132,7 +132,7 @@ func TestLockCommand_TrySemaphore(t *testing.T) { testrpc.WaitForTestAgent(t, a.RPC, "dc1") ui := cli.NewMockUi() - c := New(ui) + c := New(ui, nil) filePath := filepath.Join(a.Config.DataDir, "test_touch") args := []string{"-http-addr=" + a.HTTPAddr(), "-n=3", "-try=10s", "test/prefix", "touch", filePath} @@ -166,7 +166,7 @@ func TestLockCommand_MonitorRetry_Lock_Default(t *testing.T) { testrpc.WaitForTestAgent(t, a.RPC, "dc1") ui := cli.NewMockUi() - c := New(ui) + c := New(ui, nil) filePath := filepath.Join(a.Config.DataDir, "test_touch") args := []string{"-http-addr=" + a.HTTPAddr(), "test/prefix", "touch", filePath} @@ -201,7 +201,7 @@ func TestLockCommand_MonitorRetry_Semaphore_Default(t *testing.T) { testrpc.WaitForTestAgent(t, a.RPC, "dc1") ui := cli.NewMockUi() - c := New(ui) + c := New(ui, nil) filePath := filepath.Join(a.Config.DataDir, "test_touch") args := []string{"-http-addr=" + a.HTTPAddr(), "-n=3", "test/prefix", "touch", filePath} @@ -236,7 +236,7 @@ func TestLockCommand_MonitorRetry_Lock_Arg(t *testing.T) { testrpc.WaitForTestAgent(t, a.RPC, "dc1") ui := cli.NewMockUi() - c := New(ui) + c := New(ui, nil) filePath := filepath.Join(a.Config.DataDir, "test_touch") args := []string{"-http-addr=" + a.HTTPAddr(), "-monitor-retry=9", "test/prefix", "touch", filePath} @@ -271,7 +271,7 @@ func TestLockCommand_MonitorRetry_Semaphore_Arg(t *testing.T) { testrpc.WaitForTestAgent(t, a.RPC, "dc1") ui := cli.NewMockUi() - c := New(ui) + c := New(ui, nil) filePath := filepath.Join(a.Config.DataDir, "test_touch") args := []string{"-http-addr=" + a.HTTPAddr(), "-n=3", "-monitor-retry=9", "test/prefix", "touch", filePath} @@ -330,7 +330,7 @@ func TestLockCommand_ChildExitCode(t *testing.T) { for _, tc := range tt { t.Run(tc.name, func(t *testing.T) { ui := cli.NewMockUi() - c := New(ui) + c := New(ui, nil) if got := c.Run(tc.args); got != tc.want { t.Fatalf("got %d want %d", got, tc.want)