From adabc4eb983fc914fe9e658858acaca985058bdb Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Fri, 24 Mar 2023 12:05:50 -0600 Subject: [PATCH] input.New refactor and test --- src/device/input/input.go | 58 +++++++++++++++++++++++++--------- src/device/input/input_test.go | 23 +++++++++++++- src/device/input/refresh.go | 15 +++++---- 3 files changed, 74 insertions(+), 22 deletions(-) diff --git a/src/device/input/input.go b/src/device/input/input.go index bd4834a..c11f56c 100644 --- a/src/device/input/input.go +++ b/src/device/input/input.go @@ -3,6 +3,7 @@ package input import ( "context" "os" + "syscall" ) type Input interface { @@ -11,19 +12,46 @@ type Input interface { } func New(ctx context.Context) Input { - foo := randomCharFromRange('a', 'g') - if p, ok := os.LookupEnv("INPUT_RANDOM_WEIGHT_FILE"); ok && len(p) > 0 { - foo = randomCharFromWeightFile(p) - } - var result Input = NewRandom(foo) - if os.Getenv("INPUT_KEYBOARD") == "true" { - result = NewKeyboard() - } - if os.Getenv("INPUT_BUFFERED") == "true" { - result = NewBuffered(ctx, result) - } - if p, ok := os.LookupEnv("INPUT_REMAP_FILE"); ok && len(p) > 0 { - result = NewRemapFromFile(result, p) - } - return result + return newNew(ctx)() +} + +func newNew(ctx context.Context) func() Input { + maker := newSourceFunc() + + if os.Getenv("INPUT_BUFFERED") == "true" { + oldMaker := maker + maker = func() Input { + return NewBuffered(ctx, oldMaker()) + } + } + if p := os.Getenv("INPUT_REMAP_FILE"); p != "" { + oldMaker := maker + maker = func() Input { + return NewRemapFromFile(oldMaker(), p) + } + } + if os.Getenv("INPUT_REFRESH_ON_SIGUSR1") != "" { + oldMaker := maker + c := NewRefreshCh(syscall.SIGUSR1) + maker = func() Input { + return NewRefresh(oldMaker, c) + } + } + return maker +} + +func newSourceFunc() func() Input { + if os.Getenv("INPUT_KEYBOARD") == "true" { + singletonKeyboard := NewKeyboard() + return func() Input { + return singletonKeyboard + } + } + return func() Input { + generator := randomCharFromRange('a', 'g') + if p, ok := os.LookupEnv("INPUT_RANDOM_WEIGHT_FILE"); ok && len(p) > 0 { + generator = randomCharFromWeightFile(p) + } + return NewRandom(generator) + } } diff --git a/src/device/input/input_test.go b/src/device/input/input_test.go index eff0e2f..60d589a 100644 --- a/src/device/input/input_test.go +++ b/src/device/input/input_test.go @@ -1,6 +1,10 @@ package input -import "testing" +import ( + "context" + "os" + "testing" +) func TestInput(t *testing.T) { var _ Input = &Random{} @@ -9,3 +13,20 @@ func TestInput(t *testing.T) { var _ Input = Remap{} var _ Input = &Refresh{} } + +func TestNewNew(t *testing.T) { + t.Run("refreshing", func(t *testing.T) { + ctx, can := context.WithCancel(context.Background()) + defer can() + os.Setenv("INPUT_BUFFERED", "true") + os.Setenv("INPUT_REFRESH_ON_SIGUSR1", "true") + foo := New(ctx) + if refresh, ok := foo.(*Refresh); !ok { + t.Errorf("%T", foo) + } else if buffered, ok := refresh.input.(*Buffered); !ok { + t.Errorf("%T", refresh.input) + } else if _, ok := buffered.input.(*Random); !ok { + t.Errorf("%T", buffered.input) + } + }) +} diff --git a/src/device/input/refresh.go b/src/device/input/refresh.go index cb28eeb..37748a0 100644 --- a/src/device/input/refresh.go +++ b/src/device/input/refresh.go @@ -25,12 +25,15 @@ func NewRefresh(newInput func() Input, ch <-chan os.Signal) *Refresh { input: newInput(), } go func() { - select { - case <-ctx.Done(): - return - case sig := <-ch: - log.Println("refreshing for", sig) - result.input = newInput() + defer log.Println("refreshing done") + for { + select { + case <-ctx.Done(): + return + case sig := <-ch: + log.Println("refreshing for", sig) + result.input = newInput() + } } }() return result