From c16993ab05eb60f08f6e8043e9f3cdc209cadd45 Mon Sep 17 00:00:00 2001 From: Adrian Zankich Date: Tue, 23 Dec 2014 01:20:44 -0800 Subject: [PATCH] Fix stack trace on close --- platforms/joystick/joystick_driver.go | 13 +++++++++++-- platforms/joystick/joystick_driver_test.go | 3 +++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/platforms/joystick/joystick_driver.go b/platforms/joystick/joystick_driver.go index 044c5a37..bf1a59a5 100644 --- a/platforms/joystick/joystick_driver.go +++ b/platforms/joystick/joystick_driver.go @@ -20,6 +20,7 @@ type JoystickDriver struct { configPath string config joystickConfig poll func() sdl.Event + halt chan bool gobot.Eventer } @@ -60,6 +61,7 @@ func NewJoystickDriver(a *JoystickAdaptor, name string, config string, v ...time return sdl.PollEvent() }, interval: 10 * time.Millisecond, + halt: make(chan bool, 0), } if len(v) > 0 { @@ -107,14 +109,21 @@ func (j *JoystickDriver) Start() (errs []error) { gobot.Publish(j.Event("error"), err) } } - <-time.After(j.interval) + select { + case <-time.After(j.interval): + case <-j.halt: + return + } } }() return } // Halt stops joystick driver -func (j *JoystickDriver) Halt() (errs []error) { return } +func (j *JoystickDriver) Halt() (errs []error) { + j.halt <- true + return +} // HandleEvent publishes an specific event according to data received func (j *JoystickDriver) handleEvent(event sdl.Event) error { diff --git a/platforms/joystick/joystick_driver_test.go b/platforms/joystick/joystick_driver_test.go index 16ada0a3..d2814ca4 100644 --- a/platforms/joystick/joystick_driver_test.go +++ b/platforms/joystick/joystick_driver_test.go @@ -31,6 +31,9 @@ func TestJoystickDriverStart(t *testing.T) { func TestJoystickDriverHalt(t *testing.T) { d := initTestJoystickDriver() + go func() { + <-d.halt + }() gobot.Assert(t, len(d.Halt()), 0) }