-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
client_test.go
832 lines (661 loc) · 19.5 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
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
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
package gue
import (
"context"
"errors"
"fmt"
"os"
"sync"
"testing"
"time"
"github.com/oklog/ulid/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vgarvardt/gue/v5/adapter"
adapterTesting "github.com/vgarvardt/gue/v5/adapter/testing"
)
func TestLockJob(t *testing.T) {
for name, openFunc := range adapterTesting.AllAdaptersOpenTestPool {
t.Run(name, func(t *testing.T) {
testLockJob(t, openFunc(t))
})
}
}
func testLockJob(t *testing.T, connPool adapter.ConnPool) {
ctx := context.Background()
c, err := NewClient(connPool)
require.NoError(t, err)
newJob := &Job{
Type: "MyJob",
Args: []byte(`{invalid]json>`),
}
err = c.Enqueue(ctx, newJob)
require.NoError(t, err)
require.NotEmpty(t, newJob.ID)
j, err := c.LockJob(ctx, "")
require.NoError(t, err)
require.NotNil(t, j)
require.NotNil(t, j.tx)
t.Cleanup(func() {
err := j.Done(ctx)
assert.NoError(t, err)
})
// check values of returned Job
assert.Equal(t, newJob.ID.String(), j.ID.String())
assert.Equal(t, defaultQueueName, j.Queue)
assert.Equal(t, JobPriorityDefault, j.Priority)
assert.False(t, j.RunAt.IsZero())
assert.Equal(t, newJob.Type, j.Type)
assert.Equal(t, []byte(`{invalid]json>`), j.Args)
assert.Equal(t, int32(0), j.ErrorCount)
assert.False(t, j.LastError.Valid)
}
func TestLockJobAlreadyLocked(t *testing.T) {
for name, openFunc := range adapterTesting.AllAdaptersOpenTestPool {
t.Run(name, func(t *testing.T) {
testLockJobAlreadyLocked(t, openFunc(t))
})
}
}
func testLockJobAlreadyLocked(t *testing.T, connPool adapter.ConnPool) {
ctx := context.Background()
c, err := NewClient(connPool)
require.NoError(t, err)
err = c.Enqueue(ctx, &Job{Type: "MyJob"})
require.NoError(t, err)
j, err := c.LockJob(ctx, "")
require.NoError(t, err)
require.NotNil(t, j)
t.Cleanup(func() {
err := j.Done(ctx)
assert.NoError(t, err)
})
j2, err := c.LockJob(ctx, "")
require.NoError(t, err)
require.Nil(t, j2)
}
func TestLockJobNoJob(t *testing.T) {
for name, openFunc := range adapterTesting.AllAdaptersOpenTestPool {
t.Run(name, func(t *testing.T) {
testLockJobNoJob(t, openFunc(t))
})
}
}
func testLockJobNoJob(t *testing.T, connPool adapter.ConnPool) {
ctx := context.Background()
c, err := NewClient(connPool)
require.NoError(t, err)
j, err := c.LockJob(ctx, "")
require.NoError(t, err)
require.Nil(t, j)
}
func TestLockJobCustomQueue(t *testing.T) {
for name, openFunc := range adapterTesting.AllAdaptersOpenTestPool {
t.Run(name, func(t *testing.T) {
testLockJobCustomQueue(t, openFunc(t))
})
}
}
func testLockJobCustomQueue(t *testing.T, connPool adapter.ConnPool) {
ctx := context.Background()
c, err := NewClient(connPool)
require.NoError(t, err)
err = c.Enqueue(ctx, &Job{Type: "MyJob", Queue: "extra_priority"})
require.NoError(t, err)
j, err := c.LockJob(ctx, "")
require.NoError(t, err)
require.Nil(t, j)
j, err = c.LockJob(ctx, "extra_priority")
require.NoError(t, err)
require.NotNil(t, j)
t.Cleanup(func() {
err := j.Done(ctx)
assert.NoError(t, err)
})
err = j.Delete(ctx)
require.NoError(t, err)
}
func TestLockJobByID(t *testing.T) {
for name, openFunc := range adapterTesting.AllAdaptersOpenTestPool {
t.Run(name, func(t *testing.T) {
testLockJobByID(t, openFunc(t))
})
}
}
func testLockJobByID(t *testing.T, connPool adapter.ConnPool) {
ctx := context.Background()
c, err := NewClient(connPool)
require.NoError(t, err)
newJob := &Job{
Type: "MyJob",
}
err = c.Enqueue(ctx, newJob)
require.NoError(t, err)
require.NotEmpty(t, newJob.ID)
j, err := c.LockJobByID(ctx, newJob.ID)
require.NoError(t, err)
require.NotNil(t, j.tx)
t.Cleanup(func() {
err := j.Done(ctx)
assert.NoError(t, err)
})
// check values of returned Job
assert.Equal(t, newJob.ID.String(), j.ID.String())
assert.Equal(t, defaultQueueName, j.Queue)
assert.Equal(t, JobPriorityDefault, j.Priority)
assert.False(t, j.RunAt.IsZero())
assert.Equal(t, newJob.Type, j.Type)
assert.Equal(t, []byte(``), j.Args)
assert.Equal(t, int32(0), j.ErrorCount)
assert.False(t, j.LastError.Valid)
}
func TestLockJobByIDAlreadyLocked(t *testing.T) {
for name, openFunc := range adapterTesting.AllAdaptersOpenTestPool {
t.Run(name, func(t *testing.T) {
testLockJobByIDAlreadyLocked(t, openFunc(t))
})
}
}
func testLockJobByIDAlreadyLocked(t *testing.T, connPool adapter.ConnPool) {
ctx := context.Background()
c, err := NewClient(connPool)
require.NoError(t, err)
newJob := &Job{
Type: "MyJob",
}
err = c.Enqueue(ctx, newJob)
require.NoError(t, err)
j, err := c.LockJobByID(ctx, newJob.ID)
require.NoError(t, err)
require.NotNil(t, j)
t.Cleanup(func() {
err := j.Done(ctx)
assert.NoError(t, err)
})
j2, err := c.LockJobByID(ctx, newJob.ID)
require.Error(t, err)
require.Nil(t, j2)
}
func TestLockJobByIDNoJob(t *testing.T) {
for name, openFunc := range adapterTesting.AllAdaptersOpenTestPool {
t.Run(name, func(t *testing.T) {
testLockJobByIDNoJob(t, openFunc(t))
})
}
}
func testLockJobByIDNoJob(t *testing.T, connPool adapter.ConnPool) {
ctx := context.Background()
c, err := NewClient(connPool)
require.NoError(t, err)
j, err := c.LockJobByID(ctx, ulid.Make())
require.Error(t, err)
require.Nil(t, j)
}
func TestLockNextScheduledJob(t *testing.T) {
for name, openFunc := range adapterTesting.AllAdaptersOpenTestPool {
t.Run(name, func(t *testing.T) {
testLockNextScheduledJob(t, openFunc(t))
})
}
}
func testLockNextScheduledJob(t *testing.T, connPool adapter.ConnPool) {
ctx := context.Background()
c, err := NewClient(connPool)
require.NoError(t, err)
newJob := &Job{
Type: "MyJob",
RunAt: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
}
err = c.Enqueue(ctx, newJob)
require.NoError(t, err)
require.NotEmpty(t, newJob.ID)
j, err := c.LockNextScheduledJob(ctx, "")
require.NoError(t, err)
require.NotNil(t, j.tx)
t.Cleanup(func() {
err := j.Done(ctx)
assert.NoError(t, err)
})
// check values of returned Job
assert.Equal(t, newJob.ID.String(), j.ID.String())
assert.Equal(t, defaultQueueName, j.Queue)
assert.Equal(t, JobPriorityDefault, j.Priority)
assert.False(t, j.RunAt.IsZero())
assert.Equal(t, newJob.Type, j.Type)
assert.Equal(t, []byte(``), j.Args)
assert.Equal(t, int32(0), j.ErrorCount)
assert.False(t, j.LastError.Valid)
}
func TestLockNextScheduledJobAlreadyLocked(t *testing.T) {
for name, openFunc := range adapterTesting.AllAdaptersOpenTestPool {
t.Run(name, func(t *testing.T) {
testLockNextScheduledJobAlreadyLocked(t, openFunc(t))
})
}
}
func testLockNextScheduledJobAlreadyLocked(t *testing.T, connPool adapter.ConnPool) {
ctx := context.Background()
c, err := NewClient(connPool)
require.NoError(t, err)
err = c.Enqueue(ctx, &Job{Type: "MyJob"})
require.NoError(t, err)
j, err := c.LockNextScheduledJob(ctx, "")
require.NoError(t, err)
require.NotNil(t, j)
t.Cleanup(func() {
err := j.Done(ctx)
assert.NoError(t, err)
})
j2, err := c.LockNextScheduledJob(ctx, "")
require.NoError(t, err)
require.Nil(t, j2)
}
func TestLockNextScheduledJobNoJob(t *testing.T) {
for name, openFunc := range adapterTesting.AllAdaptersOpenTestPool {
t.Run(name, func(t *testing.T) {
testLockNextScheduledJobNoJob(t, openFunc(t))
})
}
}
func testLockNextScheduledJobNoJob(t *testing.T, connPool adapter.ConnPool) {
ctx := context.Background()
c, err := NewClient(connPool)
require.NoError(t, err)
j, err := c.LockNextScheduledJob(ctx, "")
require.NoError(t, err)
require.Nil(t, j)
}
func TestJobTx(t *testing.T) {
for name, openFunc := range adapterTesting.AllAdaptersOpenTestPool {
t.Run(name, func(t *testing.T) {
testJobTx(t, openFunc(t))
})
}
}
func testJobTx(t *testing.T, connPool adapter.ConnPool) {
ctx := context.Background()
c, err := NewClient(connPool)
require.NoError(t, err)
err = c.Enqueue(ctx, &Job{Type: "MyJob"})
require.NoError(t, err)
j, err := c.LockJob(ctx, "")
require.NoError(t, err)
require.NotNil(t, j)
t.Cleanup(func() {
err := j.Done(ctx)
assert.NoError(t, err)
})
assert.Equal(t, j.tx, j.Tx())
}
func TestJobConnRace(t *testing.T) {
for name, openFunc := range adapterTesting.AllAdaptersOpenTestPool {
t.Run(name, func(t *testing.T) {
testJobConnRace(t, openFunc(t))
})
}
}
func testJobConnRace(t *testing.T, connPool adapter.ConnPool) {
ctx := context.Background()
c, err := NewClient(connPool)
require.NoError(t, err)
err = c.Enqueue(ctx, &Job{Type: "MyJob"})
require.NoError(t, err)
j, err := c.LockJob(ctx, "")
require.NoError(t, err)
require.NotNil(t, j)
t.Cleanup(func() {
err := j.Done(ctx)
assert.NoError(t, err)
})
var wg sync.WaitGroup
wg.Add(2)
// call Tx and Done in different goroutines to make sure they are safe from races
go func() {
_ = j.Tx()
wg.Done()
}()
go func() {
err := j.Done(ctx)
assert.NoError(t, err)
wg.Done()
}()
wg.Wait()
}
func TestJobDelete(t *testing.T) {
for name, openFunc := range adapterTesting.AllAdaptersOpenTestPool {
t.Run(name, func(t *testing.T) {
testJobDelete(t, openFunc(t))
})
}
}
func testJobDelete(t *testing.T, connPool adapter.ConnPool) {
ctx := context.Background()
c, err := NewClient(connPool)
require.NoError(t, err)
job := Job{Type: "MyJob"}
err = c.Enqueue(ctx, &job)
require.NoError(t, err)
j, err := c.LockJob(ctx, "")
require.NoError(t, err)
require.NotNil(t, j)
err = j.Delete(ctx)
require.NoError(t, err)
err = j.Done(ctx)
require.NoError(t, err)
// make sure job was deleted
jj, err := c.LockJobByID(ctx, job.ID)
require.Error(t, err)
assert.True(t, errors.Is(err, adapter.ErrNoRows))
assert.Nil(t, jj)
}
func TestJobDone(t *testing.T) {
for name, openFunc := range adapterTesting.AllAdaptersOpenTestPool {
t.Run(name, func(t *testing.T) {
testJobDone(t, openFunc(t))
})
}
}
func testJobDone(t *testing.T, connPool adapter.ConnPool) {
ctx := context.Background()
c, err := NewClient(connPool)
require.NoError(t, err)
err = c.Enqueue(ctx, &Job{Type: "MyJob"})
require.NoError(t, err)
j, err := c.LockJob(ctx, "")
require.NoError(t, err)
require.NotNil(t, j)
err = j.Done(ctx)
require.NoError(t, err)
// make sure tx was cleared
assert.Nil(t, j.tx)
}
func TestJobDoneMultiple(t *testing.T) {
for name, openFunc := range adapterTesting.AllAdaptersOpenTestPool {
t.Run(name, func(t *testing.T) {
testJobDoneMultiple(t, openFunc(t))
})
}
}
func testJobDoneMultiple(t *testing.T, connPool adapter.ConnPool) {
ctx := context.Background()
c, err := NewClient(connPool)
require.NoError(t, err)
err = c.Enqueue(ctx, &Job{Type: "MyJob"})
require.NoError(t, err)
j, err := c.LockJob(ctx, "")
require.NoError(t, err)
require.NotNil(t, j)
err = j.Done(ctx)
require.NoError(t, err)
// try calling Done() again
err = j.Done(ctx)
require.NoError(t, err)
}
func TestJobError(t *testing.T) {
for name, openFunc := range adapterTesting.AllAdaptersOpenTestPool {
t.Run(name, func(t *testing.T) {
testJobError(t, openFunc(t))
})
}
}
func testJobError(t *testing.T, connPool adapter.ConnPool) {
ctx := context.Background()
c, err := NewClient(connPool)
require.NoError(t, err)
job := &Job{Type: "MyJob"}
err = c.Enqueue(ctx, job)
require.NoError(t, err)
j, err := c.LockJob(ctx, "")
require.NoError(t, err)
require.NotNil(t, j)
msg := "world\nended"
err = j.Error(ctx, errors.New(msg))
require.NoError(t, err)
// make sure job was not deleted
j2, err := c.LockJobByID(ctx, job.ID)
require.NoError(t, err)
require.NotNil(t, j2)
t.Cleanup(func() {
err := j2.Done(ctx)
assert.NoError(t, err)
})
assert.True(t, j2.LastError.Valid)
assert.Equal(t, msg, j2.LastError.String)
assert.Equal(t, int32(1), j2.ErrorCount)
assert.Greater(t, j2.RunAt.Unix(), job.RunAt.Unix())
}
func TestJobErrorCustomBackoff(t *testing.T) {
for name, openFunc := range adapterTesting.AllAdaptersOpenTestPool {
t.Run(name, func(t *testing.T) {
testJobErrorCustomBackoff(t, openFunc(t))
})
}
}
func testJobErrorCustomBackoff(t *testing.T, connPool adapter.ConnPool) {
ctx := context.Background()
customBackoff := func(retries int) time.Duration {
return time.Duration(retries) * time.Hour
}
c, err := NewClient(connPool, WithClientBackoff(customBackoff))
require.NoError(t, err)
job := &Job{Type: "MyJob"}
err = c.Enqueue(ctx, job)
require.NoError(t, err)
j, err := c.LockJob(ctx, "")
require.NoError(t, err)
require.NotNil(t, j)
msg := "world\nended"
err = j.Error(ctx, errors.New(msg))
require.NoError(t, err)
// make sure job was not deleted
j2, err := c.LockJobByID(ctx, job.ID)
require.NoError(t, err)
require.NotNil(t, j2)
t.Cleanup(func() {
err := j2.Done(ctx)
assert.NoError(t, err)
})
assert.True(t, j2.LastError.Valid)
assert.Equal(t, msg, j2.LastError.String)
assert.Equal(t, int32(1), j2.ErrorCount)
assert.Greater(t, j2.RunAt.Unix(), job.RunAt.Unix())
// a diff in a sec is possible when doing dates math, so allow it
assert.WithinDuration(t, job.RunAt.Add(time.Hour), j2.RunAt, time.Second)
}
func TestJobPriority(t *testing.T) {
for name, openFunc := range adapterTesting.AllAdaptersOpenTestPool {
t.Run(name, func(t *testing.T) {
testJobPriority(t, openFunc(t))
})
}
}
func testJobPriority(t *testing.T, connPool adapter.ConnPool) {
ctx := context.Background()
c, err := NewClient(connPool)
require.NoError(t, err)
// insert in the order different from expected to be locked
jobPriorityDefault := &Job{Type: "MyJob", Priority: JobPriorityDefault, Args: []byte(`default`)}
err = c.Enqueue(ctx, jobPriorityDefault)
require.NoError(t, err)
jobPriorityLowest := &Job{Type: "MyJob", Priority: JobPriorityLowest, Args: []byte(`lowest`)}
err = c.Enqueue(ctx, jobPriorityLowest)
require.NoError(t, err)
jobPriorityHighest := &Job{Type: "MyJob", Priority: JobPriorityHighest, Args: []byte(`highest`)}
err = c.Enqueue(ctx, jobPriorityHighest)
require.NoError(t, err)
jobPriorityLow := &Job{Type: "MyJob", Priority: JobPriorityLow, Args: []byte(`low`)}
err = c.Enqueue(ctx, jobPriorityLow)
require.NoError(t, err)
jobPriorityHigh := &Job{Type: "MyJob", Priority: JobPriorityHigh, Args: []byte(`high`)}
err = c.Enqueue(ctx, jobPriorityHigh)
require.NoError(t, err)
expectedOrder := []*Job{jobPriorityHighest, jobPriorityHigh, jobPriorityDefault, jobPriorityLow, jobPriorityLowest}
for _, expected := range expectedOrder {
j, err := c.LockJob(ctx, "")
require.NoError(t, err)
require.NotNil(t, j)
t.Cleanup(func() {
err := j.Done(ctx)
assert.NoError(t, err)
})
assert.Equal(t, expected.Priority, j.Priority)
assert.Equal(t, expected.Args, j.Args)
}
}
func findOneJob(t testing.TB, q adapter.Queryable) *Job {
t.Helper()
j := new(Job)
err := q.QueryRow(
context.Background(),
`SELECT priority, run_at, job_id, job_type, args, error_count, last_error, queue, created_at FROM gue_jobs LIMIT 1`,
).Scan(
&j.Priority,
&j.RunAt,
&j.ID,
&j.Type,
&j.Args,
&j.ErrorCount,
&j.LastError,
&j.Queue,
&j.CreatedAt,
)
if errors.Is(err, adapter.ErrNoRows) {
return nil
}
require.NoError(t, err)
return j
}
func TestAdapterQuery(t *testing.T) {
for name, openFunc := range adapterTesting.AllAdaptersOpenTestPool {
t.Run(name, func(t *testing.T) {
testAdapterQuery(t, openFunc(t))
})
}
}
func testAdapterQuery(t *testing.T, connPool adapter.ConnPool) {
ctx := context.Background()
c, err := NewClient(connPool)
require.NoError(t, err)
now := time.Now()
queue := now.Format(time.RFC3339Nano)
// schedule several jobs
j1 := Job{Queue: queue, RunAt: now, Type: "test1"}
j2 := Job{Queue: queue, RunAt: now, Type: "test2"}
j3 := Job{Queue: queue, RunAt: now, Type: "test3"}
err = c.EnqueueBatch(ctx, []*Job{&j1, &j2, &j3})
require.NoError(t, err)
// test pool
testQueryableQuery(ctx, t, connPool, queue, &j1, &j2, &j3)
// test connection
conn, err := connPool.Acquire(ctx)
require.NoError(t, err)
testQueryableQuery(ctx, t, conn, queue, &j1, &j2, &j3)
err = conn.Release()
assert.NoError(t, err)
// test transaction
tx, err := connPool.Begin(ctx)
require.NoError(t, err)
testQueryableQuery(ctx, t, tx, queue, &j1, &j2, &j3)
err = tx.Commit(ctx)
assert.NoError(t, err)
}
func testQueryableQuery(ctx context.Context, t *testing.T, q adapter.Queryable, queue string, j1, j2, j3 *Job) {
t.Helper()
rows, err := q.Query(ctx, `SELECT job_id, job_type FROM gue_jobs WHERE queue = $1 ORDER BY job_id ASC`, queue)
require.NoError(t, err)
var jobs []*Job
for rows.Next() {
j := new(Job)
err := rows.Scan(&j.ID, &j.Type)
require.NoError(t, err)
jobs = append(jobs, j)
}
require.Len(t, jobs, 3)
err = rows.Err()
require.NoError(t, err)
assert.Equal(t, j1.ID.String(), jobs[0].ID.String())
assert.Equal(t, j1.Type, jobs[0].Type)
assert.Equal(t, j2.ID.String(), jobs[1].ID.String())
assert.Equal(t, j2.Type, jobs[1].Type)
assert.Equal(t, j3.ID.String(), jobs[2].ID.String())
assert.Equal(t, j3.Type, jobs[2].Type)
}
func TestMultiSchema(t *testing.T) {
connPool := adapterTesting.OpenTestPoolLibPQCustomSchemas(t, "gue", "foo")
ctx := context.Background()
// create table with the explicitly set second schema as gue table was already created in the first one
_, err := connPool.Exec(ctx, "CREATE TABLE IF NOT EXISTS foo.bar ( id serial NOT NULL PRIMARY KEY, data text NOT NULL )")
require.NoError(t, err)
// insert into created table w/out setting schema explicitly - search_path should take care of this
_, err = connPool.Exec(ctx, "INSERT INTO bar (data) VALUES ('baz')")
require.NoError(t, err)
// run basic gue client test to ensure it works as expected in its own schema known to search_path
c, err := NewClient(connPool)
require.NoError(t, err)
newJob := &Job{
Type: "MyJob",
}
err = c.Enqueue(ctx, newJob)
require.NoError(t, err)
require.NotEmpty(t, newJob.ID)
j, err := c.LockJob(ctx, "")
require.NoError(t, err)
require.NotNil(t, j.tx)
t.Cleanup(func() {
err := j.Done(ctx)
assert.NoError(t, err)
})
}
func TestJobIDMigration(t *testing.T) {
connPool := adapterTesting.OpenTestPoolLibPQCustomSchemas(t, "job_id_migration_01", "job_id_migration_02")
ctx := context.Background()
// create a table with the serial ID
// editorconfig-checker-disable
_, err := connPool.Exec(ctx, `
DROP TABLE IF EXISTS gue_jobs;
CREATE TABLE gue_jobs
(
job_id BIGSERIAL NOT NULL PRIMARY KEY,
priority SMALLINT NOT NULL,
run_at TIMESTAMPTZ NOT NULL,
job_type TEXT NOT NULL,
args BYTEA NOT NULL,
error_count INTEGER NOT NULL DEFAULT 0,
last_error TEXT,
queue TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL,
updated_at TIMESTAMPTZ NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_gue_jobs_selector ON gue_jobs (queue, run_at, priority);
`)
require.NoError(t, err)
// editorconfig-checker-enable
// insert several records to test if the data migration works fine
now := time.Now()
const queueName string = "some-queue"
for i := 0; i < 101; i++ {
_, err = connPool.Exec(ctx, `INSERT INTO gue_jobs
(queue, priority, run_at, job_type, args, created_at, updated_at)
VALUES
($1, $2, $3, $4, $5, $6, $6)
`, queueName, 0, now, "foo-bar", []byte(fmt.Sprintf(`{"job":%d}`, i)), now)
require.NoError(t, err)
}
migrationSQL, err := os.ReadFile("./migrations/job_id_to_ulid.sql")
require.NoError(t, err)
_, err = connPool.Exec(ctx, string(migrationSQL))
require.NoError(t, err)
// ensure it is possible to retrieve a job from the DB after the conversion
c, err := NewClient(connPool)
require.NoError(t, err)
j1, err := c.LockJob(ctx, queueName)
require.NoError(t, err)
require.NotNil(t, j1)
t.Logf("Locked a job: %s %s", j1.ID.String(), string(j1.Args))
err = j1.Delete(ctx)
require.NoError(t, err)
err = j1.Done(ctx)
require.NoError(t, err)
j2, err := c.LockJobByID(ctx, j1.ID)
require.Error(t, err)
require.Nil(t, j2)
}