-
Notifications
You must be signed in to change notification settings - Fork 0
/
harvester_test.go
101 lines (75 loc) · 2.37 KB
/
harvester_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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package captchasolve
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
captchatoolsgo "github.com/Matthew17-21/Captcha-Tools/captchatools-go"
)
type mockHarvester struct {
mock.Mock
}
func (m *mockHarvester) GetTokenWithContext(ctx context.Context, additional ...*captchatoolsgo.AdditionalData) (*captchatoolsgo.CaptchaAnswer, error) {
args := m.Called(ctx, additional)
return args.Get(0).(*captchatoolsgo.CaptchaAnswer), args.Error(1)
}
func TestStartHarvesters(t *testing.T) {
ctx := context.Background()
mockLogger := &mockLogger{}
mockLogger.On("Info", mock.Anything, mock.Anything).Return()
mockHarvester := &mockHarvester{}
mockHarvester.On("GetTokenWithContext", mock.Anything, mock.Anything).Return(&captchatoolsgo.CaptchaAnswer{}, nil)
mockQueue := &mockQueue{}
mockQueue.On("Enqueue", mock.Anything).Return(nil)
c := &captchasolve{
config: config{
logger: mockLogger,
harvesters: []captchatoolsgo.Harvester{mockHarvester},
maxGoroutines: 1,
},
queue: mockQueue,
}
c.startHarvesters(ctx)
mockLogger.AssertExpectations(t)
mockHarvester.AssertExpectations(t)
mockQueue.AssertExpectations(t)
}
func TestHarvestToken_Success(t *testing.T) {
ctx := context.Background()
resultsChan := make(chan result, 1)
mockLogger := &mockLogger{}
mockLogger.On("Info", mock.Anything, mock.Anything).Return()
mockHarvester := &mockHarvester{}
mockHarvester.On("GetTokenWithContext", mock.Anything, mock.Anything).Return(&captchatoolsgo.CaptchaAnswer{}, nil)
c := &captchasolve{
config: config{
logger: mockLogger,
},
}
c.harvestToken(ctx, mockHarvester, resultsChan)
res := <-resultsChan
assert.NoError(t, res.err)
assert.NotNil(t, res.token)
mockLogger.AssertExpectations(t)
mockHarvester.AssertExpectations(t)
}
func TestProcessResults_Error(t *testing.T) {
ctx := context.Background()
resultsChan := make(chan result, 1)
mockLogger := &mockLogger{}
mockLogger.On("Info", mock.Anything, mock.Anything).Return()
mockLogger.On("Error", mock.Anything, mock.Anything).Return()
mockQueue := &mockQueue{}
c := &captchasolve{
config: config{
logger: mockLogger,
},
queue: mockQueue,
}
resultsChan <- result{token: nil, err: errors.New("test error")}
close(resultsChan)
_, err := c.processResults(ctx, resultsChan)
assert.Error(t, err)
mockLogger.AssertExpectations(t)
}