forked from tonicpow/go-minercraft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
660 lines (562 loc) · 17 KB
/
client_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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
package minercraft
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
testAPIVersion = "1.4.0" // Old version was 0.1.0
testEncoding = "UTF-8"
testMimeType = "application/json"
testMinerID = "1234567"
testMinerName = "TestMiner"
testMinerToken = "0987654321"
testMinerURL = "https://testminer.com"
testTx = "7e0c4651fc256c0433bd704d7e13d24c8d10235f4b28ba192849c5d318de974b"
testAPIType = "mAPI"
)
// mockHTTPDefaultClient for mocking requests
type mockHTTPDefaultClient struct{}
// Do is a mock http request
func (m *mockHTTPDefaultClient) Do(req *http.Request) (*http.Response, error) {
resp := new(http.Response)
resp.StatusCode = http.StatusBadRequest
// No req found
if req == nil {
return resp, fmt.Errorf("missing request")
}
if req.URL.String() == "/test" {
resp.StatusCode = http.StatusOK
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(`{"message":"test"}`)))
}
// Default is valid
return resp, nil
}
// newTestClient returns a client for mocking (using a custom HTTP interface)
func newTestClient(httpClient HTTPInterface) ClientInterface {
client, _ := NewClient(nil, httpClient, testAPIType, nil, nil)
// client.httpClient = httpClient
return client
}
// TestNewClient tests the method NewClient()
func TestNewClient(t *testing.T) {
t.Parallel()
t.Run("valid new client", func(t *testing.T) {
client, err := NewClient(nil, nil, testAPIType, nil, nil)
assert.NotNil(t, client)
assert.NoError(t, err)
// Test default miners
assert.Equal(t, 2, len(client.Miners()))
})
t.Run("custom http client", func(t *testing.T) {
client, err := NewClient(nil, http.DefaultClient, testAPIType, nil, nil)
assert.NotNil(t, client)
assert.NoError(t, err)
})
t.Run("default miners", func(t *testing.T) {
client, err := NewClient(nil, nil, testAPIType, nil, nil)
assert.NotNil(t, client)
assert.NoError(t, err)
// Get Taal
miner := client.MinerByName(MinerTaal)
assert.Equal(t, MinerTaal, miner.Name)
// Get Mempool
// miner = client.MinerByName(MinerMempool)
// assert.Equal(t, MinerMempool, miner.Name)
// Get Matterpool
// miner = client.MinerByName(MinerMatterpool)
// assert.Equal(t, MinerMatterpool, miner.Name)
// Get GorillaPool
miner = client.MinerByName(MinerGorillaPool)
assert.Equal(t, MinerGorillaPool, miner.Name)
})
t.Run("custom miners", func(t *testing.T) {
miners := []*Miner{{
MinerID: testMinerID,
Name: testMinerName,
}}
minerAPIs := []*MinerAPIs{{MinerID: testMinerID, APIs: []API{{Token: testMinerToken, URL: testMinerURL, Type: testAPIType}}}}
client, err := NewClient(nil, nil, testAPIType, miners, minerAPIs)
assert.NotNil(t, client)
assert.NoError(t, err)
// Get test miner
miner := client.MinerByName(testMinerName)
assert.Equal(t, testMinerName, miner.Name)
assert.Equal(t, 1, len(client.Miners()))
})
}
// ExampleNewClient example using NewClient()
func ExampleNewClient() {
client, err := NewClient(nil, nil, testAPIType, nil, nil)
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
fmt.Printf("created new client with %d default miners", len(client.Miners()))
// Output:created new client with 2 default miners
}
// BenchmarkNewClient benchmarks the method NewClient()
func BenchmarkNewClient(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = NewClient(nil, nil, testAPIType, nil, nil)
}
}
// TestDefaultClientOptions tests setting DefaultClientOptions()
func TestDefaultClientOptions(t *testing.T) {
t.Parallel()
t.Run("default client options", func(t *testing.T) {
options := DefaultClientOptions()
assert.Equal(t, defaultUserAgent, options.UserAgent)
assert.Equal(t, 2.0, options.BackOffExponentFactor)
assert.Equal(t, 2*time.Millisecond, options.BackOffInitialTimeout)
assert.Equal(t, 2*time.Millisecond, options.BackOffMaximumJitterInterval)
assert.Equal(t, 10*time.Millisecond, options.BackOffMaxTimeout)
assert.Equal(t, 20*time.Second, options.DialerKeepAlive)
assert.Equal(t, 5*time.Second, options.DialerTimeout)
assert.Equal(t, 2, options.RequestRetryCount)
assert.Equal(t, 30*time.Second, options.RequestTimeout)
assert.Equal(t, 3*time.Second, options.TransportExpectContinueTimeout)
assert.Equal(t, 20*time.Second, options.TransportIdleTimeout)
assert.Equal(t, 10, options.TransportMaxIdleConnections)
assert.Equal(t, 5*time.Second, options.TransportTLSHandshakeTimeout)
})
t.Run("no retry", func(t *testing.T) {
options := DefaultClientOptions()
options.RequestRetryCount = 0
client, err := NewClient(options, nil, testAPIType, nil, nil)
assert.NotNil(t, client)
assert.NoError(t, err)
})
}
// ExampleDefaultClientOptions example using DefaultClientOptions()
func ExampleDefaultClientOptions() {
options := DefaultClientOptions()
options.UserAgent = "Custom UserAgent v1.0"
client, err := NewClient(options, nil, testAPIType, nil, nil)
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
fmt.Printf("created new client with user agent: %s", client.UserAgent())
// Output:created new client with user agent: Custom UserAgent v1.0
}
// BenchmarkDefaultClientOptions benchmarks the method DefaultClientOptions()
func BenchmarkDefaultClientOptions(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = DefaultClientOptions()
}
}
// TestClient_AddMiner tests the method AddMiner()
func TestClient_AddMiner(t *testing.T) {
t.Parallel()
t.Run("valid cases", func(t *testing.T) {
// Create the list of tests
var tests = []struct {
testCase string
inputMiner Miner
inputAPIs []API
expectedName string
expectedURL string
}{
{
"valid miner",
Miner{
MinerID: testMinerID,
Name: "Test",
},
[]API{
API{
Token: testMinerToken,
URL: "https://testminer.com",
Type: testAPIType,
},
},
"Test",
"https://testminer.com",
},
}
// Run tests
client := newTestClient(&mockHTTPDefaultClient{})
for _, test := range tests {
t.Run(test.testCase, func(t *testing.T) {
err := client.AddMiner(test.inputMiner, test.inputAPIs)
assert.NoError(t, err)
// Get the miner
miner := client.MinerByName(test.inputMiner.Name)
assert.Equal(t, test.expectedName, miner.Name)
assert.Equal(t, test.expectedURL, test.inputAPIs[0].URL)
})
}
})
t.Run("invalid cases", func(t *testing.T) {
// Create the list of tests
var tests = []struct {
testCase string
inputMiner Miner
inputAPIs []API
}{
{
"duplicate miner - by name",
Miner{
MinerID: testMinerID + "123",
Name: "Test",
},
[]API{
{
Token: testMinerToken,
URL: testMinerURL,
Type: testAPIType,
},
},
},
{
"duplicate miner - by id",
Miner{
MinerID: testMinerID,
Name: "Test123",
},
[]API{
{
Token: testMinerToken,
URL: testMinerURL,
Type: testAPIType,
},
},
},
{
"missing miner name",
Miner{
MinerID: testMinerID,
Name: "",
},
[]API{
{
Token: testMinerToken,
URL: testMinerURL,
Type: testAPIType,
},
},
},
{
"missing miner url",
Miner{
MinerID: testMinerID,
Name: "TestURL",
},
[]API{
{
Token: testMinerToken,
URL: testMinerURL,
Type: testAPIType,
},
},
},
{
"invalid miner url - http",
Miner{
MinerID: testMinerID,
Name: "TestURL",
},
[]API{
{
Token: testMinerToken,
URL: "www.domain.com",
Type: testAPIType,
},
},
},
{
"invalid miner url - trigger parse error",
Miner{
MinerID: testMinerID,
Name: "TestURL",
},
[]API{
{
Token: testMinerToken,
URL: "postgres://user:abc{[email protected]:5432/db?sslmode=require",
Type: testAPIType,
},
},
},
}
// Run tests
client := newTestClient(&mockHTTPDefaultClient{})
// Add a miner to start
err := client.AddMiner(Miner{MinerID: testMinerID, Name: "Test"}, []API{API{URL: testMinerURL, Type: testAPIType}})
assert.NoError(t, err)
for _, test := range tests {
t.Run(test.testCase, func(t *testing.T) {
err = client.AddMiner(test.inputMiner, test.inputAPIs)
assert.Error(t, err)
})
}
})
}
// ExampleClient_AddMiner example using AddMiner()
func ExampleClient_AddMiner() {
client, err := NewClient(nil, nil, testAPIType, nil, nil)
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
// Add a miner
if err = client.AddMiner(Miner{Name: testMinerName}, []API{{URL: testMinerURL, Type: testAPIType}}); err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
// Get miner by name
fmt.Printf("created new miner named: %s", client.MinerByName(testMinerName).Name)
// Output:created new miner named: TestMiner
}
// BenchmarkClient_AddMiner benchmarks the method AddMiner()
func BenchmarkClient_AddMiner(b *testing.B) {
client, _ := NewClient(nil, nil, testAPIType, nil, nil)
for i := 0; i < b.N; i++ {
_ = client.AddMiner(Miner{Name: testMinerName}, []API{{URL: testMinerURL, Type: testAPIType}})
}
}
// TestClient_MinerByName tests the method MinerByName()
func TestClient_MinerByName(t *testing.T) {
t.Parallel()
t.Run("get valid miner", func(t *testing.T) {
client := newTestClient(&mockHTTPDefaultClient{})
// Add a valid miner
err := client.AddMiner(Miner{
Name: testMinerName,
}, []API{{URL: testMinerURL, Type: testAPIType}})
assert.NoError(t, err)
// Get valid miner
miner := client.MinerByName(testMinerName)
assert.NotNil(t, miner)
})
t.Run("get invalid miner", func(t *testing.T) {
client := newTestClient(&mockHTTPDefaultClient{})
// Add a valid miner
err := client.AddMiner(Miner{
Name: testMinerName,
}, []API{{URL: testMinerURL, Type: testAPIType}},
)
assert.NoError(t, err)
// Get invalid miner
miner := client.MinerByName("Unknown")
assert.Nil(t, miner)
})
}
// ExampleClient_MinerByName example using MinerByName()
func ExampleClient_MinerByName() {
client, err := NewClient(nil, nil, testAPIType, nil, nil)
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
// Add a miner
if err = client.AddMiner(Miner{Name: testMinerName}, []API{{URL: testMinerURL, Type: testAPIType}}); err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
// Get miner by name
fmt.Printf("created new miner named: %s", client.MinerByName(testMinerName).Name)
// Output:created new miner named: TestMiner
}
// BenchmarkClient_MinerByName benchmarks the method MinerByName()
func BenchmarkClient_MinerByName(b *testing.B) {
client, _ := NewClient(nil, nil, testAPIType, nil, nil)
_ = client.AddMiner(Miner{Name: testMinerName}, []API{{URL: testMinerURL, Type: testAPIType}})
for i := 0; i < b.N; i++ {
_ = client.MinerByName(testMinerName)
}
}
// TestClient_MinerByID tests the method MinerByID()
func TestClient_MinerByID(t *testing.T) {
t.Parallel()
t.Run("get valid miner", func(t *testing.T) {
client := newTestClient(&mockHTTPDefaultClient{})
// Add a valid miner
err := client.AddMiner(Miner{
Name: testMinerName,
MinerID: testMinerID,
}, []API{{URL: testMinerURL, Type: testAPIType}})
assert.NoError(t, err)
// Get valid miner
miner := client.MinerByID(testMinerID)
assert.NotNil(t, miner)
})
t.Run("get invalid miner", func(t *testing.T) {
client := newTestClient(&mockHTTPDefaultClient{})
// Add a valid miner
err := client.AddMiner(Miner{
Name: testMinerName,
MinerID: testMinerID,
}, []API{{URL: testMinerURL, Type: testAPIType}})
assert.NoError(t, err)
// Get invalid miner
miner := client.MinerByID("00000")
assert.Nil(t, miner)
})
}
// ExampleClient_MinerByID example using MinerByID()
func ExampleClient_MinerByID() {
client, err := NewClient(nil, nil, testAPIType, nil, nil)
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
// Add a miner
if err = client.AddMiner(Miner{Name: testMinerName, MinerID: testMinerID}, []API{{URL: testMinerURL, Type: testAPIType}}); err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
// Get miner by id
fmt.Printf("created new miner named: %s", client.MinerByID(testMinerID).Name)
// Output:created new miner named: TestMiner
}
// BenchmarkClient_MinerByID benchmarks the method MinerByID()
func BenchmarkClient_MinerByID(b *testing.B) {
client, _ := NewClient(nil, nil, testAPIType, nil, nil)
_ = client.AddMiner(Miner{Name: testMinerName, MinerID: testMinerID}, []API{API{URL: testMinerURL, Type: testAPIType}})
for i := 0; i < b.N; i++ {
_ = client.MinerByID(testMinerID)
}
}
// TestClient_MinerUpdateToken tests the method MinerUpdateToken()
func TestClient_MinerUpdateToken(t *testing.T) {
t.Parallel()
t.Run("update valid miner", func(t *testing.T) {
client := newTestClient(&mockHTTPDefaultClient{})
// Add a valid miner
err := client.AddMiner(Miner{
Name: testMinerName,
MinerID: testMinerID,
}, []API{{URL: testMinerURL, Type: testAPIType, Token: testMinerToken}})
assert.NoError(t, err)
// Update a valid miner token
client.MinerUpdateToken(testMinerName, "99999", testAPIType)
// Get valid miner
miner := client.MinerByID(testMinerID)
api, err := client.MinerAPIByMinerID(testMinerID, testAPIType)
assert.NoError(t, err)
assert.NotNil(t, miner)
assert.Equal(t, "99999", api.Token)
})
t.Run("update unknown miner", func(t *testing.T) {
client := newTestClient(&mockHTTPDefaultClient{})
// Add a valid miner
err := client.AddMiner(Miner{
Name: testMinerName,
MinerID: testMinerID,
}, []API{API{URL: testMinerURL, Type: testAPIType, Token: testMinerToken}})
assert.NoError(t, err)
// Update a invalid miner token
client.MinerUpdateToken("Unknown", "99999", testAPIType)
})
}
// ExampleClient_MinerUpdateToken example using MinerUpdateToken()
func ExampleClient_MinerUpdateToken() {
client, err := NewClient(nil, nil, testAPIType, nil, nil)
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
// Update existing miner token
client.MinerUpdateToken(MinerTaal, "9999", testAPIType)
taal := client.MinerByName(MinerTaal)
api, err := client.MinerAPIByMinerID(taal.MinerID, client.APIType())
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
// Get miner by id
fmt.Printf("miner token found: %s", api.Token)
// Output:miner token found: 9999
}
// BenchmarkClient_MinerUpdateToken benchmarks the method MinerUpdateToken()
func BenchmarkClient_MinerUpdateToken(b *testing.B) {
client, _ := NewClient(nil, nil, testAPIType, nil, nil)
for i := 0; i < b.N; i++ {
_ = client.MinerByName(MinerTaal)
}
}
// TestClient_RemoveMiner will remove a miner by name or ID
func TestClient_RemoveMiner(t *testing.T) {
t.Parallel()
t.Run("remove a valid miner", func(t *testing.T) {
client := newTestClient(&mockHTTPDefaultClient{})
// Remove miner
removed := client.RemoveMiner(client.MinerByName(MinerTaal))
assert.Equal(t, true, removed)
// Try to get the miner
miner := client.MinerByName(MinerTaal)
assert.Nil(t, miner)
})
t.Run("remove an invalid miner", func(t *testing.T) {
client := newTestClient(&mockHTTPDefaultClient{})
// Unknown miner
dummyMiner := &Miner{
MinerID: "dummy",
Name: "dummy",
}
// Remove miner
removed := client.RemoveMiner(dummyMiner)
assert.Equal(t, false, removed)
})
t.Run("remove a nil miner", func(t *testing.T) {
client := newTestClient(&mockHTTPDefaultClient{})
// Remove miner
assert.Panics(t, func() {
removed := client.RemoveMiner(nil)
assert.Equal(t, false, removed)
})
})
}
// ExampleClient_MinerUpdateToken example using RemoveMiner()
func ExampleClient_RemoveMiner() {
client, err := NewClient(nil, nil, testAPIType, nil, nil)
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
// Update existing miner token
client.RemoveMiner(client.MinerByName(MinerTaal))
// Show response
fmt.Printf("total miners: %d", len(client.Miners()))
// Output:total miners: 1
}
// BenchmarkClient_RemoveMiner benchmarks the method RemoveMiner()
func BenchmarkClient_RemoveMiner(b *testing.B) {
client, _ := NewClient(nil, nil, testAPIType, nil, nil)
miner := client.MinerByName(MinerTaal)
for i := 0; i < b.N; i++ {
_ = client.RemoveMiner(miner)
}
}
// TestDefaultMiners will test the method DefaultMiners()
func TestDefaultMiners(t *testing.T) {
t.Run("default json", func(t *testing.T) {
miners, err := DefaultMiners()
require.NoError(t, err)
require.NotNil(t, miners)
// assert.Equal(t, MinerMempool, miners[1].Name)
//assert.Equal(t, MinerMatterpool, miners[1].Name)
assert.Equal(t, 2, len(miners))
assert.Equal(t, MinerTaal, miners[0].Name)
assert.Equal(t, MinerGorillaPool, miners[1].Name)
})
}
// ExampleDefaultMiners example using DefaultMiners()
func ExampleDefaultMiners() {
miners, _ := DefaultMiners()
fmt.Printf("total miners: %d", len(miners))
// Output:total miners: 2
}
// BenchmarkDefaultMiners benchmarks the method DefaultMiners()
func BenchmarkDefaultMiners(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = DefaultMiners()
}
}