From 310bebf8f29979b0929b30d48ca320829d4133af Mon Sep 17 00:00:00 2001 From: Adriano Caloiaro Date: Mon, 25 Mar 2024 11:44:11 -0600 Subject: [PATCH] fix: An incorrect error was thrown when jobs exceeding their deadline were scheduled Co-authored-by: Philip Constantinou <1383834+pconstantinou@users.noreply.github.com> --- backends/postgres/postgres_backend_test.go | 46 ++++++++++++++++++++++ flake.nix | 2 +- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/backends/postgres/postgres_backend_test.go b/backends/postgres/postgres_backend_test.go index 9a33ae8..71af138 100644 --- a/backends/postgres/postgres_backend_test.go +++ b/backends/postgres/postgres_backend_test.go @@ -1020,3 +1020,49 @@ func TestGetPQConnectionString(t *testing.T) { }) } } + +// TestJobWithPastDeadline ensures that when a job is scheduled and its deadline is in the past, that the job is updated +// with an error indicating that its deadline was not met +// https://github.com/acaloiaro/neoq/issues/123 +func TestJobWithPastDeadline(t *testing.T) { + connString, _ := prepareAndCleanupDB(t) + const queue = "testing" + maxRetries := 5 + done := make(chan bool) + defer close(done) + + ctx := context.Background() + nq, err := neoq.New(ctx, neoq.WithBackend(postgres.Backend), postgres.WithConnectionString(connString)) + if err != nil { + t.Fatal(err) + } + defer nq.Shutdown(ctx) + + h := handler.New(queue, func(_ context.Context) (err error) { + done <- true + return + }) + + err = nq.Start(ctx, h) + if err != nil { + t.Error(err) + } + + // deadline in the past + deadline := time.Now().UTC().Add(time.Duration(-5) * time.Second) + jid, e := nq.Enqueue(ctx, &jobs.Job{ + Queue: queue, + Payload: map[string]interface{}{ + "message": "hello world", + }, + Deadline: &deadline, + MaxRetries: &maxRetries, + }) + if e != nil || jid == jobs.DuplicateJobID { + t.Error(e) + } + + if e != nil && !errors.Is(e, jobs.ErrJobExceededDeadline) { + t.Error(err) + } +} diff --git a/flake.nix b/flake.nix index cd68305..b79134a 100644 --- a/flake.nix +++ b/flake.nix @@ -22,7 +22,7 @@ in { devShells = forEachSystem (system: let pkgs = nixpkgs.legacyPackages.${system}; - postgresPort = 5433; + postgresPort = 5434; redisPort = 6380; in { default = devenv.lib.mkShell {