2023-02-25 03:33:38 +00:00
|
|
|
package postgres_test
|
2023-02-22 19:27:45 +00:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2023-03-17 15:53:16 +00:00
|
|
|
"errors"
|
2023-03-18 19:22:27 +00:00
|
|
|
"fmt"
|
2023-02-22 19:27:45 +00:00
|
|
|
"os"
|
2023-04-26 16:54:02 +00:00
|
|
|
"strings"
|
2023-02-22 19:27:45 +00:00
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
2023-02-25 03:33:38 +00:00
|
|
|
"github.com/acaloiaro/neoq"
|
|
|
|
|
"github.com/acaloiaro/neoq/backends/postgres"
|
|
|
|
|
"github.com/acaloiaro/neoq/handler"
|
2023-03-18 19:22:27 +00:00
|
|
|
"github.com/acaloiaro/neoq/internal"
|
2023-02-25 03:33:38 +00:00
|
|
|
"github.com/acaloiaro/neoq/jobs"
|
2023-05-03 02:17:53 +00:00
|
|
|
"github.com/acaloiaro/neoq/logging"
|
2023-04-26 16:54:02 +00:00
|
|
|
"github.com/acaloiaro/neoq/testutils"
|
|
|
|
|
"github.com/jackc/pgx/v5"
|
2023-02-22 19:27:45 +00:00
|
|
|
)
|
|
|
|
|
|
2023-03-07 16:24:46 +00:00
|
|
|
var errPeriodicTimeout = errors.New("timed out waiting for periodic job")
|
|
|
|
|
|
2023-04-26 16:54:02 +00:00
|
|
|
func flushDB() {
|
2023-08-27 08:31:52 +00:00
|
|
|
ctx := context.Background()
|
2023-04-26 16:54:02 +00:00
|
|
|
dbURL := os.Getenv("TEST_DATABASE_URL")
|
|
|
|
|
if dbURL == "" {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
conn, err := pgx.Connect(context.Background(), dbURL)
|
|
|
|
|
if err != nil {
|
2023-08-27 08:31:52 +00:00
|
|
|
// an error was encountered connecting to the db. this may simply mean that we're running tests against an
|
|
|
|
|
// uninitialized database and the database needs to be created. By creating a new pg backend instance with
|
|
|
|
|
// neoq.New, we run the db initialization process.
|
|
|
|
|
// if no errors return from `New`, then we've succeeded
|
|
|
|
|
var newErr error
|
|
|
|
|
_, newErr = neoq.New(ctx, neoq.WithBackend(postgres.Backend), postgres.WithConnectionString(dbURL))
|
|
|
|
|
if newErr != nil {
|
|
|
|
|
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-04-26 16:54:02 +00:00
|
|
|
}
|
|
|
|
|
defer conn.Close(context.Background())
|
|
|
|
|
|
|
|
|
|
_, err = conn.Query(context.Background(), "DELETE FROM neoq_jobs") // nolint: gocritic
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Fprintf(os.Stderr, "'neoq_jobs' table flush failed: %v\n", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
|
flushDB()
|
|
|
|
|
code := m.Run()
|
|
|
|
|
os.Exit(code)
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-25 03:33:38 +00:00
|
|
|
// TestBasicJobProcessing tests that the postgres backend is able to process the most basic jobs with the
|
2023-02-22 19:27:45 +00:00
|
|
|
// most basic configuration.
|
2023-02-25 03:33:38 +00:00
|
|
|
func TestBasicJobProcessing(t *testing.T) {
|
2023-03-18 16:52:48 +00:00
|
|
|
const queue = "testing"
|
2023-02-22 19:27:45 +00:00
|
|
|
done := make(chan bool)
|
2023-03-17 15:53:16 +00:00
|
|
|
defer close(done)
|
|
|
|
|
|
2023-08-19 23:46:27 +00:00
|
|
|
timeoutTimer := time.After(5 * time.Second)
|
2023-02-22 19:27:45 +00:00
|
|
|
|
2023-08-19 23:46:27 +00:00
|
|
|
connString := os.Getenv("TEST_DATABASE_URL")
|
2023-02-22 19:27:45 +00:00
|
|
|
if connString == "" {
|
|
|
|
|
t.Skip("Skipping: TEST_DATABASE_URL not set")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-15 14:26:23 +00:00
|
|
|
ctx := context.Background()
|
2023-04-02 01:03:55 +00:00
|
|
|
nq, err := neoq.New(ctx, neoq.WithBackend(postgres.Backend), postgres.WithConnectionString(connString))
|
2023-02-22 19:27:45 +00:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2023-02-25 03:33:38 +00:00
|
|
|
defer nq.Shutdown(ctx)
|
2023-02-22 19:27:45 +00:00
|
|
|
|
2023-09-15 14:26:23 +00:00
|
|
|
h := handler.New(queue, func(_ context.Context) (err error) {
|
2023-02-22 19:27:45 +00:00
|
|
|
done <- true
|
|
|
|
|
return
|
|
|
|
|
})
|
|
|
|
|
|
2023-09-15 14:26:23 +00:00
|
|
|
err = nq.Start(ctx, h)
|
2023-02-25 03:33:38 +00:00
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err)
|
|
|
|
|
}
|
2023-02-22 19:27:45 +00:00
|
|
|
|
2023-08-27 08:31:52 +00:00
|
|
|
deadline := time.Now().UTC().Add(5 * time.Second)
|
2023-03-17 15:53:16 +00:00
|
|
|
jid, e := nq.Enqueue(ctx, &jobs.Job{
|
|
|
|
|
Queue: queue,
|
|
|
|
|
Payload: map[string]interface{}{
|
|
|
|
|
"message": "hello world",
|
|
|
|
|
},
|
2023-08-27 08:31:52 +00:00
|
|
|
Deadline: &deadline,
|
2023-03-17 15:53:16 +00:00
|
|
|
})
|
|
|
|
|
if e != nil || jid == jobs.DuplicateJobID {
|
|
|
|
|
t.Error(e)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
select {
|
|
|
|
|
case <-timeoutTimer:
|
|
|
|
|
err = jobs.ErrJobTimeout
|
|
|
|
|
case <-done:
|
2023-02-22 19:27:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err)
|
|
|
|
|
}
|
2023-04-26 16:54:02 +00:00
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
|
flushDB()
|
|
|
|
|
})
|
2023-02-22 19:27:45 +00:00
|
|
|
}
|
2023-03-07 16:24:46 +00:00
|
|
|
|
2023-09-02 10:55:56 +00:00
|
|
|
// TestDuplicateJobRejection tests that the backend rejects jobs that are duplicates
|
|
|
|
|
func TestDuplicateJobRejection(t *testing.T) {
|
|
|
|
|
const queue = "testing"
|
|
|
|
|
|
|
|
|
|
connString := os.Getenv("TEST_DATABASE_URL")
|
|
|
|
|
if connString == "" {
|
|
|
|
|
t.Skip("Skipping: TEST_DATABASE_URL not set")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx := context.TODO()
|
|
|
|
|
nq, err := neoq.New(ctx, neoq.WithBackend(postgres.Backend), postgres.WithConnectionString(connString))
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
defer nq.Shutdown(ctx)
|
|
|
|
|
|
|
|
|
|
jid, e := nq.Enqueue(ctx, &jobs.Job{
|
|
|
|
|
Queue: queue,
|
|
|
|
|
Payload: map[string]interface{}{
|
|
|
|
|
"message": "hello world",
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
if e != nil || jid == jobs.DuplicateJobID {
|
|
|
|
|
err = e
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, e2 := nq.Enqueue(ctx, &jobs.Job{
|
|
|
|
|
Queue: queue,
|
|
|
|
|
Payload: map[string]interface{}{
|
|
|
|
|
"message": "hello world",
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// we submitted two duplicate jobs; the error should be a duplicate job error
|
|
|
|
|
if !errors.Is(e2, postgres.ErrDuplicateJob) {
|
|
|
|
|
err = e2
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
|
flushDB()
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-18 16:52:48 +00:00
|
|
|
// TestBasicJobMultipleQueue tests that the postgres backend is able to process jobs on multiple queues
|
|
|
|
|
func TestBasicJobMultipleQueue(t *testing.T) {
|
|
|
|
|
const queue = "testing"
|
|
|
|
|
const queue2 = "testing2"
|
|
|
|
|
done := make(chan bool)
|
|
|
|
|
doneCnt := 0
|
|
|
|
|
|
2023-08-19 23:46:27 +00:00
|
|
|
timeoutTimer := time.After(5 * time.Second)
|
2023-03-18 16:52:48 +00:00
|
|
|
|
2023-08-19 23:46:27 +00:00
|
|
|
connString := os.Getenv("TEST_DATABASE_URL")
|
2023-03-18 16:52:48 +00:00
|
|
|
if connString == "" {
|
|
|
|
|
t.Skip("Skipping: TEST_DATABASE_URL not set")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx := context.TODO()
|
2023-04-02 01:03:55 +00:00
|
|
|
nq, err := neoq.New(ctx, neoq.WithBackend(postgres.Backend), postgres.WithConnectionString(connString))
|
2023-03-18 16:52:48 +00:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
defer nq.Shutdown(ctx)
|
|
|
|
|
|
2023-09-15 14:26:23 +00:00
|
|
|
h := handler.New(queue, func(_ context.Context) (err error) {
|
2023-03-18 16:52:48 +00:00
|
|
|
done <- true
|
|
|
|
|
return
|
|
|
|
|
})
|
|
|
|
|
|
2023-09-15 14:26:23 +00:00
|
|
|
h2 := handler.New(queue2, func(_ context.Context) (err error) {
|
2023-03-18 16:52:48 +00:00
|
|
|
done <- true
|
|
|
|
|
return
|
|
|
|
|
})
|
|
|
|
|
|
2023-09-15 14:26:23 +00:00
|
|
|
err = nq.Start(ctx, h)
|
2023-03-18 16:52:48 +00:00
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err)
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-15 14:26:23 +00:00
|
|
|
err = nq.Start(ctx, h2)
|
2023-03-18 16:52:48 +00:00
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err)
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-22 13:27:15 +00:00
|
|
|
go func() {
|
|
|
|
|
jid, e := nq.Enqueue(ctx, &jobs.Job{
|
|
|
|
|
Queue: queue,
|
|
|
|
|
Payload: map[string]interface{}{
|
|
|
|
|
"message": fmt.Sprintf("hello world: %d", internal.RandInt(10000000000)),
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
if e != nil || jid == jobs.DuplicateJobID {
|
|
|
|
|
t.Error(e)
|
|
|
|
|
}
|
2023-03-18 16:52:48 +00:00
|
|
|
|
2023-09-22 13:27:15 +00:00
|
|
|
jid2, e := nq.Enqueue(ctx, &jobs.Job{
|
|
|
|
|
Queue: queue2,
|
|
|
|
|
Payload: map[string]interface{}{
|
|
|
|
|
"message": fmt.Sprintf("hello world: %d", internal.RandInt(10000000000)),
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
if e != nil || jid2 == jobs.DuplicateJobID {
|
|
|
|
|
t.Error(e)
|
|
|
|
|
}
|
|
|
|
|
}()
|
2023-03-18 16:52:48 +00:00
|
|
|
|
|
|
|
|
results_loop:
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-timeoutTimer:
|
|
|
|
|
err = jobs.ErrJobTimeout
|
|
|
|
|
break results_loop
|
|
|
|
|
case <-done:
|
|
|
|
|
doneCnt++
|
|
|
|
|
if doneCnt == 2 {
|
|
|
|
|
break results_loop
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err)
|
|
|
|
|
}
|
2023-04-26 16:54:02 +00:00
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
|
flushDB()
|
|
|
|
|
})
|
2023-03-18 16:52:48 +00:00
|
|
|
}
|
|
|
|
|
|
2023-03-07 16:24:46 +00:00
|
|
|
func TestCron(t *testing.T) {
|
2023-03-17 15:53:16 +00:00
|
|
|
done := make(chan bool, 1)
|
|
|
|
|
defer close(done)
|
2023-03-07 16:24:46 +00:00
|
|
|
const cron = "* * * * * *"
|
2023-08-19 23:46:27 +00:00
|
|
|
connString := os.Getenv("TEST_DATABASE_URL")
|
2023-03-07 16:24:46 +00:00
|
|
|
if connString == "" {
|
|
|
|
|
t.Skip("Skipping: TEST_DATABASE_URL not set")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx := context.TODO()
|
2023-04-02 01:03:55 +00:00
|
|
|
nq, err := neoq.New(ctx, neoq.WithBackend(postgres.Backend), postgres.WithConnectionString(connString))
|
2023-03-07 16:24:46 +00:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
defer nq.Shutdown(ctx)
|
|
|
|
|
|
2023-09-15 14:26:23 +00:00
|
|
|
h := handler.NewPeriodic(func(ctx context.Context) (err error) {
|
2023-03-07 16:24:46 +00:00
|
|
|
done <- true
|
|
|
|
|
return
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
h.WithOptions(
|
2023-04-15 01:11:22 +00:00
|
|
|
handler.JobTimeout(500*time.Millisecond),
|
2023-03-07 16:24:46 +00:00
|
|
|
handler.Concurrency(1),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
err = nq.StartCron(ctx, cron, h)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// allow time for listener to start
|
|
|
|
|
time.Sleep(5 * time.Millisecond)
|
|
|
|
|
|
|
|
|
|
select {
|
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
|
err = errPeriodicTimeout
|
|
|
|
|
case <-done:
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err)
|
|
|
|
|
}
|
2023-04-26 16:54:02 +00:00
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
|
flushDB()
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TestBasicJobProcessingWithErrors tests that the postgres backend is able to update the status of jobs that fail
|
|
|
|
|
func TestBasicJobProcessingWithErrors(t *testing.T) {
|
|
|
|
|
const queue = "testing"
|
2023-08-27 08:31:52 +00:00
|
|
|
logsChan := make(chan string, 100)
|
2023-08-19 23:46:27 +00:00
|
|
|
timeoutTimer := time.After(5 * time.Second)
|
|
|
|
|
connString := os.Getenv("TEST_DATABASE_URL")
|
2023-04-26 16:54:02 +00:00
|
|
|
if connString == "" {
|
|
|
|
|
t.Skip("Skipping: TEST_DATABASE_URL not set")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx := context.TODO()
|
2023-05-03 02:17:53 +00:00
|
|
|
nq, err := neoq.New(ctx,
|
|
|
|
|
neoq.WithBackend(postgres.Backend),
|
|
|
|
|
postgres.WithConnectionString(connString),
|
|
|
|
|
neoq.WithLogLevel(logging.LogLevelError))
|
2023-04-26 16:54:02 +00:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
defer nq.Shutdown(ctx)
|
|
|
|
|
|
2023-09-15 14:26:23 +00:00
|
|
|
h := handler.New(queue, func(_ context.Context) (err error) {
|
2023-04-26 16:54:02 +00:00
|
|
|
err = errors.New("something bad happened") // nolint: goerr113
|
|
|
|
|
return
|
|
|
|
|
})
|
|
|
|
|
|
2023-09-05 08:41:16 +00:00
|
|
|
nq.SetLogger(testutils.NewTestLogger(logsChan))
|
2023-05-03 02:17:53 +00:00
|
|
|
|
2023-09-15 14:26:23 +00:00
|
|
|
err = nq.Start(ctx, h)
|
2023-04-26 16:54:02 +00:00
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jid, e := nq.Enqueue(ctx, &jobs.Job{
|
|
|
|
|
Queue: queue,
|
|
|
|
|
Payload: map[string]interface{}{
|
|
|
|
|
"message": "hello world",
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
if e != nil || jid == jobs.DuplicateJobID {
|
|
|
|
|
t.Error(e)
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-27 08:31:52 +00:00
|
|
|
results_loop:
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-timeoutTimer:
|
|
|
|
|
err = jobs.ErrJobTimeout
|
|
|
|
|
break results_loop
|
|
|
|
|
case actualLogMsg := <-logsChan:
|
|
|
|
|
expectedLogMsg := "job failed to process: something bad happened"
|
|
|
|
|
if strings.Contains(actualLogMsg, expectedLogMsg) {
|
|
|
|
|
err = nil
|
|
|
|
|
break results_loop
|
|
|
|
|
}
|
|
|
|
|
err = fmt.Errorf("'%s' NOT CONTAINS '%s'", actualLogMsg, expectedLogMsg) //nolint:all
|
2023-04-26 16:54:02 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
|
flushDB()
|
|
|
|
|
})
|
2023-03-07 16:24:46 +00:00
|
|
|
}
|