Debian Codegraph @

src/proxy_test.go

raw
1 package fzf
2
3 import (
4 "strconv"
5 "sync"
6 "testing"
7 "time"
8 )
9
10 func TestLabelUpdater(t *testing.T) {
11 var mu sync.Mutex
12 applied := []int{}
13 // The goroutine can still be running after the test function returns, so
14 // it records the label instead of reporting it, and -1 stands in for one
15 // that could not be parsed
16 updater := &labelUpdater{set: func(label string) {
17 n, err := strconv.Atoi(label)
18 if err != nil {
19 n = -1
20 }
21 mu.Lock()
22 applied = append(applied, n)
23 mu.Unlock()
24 // Long enough for the burst to pile up behind the first update
25 time.Sleep(time.Millisecond)
26 }}
27
28 const last = 99
29 for i := 0; i <= last; i++ {
30 updater.update(strconv.Itoa(i))
31 }
32
33 // However many updates are dropped, the final one is always applied
34 deadline := time.Now().Add(5 * time.Second)
35 for {
36 mu.Lock()
37 done := len(applied) > 0 && applied[len(applied)-1] == last
38 mu.Unlock()
39 if done {
40 break
41 }
42 if time.Now().After(deadline) {
43 t.Fatalf("last update was not applied: %v", applied)
44 }
45 time.Sleep(time.Millisecond)
46 }
47
48 mu.Lock()
49 defer mu.Unlock()
50 // Updates are never applied out of order, so the border cannot be left
51 // showing a label older than the last one requested
52 for i, n := range applied {
53 if i > 0 && n <= applied[i-1] {
54 t.Errorf("applied out of order: %v", applied)
55 break
56 }
57 }
58 // A burst collapses instead of running one command per update. The
59 // updates are queued in a tight loop while each one takes a millisecond
60 // to apply, so all of them landing would mean no coalescing at all
61 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 }