add ctx to v01

master
bel 2023-03-25 10:15:14 -06:00
parent 51ae1b27b4
commit 3dd0a557d4
3 changed files with 13 additions and 5 deletions

View File

@ -13,7 +13,7 @@ type Parser interface {
func New(ctx context.Context, src raw.Raw) Parser { func New(ctx context.Context, src raw.Raw) Parser {
if os.Getenv("BUTTON_PARSER_V01") == "true" { if os.Getenv("BUTTON_PARSER_V01") == "true" {
return NewV01(src) return NewV01(ctx, src)
} }
return NewPlaintext(src) return NewPlaintext(src)
} }

View File

@ -1,6 +1,7 @@
package button package button
import ( import (
"context"
"encoding/json" "encoding/json"
"io/ioutil" "io/ioutil"
"log" "log"
@ -15,6 +16,8 @@ var debugging = os.Getenv("DEBUG") == "true"
type ( type (
V01 struct { V01 struct {
ctx context.Context
can context.CancelFunc
src raw.Raw src raw.Raw
cfg v01Cfg cfg v01Cfg
} }
@ -36,17 +39,21 @@ type (
v01Transformation map[string]string v01Transformation map[string]string
) )
func NewV01(src raw.Raw) V01 { func NewV01(ctx context.Context, src raw.Raw) V01 {
var cfg v01Cfg var cfg v01Cfg
b, _ := ioutil.ReadFile(os.Getenv("BUTTON_PARSER_V01_CONFIG")) b, _ := ioutil.ReadFile(os.Getenv("BUTTON_PARSER_V01_CONFIG"))
yaml.Unmarshal(b, &cfg) yaml.Unmarshal(b, &cfg)
ctx, can := context.WithCancel(ctx)
return V01{ return V01{
ctx: ctx,
can: can,
src: src, src: src,
cfg: cfg, cfg: cfg,
} }
} }
func (v01 V01) Close() { func (v01 V01) Close() {
v01.can()
v01.src.Close() v01.src.Close()
} }

View File

@ -1,6 +1,7 @@
package button_test package button_test
import ( import (
"context"
"fmt" "fmt"
"mayhem-party/src/device/input/button" "mayhem-party/src/device/input/button"
"os" "os"
@ -12,7 +13,7 @@ import (
func TestV01(t *testing.T) { func TestV01(t *testing.T) {
src := constSrc(fmt.Sprintf(`{"T":%v,"U":"bel","Y":"abc","N":"cde"}`, time.Now().UnixNano()/int64(time.Millisecond)-50)) 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()) t.Logf("(%v) %s", len(src), src.Read())
v01 := button.NewV01(src) v01 := button.NewV01(context.Background(), src)
got := v01.Read() got := v01.Read()
want := []button.Button{ want := []button.Button{
{Down: true, Char: 'a'}, {Down: true, Char: 'a'},
@ -48,7 +49,7 @@ func TestV01WithCfg(t *testing.T) {
os.Setenv("BUTTON_PARSER_V01_CONFIG", p) os.Setenv("BUTTON_PARSER_V01_CONFIG", p)
t.Run("unknown user ignored", func(t *testing.T) { 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() got := v01.Read()
if len(got) != 0 { if len(got) != 0 {
t.Error(got) t.Error(got)
@ -56,7 +57,7 @@ func TestV01WithCfg(t *testing.T) {
}) })
t.Run("player2", func(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() got := v01.Read()
if len(got) != 2 { if len(got) != 2 {
t.Error(got) t.Error(got)