Skip to content

Commit 29cee65

Browse files
Update the tests of the handlers.ConcurrentHandler structure
1 parent a1641b1 commit 29cee65

File tree

3 files changed

+22
-124
lines changed

3 files changed

+22
-124
lines changed

handlers/concurrent_handler_test.go

Lines changed: 22 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -4,65 +4,23 @@ import (
44
"context"
55
"testing"
66

7-
"github.com/stretchr/testify/assert"
87
"github.com/stretchr/testify/mock"
98
"github.com/thewizardplusplus/go-crawler/models"
109
)
1110

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
4914
}
5015

5116
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)
5520
}{
5621
{
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{
6624
links: []models.SourcedLink{
6725
{
6826
SourceLink: "http://example.com/",
@@ -74,20 +32,13 @@ func TestConcurrentHandler_running(test *testing.T) {
7432
},
7533
},
7634
},
77-
runHandler: func(ctx context.Context, handler ConcurrentHandler) {
78-
handler.Run(ctx)
35+
startHandler: func(ctx context.Context, handler ConcurrentHandler) {
36+
handler.Start(ctx)
7937
},
8038
},
8139
{
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{
9142
links: []models.SourcedLink{
9243
{
9344
SourceLink: "http://example.com/",
@@ -99,54 +50,26 @@ func TestConcurrentHandler_running(test *testing.T) {
9950
},
10051
},
10152
},
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)
10455
},
10556
},
10657
} {
10758
test.Run(data.name, func(test *testing.T) {
10859
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)
11162
}
11263

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)
11866

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)
12569
}
126-
data.runHandler(context.Background(), handler)
70+
concurrentHandler.Stop()
12771

128-
mock.AssertExpectationsForObjects(
129-
test,
130-
data.fields.stoppingCtxCanceller,
131-
innerHandler,
132-
)
72+
mock.AssertExpectationsForObjects(test, innerHandler)
13373
})
13474
}
13575
}
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-
}

handlers/mock_context_canceller_interface_test.go

Lines changed: 0 additions & 15 deletions
This file was deleted.

handlers/mock_gen.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,3 @@ type LinkChecker interface {
2323
type LinkHandler interface {
2424
models.LinkHandler
2525
}
26-
27-
//go:generate mockery --name=ContextCancellerInterface --inpackage --case=underscore --testonly
28-
29-
// ContextCancellerInterface ...
30-
//
31-
// It is used only for mock generating.
32-
//
33-
type ContextCancellerInterface interface {
34-
CancelContext()
35-
}

0 commit comments

Comments
 (0)