proxy_test.go
This file
Path
src/proxy_test.go
65 lines · 1.7 KiB · Go
Last changed by
Separate floating pane border label from pane titleJunegunn Choibefore adoption50e6c02
History of this fileTake it
Separate floating pane border label from pane titleJunegunn Choibefore adoption50e6c02
lines 1–65why, one step back1package fzf23import (4 "strconv"5 "sync"6 "testing"7 "time"8)910func TestLabelUpdater(t *testing.T) {11 var mu sync.Mutex12 applied := []int{}13 // The goroutine can still be running after the test function returns, so14 // it records the label instead of reporting it, and -1 stands in for one15 // that could not be parsed16 updater := &labelUpdater{set: func(label string) {17 n, err := strconv.Atoi(label)18 if err != nil {19 n = -120 }21 mu.Lock()22 applied = append(applied, n)23 mu.Unlock()24 // Long enough for the burst to pile up behind the first update25 time.Sleep(time.Millisecond)26 }}2728 const last = 9929 for i := 0; i <= last; i++ {30 updater.update(strconv.Itoa(i))31 }3233 // However many updates are dropped, the final one is always applied34 deadline := time.Now().Add(5 * time.Second)35 for {36 mu.Lock()37 done := len(applied) > 0 && applied[len(applied)-1] == last38 mu.Unlock()39 if done {40 break41 }42 if time.Now().After(deadline) {43 t.Fatalf("last update was not applied: %v", applied)44 }45 time.Sleep(time.Millisecond)46 }4748 mu.Lock()49 defer mu.Unlock()50 // Updates are never applied out of order, so the border cannot be left51 // showing a label older than the last one requested52 for i, n := range applied {53 if i > 0 && n <= applied[i-1] {54 t.Errorf("applied out of order: %v", applied)55 break56 }57 }58 // A burst collapses instead of running one command per update. The59 // updates are queued in a tight loop while each one takes a millisecond60 // to apply, so all of them landing would mean no coalescing at all61 if len(applied) >= last+1 {62 t.Errorf("expected updates to be coalesced, applied all %d", len(applied))63 }64 t.Logf("applied %d of %d updates", len(applied), last+1)65}