neoq/neoq_test.go

233 lines
4.9 KiB
Go

package neoq
import (
"context"
"errors"
"fmt"
"log"
"os"
"strings"
"testing"
"time"
"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"
)
var errTrigger = errors.New("triggerering a log error")
var errPeriodicTimeout = errors.New("timed out waiting for periodic job")
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
}
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 := New(ctx, WithBackend(postgres.Backend), postgres.WithConnectionString(pgURL))
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
}
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
}
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 := New(ctx, WithBackend(postgres.Backend), postgres.WithConnectionString(pgURL))
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
}
func TestStart(t *testing.T) {
const queue = "testing"
timeout := false
numJobs := 1
doneCnt := 0
var done = make(chan bool, numJobs)
ctx := context.TODO()
nq, err := New(ctx, WithBackend(memory.Backend))
if err != nil {
t.Fatal(err)
}
defer nq.Shutdown(ctx)
h := handler.New(func(ctx context.Context) (err error) {
done <- true
return
})
h.WithOptions(
handler.JobTimeout(500*time.Millisecond),
handler.Concurrency(1),
)
// process jobs on the test queue
err = nq.Start(ctx, queue, h)
if err != nil {
t.Error(err)
}
// allow time for processor to start
time.Sleep(5 * time.Millisecond)
for i := 0; i < numJobs; i++ {
jid, err := nq.Enqueue(ctx, &jobs.Job{
Queue: queue,
Payload: map[string]interface{}{
"message": fmt.Sprintf("hello world: %d", i),
},
})
if err != nil || jid == jobs.DuplicateJobID {
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
err = errors.New("timed out waiting for job") // nolint: goerr113
case <-done:
doneCnt++
}
if doneCnt >= numJobs {
break
}
if timeout {
break
}
}
if timeout {
t.Error(err)
}
}
func TestStartCron(t *testing.T) {
const cron = "* * * * * *"
ctx := context.TODO()
nq, err := New(ctx, WithBackend(memory.Backend))
if err != nil {
t.Fatal(err)
}
defer nq.Shutdown(ctx)
var done = make(chan bool)
h := handler.New(func(ctx context.Context) (err error) {
done <- true
return
})
h.WithOptions(
handler.JobTimeout(500*time.Millisecond),
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)
}
}
func TestSetLogger(t *testing.T) {
const queue = "testing"
var done = make(chan bool)
buf := &strings.Builder{}
ctx := context.TODO()
nq, err := New(ctx, WithBackend(memory.Backend))
if err != nil {
t.Fatal(err)
}
defer nq.Shutdown(ctx)
nq.SetLogger(testutils.TestLogger{L: log.New(buf, "", 0), Done: done})
h := handler.New(func(ctx context.Context) (err error) {
err = errTrigger
return
})
if err != nil {
t.Error(err)
}
err = nq.Start(ctx, queue, h)
if err != nil {
t.Error(err)
}
_, err = nq.Enqueue(ctx, &jobs.Job{Queue: queue})
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
}
}