-
Notifications
You must be signed in to change notification settings - Fork 0
/
openbar_test.go
77 lines (63 loc) · 1.33 KB
/
openbar_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package openbar_test
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"openbar"
"sync"
"testing"
"time"
)
func TestOpenBar(t *testing.T) {
w1, w2 := new(sync.WaitGroup), new(sync.WaitGroup)
w1.Add(1)
w2.Add(1)
stdout := bytes.NewBuffer(nil)
stderr := bytes.NewBuffer(nil)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// We need a sync.Once here to wait for at least one screen update.
var once sync.Once
module := openbar.ModuleFunc(func() (string, error) {
defer once.Do(w1.Done)
return "hello", nil
})
go func() {
defer w2.Done()
if err := openbar.Run(
ctx,
openbar.WithOutput(stdout),
openbar.WithError(stderr),
openbar.WithModule(module, 10*time.Hour),
openbar.WithJitter(0),
); err != nil {
t.Error(err)
}
}()
w1.Wait() // Wait for update.
cancel() // Stop.
w2.Wait() // Wait for shutdown.
if stderr.String() != "" {
t.Error(stderr.String())
}
// Remove the last comma and close the infinite array.
stdout.Truncate(stdout.Len() - 1)
stdout.WriteByte(0x5D)
t.Log(stdout.String())
line1, err := stdout.ReadBytes(0x0A)
if err != nil {
t.Error(err)
}
line2, err := stdout.ReadBytes(0x0A)
if !errors.Is(err, io.EOF) {
t.Error(err)
}
if !json.Valid(line1) {
t.Error("invalid header")
}
if !json.Valid(line2) {
t.Error("invalid body")
}
}