-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder_accessors_test.go
More file actions
132 lines (122 loc) · 3 KB
/
Copy pathdecoder_accessors_test.go
File metadata and controls
132 lines (122 loc) · 3 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//go:build amd64 || arm64
package ffmpeg
import (
"runtime"
"sync"
"testing"
"time"
)
func TestDecoderNativeAccessorsConcurrentClose(t *testing.T) {
if !requireFFmpeg(t) {
return
}
decoder, err := NewDecoder(createTestVideo(t), nil)
if err != nil {
t.Fatal(err)
}
const readers = 8
start := make(chan struct{})
stop := make(chan struct{})
var ready sync.WaitGroup
var done sync.WaitGroup
ready.Add(readers)
done.Add(readers)
for range readers {
go func() {
defer done.Done()
<-start
first := true
for {
select {
case <-stop:
return
default:
}
_ = decoder.NumStreams()
_ = decoder.DurationMicroseconds()
_ = decoder.BitRate()
_ = decoder.ProbeScore()
_ = decoder.Programs()
_ = decoder.DataStreams()
_ = decoder.HasSubtitle()
_ = decoder.SubtitleStream()
_ = decoder.TotalFrames()
if first {
first = false
ready.Done()
}
}
}()
}
close(start)
ready.Wait()
runtime.Gosched()
if err := decoder.Close(); err != nil {
t.Fatal(err)
}
close(stop)
done.Wait()
if got := decoder.NumStreams(); got != 0 {
t.Errorf("NumStreams after Close = %d, want 0", got)
}
if got := decoder.DurationMicroseconds(); got != 0 {
t.Errorf("DurationMicroseconds after Close = %d, want 0", got)
}
if got := decoder.BitRate(); got != 0 {
t.Errorf("BitRate after Close = %d, want 0", got)
}
if got := decoder.ProbeScore(); got != 0 {
t.Errorf("ProbeScore after Close = %d, want 0", got)
}
}
func TestDecoderNativeAccessorsUseOperationLock(t *testing.T) {
if !requireFFmpeg(t) {
return
}
decoder, err := NewDecoder(createTestVideo(t), nil)
if err != nil {
t.Fatal(err)
}
defer decoder.Close()
accessors := []struct {
name string
call func()
}{
{name: "NumStreams", call: func() { _ = decoder.NumStreams() }},
{name: "DurationMicroseconds", call: func() { _ = decoder.DurationMicroseconds() }},
{name: "BitRate", call: func() { _ = decoder.BitRate() }},
{name: "ProbeScore", call: func() { _ = decoder.ProbeScore() }},
{name: "Programs", call: func() { _ = decoder.Programs() }},
{name: "DataStreams", call: func() { _ = decoder.DataStreams() }},
{name: "HasSubtitle", call: func() { _ = decoder.HasSubtitle() }},
{name: "SubtitleStream", call: func() { _ = decoder.SubtitleStream() }},
{name: "TotalFrames", call: func() { _ = decoder.TotalFrames() }},
}
decoder.mu.Lock()
started := make(chan struct{}, len(accessors))
finished := make(chan string, len(accessors))
for _, accessor := range accessors {
go func() {
started <- struct{}{}
accessor.call()
finished <- accessor.name
}()
}
for range accessors {
<-started
}
select {
case name := <-finished:
decoder.mu.Unlock()
t.Fatalf("%s returned without acquiring the decoder operation lock", name)
case <-time.After(25 * time.Millisecond):
}
decoder.mu.Unlock()
for range accessors {
select {
case <-finished:
case <-time.After(time.Second):
t.Fatal("decoder accessor remained blocked after releasing the operation lock")
}
}
}