From ae1e32391c2d7916591a3c0025c71258ffa26cc4 Mon Sep 17 00:00:00 2001 From: bel Date: Sat, 25 Mar 2023 10:50:39 -0600 Subject: [PATCH] refresh neither leaks wraps, allows 2 of the same at once, nor closes raws --- src/device/input/button/parser.go | 1 + src/device/input/button/plaintext.go | 2 + src/device/input/button/plaintext_test.go | 2 - src/device/input/button/v01.go | 5 ++ src/device/input/input.go | 1 - src/device/input/input_exported_test.go | 9 ++-- src/device/input/raw/keyboard.go | 3 -- src/device/input/raw/random.go | 3 -- src/device/input/raw/raw.go | 1 - src/device/input/raw/udp.go | 3 -- src/device/input/wrap/buffered.go | 6 +++ src/device/input/wrap/protocol.go | 24 ---------- src/device/input/wrap/refresh.go | 22 +++++---- src/device/input/wrap/refresh_test.go | 52 ++++++++++++++++++++- src/device/input/wrap/remap.go | 5 ++ src/device/input/wrap/wrap.go | 4 +- src/device/input/wrap/wrap_exported_test.go | 10 ---- src/device/input/wrap/wrap_test.go | 5 +- 18 files changed, 94 insertions(+), 64 deletions(-) delete mode 100644 src/device/input/wrap/protocol.go delete mode 100644 src/device/input/wrap/wrap_exported_test.go diff --git a/src/device/input/button/parser.go b/src/device/input/button/parser.go index 69657f7..9ca66d2 100644 --- a/src/device/input/button/parser.go +++ b/src/device/input/button/parser.go @@ -9,6 +9,7 @@ import ( type Parser interface { Read() []Button Close() + CloseWrap() raw.Raw } func New(ctx context.Context, src raw.Raw) Parser { diff --git a/src/device/input/button/plaintext.go b/src/device/input/button/plaintext.go index 23c12d4..84df7b8 100644 --- a/src/device/input/button/plaintext.go +++ b/src/device/input/button/plaintext.go @@ -23,6 +23,8 @@ func NewPlaintext(src raw.Raw) Plaintext { func (p Plaintext) Close() { p.src.Close() } +func (p Plaintext) CloseWrap() raw.Raw { return p.src } + func (p Plaintext) Read() []Button { b := p.src.Read() buttons := make([]Button, 0, len(b)) diff --git a/src/device/input/button/plaintext_test.go b/src/device/input/button/plaintext_test.go index 7370fc5..f293aab 100644 --- a/src/device/input/button/plaintext_test.go +++ b/src/device/input/button/plaintext_test.go @@ -22,8 +22,6 @@ func TestPlaintext(t *testing.T) { type constSrc string -func (c constSrc) Refresh() {} - func (c constSrc) Close() {} func (c constSrc) Read() []byte { diff --git a/src/device/input/button/v01.go b/src/device/input/button/v01.go index 196580a..b3da54f 100644 --- a/src/device/input/button/v01.go +++ b/src/device/input/button/v01.go @@ -52,6 +52,11 @@ func NewV01(ctx context.Context, src raw.Raw) V01 { } } +func (v01 V01) CloseWrap() raw.Raw { + v01.can() + return v01.src +} + func (v01 V01) Close() { v01.can() v01.src.Close() diff --git a/src/device/input/input.go b/src/device/input/input.go index f5d6d44..c3ca210 100644 --- a/src/device/input/input.go +++ b/src/device/input/input.go @@ -15,7 +15,6 @@ type Input interface { func New(ctx context.Context) Input { src := raw.New(ctx) return wrap.New(ctx, func() button.Parser { - src.Refresh() return button.New(ctx, src) }) } diff --git a/src/device/input/input_exported_test.go b/src/device/input/input_exported_test.go index bc9e1b2..9dd1218 100644 --- a/src/device/input/input_exported_test.go +++ b/src/device/input/input_exported_test.go @@ -3,6 +3,7 @@ package input_test import ( "context" "mayhem-party/src/device/input" + "mayhem-party/src/device/input/wrap" "os" "path" "testing" @@ -30,10 +31,10 @@ func TestNewRemapped(t *testing.T) { t.Fatal(err) } - os.Setenv("WRAP_REMAP_FILE", remap) + wrap.FlagRemapFile = remap os.Setenv("RAW_RANDOM_WEIGHT_FILE", rand) t.Cleanup(func() { - os.Unsetenv("WRAP_REMAP_FILE") + wrap.FlagRemapFile = "" os.Unsetenv("RAW_RANDOM_WEIGHT_FILE") }) @@ -50,9 +51,9 @@ func TestNewRemapped(t *testing.T) { } func TestNewBuffered(t *testing.T) { - os.Setenv("WRAP_BUFFERED", "true") + wrap.FlagBuffered = true t.Cleanup(func() { - os.Unsetenv("WRAP_BUFFERED") + wrap.FlagBuffered = false }) r := input.New(context.Background()) diff --git a/src/device/input/raw/keyboard.go b/src/device/input/raw/keyboard.go index 55ffef5..f9d53ee 100644 --- a/src/device/input/raw/keyboard.go +++ b/src/device/input/raw/keyboard.go @@ -31,9 +31,6 @@ func NewKeyboard() Keyboard { return Keyboard{} } -func (kb Keyboard) Refresh() { -} - func (kb Keyboard) Close() { switch runtime.GOOS { case "linux": diff --git a/src/device/input/raw/random.go b/src/device/input/raw/random.go index 514f718..419041b 100644 --- a/src/device/input/raw/random.go +++ b/src/device/input/raw/random.go @@ -22,9 +22,6 @@ func NewRandom(generator func() byte) *Random { return &Random{generator: generator} } -func (r *Random) Refresh() { -} - func (r *Random) Close() { } diff --git a/src/device/input/raw/raw.go b/src/device/input/raw/raw.go index 2c7eb21..27b2d62 100644 --- a/src/device/input/raw/raw.go +++ b/src/device/input/raw/raw.go @@ -9,7 +9,6 @@ import ( type Raw interface { Read() []byte Close() - Refresh() } func New(ctx context.Context) Raw { diff --git a/src/device/input/raw/udp.go b/src/device/input/raw/udp.go index 148ebe0..edfc7b4 100644 --- a/src/device/input/raw/udp.go +++ b/src/device/input/raw/udp.go @@ -55,9 +55,6 @@ func (udp UDP) Read() []byte { } } -func (udp UDP) Refresh() { -} - func (udp UDP) Close() { udp.conn.Close() } diff --git a/src/device/input/wrap/buffered.go b/src/device/input/wrap/buffered.go index 0ef5cec..c30a63e 100644 --- a/src/device/input/wrap/buffered.go +++ b/src/device/input/wrap/buffered.go @@ -3,6 +3,7 @@ package wrap import ( "context" "mayhem-party/src/device/input/button" + "mayhem-party/src/device/input/raw" "os" "sync" "time" @@ -57,6 +58,11 @@ func (b *Buffered) listen() { } } +func (b *Buffered) CloseWrap() raw.Raw { + b.can() + return b.input.CloseWrap() +} + func (b *Buffered) Close() { b.input.Close() b.can() diff --git a/src/device/input/wrap/protocol.go b/src/device/input/wrap/protocol.go deleted file mode 100644 index 1143e74..0000000 --- a/src/device/input/wrap/protocol.go +++ /dev/null @@ -1,24 +0,0 @@ -package wrap - -import ( - "mayhem-party/src/device/input/button" - "mayhem-party/src/device/input/raw" -) - -type Protocol struct { - src raw.Raw -} - -func NewProtocol(src raw.Raw) Protocol { - return Protocol{ - src: src, - } -} - -func (p Protocol) Close() { - p.src.Close() -} - -func (p Protocol) Read() []button.Button { - panic(nil) -} diff --git a/src/device/input/wrap/refresh.go b/src/device/input/wrap/refresh.go index fbf5dab..f6c83b9 100644 --- a/src/device/input/wrap/refresh.go +++ b/src/device/input/wrap/refresh.go @@ -4,13 +4,14 @@ import ( "context" "log" "mayhem-party/src/device/input/button" + "mayhem-party/src/device/input/raw" "os" "os/signal" "syscall" ) var ( - ChanSigUsr1 = func() chan os.Signal { + chSigUsr1 = func() chan os.Signal { c := make(chan os.Signal, 1) signal.Notify(c, syscall.SIGUSR1) return c @@ -22,12 +23,12 @@ type Refresh struct { input Wrap } -func NewRefresh(newWrap func() Wrap) *Refresh { - return NewRefreshWith(newWrap, ChanSigUsr1) +func NewRefresh(ctx context.Context, newWrap func() Wrap) *Refresh { + return NewRefreshWith(ctx, newWrap, chSigUsr1) } -func NewRefreshWith(newWrap func() Wrap, ch <-chan os.Signal) *Refresh { - ctx, can := context.WithCancel(context.Background()) +func NewRefreshWith(ctx context.Context, newWrap func() Wrap, ch <-chan os.Signal) *Refresh { + ctx, can := context.WithCancel(ctx) result := &Refresh{ can: can, input: newWrap(), @@ -40,16 +41,19 @@ func NewRefreshWith(newWrap func() Wrap, ch <-chan os.Signal) *Refresh { return case sig := <-ch: log.Println("refreshing for", sig) - newInput := newWrap() - oldInput := result.input - result.input = newInput - oldInput.Close() + result.input.CloseWrap() + result.input = newWrap() } } }() return result } +func (r *Refresh) CloseWrap() raw.Raw { + r.can() + return r.input.CloseWrap() +} + func (r *Refresh) Read() []button.Button { return r.input.Read() } diff --git a/src/device/input/wrap/refresh_test.go b/src/device/input/wrap/refresh_test.go index 9c21728..8a4f596 100644 --- a/src/device/input/wrap/refresh_test.go +++ b/src/device/input/wrap/refresh_test.go @@ -1,6 +1,8 @@ package wrap import ( + "context" + "mayhem-party/src/device/input/button" "os" "syscall" "testing" @@ -15,7 +17,7 @@ func TestRefresh(t *testing.T) { } ch := make(chan os.Signal, 1) defer close(ch) - refresh := NewRefreshWith(generator, ch) + refresh := NewRefreshWith(context.Background(), generator, ch) defer refresh.Close() assertIts := func(t *testing.T, b byte) { @@ -42,3 +44,51 @@ func TestRefresh(t *testing.T) { assertIts(t, byte('c')) }) } + +func TestRefreshDoesntCloseSources(t *testing.T) { + src := &telemetrySrc{} + newParsers := 0 + newParser := func() Wrap { + newParsers += 1 + return button.NewPlaintext(src) + } + ctx, can := context.WithCancel(context.Background()) + defer can() + refresh := NewRefresh(ctx, newParser) + if newParsers != 1 { + t.Error(newParsers) + } + + for i := 0; i < 5; i++ { + refresh.Read() + } + if want := (telemetrySrc{reads: 5}); *src != want { + t.Errorf("%+v", *src) + } else if newParsers != 1 { + t.Error(newParsers) + } + + for i := 0; i < 5; i++ { + chSigUsr1 <- syscall.SIGINT + } + time.Sleep(time.Millisecond * 250) + if want := (telemetrySrc{reads: 5}); *src != want { + t.Errorf("want %+v, got %+v", want, *src) + } else if newParsers != 6 { + t.Error(newParsers) + } +} + +type telemetrySrc struct { + closes int + reads int +} + +func (src *telemetrySrc) Close() { + src.closes += 1 +} + +func (src *telemetrySrc) Read() []byte { + src.reads += 1 + return []byte("foo") +} diff --git a/src/device/input/wrap/remap.go b/src/device/input/wrap/remap.go index 97e1e68..1c70479 100644 --- a/src/device/input/wrap/remap.go +++ b/src/device/input/wrap/remap.go @@ -2,6 +2,7 @@ package wrap import ( "mayhem-party/src/device/input/button" + "mayhem-party/src/device/input/raw" "os" "github.com/go-yaml/yaml" @@ -38,6 +39,10 @@ func NewRemap(input Wrap, m map[byte]byte) Remap { } } +func (re Remap) CloseWrap() raw.Raw { + return re.input.CloseWrap() +} + func (re Remap) Close() { re.input.Close() } diff --git a/src/device/input/wrap/wrap.go b/src/device/input/wrap/wrap.go index eb8cf29..65814eb 100644 --- a/src/device/input/wrap/wrap.go +++ b/src/device/input/wrap/wrap.go @@ -3,6 +3,7 @@ package wrap import ( "context" "mayhem-party/src/device/input/button" + "mayhem-party/src/device/input/raw" "os" ) @@ -15,6 +16,7 @@ var ( type Wrap interface { Read() []button.Button Close() + CloseWrap() raw.Raw } func New(ctx context.Context, srcFunc func() button.Parser) Wrap { @@ -36,7 +38,7 @@ func New(ctx context.Context, srcFunc func() button.Parser) Wrap { if FlagRefreshOnSigUsr1 { oldMaker := maker maker = func() Wrap { - return NewRefresh(oldMaker) + return NewRefresh(ctx, oldMaker) } } return maker() diff --git a/src/device/input/wrap/wrap_exported_test.go b/src/device/input/wrap/wrap_exported_test.go deleted file mode 100644 index 2eea17f..0000000 --- a/src/device/input/wrap/wrap_exported_test.go +++ /dev/null @@ -1,10 +0,0 @@ -package wrap_test - -import ( - "mayhem-party/src/device/input/wrap" - "testing" -) - -func TestNewRefreshing(t *testing.T) { - wrap.FlagBuffered = true -} diff --git a/src/device/input/wrap/wrap_test.go b/src/device/input/wrap/wrap_test.go index d6c060a..71b6e47 100644 --- a/src/device/input/wrap/wrap_test.go +++ b/src/device/input/wrap/wrap_test.go @@ -2,6 +2,7 @@ package wrap import ( "mayhem-party/src/device/input/button" + "mayhem-party/src/device/input/raw" "testing" ) @@ -10,12 +11,12 @@ func TestWrap(t *testing.T) { var _ Wrap = &Refresh{} var _ Wrap = &Buffered{} var _ Wrap = &Remap{} - var _ Wrap = Protocol{} } type dummyParser button.Button -func (d dummyParser) Close() {} +func (d dummyParser) CloseWrap() raw.Raw { return nil } +func (d dummyParser) Close() {} func (d dummyParser) Read() []button.Button { return []button.Button{button.Button(d)} }