-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathconn_test.go
785 lines (673 loc) · 20.9 KB
/
conn_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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
package beanstalk
import (
"bytes"
"context"
"errors"
"net"
"net/textproto"
"sync"
"testing"
"time"
)
// Line describes a read line from the client.
type Line struct {
lineno int
line string
}
// At validates if the specified string is present at a specific line number.
func (line Line) At(lineno int, s string) bool {
return lineno == line.lineno && s == line.line
}
// Server implements a test beanstalk server.
type Server struct {
listener net.Listener
mu sync.RWMutex
lineno int
handler func(line Line) string
closeC chan struct{}
closeOnce sync.Once
}
// NewServer returns a new Server.
func NewServer() *Server {
listener, err := net.Listen("tcp", "localhost:0")
if err != nil {
panic("Unable to set up listening socket for text beanstalk server: " + err.Error())
}
server := &Server{listener: listener, closeC: make(chan struct{})}
go server.accept()
return server
}
// Close the server socket.
func (server *Server) Close() {
server.closeOnce.Do(func() {
close(server.closeC)
_ = server.listener.Close()
})
}
// accept incoming connections.
func (server *Server) accept() {
defer server.listener.Close()
for {
conn, err := server.listener.Accept()
if err != nil {
return
}
server.handleConn(textproto.NewConn(conn))
}
}
// handleConn handles an existing client connection.
func (server *Server) handleConn(conn *textproto.Conn) {
defer conn.Close()
lineC := make(chan string)
// Read incoming lines and send them to lineC.
go func() {
for {
line, err := conn.ReadLine()
if err != nil {
close(lineC)
return
}
select {
case lineC <- line:
case <-server.closeC:
return
}
}
}()
// Process incoming lines and send them to the configured handler.
for {
var line string
var closed bool
select {
case line, closed = <-lineC:
if !closed {
return
}
case <-server.closeC:
return
}
// Execute this in an inline function so that the lock/unlock mechanism
// is handled elegantly.
func() {
server.mu.RLock()
defer server.mu.RUnlock()
server.lineno++
if server.handler != nil {
if resp := server.handler(Line{server.lineno, line}); resp != "" {
_ = conn.PrintfLine(resp)
}
}
}()
}
}
// HandleFunc registers the handler function that should be called for every
// line that this server receives from the client.
func (server *Server) HandleFunc(handler func(line Line) string) {
server.mu.Lock()
defer server.mu.Unlock()
server.lineno = 0
server.handler = handler
}
// Socket returns the host:port combo that this server is listening on.
func (server *Server) Socket() string {
return server.listener.Addr().String()
}
func TestConn(t *testing.T) {
server := NewServer()
defer server.Close()
var conn *Conn
ctx := context.Background()
// Dial the beanstalk server and set up a client connection.
t.Run("Dial", func(t *testing.T) {
var err error
conn, err = Dial(server.Socket(), Config{})
if err != nil {
t.Fatalf("Unable to dial to beanstalk server: %s", err)
}
})
defer conn.Close()
// bury a job.
t.Run("bury", func(t *testing.T) {
server.HandleFunc(func(line Line) string {
switch {
case line.At(1, "bury 1 10"):
return "BURIED"
default:
t.Fatalf("Unexpected client request at line %d: %s", line.lineno, line.line)
}
return ""
})
if err := conn.Bury(ctx, &Job{ID: 1}, 10); err != nil {
t.Fatalf("Error burying job: %s", err)
}
// NotFound tests what happens when the NOT_FOUND error is returned.
t.Run("NotFound", func(t *testing.T) {
server.HandleFunc(func(line Line) string {
switch {
case line.At(1, "bury 2 11"):
return "NOT_FOUND"
default:
t.Fatalf("Unexpected client request at line %d: %s", line.lineno, line.line)
}
return ""
})
err := conn.Bury(ctx, &Job{ID: 2}, 11)
switch {
case errors.Is(err, ErrNotFound):
case err != nil:
t.Fatalf("Error burying job: %s", err)
}
})
})
// delete a job.
t.Run("Delete", func(t *testing.T) {
server.HandleFunc(func(line Line) string {
switch {
case line.At(1, "delete 3"):
return "DELETED"
default:
t.Fatalf("Unexpected client request at line %d: %s", line.lineno, line.line)
}
return ""
})
if err := conn.Delete(ctx, 3); err != nil {
t.Fatalf("Error deleting job: %s", err)
}
// NotFound tests what happens when the NOT_FOUND error is returned.
t.Run("NotFound", func(t *testing.T) {
server.HandleFunc(func(line Line) string {
switch {
case line.At(1, "delete 4"):
return "NOT_FOUND"
default:
t.Fatalf("Unexpected client request at line %d: %s", line.lineno, line.line)
}
return ""
})
err := conn.Delete(ctx, 4)
switch {
case errors.Is(err, ErrNotFound):
case err != nil:
t.Fatalf("Error deleting job: %s", err)
}
})
})
// Ignore watching a tube.
t.Run("Ignore", func(t *testing.T) {
server.HandleFunc(func(line Line) string {
if line.At(1, "ignore foo") {
return "WATCHING 1"
}
t.Fatalf("Unexpected client request: %s", line.line)
return ""
})
if err := conn.Ignore(ctx, "foo"); err != nil {
t.Fatalf("Error ignoring tube: %s", err)
}
// NotIgnored test what happens if the ignore command fails because it tried
// to ignore the only tube this connection was watching.
t.Run("NotIgnored", func(t *testing.T) {
server.HandleFunc(func(line Line) string {
if line.At(1, "ignore bar") {
return "NOT_IGNORED"
}
t.Fatalf("Unexpected client request: %s", line.line)
return ""
})
err := conn.Ignore(ctx, "bar")
switch {
case errors.Is(err, ErrNotIgnored):
case err != nil:
t.Fatalf("Error ignoring tube: %s", err)
}
})
})
// Put a new message into a tube.
t.Run("Put", func(t *testing.T) {
server.HandleFunc(func(line Line) string {
switch {
case line.At(1, "use foobar"):
return "USING foobar"
case line.At(2, "put 1024 10 60 11"):
case line.At(3, "Hello World"):
return "INSERTED 5"
default:
t.Fatalf("Unexpected client request at line %d: %s", line.lineno, line.line)
}
return ""
})
id, err := conn.Put(ctx, "foobar", []byte("Hello World"), PutParams{Priority: 1024, Delay: 10 * time.Second, TTR: 60 * time.Second})
switch {
case err != nil:
t.Fatalf("Error inserting a new job: %s", err)
case id != 5:
t.Fatalf("Expected job ID 5, but got %d", id)
}
// OnSameTube tests if the command order makes sense if another message is
// put into the same tube.
t.Run("OnSameTube", func(t *testing.T) {
server.HandleFunc(func(line Line) string {
switch {
case line.At(1, "put 1024 10 60 11"):
case line.At(2, "Hello World"):
return "INSERTED 6"
default:
t.Fatalf("Unexpected client request at line %d: %s", line.lineno, line.line)
}
return ""
})
id, err := conn.Put(ctx, "foobar", []byte("Hello World"), PutParams{Priority: 1024, Delay: 10 * time.Second, TTR: 60 * time.Second})
switch {
case err != nil:
t.Fatalf("Error inserting a new job: %s", err)
case id != 6:
t.Fatalf("Expected job ID 6, but got %d", id)
}
})
// OnDifferentTube tests if the command order makes sense if a message is
// put into a different tube than the previous message.
t.Run("OnDifferentTube", func(t *testing.T) {
server.HandleFunc(func(line Line) string {
switch {
case line.At(1, "use zoink"):
return "USING zoink"
case line.At(2, "put 512 15 30 10"):
case line.At(3, "Hello Narf"):
return "INSERTED 7"
default:
t.Fatalf("Unexpected client request at line %d: %s", line.lineno, line.line)
}
return ""
})
id, err := conn.Put(ctx, "zoink", []byte("Hello Narf"), PutParams{Priority: 512, Delay: 15 * time.Second, TTR: 30 * time.Second})
switch {
case err != nil:
t.Fatalf("Error inserting a new job: %s", err)
case id != 7:
t.Fatalf("Expected job ID 7, but got %d", id)
}
})
})
// release tests the release method, responsible for releasing jobs back.
t.Run("release", func(t *testing.T) {
server.HandleFunc(func(line Line) string {
switch {
case line.At(1, "release 8 12 20"):
return "RELEASED"
default:
t.Fatalf("Unexpected client request at line %d: %s", line.lineno, line.line)
}
return ""
})
err := conn.Release(ctx, &Job{ID: 8}, 12, 20*time.Second)
if err != nil {
t.Fatalf("Error releasing job: %s", err)
}
// Buried tests what happens when the BURIED error is returned.
t.Run("Buried", func(t *testing.T) {
server.HandleFunc(func(line Line) string {
switch {
case line.At(1, "release 9 13 21"):
return "BURIED"
default:
t.Fatalf("Unexpected client request at line %d: %s", line.lineno, line.line)
}
return ""
})
err = conn.Release(ctx, &Job{ID: 9}, 13, 21*time.Second)
switch {
case errors.Is(err, ErrBuried):
case err != nil:
t.Fatalf("Expected the ErrBuried error, but got %s", err)
}
})
// NotFound tests what happens when the NOT_FOUND error is returned.
t.Run("NotFound", func(t *testing.T) {
server.HandleFunc(func(line Line) string {
switch {
case line.At(1, "release 10 14 22"):
return "NOT_FOUND"
default:
t.Fatalf("Unexpected client request at line %d: %s", line.lineno, line.line)
}
return ""
})
err = conn.Release(ctx, &Job{ID: 10}, 14, 22*time.Second)
switch {
case errors.Is(err, ErrNotFound):
case err != nil:
t.Fatalf("Expected the ErrNotFound error, but got %s", err)
}
})
})
// ReserveWithTimeout tests the ReserveWithTimeout method.
t.Run("ReserveWithTimeout", func(t *testing.T) {
server.HandleFunc(func(line Line) string {
switch {
case line.At(1, "reserve-with-timeout 1"):
return "RESERVED 12 11\r\nHello World"
case line.At(2, "stats-job 12"):
return "OK 166\r\n---\r\nid: 12\r\ntube: default\r\nstate: reserved\r\npri: 512\r\nage: 23\r\ndelay: 15\r\nttr: 30\r\ntime-left: 25\r\nfile: 6\r\nreserves: 1\r\ntimeouts: 4\r\nreleases: 5\r\nburies: 2\r\nkicks: 7"
default:
t.Fatalf("Unexpected client request at line %d: %s", line.lineno, line.line)
}
return ""
})
job, err := conn.ReserveWithTimeout(ctx, 1*time.Second)
switch {
case err != nil:
t.Fatalf("Error reserving a job: %s", err)
case job == nil:
t.Fatal("Expected job, but got nothing")
// Validate the basic attributes.
case job.ID != 12:
t.Fatalf("Expected job ID 12, but got %d", job.ID)
case !bytes.Equal(job.Body, []byte(`Hello World`)):
t.Fatalf("Expected job body to be \"Hello World\", but got %q", string(job.Body))
case job.ReservedAt.IsZero():
t.Fatal("Expected job ReservedAt to be set, but it was not")
// Validate the Stats.
case job.Stats.Tube != "default":
t.Fatalf("Expected job tube default, but got %s", job.Stats.Tube)
case job.Stats.State != "reserved":
t.Fatalf("Expected job state reserved, but got %s", job.Stats.State)
case job.Stats.Age != 23*time.Second:
t.Fatalf("Expected job age to be 23s, but got %s", job.Stats.Age)
case job.Stats.TimeLeft != 25*time.Second:
t.Fatalf("Expected job time left to be 25s, but got %s", job.Stats.TimeLeft)
case job.Stats.File != 6:
t.Fatalf("Expected job binfile number to be 6, but got %d", job.Stats.File)
case job.Stats.Reserves != 1:
t.Fatalf("Expected job reserved to be 1, but got %d", job.Stats.Reserves)
case job.Stats.Timeouts != 4:
t.Fatalf("Expected job timeouts to be 4, but got %d", job.Stats.Timeouts)
case job.Stats.Releases != 5:
t.Fatalf("Expected job release to be 5, but got %d", job.Stats.Releases)
case job.Stats.Buries != 2:
t.Fatalf("Expected job buries to be 2, but got %d", job.Stats.Buries)
case job.Stats.Kicks != 7:
t.Fatalf("Expected job kicks to be 7, but got %d", job.Stats.Kicks)
// Validate the PutParams.
case job.Stats.PutParams.Priority != 512:
t.Fatalf("Expected job priority to be 512, but got %d", job.Stats.PutParams.Priority)
case job.Stats.PutParams.Delay != 15*time.Second:
t.Fatalf("Expected job TTR to be 15s, but got %s", job.Stats.PutParams.Delay)
case job.Stats.PutParams.TTR != 30*time.Second:
t.Fatalf("Expected job TTR to be 30s, but got %s", job.Stats.PutParams.TTR)
}
// WithTimeout tests if a TIMED_OUT response is properly handled.
t.Run("WithTimeout", func(t *testing.T) {
server.HandleFunc(func(line Line) string {
switch {
case line.At(1, "reserve-with-timeout 2"):
return "TIMED_OUT"
default:
t.Fatalf("Unexpected client request at line %d: %s", line.lineno, line.line)
}
return ""
})
job, err := conn.ReserveWithTimeout(ctx, 2*time.Second)
switch {
case err != nil:
t.Fatalf("Error reserving a job: %s", err)
case job != nil:
t.Fatalf("Expected job to be nil, but got %#v", job)
}
})
// WithDeadline tests if a DEADLINE_SOON response is properly handled.
t.Run("WithDeadline", func(t *testing.T) {
server.HandleFunc(func(line Line) string {
switch {
case line.At(1, "reserve-with-timeout 3"):
return "DEADLINE_SOON"
default:
t.Fatalf("Unexpected client request at line %d: %s", line.lineno, line.line)
}
return ""
})
job, err := conn.ReserveWithTimeout(ctx, 3*time.Second)
switch {
case err != nil:
t.Fatalf("Error reserving a job: %s", err)
case job != nil:
t.Fatalf("Expected job to be nil, but got %#v", job)
}
})
// WithNotFound tests if the situation where a reserved job expired before
// the stats-job command could return successfully.
t.Run("WithNotFound", func(t *testing.T) {
server.HandleFunc(func(line Line) string {
switch {
case line.At(1, "reserve-with-timeout 4"):
return "RESERVED 13 11\r\nHello World"
case line.At(2, "stats-job 13"):
return "NOT_FOUND"
default:
t.Fatalf("Unexpected client request at line %d: %s", line.lineno, line.line)
}
return ""
})
job, err := conn.ReserveWithTimeout(ctx, 4*time.Second)
switch {
case err != nil:
t.Fatalf("Error reserving a job: %s", err)
case job != nil:
t.Fatalf("Expected job to be nil, but got %#v", job)
}
})
})
// touch an existing job.
t.Run("touch", func(t *testing.T) {
server.HandleFunc(func(line Line) string {
switch {
case line.At(1, "touch 13"):
return "TOUCHED"
default:
t.Fatalf("Unexpected client request at line %d: %s", line.lineno, line.line)
}
return ""
})
job := &Job{ID: 13}
job.Stats.PutParams.TTR = 5 * time.Second
err := conn.Touch(ctx, job)
switch {
case err != nil:
t.Fatalf("Error watching a channel: %s", err)
case job.Stats.PutParams.TTR != 5*time.Second:
t.Fatalf("Expected job TTR to be 5s, but got %s", job.Stats.PutParams.TTR)
case job.Stats.TimeLeft != 4*time.Second:
t.Fatalf("Expected job time left to be 4s, but got %s", job.Stats.TimeLeft)
case job.ReservedAt.IsZero():
t.Fatal("Expected job ReservedAt to be set, but it was not")
}
t.Run("NotFound", func(t *testing.T) {
server.HandleFunc(func(line Line) string {
switch {
case line.At(1, "touch 14"):
return "NOT_FOUND"
default:
t.Fatalf("Unexpected client request at line %d: %s", line.lineno, line.line)
}
return ""
})
err = conn.Touch(ctx, &Job{ID: 14})
switch {
case errors.Is(err, ErrNotFound):
case err != nil:
t.Fatalf("Expected the ErrNotFound error, but got %s", err)
}
})
})
// Watch a new tube.
t.Run("Watch", func(t *testing.T) {
server.HandleFunc(func(line Line) string {
switch {
case line.At(1, "watch events"):
return "WATCHING 2"
default:
t.Fatalf("Unexpected client request at line %d: %s", line.lineno, line.line)
}
return ""
})
if err := conn.Watch(ctx, "events"); err != nil {
t.Fatalf("Error watching a channel: %s", err)
}
// ErrTubeTooLong tests if a client-side error is returned if the tube name
// is too long.
t.Run("ErrTubeTooLong", func(t *testing.T) {
err := conn.Watch(ctx, string(make([]byte, 201)))
switch {
case errors.Is(err, ErrTubeTooLong):
case err != nil:
t.Fatalf("Expected the ErrTubeTooLong error, but got %s", err)
}
})
})
t.Run("Kick", func(t *testing.T) {
server.HandleFunc(func(line Line) string {
switch {
case line.At(1, "use default"):
return "USING default"
case line.At(2, "kick 10"):
return "KICKED 10"
default:
t.Fatalf("Unexpected client request at line %d: %s", line.lineno, line.line)
}
return ""
})
count, err := conn.Kick(ctx, "default", 10)
if err != nil {
t.Fatalf("Error kicking jobs: %s", err)
}
if count != 10 {
t.Fatalf("Unexpected number of kicked jobs, expected %d, actual %d", 10, count)
}
t.Run("NoBuriedJobs", func(t *testing.T) {
server.HandleFunc(func(line Line) string {
switch {
case line.At(1, "kick 10"):
return "KICKED 0"
default:
t.Fatalf("Unexpected client request at line %d: %s", line.lineno, line.line)
}
return ""
})
count, err := conn.Kick(ctx, "default", 10)
switch {
case err != nil:
t.Fatalf("Error kicking job: %s", err)
case count != 0:
t.Fatalf("Unexpected number of kicked jobs, expected %d, actual %d", 0, count)
}
})
})
t.Run("kick", func(t *testing.T) {
server.HandleFunc(func(line Line) string {
switch {
case line.At(1, "kick-job 1"):
return "KICKED"
default:
t.Fatalf("Unexpected client request at line %d: %s", line.lineno, line.line)
}
return ""
})
job := Job{ID: 1}
job.Stats.Tube = "default"
if err := conn.KickJob(ctx, &job); err != nil {
t.Fatalf("Error kicking job: %s", err)
}
// NotFound tests what happens when the NOT_FOUND error is returned.
t.Run("JobNotFound", func(t *testing.T) {
server.HandleFunc(func(line Line) string {
switch {
case line.At(1, "kick-job 1"):
return "NOT_FOUND"
default:
t.Fatalf("Unexpected client request at line %d: %s", line.lineno, line.line)
}
return ""
})
job := Job{ID: 1}
job.Stats.Tube = "default"
err := conn.KickJob(ctx, &job)
switch {
case errors.Is(err, ErrNotFound):
case err != nil:
t.Fatalf("Error kicking job: %s", err)
}
})
})
t.Run("PeekDelayed", func(t *testing.T) {
server.HandleFunc(func(line Line) string {
switch {
case line.At(1, "use events"):
return "USING events"
case line.At(2, "peek-delayed"):
return "FOUND 1 11\nHello world"
case line.At(3, "stats-job 1"):
return "OK 153\n---\nid: 1\ntube: default\nstate: delayed\npri: 1024\nage: 39\ndelay: 120\nttr: 60\ntime-left: 80\nfile: 6\nreserves: 1\ntimeouts: 4\nreleases: 5\nburies: 2\nkicks: 7\n"
default:
t.Fatalf("Unexpected client request at line %d: %s", line.lineno, line.line)
}
return ""
})
job, err := conn.PeekDelayed(ctx, "events")
switch {
case err != nil:
t.Fatalf("Error picking delayed job: %s", err)
case job == nil:
t.Fatal("Expected job, but got nothing")
// Validate the basic attributes.
case job.ID != 1:
t.Fatalf("Unexpected job ID. Expected: 1, actual: %d", job.ID)
case !bytes.Equal(job.Body, []byte(`Hello world`)):
t.Fatalf("Expected job body to be \"Hello world\", but got %q", string(job.Body))
// Validate the Stats.
case job.Stats.Tube != "default":
t.Fatalf("Expected job tube default, but got %s", job.Stats.Tube)
case job.Stats.State != "delayed":
t.Fatalf("Expected job state delayed, but got %s", job.Stats.State)
case job.Stats.Age != 39*time.Second:
t.Fatalf("Expected job age to be 39s, but got %s", job.Stats.Age)
case job.Stats.TimeLeft != 80*time.Second:
t.Fatalf("Expected job time left to be 80s, but got %s", job.Stats.TimeLeft)
case job.Stats.File != 6:
t.Fatalf("Expected job binfile number to be 6, but got %d", job.Stats.File)
case job.Stats.Reserves != 1:
t.Fatalf("Expected job reserved to be 1, but got %d", job.Stats.Reserves)
case job.Stats.Timeouts != 4:
t.Fatalf("Expected job timeouts to be 4, but got %d", job.Stats.Timeouts)
case job.Stats.Releases != 5:
t.Fatalf("Expected job release to be 5, but got %d", job.Stats.Releases)
case job.Stats.Buries != 2:
t.Fatalf("Expected job buries to be 2, but got %d", job.Stats.Buries)
case job.Stats.Kicks != 7:
t.Fatalf("Expected job kicks to be 7, but got %d", job.Stats.Kicks)
// Validate the PutParams.
case job.Stats.PutParams.Priority != 1024:
t.Fatalf("Expected job priority to be 1024, but got %d", job.Stats.PutParams.Priority)
case job.Stats.PutParams.Delay != 120*time.Second:
t.Fatalf("Expected job TTR to be 120s, but got %s", job.Stats.PutParams.Delay)
case job.Stats.PutParams.TTR != 60*time.Second:
t.Fatalf("Expected job TTR to be 60s, but got %s", job.Stats.PutParams.TTR)
}
t.Run("JobNotFound", func(t *testing.T) {
server.HandleFunc(func(line Line) string {
switch {
case line.At(1, "peek-delayed"):
return "NOT_FOUND"
default:
t.Fatalf("Unexpected client request at line %d: %s", line.lineno, line.line)
}
return ""
})
job, err := conn.PeekDelayed(ctx, "events")
switch {
case err != nil:
t.Fatalf("Error picking delayed job: %s", err)
case job != nil:
t.Fatalf("Expected job not found, but got something: %+v", job)
}
})
})
}