|
1
|
|
|
package producer_test |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"context" |
|
5
|
|
|
"net/http" |
|
6
|
|
|
"testing" |
|
7
|
|
|
|
|
8
|
|
|
"github.com/stefanoj3/dirstalk/pkg/scan" |
|
9
|
|
|
"github.com/stefanoj3/dirstalk/pkg/scan/producer" |
|
10
|
|
|
"github.com/stretchr/testify/assert" |
|
11
|
|
|
) |
|
12
|
|
|
|
|
13
|
|
|
func TestDictionaryProducerShouldProduce(t *testing.T) { |
|
14
|
|
|
t.Parallel() |
|
15
|
|
|
|
|
16
|
|
|
const depth = 4 |
|
17
|
|
|
|
|
18
|
|
|
sut := producer.NewDictionaryProducer( |
|
19
|
|
|
[]string{http.MethodGet, http.MethodPost}, |
|
20
|
|
|
[]string{"/home", "/about"}, |
|
21
|
|
|
depth, |
|
22
|
|
|
) |
|
23
|
|
|
|
|
24
|
|
|
results := make([]scan.Target, 0, 4) |
|
25
|
|
|
|
|
26
|
|
|
producerChannel := sut.Produce(context.Background()) |
|
27
|
|
|
for r := range producerChannel { |
|
28
|
|
|
results = append(results, r) |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
assert.Len(t, results, 4) |
|
32
|
|
|
|
|
33
|
|
|
expectedResults := []scan.Target{ |
|
34
|
|
|
{ |
|
35
|
|
|
Depth: depth, |
|
36
|
|
|
Path: "/home", |
|
37
|
|
|
Method: http.MethodGet, |
|
38
|
|
|
}, |
|
39
|
|
|
{ |
|
40
|
|
|
Depth: depth, |
|
41
|
|
|
Path: "/home", |
|
42
|
|
|
Method: http.MethodPost, |
|
43
|
|
|
}, |
|
44
|
|
|
{ |
|
45
|
|
|
Depth: depth, |
|
46
|
|
|
Path: "/about", |
|
47
|
|
|
Method: http.MethodGet, |
|
48
|
|
|
}, |
|
49
|
|
|
{ |
|
50
|
|
|
Depth: depth, |
|
51
|
|
|
Path: "/about", |
|
52
|
|
|
Method: http.MethodPost, |
|
53
|
|
|
}, |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
assert.Equal(t, expectedResults, results) |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
func TestDictionaryProducerCanBeCanceled(t *testing.T) { |
|
60
|
|
|
t.Parallel() |
|
61
|
|
|
|
|
62
|
|
|
const depth = 4 |
|
63
|
|
|
|
|
64
|
|
|
sut := producer.NewDictionaryProducer( |
|
65
|
|
|
[]string{http.MethodGet, http.MethodPost, http.MethodPatch, http.MethodDelete}, |
|
66
|
|
|
[]string{"/home", "/about", "/index", "/search", "/tomato"}, |
|
67
|
|
|
depth, |
|
68
|
|
|
) |
|
69
|
|
|
|
|
70
|
|
|
ctx, cancelFunc := context.WithCancel(context.Background()) |
|
71
|
|
|
|
|
72
|
|
|
producerChannel := sut.Produce(ctx) |
|
73
|
|
|
|
|
74
|
|
|
cancelFunc() |
|
75
|
|
|
|
|
76
|
|
|
resultsCount := 0 |
|
77
|
|
|
|
|
78
|
|
|
for range producerChannel { |
|
79
|
|
|
resultsCount++ |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
// 11 is the size of the producer buffer |
|
83
|
|
|
assert.True(t, resultsCount <= 11) |
|
84
|
|
|
} |
|
85
|
|
|
|