buffer blocks while no changes underlying, test buffer
parent
bfdab392a0
commit
5c5a371f55
|
|
@ -55,6 +55,20 @@ func (b *Buffered) Close() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Buffered) Read() []Button {
|
func (b *Buffered) Read() []Button {
|
||||||
|
for b.ctx.Err() == nil {
|
||||||
|
result := b.read()
|
||||||
|
if len(result) > 0 {
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case <-b.ctx.Done():
|
||||||
|
case <-time.After(b.listenInterval):
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return []Button{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Buffered) read() []Button {
|
||||||
b.lock.Lock()
|
b.lock.Lock()
|
||||||
defer b.lock.Unlock()
|
defer b.lock.Unlock()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,5 +12,9 @@ func New() Input {
|
||||||
if p, ok := os.LookupEnv("INPUT_RANDOM_WEIGHT_FILE"); ok {
|
if p, ok := os.LookupEnv("INPUT_RANDOM_WEIGHT_FILE"); ok {
|
||||||
foo = randomCharFromWeightFile(p)
|
foo = randomCharFromWeightFile(p)
|
||||||
}
|
}
|
||||||
return NewRandom(foo)
|
var result Input = NewRandom(foo)
|
||||||
|
if os.Getenv("INPUT_BUFFERED") == "true" {
|
||||||
|
result = NewBuffered(result)
|
||||||
|
}
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,13 +17,33 @@ func TestNew(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNewBuffered(t *testing.T) {
|
||||||
|
os.Setenv("INPUT_BUFFERED", "true")
|
||||||
|
t.Cleanup(func() {
|
||||||
|
os.Unsetenv("INPUT_BUFFERED")
|
||||||
|
})
|
||||||
|
|
||||||
|
r := input.New()
|
||||||
|
for i := 0; i < 15; i++ {
|
||||||
|
batch := r.Read()
|
||||||
|
for j := range batch {
|
||||||
|
t.Logf("[%d][%d] %c|%v", i, j, batch[j].Char, batch[j].Down)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestNewRandomWeightFile(t *testing.T) {
|
func TestNewRandomWeightFile(t *testing.T) {
|
||||||
d := t.TempDir()
|
d := t.TempDir()
|
||||||
p := path.Join(d, "f")
|
p := path.Join(d, "f")
|
||||||
if err := os.WriteFile(p, []byte(`{"a":1}`), os.ModePerm); err != nil {
|
if err := os.WriteFile(p, []byte(`{"a":1}`), os.ModePerm); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
os.Setenv("INPUT_RANDOM_WEIGHT_FILE", p)
|
os.Setenv("INPUT_RANDOM_WEIGHT_FILE", p)
|
||||||
|
t.Cleanup(func() {
|
||||||
|
os.Unsetenv("INPUT_RANDOM_WEIGHT_FILE")
|
||||||
|
})
|
||||||
|
|
||||||
r := input.New()
|
r := input.New()
|
||||||
for i := 0; i < 15; i++ {
|
for i := 0; i < 15; i++ {
|
||||||
batch := r.Read()
|
batch := r.Read()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue