samsondav / rihanna

Rihanna is a high performance postgres-backed job queue for Elixir
MIT License
439 stars 49 forks source link

use rihanna in unit tests #53

Closed ghost closed 5 years ago

ghost commented 5 years ago

👋

I've added a simple test like this and is seems to be failing:

  describe "enqueue" do
    test "valid job" do
      # tracing shows that `:poll`s are being made
      # Rexbug.start(["Rihanna.JobDispatcher :: return", "Rihanna.Config :: return"],
      #   msgs: 1000,
      #   time: 60000
      # )

      assert {:ok, %Rihanna.Job{}} = Rihanna.enqueue({Kernel, :send, [self(), {:work, :done}]})
      assert_receive {:work, :done}, 60000 # never arrives
    end
  end

The repo that rihanna uses runs in an ecto sandbox.

I've also noted that rihanna_jobs table doesn't seem to be rolled back after the test exits. Does rihanna run job polling queries outside of the sandbox transaction? Then it would explain why the job created in the test is never executed.

ghost commented 5 years ago

According to https://github.com/samphilipd/rihanna/issues/38 it should be possible ... Am I holding it wrong?

# in config.exs
config :rihanna,
  producer_postgres_connection: {Ecto, App.Repo}

# in application.ex
{Rihanna.Supervisor, postgrex: App.Repo.config()}
lpil commented 5 years ago

I would advise not running the poller in test and instead testing that the job exists in the jobs table.

The jobs themselves can then be executed and tested synchronously by calling them directly.

lpil commented 5 years ago

The table should be rolled back when using the sandbox. Can you share a minimal example project that replicates the bug? We'll be happy to take a look at that.

ghost commented 5 years ago

The table should be rolled back when using the sandbox. Can you share a minimal example project that replicates the bug? We'll be happy to take a look at that.

Hi @lpil! You are right, the transaction is indeed rolled back, but the autoincremented job id is not -- that's what got confused, sorry.

lpil commented 5 years ago

Great, glad we could help!