-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbroadcaster_test.go
More file actions
71 lines (60 loc) · 1.11 KB
/
Copy pathbroadcaster_test.go
File metadata and controls
71 lines (60 loc) · 1.11 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
// MIT License
// Copyright (c) 2025 Pooyan Khanjankhani
package main
import (
"io"
"strings"
"sync"
"testing"
)
type BroadcasterTest struct {
t *testing.T
b *Broadcaster
str string
}
func (this *BroadcasterTest) Run() {
input := strings.NewReader(this.str)
var wg sync.WaitGroup
// 1: boardcaster run
// 2,3,4,...: check pipe
pipesCnt := 10000
wg.Add(1 + pipesCnt)
for i := 0; i < pipesCnt; i++ {
r, w := io.Pipe()
this.b.Add(w)
go this.checkOutput(r, w, &wg)
}
go func() {
defer wg.Done()
err := this.b.Run(input)
if err != nil {
this.t.Error(err)
this.t.Fail()
}
}()
wg.Wait()
}
func (this *BroadcasterTest) checkOutput(
r *io.PipeReader, w *io.PipeWriter,
wg *sync.WaitGroup,
) {
defer wg.Done()
defer this.b.Remove(w)
defer r.Close()
b, err := io.ReadAll(r)
if err != nil {
this.t.Error(err)
this.t.Fail()
}
if string(b) != this.str {
this.t.Errorf(
"unexpected received value: expected: %s, received: %s",
this.str, string(b),
)
this.t.Fail()
}
}
func TestBroadcaster(t *testing.T) {
bt := BroadcasterTest{t, NewBroadcaster(), "0123456789"}
bt.Run()
}