-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharx2_test.go
75 lines (62 loc) · 1.48 KB
/
arx2_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
package arx2_test
import (
"sync"
"testing"
"time"
"github.com/MichalMichalak/arx2/cnf"
"github.com/MichalMichalak/arx2/log"
"github.com/MichalMichalak/arx2/provider"
"github.com/MichalMichalak/arx2/svc"
"github.com/stretchr/testify/require"
)
func TestArx2(t *testing.T) {
numChan := make(chan int)
p1 := provider.NewMyDoer()
p2 := provider.NewMyConsumer(numChan)
l := log.NewServiceLogger(log.SeverityDebug)
s, err := svc.NewBuilder().Logger(l).Name("s1").Provider(p1).Provider(p2).ConfigPath("_temp/config.yaml").Build()
require.NoError(t, err)
require.NotNil(t, s)
wg := &sync.WaitGroup{}
wg.Add(2)
go f1(s, t, wg)
go f2(numChan, wg)
wg.Wait()
}
func f1(s *svc.Service, t *testing.T, wg *sync.WaitGroup) {
time.Sleep(1 * time.Second)
err := s.Run()
require.NoError(t, err)
wg.Done()
}
func f2(numChan chan int, wg *sync.WaitGroup) {
time.Sleep(1 * time.Second)
for i := 1; i < 10; i++ {
numChan <- i
}
numChan <- 0
wg.Done()
}
func TestConsumer(t *testing.T) {
numChan := make(chan int)
p := provider.NewMyConsumer(numChan)
wg := sync.WaitGroup{}
wg.Add(2)
logger := log.NewServiceLogger(log.SeverityDebug)
ctx := svc.NewServiceContext("s1", logger, map[string]svc.Provider{})
err := p.Configure(ctx, cnf.NewResolver(logger, []string{"_temp/config.yaml"}))
require.NoError(t, err)
go func() {
err := p.Run()
require.NoError(t, err)
wg.Done()
}()
go func() {
numChan <- 1
numChan <- 2
numChan <- 3
numChan <- 0
wg.Done()
}()
wg.Wait()
}