0

I currently have functionality which tracks when a user connects to a channel and when a user leaves a channel. This is implemented from this stackoverflow answer here.

I have written the following test (see commit) to verify my callback logic:

socket = subscribe_and_join!(socket, PostChannel, "post:#{post.id}")

assert (length Records.list_perceptions()) == 1
...
assert head.counter == 1

close(socket)
:timer.sleep(10) # Is this necessary?

assert (length Records.list_perceptions()) == 1
...
assert head.counter == 0

When testing this without the delay, the monitor callback is not triggered. With the delay removed, my test assertions fail and I get GenServer shutdown errors after the tests complete:

00:48:10.496 [error] Task #PID<0.381.0> started from :post terminating
** (stop) exited in: GenServer.call(#PID<0.373.0>, {:checkout, #Reference<0.0.2.400>, true, 15000}, 5000)
    ** (EXIT) shutdown: "owner #PID<0.372.0> exited with: shutdown"
    (db_connection) lib/db_connection/ownership/proxy.ex:32: DBConnection.Ownership.Proxy.checkout/2
    (db_connection) lib/db_connection.ex:920: DBConnection.checkout/2
    (db_connection) lib/db_connection.ex:742: DBConnection.run/3
    (db_connection) lib/db_connection.ex:636: DBConnection.execute/4
    (ecto) lib/ecto/adapters/postgres/connection.ex:105: Ecto.Adapters.Postgres.Connection.execute/4
    (ecto) lib/ecto/adapters/sql.ex:243: Ecto.Adapters.SQL.sql_call/6
    (ecto) lib/ecto/adapters/sql.ex:441: Ecto.Adapters.SQL.execute_or_reset/7
    (ecto) lib/ecto/repo/queryable.ex:130: Ecto.Repo.Queryable.execute/5
    (ecto) lib/ecto/repo/queryable.ex:35: Ecto.Repo.Queryable.all/4
    (ecto) lib/ecto/repo/queryable.ex:76: Ecto.Repo.Queryable.one!/4
    (udia) lib/udia_web/channels/post_channel.ex:79: UdiaWeb.PostChannel.leave/2
    (elixir) lib/task/supervised.ex:85: Task.Supervised.do_apply/2
    (stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3
Function: #Function<0.28181301/0 in ChannelWatcher.handle_info/2>
    Args: []

Is there a way to test my functionality cleanly without introducing a delay in my test case?

Alexander
  • 841
  • 1
  • 9
  • 23
  • May be you could try to set the timeout of the close/2 function to 0: `close(socket, 0)` – guitarman Aug 15 '17 at 07:57
  • @guitarman Thanks for the suggestion! I just tried `close(socket, 0)` and removed the timer sleep, still get the error message written above – Alexander Aug 16 '17 at 12:13

0 Answers0