neoq/neoq_test.go

301 lines
6.3 KiB
Go
Raw Normal View History

package neoq_test
2023-02-13 20:24:49 +00:00
import (
"context"
"errors"
"fmt"
2023-02-25 03:33:38 +00:00
"os"
"strings"
2023-02-13 20:24:49 +00:00
"testing"
"time"
2023-02-25 03:33:38 +00:00
"github.com/acaloiaro/neoq"
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
)
var (
errTrigger = errors.New("triggerering a log error")
errPeriodicTimeout = errors.New("timed out waiting for periodic job")
)
2023-02-25 03:33:38 +00:00
func ExampleNew() {
ctx := context.Background()
nq, err := neoq.New(ctx, neoq.WithBackend(memory.Backend))
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 default memory backend")
// Output: neoq initialized with default memory backend
}
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
}
nq, err := neoq.New(ctx, neoq.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-25 03:33:38 +00:00
func ExampleWithBackend() {
ctx := context.Background()
nq, err := neoq.New(ctx, neoq.WithBackend(memory.Backend))
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 memory backend")
// Output: neoq initialized with memory backend
}
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
}
nq, err := neoq.New(ctx, neoq.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-25 03:33:38 +00:00
func TestStart(t *testing.T) {
const queue = "testing"
timeout := false
numJobs := 1
doneCnt := 0
done := make(chan bool, numJobs)
2023-02-19 23:52:37 +00:00
ctx := context.TODO()
nq, err := neoq.New(ctx, neoq.WithBackend(memory.Backend))
2023-02-13 20:24:49 +00:00
if err != nil {
t.Fatal(err)
}
defer nq.Shutdown(ctx)
2023-02-13 20:24:49 +00:00
h := handler.New(queue, 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, 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
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
}
}
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()
nq, err := neoq.New(ctx, neoq.WithBackend(memory.Backend))
2023-02-17 19:11:55 +00:00
if err != nil {
t.Fatal(err)
}
defer nq.Shutdown(ctx)
2023-02-17 19:11:55 +00:00
done := make(chan bool)
h := handler.NewPeriodic(func(ctx context.Context) (err error) {
2023-02-17 19:11:55 +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-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
time.Sleep(5 * time.Millisecond)
2023-02-17 19:11:55 +00:00
select {
case <-time.After(1 * time.Second):
err = errPeriodicTimeout
2023-02-17 19:11:55 +00:00
case <-done:
}
if err != nil {
2023-02-17 19:11:55 +00:00
t.Error(err)
}
}
2023-02-25 03:33:38 +00:00
func TestSetLogger(t *testing.T) {
2023-08-27 08:31:52 +00:00
timeoutTimer := time.After(5 * time.Second)
const queue = "testing"
2023-08-27 08:31:52 +00:00
logsChan := make(chan string, 10)
ctx := context.Background()
nq, err := neoq.New(ctx, neoq.WithBackend(memory.Backend))
if err != nil {
t.Fatal(err)
}
defer nq.Shutdown(ctx)
2023-09-05 08:41:16 +00:00
nq.SetLogger(testutils.NewTestLogger(logsChan))
2023-02-25 03:33:38 +00:00
h := handler.New(queue, func(ctx context.Context) (err error) {
err = errTrigger
return
})
if err != nil {
t.Error(err)
}
err = nq.Start(ctx, h)
if err != nil {
t.Error(err)
}
2023-02-25 03:33:38 +00:00
_, err = nq.Enqueue(ctx, &jobs.Job{Queue: queue})
if err != nil {
t.Error(err)
}
expectedLogMsg := "adding a new job [queue=testing]"
2023-08-27 08:31:52 +00:00
results_loop:
for {
select {
case <-timeoutTimer:
err = jobs.ErrJobTimeout
break results_loop
case actualLogMsg := <-logsChan:
if strings.Contains(actualLogMsg, expectedLogMsg) {
err = nil
break results_loop
}
err = fmt.Errorf("'%s' NOT CONTAINS '%s'", actualLogMsg, expectedLogMsg) //nolint:all
}
}
if err != nil {
t.Error(err)
}
}
func TestHandlerRecoveryCallback(t *testing.T) {
const queue = "testing"
timeoutTimer := time.After(5 * time.Second)
recoveryFuncCalled := make(chan bool, 1)
ctx := context.Background()
nq, err := neoq.New(ctx,
neoq.WithBackend(memory.Backend),
neoq.WithRecoveryCallback(func(_ context.Context, _ error) (err error) {
recoveryFuncCalled <- true
return
}))
if err != nil {
t.Fatal(err)
}
defer nq.Shutdown(ctx)
h := handler.New(queue, func(ctx context.Context) (err error) {
panic("abort mission!")
})
h.WithOptions(
handler.JobTimeout(500*time.Millisecond),
handler.Concurrency(1),
)
// process jobs on the test queue
err = nq.Start(ctx, h)
if err != nil {
t.Error(err)
}
jid, err := nq.Enqueue(ctx, &jobs.Job{
Queue: queue,
Payload: map[string]interface{}{
"message": "hello world",
},
})
if err != nil || jid == jobs.DuplicateJobID {
t.Fatal("job was not enqueued. either it was duplicate or this error caused it:", err)
}
select {
case <-timeoutTimer:
err = errors.New("timed out waiting for job") // nolint: goerr113
return
case <-recoveryFuncCalled:
break
}
if err != nil {
t.Error(err)
}
}