Skip to content

Commit

Permalink
Keep Alive for non-interactive sessions
Browse files Browse the repository at this point in the history
[#101789464]

Signed-off-by: Andrew Edgar <[email protected]>
  • Loading branch information
jfmyers9 authored and andrew-edgar committed Sep 23, 2015
1 parent b5ed976 commit b88aead
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
5 changes: 5 additions & 0 deletions cf-plugin/cmd/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,11 @@ func (c *secureShell) InteractiveSession() error {
}

func (c *secureShell) Wait() error {
keepaliveStopCh := make(chan struct{})
defer close(keepaliveStopCh)

go keepalive(c.secureClient.Conn(), time.NewTicker(c.keepAliveInterval), keepaliveStopCh)

return c.secureClient.Wait()
}

Expand Down
47 changes: 43 additions & 4 deletions cf-plugin/cmd/ssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,7 @@ var _ = Describe("Diego SSH Plugin", func() {

Describe("Wait", func() {
var opts *options.SSHOptions
var waitErr error

BeforeEach(func() {
opts = &options.SSHOptions{
Expand All @@ -1176,14 +1177,52 @@ var _ = Describe("Diego SSH Plugin", func() {
JustBeforeEach(func() {
connectErr := secureShell.Connect(opts)
Expect(connectErr).NotTo(HaveOccurred())
})

It("calls close on the secureClient", func() {
err := secureShell.Wait()
Expect(err).NotTo(HaveOccurred())
waitErr = secureShell.Wait()
})

It("calls wait on the secureClient", func() {
Expect(waitErr).NotTo(HaveOccurred())
Expect(fakeSecureClient.WaitCallCount()).To(Equal(1))
})

Describe("keep alive messages", func() {
var times []time.Time
var timesCh chan []time.Time
var done chan struct{}

BeforeEach(func() {
keepAliveDuration = 100 * time.Millisecond

times = []time.Time{}
timesCh = make(chan []time.Time, 1)
done = make(chan struct{}, 1)

fakeConnection.SendRequestStub = func(reqName string, wantReply bool, message []byte) (bool, []byte, error) {
Expect(reqName).To(Equal("[email protected]"))
Expect(wantReply).To(BeTrue())
Expect(message).To(BeNil())

times = append(times, time.Now())
if len(times) == 3 {
timesCh <- times
close(done)
}
return true, nil, nil
}

fakeSecureClient.WaitStub = func() error {
Eventually(done).Should(BeClosed())
return nil
}
})

It("sends keep alive messages at the expected interval", func() {
Expect(waitErr).NotTo(HaveOccurred())
times := <-timesCh
Expect(times[2]).To(BeTemporally("~", times[0].Add(200*time.Millisecond), 100*time.Millisecond))
})
})
})

Describe("Close", func() {
Expand Down
2 changes: 1 addition & 1 deletion cf-plugin/ssh_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (p *SSHPlugin) GetMetadata() plugin.PluginMetadata {
Version: plugin.VersionType{
Major: 0,
Minor: 2,
Build: 0,
Build: 1,
},
Commands: []plugin.Command{
{
Expand Down

0 comments on commit b88aead

Please sign in to comment.