@@ -4,65 +4,23 @@ import (
4
4
"context"
5
5
"testing"
6
6
7
- "github.com/stretchr/testify/assert"
8
7
"github.com/stretchr/testify/mock"
9
8
"github.com/thewizardplusplus/go-crawler/models"
10
9
)
11
10
12
- func TestNewConcurrentHandler (test * testing.T ) {
13
- innerHandler := new (MockLinkHandler )
14
- handler := NewConcurrentHandler (1000 , innerHandler )
15
-
16
- mock .AssertExpectationsForObjects (test , innerHandler )
17
- assert .Equal (test , innerHandler , handler .linkHandler )
18
- assert .Equal (test , & startModeHolder {}, handler .startMode )
19
- for _ , field := range []interface {}{
20
- handler .stoppingCtx ,
21
- handler .stoppingCtxCanceller ,
22
- handler .links ,
23
- } {
24
- assert .NotNil (test , field )
25
- }
26
- assert .Len (test , handler .links , 0 )
27
- assert .Equal (test , 1000 , cap (handler .links ))
28
- }
29
-
30
- func TestConcurrentHandler_HandleLink (test * testing.T ) {
31
- link := models.SourcedLink {
32
- SourceLink : "http://example.com/" ,
33
- Link : "http://example.com/test" ,
34
- }
35
-
36
- links := make (chan models.SourcedLink , 1 )
37
- handler := ConcurrentHandler {links : links }
38
- handler .HandleLink (context .Background (), link )
39
-
40
- gotLink := <- handler .links
41
- assert .Equal (test , link , gotLink )
42
- }
43
-
44
- func TestConcurrentHandler_running (test * testing.T ) {
45
- type fields struct {
46
- startMode * startModeHolder
47
- stoppingCtxCanceller ContextCancellerInterface
48
- links []models.SourcedLink
11
+ func TestConcurrentHandler (test * testing.T ) {
12
+ type args struct {
13
+ links []models.SourcedLink
49
14
}
50
15
51
16
for _ , data := range []struct {
52
- name string
53
- fields fields
54
- runHandler func (ctx context.Context , handler ConcurrentHandler )
17
+ name string
18
+ args args
19
+ startHandler func (ctx context.Context , handler ConcurrentHandler )
55
20
}{
56
21
{
57
- name : "with the Run() method" ,
58
- fields : fields {
59
- startMode : & startModeHolder {},
60
- stoppingCtxCanceller : func () ContextCancellerInterface {
61
- stoppingCtxCanceller := new (MockContextCancellerInterface )
62
- stoppingCtxCanceller .On ("CancelContext" ).Return ().Once ()
63
-
64
- return stoppingCtxCanceller
65
- }(),
22
+ name : "with the Start() method" ,
23
+ args : args {
66
24
links : []models.SourcedLink {
67
25
{
68
26
SourceLink : "http://example.com/" ,
@@ -74,20 +32,13 @@ func TestConcurrentHandler_running(test *testing.T) {
74
32
},
75
33
},
76
34
},
77
- runHandler : func (ctx context.Context , handler ConcurrentHandler ) {
78
- handler .Run (ctx )
35
+ startHandler : func (ctx context.Context , handler ConcurrentHandler ) {
36
+ handler .Start (ctx )
79
37
},
80
38
},
81
39
{
82
- name : "with the RunConcurrently() method" ,
83
- fields : fields {
84
- startMode : & startModeHolder {},
85
- stoppingCtxCanceller : func () ContextCancellerInterface {
86
- stoppingCtxCanceller := new (MockContextCancellerInterface )
87
- stoppingCtxCanceller .On ("CancelContext" ).Return ().Once ()
88
-
89
- return stoppingCtxCanceller
90
- }(),
40
+ name : "with the StartConcurrently() method" ,
41
+ args : args {
91
42
links : []models.SourcedLink {
92
43
{
93
44
SourceLink : "http://example.com/" ,
@@ -99,54 +50,26 @@ func TestConcurrentHandler_running(test *testing.T) {
99
50
},
100
51
},
101
52
},
102
- runHandler : func (ctx context.Context , handler ConcurrentHandler ) {
103
- handler .RunConcurrently (ctx , 10 )
53
+ startHandler : func (ctx context.Context , handler ConcurrentHandler ) {
54
+ handler .StartConcurrently (ctx , 10 )
104
55
},
105
56
},
106
57
} {
107
58
test .Run (data .name , func (test * testing.T ) {
108
59
innerHandler := new (MockLinkHandler )
109
- for _ , link := range data .fields .links {
110
- innerHandler .On ("HandleLink" , context .Background (), link ).Return ()
60
+ for _ , link := range data .args .links {
61
+ innerHandler .On ("HandleLink" , context .Background (), link ).Return (). Times ( 1 )
111
62
}
112
63
113
- linkChannel := make (chan models.SourcedLink , len (data .fields .links ))
114
- for _ , link := range data .fields .links {
115
- linkChannel <- link
116
- }
117
- close (linkChannel )
64
+ concurrentHandler := NewConcurrentHandler (1000 , innerHandler )
65
+ go data .startHandler (context .Background (), concurrentHandler )
118
66
119
- handler := ConcurrentHandler {
120
- linkHandler : innerHandler ,
121
-
122
- startMode : data .fields .startMode ,
123
- stoppingCtxCanceller : data .fields .stoppingCtxCanceller .CancelContext ,
124
- links : linkChannel ,
67
+ for _ , link := range data .args .links {
68
+ concurrentHandler .HandleLink (context .Background (), link )
125
69
}
126
- data . runHandler ( context . Background (), handler )
70
+ concurrentHandler . Stop ( )
127
71
128
- mock .AssertExpectationsForObjects (
129
- test ,
130
- data .fields .stoppingCtxCanceller ,
131
- innerHandler ,
132
- )
72
+ mock .AssertExpectationsForObjects (test , innerHandler )
133
73
})
134
74
}
135
75
}
136
-
137
- func TestConcurrentHandler_Stop (test * testing.T ) {
138
- stoppingCtx , stoppingCtxCanceller := context .WithCancel (context .Background ())
139
- stoppingCtxCanceller ()
140
-
141
- links := make (chan models.SourcedLink )
142
- handler := ConcurrentHandler {stoppingCtx : stoppingCtx , links : links }
143
- handler .Stop ()
144
-
145
- isNotClosed := true
146
- select {
147
- case _ , isNotClosed = <- handler .links :
148
- default : // to prevent blocking
149
- }
150
-
151
- assert .False (test , isNotClosed )
152
- }
0 commit comments