1package app
2
3import (
4 "strings"
5 "sync/atomic"
6 "testing"
7 "time"
8
9 "github.com/bnema/zerowrap"
10 "github.com/stretchr/testify/mock"
11)
12
13// Every running output is asked, in parallel, with the bound; an output
14// that has stopped is not.
15func TestPrepareSeatDisableAsksRunningOutputs(t *testing.T) {
16 b := &drmBackend{log: zerowrap.Default()}
17 a, c, gone := newMockseatDisablePreparer(t), newMockseatDisablePreparer(t), newMockseatDisablePreparer(t)
18 var asked atomic.Int32
19 for _, p := range []*mockseatDisablePreparer{a, c} {
20 p.EXPECT().PrepareSeatDisable(seatDisableTimeout).RunAndReturn(func(time.Duration) bool { asked.Add(1); return true }).Once()
21 }
22 b.addPreparer(a)
23 b.addPreparer(c)
24 b.addPreparer(gone)
25 b.removePreparer(gone)
26 b.prepareSeatDisable()
27 if asked.Load() != 2 {
28 t.Fatalf("%d outputs asked", asked.Load())
29 }
30}
31
32// A late output is reported once and the disable goes on; outputs that
33// answer in time, or none at all, log nothing.
34func TestPrepareSeatDisableWarnsOnlyWhenLate(t *testing.T) {
35 for _, tc := range []struct {
36 name string
37 acks []bool
38 warns bool
39 }{
40 {"no outputs", nil, false},
41 {"in time", []bool{true, true}, false},
42 {"one late", []bool{true, false}, true},
43 } {
44 t.Run(tc.name, func(t *testing.T) {
45 log := &lockedBuffer{}
46 b := &drmBackend{log: zerowrap.New(zerowrap.Config{Format: "json", Output: log})}
47 for _, ack := range tc.acks {
48 p := newMockseatDisablePreparer(t)
49 p.EXPECT().PrepareSeatDisable(mock.Anything).Return(ack).Once()
50 b.addPreparer(p)
51 }
52 b.prepareSeatDisable()
53 if got := strings.Contains(log.String(), `"outputs":1`); got != tc.warns {
54 t.Fatalf("late warning %v, want %v: %s", got, tc.warns, log.String())
55 }
56 })
57 }
58}