From 3dd0a557d476e45795d19763056a40108e986742 Mon Sep 17 00:00:00 2001 From: bel Date: Sat, 25 Mar 2023 10:15:14 -0600 Subject: [PATCH] add ctx to v01 --- src/device/input/button/parser.go | 2 +- src/device/input/button/v01.go | 9 ++++++++- src/device/input/button/v01_exported_test.go | 7 ++++--- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/device/input/button/parser.go b/src/device/input/button/parser.go index 2a45977..69657f7 100644 --- a/src/device/input/button/parser.go +++ b/src/device/input/button/parser.go @@ -13,7 +13,7 @@ type Parser interface { func New(ctx context.Context, src raw.Raw) Parser { if os.Getenv("BUTTON_PARSER_V01") == "true" { - return NewV01(src) + return NewV01(ctx, src) } return NewPlaintext(src) } diff --git a/src/device/input/button/v01.go b/src/device/input/button/v01.go index 61c6843..196580a 100644 --- a/src/device/input/button/v01.go +++ b/src/device/input/button/v01.go @@ -1,6 +1,7 @@ package button import ( + "context" "encoding/json" "io/ioutil" "log" @@ -15,6 +16,8 @@ var debugging = os.Getenv("DEBUG") == "true" type ( V01 struct { + ctx context.Context + can context.CancelFunc src raw.Raw cfg v01Cfg } @@ -36,17 +39,21 @@ type ( v01Transformation map[string]string ) -func NewV01(src raw.Raw) V01 { +func NewV01(ctx context.Context, src raw.Raw) V01 { var cfg v01Cfg b, _ := ioutil.ReadFile(os.Getenv("BUTTON_PARSER_V01_CONFIG")) yaml.Unmarshal(b, &cfg) + ctx, can := context.WithCancel(ctx) return V01{ + ctx: ctx, + can: can, src: src, cfg: cfg, } } func (v01 V01) Close() { + v01.can() v01.src.Close() } diff --git a/src/device/input/button/v01_exported_test.go b/src/device/input/button/v01_exported_test.go index 4d7af95..29a2176 100644 --- a/src/device/input/button/v01_exported_test.go +++ b/src/device/input/button/v01_exported_test.go @@ -1,6 +1,7 @@ package button_test import ( + "context" "fmt" "mayhem-party/src/device/input/button" "os" @@ -12,7 +13,7 @@ import ( func TestV01(t *testing.T) { src := constSrc(fmt.Sprintf(`{"T":%v,"U":"bel","Y":"abc","N":"cde"}`, time.Now().UnixNano()/int64(time.Millisecond)-50)) t.Logf("(%v) %s", len(src), src.Read()) - v01 := button.NewV01(src) + v01 := button.NewV01(context.Background(), src) got := v01.Read() want := []button.Button{ {Down: true, Char: 'a'}, @@ -48,7 +49,7 @@ func TestV01WithCfg(t *testing.T) { os.Setenv("BUTTON_PARSER_V01_CONFIG", p) t.Run("unknown user ignored", func(t *testing.T) { - v01 := button.NewV01(constSrc(`{"U":"qt","Y":"w"}`)) + v01 := button.NewV01(context.Background(), constSrc(`{"U":"qt","Y":"w"}`)) got := v01.Read() if len(got) != 0 { t.Error(got) @@ -56,7 +57,7 @@ func TestV01WithCfg(t *testing.T) { }) t.Run("player2", func(t *testing.T) { - v01 := button.NewV01(constSrc(`{"U":"bel","Y":"w","N":"w"}`)) + v01 := button.NewV01(context.Background(), constSrc(`{"U":"bel","Y":"w","N":"w"}`)) got := v01.Read() if len(got) != 2 { t.Error(got)