-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient_codec_test.go
More file actions
89 lines (79 loc) · 1.79 KB
/
Copy pathclient_codec_test.go
File metadata and controls
89 lines (79 loc) · 1.79 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
package compose
import (
"bytes"
"runtime"
"sync"
"sync/atomic"
"testing"
)
func TestClientSetCompressionConcurrentPublishFrame(t *testing.T) {
addr := tempSocket(t)
srv, err := Listen(addr)
if err != nil {
t.Fatalf("Listen: %v", err)
}
t.Cleanup(func() { _ = srv.Close() })
const (
width = 32
height = 32
frames = 128
)
expectedPixels := makePixels(width, height, 0xA5)
var received atomic.Int64
var invalid atomic.Bool
srv.OnFrame(func(f Frame) {
if f.Width != width || f.Height != height || !bytes.Equal(f.Pixels, expectedPixels) {
invalid.Store(true)
}
received.Add(1)
})
client, err := Dial(addr, WithName("codec-race"), WithFrameSize(width, height))
if err != nil {
t.Fatalf("Dial: %v", err)
}
t.Cleanup(func() { _ = client.Close() })
start := make(chan struct{})
errs := make(chan error, frames)
var wg sync.WaitGroup
wg.Add(2)
// Publish and switch codecs from separate goroutines. The scheduler hint
// keeps both operations interleaved while retaining deterministic inputs.
go func() {
defer wg.Done()
<-start
for i := 0; i < frames; i++ {
if err := client.PublishFrame(Frame{
Pixels: expectedPixels,
Width: width,
Height: height,
}); err != nil {
errs <- err
}
runtime.Gosched()
}
}()
go func() {
defer wg.Done()
<-start
for i := 0; i < frames*2; i++ {
if i%2 == 0 {
client.SetCompression("lz4")
} else {
client.SetCompression("raw")
}
runtime.Gosched()
}
}()
close(start)
wg.Wait()
close(errs)
for publishErr := range errs {
t.Errorf("PublishFrame: %v", publishErr)
}
if !waitFor(t, func() bool { return received.Load() == frames }) {
t.Fatalf("received %d/%d frames", received.Load(), frames)
}
if invalid.Load() {
t.Fatal("received frame did not match the published payload")
}
}