2023-02-13 20:24:49 +00:00
|
|
|
package neoq
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
2023-02-24 17:09:20 +00:00
|
|
|
"log"
|
2023-02-25 03:33:38 +00:00
|
|
|
"os"
|
2023-02-24 17:09:20 +00:00
|
|
|
"strings"
|
2023-02-13 20:24:49 +00:00
|
|
|
"testing"
|
|
|
|
|
"time"
|
2023-02-25 03:33:38 +00:00
|
|
|
|
|
|
|
|
"github.com/acaloiaro/neoq/backends/memory"
|
|
|
|
|
"github.com/acaloiaro/neoq/backends/postgres"
|
|
|
|
|
"github.com/acaloiaro/neoq/handler"
|
|
|
|
|
"github.com/acaloiaro/neoq/jobs"
|
|
|
|
|
"github.com/acaloiaro/neoq/testutils"
|
2023-02-13 20:24:49 +00:00
|
|
|
)
|
|
|
|
|
|
2023-02-24 17:09:20 +00:00
|
|
|
var errTrigger = errors.New("triggerering a log error")
|
|
|
|
|
var errPeriodicTimeout = errors.New("timed out waiting for periodic job")
|
|
|
|
|
|
2023-02-25 03:33:38 +00:00
|
|
|
func ExampleNew() {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
nq, err := New(ctx, WithBackend(memory.Backend))
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("initializing a new Neoq with no params should not return an error:", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer nq.Shutdown(ctx)
|
|
|
|
|
|
|
|
|
|
fmt.Println("neoq initialized with default memory backend")
|
|
|
|
|
// Output: neoq initialized with default memory backend
|
2023-02-24 17:09:20 +00:00
|
|
|
}
|
|
|
|
|
|
2023-02-25 03:33:38 +00:00
|
|
|
func ExampleNew_postgres() {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
var pgURL string
|
|
|
|
|
var ok bool
|
|
|
|
|
if pgURL, ok = os.LookupEnv("TEST_DATABASE_URL"); !ok {
|
|
|
|
|
fmt.Println("Please set TEST_DATABASE_URL environment variable")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-02 01:03:55 +00:00
|
|
|
nq, err := New(ctx, WithBackend(postgres.Backend), postgres.WithConnectionString(pgURL))
|
2023-02-25 03:33:38 +00:00
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("neoq's postgres backend failed to initialize:", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer nq.Shutdown(ctx)
|
|
|
|
|
|
|
|
|
|
fmt.Println("neoq initialized with postgres backend")
|
|
|
|
|
// Output: neoq initialized with postgres backend
|
2023-02-24 17:09:20 +00:00
|
|
|
}
|
2023-02-25 03:33:38 +00:00
|
|
|
|
|
|
|
|
func ExampleWithBackend() {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
nq, err := New(ctx, WithBackend(memory.Backend))
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("initializing a new Neoq with no params should not return an error:", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer nq.Shutdown(ctx)
|
|
|
|
|
|
|
|
|
|
fmt.Println("neoq initialized with memory backend")
|
|
|
|
|
// Output: neoq initialized with memory backend
|
2023-02-24 17:09:20 +00:00
|
|
|
}
|
2023-02-25 03:33:38 +00:00
|
|
|
|
|
|
|
|
func ExampleWithBackend_postgres() {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
var pgURL string
|
|
|
|
|
var ok bool
|
|
|
|
|
if pgURL, ok = os.LookupEnv("TEST_DATABASE_URL"); !ok {
|
|
|
|
|
fmt.Println("Please set TEST_DATABASE_URL environment variable")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-02 01:03:55 +00:00
|
|
|
nq, err := New(ctx, WithBackend(postgres.Backend), postgres.WithConnectionString(pgURL))
|
2023-02-25 03:33:38 +00:00
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("initializing a new Neoq with no params should not return an error:", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer nq.Shutdown(ctx)
|
|
|
|
|
|
|
|
|
|
fmt.Println("neoq initialized with postgres backend")
|
|
|
|
|
// Output: neoq initialized with postgres backend
|
2023-02-24 17:09:20 +00:00
|
|
|
}
|
|
|
|
|
|
2023-02-25 03:33:38 +00:00
|
|
|
func TestStart(t *testing.T) {
|
2023-02-22 15:39:59 +00:00
|
|
|
const queue = "testing"
|
|
|
|
|
timeout := false
|
|
|
|
|
numJobs := 1
|
|
|
|
|
doneCnt := 0
|
|
|
|
|
var done = make(chan bool, numJobs)
|
2023-02-19 23:52:37 +00:00
|
|
|
|
2023-02-22 15:39:59 +00:00
|
|
|
ctx := context.TODO()
|
2023-02-25 03:33:38 +00:00
|
|
|
nq, err := New(ctx, WithBackend(memory.Backend))
|
2023-02-13 20:24:49 +00:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2023-02-24 17:09:20 +00:00
|
|
|
defer nq.Shutdown(ctx)
|
2023-02-13 20:24:49 +00:00
|
|
|
|
2023-02-25 03:33:38 +00:00
|
|
|
h := handler.New(func(ctx context.Context) (err error) {
|
2023-02-13 20:24:49 +00:00
|
|
|
done <- true
|
|
|
|
|
return
|
|
|
|
|
})
|
2023-02-25 03:33:38 +00:00
|
|
|
h.WithOptions(
|
2023-04-15 01:11:22 +00:00
|
|
|
handler.JobTimeout(500*time.Millisecond),
|
2023-02-25 03:33:38 +00:00
|
|
|
handler.Concurrency(1),
|
2023-02-23 02:47:57 +00:00
|
|
|
)
|
2023-02-13 20:24:49 +00:00
|
|
|
|
2023-02-25 03:33:38 +00:00
|
|
|
// process jobs on the test queue
|
|
|
|
|
err = nq.Start(ctx, queue, h)
|
2023-02-13 20:24:49 +00:00
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err)
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-25 03:33:38 +00:00
|
|
|
// allow time for processor to start
|
2023-02-22 15:39:59 +00:00
|
|
|
time.Sleep(5 * time.Millisecond)
|
2023-02-13 20:24:49 +00:00
|
|
|
|
|
|
|
|
for i := 0; i < numJobs; i++ {
|
2023-02-25 03:33:38 +00:00
|
|
|
jid, err := nq.Enqueue(ctx, &jobs.Job{
|
2023-02-13 20:24:49 +00:00
|
|
|
Queue: queue,
|
|
|
|
|
Payload: map[string]interface{}{
|
2023-02-16 23:15:39 +00:00
|
|
|
"message": fmt.Sprintf("hello world: %d", i),
|
2023-02-13 20:24:49 +00:00
|
|
|
},
|
|
|
|
|
})
|
2023-04-15 01:11:22 +00:00
|
|
|
if err != nil || jid == jobs.DuplicateJobID {
|
2023-02-13 20:24:49 +00:00
|
|
|
t.Fatal("job was not enqueued. either it was duplicate or this error caused it:", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
|
timeout = true
|
2023-02-25 03:33:38 +00:00
|
|
|
err = errors.New("timed out waiting for job") // nolint: goerr113
|
2023-02-13 20:24:49 +00:00
|
|
|
case <-done:
|
|
|
|
|
doneCnt++
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if doneCnt >= numJobs {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if timeout {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-24 17:09:20 +00:00
|
|
|
if timeout {
|
2023-02-13 20:24:49 +00:00
|
|
|
t.Error(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-17 19:11:55 +00:00
|
|
|
|
2023-02-25 03:33:38 +00:00
|
|
|
func TestStartCron(t *testing.T) {
|
2023-02-17 19:11:55 +00:00
|
|
|
const cron = "* * * * * *"
|
2023-02-18 04:06:03 +00:00
|
|
|
ctx := context.TODO()
|
2023-02-25 03:33:38 +00:00
|
|
|
nq, err := New(ctx, WithBackend(memory.Backend))
|
2023-02-17 19:11:55 +00:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2023-02-24 17:09:20 +00:00
|
|
|
defer nq.Shutdown(ctx)
|
2023-02-17 19:11:55 +00:00
|
|
|
|
|
|
|
|
var done = make(chan bool)
|
2023-02-25 03:33:38 +00:00
|
|
|
h := handler.New(func(ctx context.Context) (err error) {
|
2023-02-17 19:11:55 +00:00
|
|
|
done <- true
|
|
|
|
|
return
|
|
|
|
|
})
|
2023-02-17 19:44:16 +00:00
|
|
|
|
2023-02-25 03:33:38 +00:00
|
|
|
h.WithOptions(
|
2023-04-15 01:11:22 +00:00
|
|
|
handler.JobTimeout(500*time.Millisecond),
|
2023-02-25 03:33:38 +00:00
|
|
|
handler.Concurrency(1),
|
2023-02-23 02:47:57 +00:00
|
|
|
)
|
2023-02-17 19:11:55 +00:00
|
|
|
|
2023-02-25 03:33:38 +00:00
|
|
|
err = nq.StartCron(ctx, cron, h)
|
2023-02-17 19:11:55 +00:00
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// allow time for listener to start
|
2023-02-22 15:39:59 +00:00
|
|
|
time.Sleep(5 * time.Millisecond)
|
2023-02-17 19:11:55 +00:00
|
|
|
|
|
|
|
|
select {
|
2023-02-24 17:09:20 +00:00
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
|
err = errPeriodicTimeout
|
2023-02-17 19:11:55 +00:00
|
|
|
case <-done:
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-24 17:09:20 +00:00
|
|
|
if err != nil {
|
2023-02-17 19:11:55 +00:00
|
|
|
t.Error(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-24 17:09:20 +00:00
|
|
|
|
2023-02-25 03:33:38 +00:00
|
|
|
func TestSetLogger(t *testing.T) {
|
2023-02-24 17:09:20 +00:00
|
|
|
const queue = "testing"
|
|
|
|
|
var done = make(chan bool)
|
2023-02-25 03:33:38 +00:00
|
|
|
buf := &strings.Builder{}
|
2023-02-24 17:09:20 +00:00
|
|
|
ctx := context.TODO()
|
|
|
|
|
|
2023-02-25 03:33:38 +00:00
|
|
|
nq, err := New(ctx, WithBackend(memory.Backend))
|
2023-02-24 17:09:20 +00:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
defer nq.Shutdown(ctx)
|
|
|
|
|
|
2023-02-25 03:33:38 +00:00
|
|
|
nq.SetLogger(testutils.TestLogger{L: log.New(buf, "", 0), Done: done})
|
|
|
|
|
|
|
|
|
|
h := handler.New(func(ctx context.Context) (err error) {
|
2023-02-24 17:09:20 +00:00
|
|
|
err = errTrigger
|
|
|
|
|
return
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err)
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-25 03:33:38 +00:00
|
|
|
err = nq.Start(ctx, queue, h)
|
2023-02-24 17:09:20 +00:00
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err)
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-25 03:33:38 +00:00
|
|
|
_, err = nq.Enqueue(ctx, &jobs.Job{Queue: queue})
|
2023-02-24 17:09:20 +00:00
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
<-done
|
|
|
|
|
expectedLogMsg := "job failed job failed to process: triggerering a log error"
|
|
|
|
|
actualLogMsg := strings.Trim(buf.String(), "\n")
|
|
|
|
|
if actualLogMsg != expectedLogMsg {
|
|
|
|
|
t.Error(fmt.Errorf("%s != %s", actualLogMsg, expectedLogMsg)) //nolint:all
|
|
|
|
|
}
|
|
|
|
|
}
|