-
-
Notifications
You must be signed in to change notification settings - Fork 151
/
services_test.go
276 lines (235 loc) · 6.31 KB
/
services_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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package integram
import (
"errors"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/mrjones/oauth"
"golang.org/x/oauth2"
"gopkg.in/mgo.v2"
"net/http"
"os"
"strings"
"testing"
"time"
)
type serviceTestConfig struct {
service *Service
}
var totalServices int
// Service returns *integram.Service
func (sc serviceTestConfig) Service() *Service {
return sc.service
}
func dumbFuncWithParam(a bool) error {
if a {
a = false
}
return nil
}
var dCounter = 0
func dumbFuncWithError(es string) error {
dCounter++
return errors.New(es)
}
type dumbStruct struct {
A float64
B struct {
A string
B int
}
}
func dumbFuncWithParams(a, b int, d dumbStruct) error {
if a != b {
a = b
}
if d.B.A == "" {
d.A = 0
}
dCounter++
return nil
}
func dumbFuncWithContext(c *Context) error {
dCounter++
return nil
}
func dumbFuncWithContextAndParam(c *Context, a bool) error {
if a {
a = false
}
dCounter++
return nil
}
func dumbFuncWithContextAndParams(c *Context, a, b int) error {
if a != b {
a = b
}
dCounter++
return nil
}
var db *mgo.Database
func TestMain(t *testing.M) {
db = mongoSession.Clone().DB(mongo.Database)
defer db.Session.Close()
registerServices()
code := t.Run()
clearData()
os.Exit(code)
}
func TestRegister(t *testing.T) {
if totalServices > len(services) {
t.Errorf("Register() = len(services)==%d, want %d", len(services), totalServices)
}
}
func registerServices() {
if len(services) == 0 {
log.SetLevel(log.DebugLevel)
servicesToRegister := []struct {
service Servicer
botToken string
}{
{serviceTestConfig{&Service{
Name: "servicewithoauth1",
NameToPrint: "ServiceWithOAuth1",
DefaultOAuth1: &DefaultOAuth1{
Key: "ID",
Secret: "SECRET",
RequestTokenURL: "https://sub.example.com/1/OAuthGetRequestToken",
AuthorizeTokenURL: "https://sub.example.com/1/OAuthAuthorizeToken",
AccessTokenURL: "https://sub.example.com/1/OAuthGetAccessToken",
AdditionalAuthorizationURLParams: map[string]string{
"name": "Integram",
"expiration": "never",
"scope": "read,write",
},
AccessTokenReceiver: func(serviceContext *Context, r *http.Request, requestToken *oauth.RequestToken) (token string, err error) {
return "token", nil
},
},
}}, ""},
{serviceTestConfig{&Service{
Name: "servicewithoauth2",
NameToPrint: "ServiceWithOAuth2",
DefaultOAuth2: &DefaultOAuth2{
Config: oauth2.Config{
ClientID: "ID",
ClientSecret: "SECRET",
Endpoint: oauth2.Endpoint{
AuthURL: "https://sub.example.com/oauth/authorize",
TokenURL: "https://sub.example.com/oauth/token",
},
},
},
}}, ""},
{serviceTestConfig{&Service{
Name: "servicewithjobs",
NameToPrint: "ServiceWithJobs",
Jobs: []Job{
{dumbFuncWithParam, 10, JobRetryFibonacci},
{dumbFuncWithParams, 10, JobRetryLinear},
{dumbFuncWithError, 3, JobRetryFibonacci},
},
}}, ""},
{serviceTestConfig{&Service{
Name: "servicewithactions",
NameToPrint: "ServiceWithActions",
Actions: []interface{}{
dumbFuncWithContext,
dumbFuncWithContextAndParam,
dumbFuncWithContextAndParams,
},
}}, ""},
{serviceTestConfig{&Service{
Name: "servicewithbottoken",
NameToPrint: "ServiceWithBotToken",
}}, os.Getenv("INTEGRAM_TEST_BOT_TOKEN")},
}
for _, s := range servicesToRegister {
Register(s.service, s.botToken)
}
totalServices = len(servicesToRegister)
}
go func() { Run() }()
time.Sleep(time.Second * 3)
}
func TestService_Bot(t *testing.T) {
s, err := serviceByName("servicewithbottoken")
if err != nil || s == nil {
t.Errorf("TestService_Bot() 'servicewithbottoken' not found")
return
}
bt := strings.Split(os.Getenv("INTEGRAM_TEST_BOT_TOKEN"), ":")
bot := s.Bot()
if bot == nil || fmt.Sprintf("%d", bot.ID) != bt[0] || len(bot.services) == 0 || bot.services[0].Name != "servicewithbottoken" || bot.token != bt[1] {
t.Errorf("TestService_Bot() bad bot returned for services")
}
}
func TestService_DefaultOAuthProvider(t *testing.T) {
servicesWithOAP := []string{"servicewithoauth1", "servicewithoauth2"}
for _, sn := range servicesWithOAP {
s, err := serviceByName(sn)
if err != nil || s == nil {
t.Errorf("TestService_DefaultOAuthProvider() '%s' not found", sn)
return
}
oap := s.DefaultOAuthProvider()
if oap == nil || oap.ID == "" || oap.Service != sn || oap.BaseURL.String() != "https://sub.example.com" {
t.Errorf("TestService_DefaultOAuthProvider() bad DefaultOAuthProvider returned for '%s'", sn)
}
}
}
func TestService_DoJob(t *testing.T) {
s, err := serviceByName("servicewithjobs")
if err != nil || s == nil {
t.Error("TestService_DoJob 'servicewithjobs' not found")
return
}
dCounter = 0
job, err := s.DoJob(dumbFuncWithParams, 1, 2, dumbStruct{A: 1.0, B: struct {
A string
B int
}{A: "_",
B: 1.0}})
if err != nil || job == nil || job.Id() == "" {
t.Error("TestService_DoJob failed")
}
maxTimeToFinishJob := time.Second * 5
for dCounter == 0 && maxTimeToFinishJob > 0 {
time.Sleep(time.Millisecond * 50)
maxTimeToFinishJob = maxTimeToFinishJob - time.Millisecond*50
}
if maxTimeToFinishJob <= 0 {
t.Errorf("TestService_DoJob 'dumbFuncWithParams' not finished after maxTimeToFinishJob: instead got status: %s", job.Status())
}
// reset global retries counter
dCounter = 0
job, err = s.DoJob(dumbFuncWithError, "errText")
if err != nil || job == nil || job.Id() == "" {
t.Errorf("TestService_DoJob failed with err: %v", err)
}
err = job.Refresh()
if err != nil {
t.Error(err)
}
maxTimeToFinishRetries := time.Second * 6
prevTime := job.Time()
fmt.Println(prevTime)
prevdCounter := 0
for dCounter < 4 && maxTimeToFinishJob > 0 {
maxTimeToFinishRetries = maxTimeToFinishRetries - time.Millisecond*100
time.Sleep(time.Millisecond * 100)
if dCounter > prevdCounter {
fmt.Printf("dCounter: %d\n", dCounter)
job.Refresh()
fmt.Printf("time: %d\n", job.Time())
jt := job.Time()
if dCounter < 4 && jt <= prevTime {
t.Error("TestService_DoJob next try job's1 time must be greater than on prev attempt")
}
prevTime = jt
prevdCounter = dCounter
}
}
if maxTimeToFinishJob <= 0 {
t.Errorf("TestService_DoJob 'dumbFuncWithError' not enough retries after 6 secs. want 4, got %d instead", dCounter)
}
}